Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /debug/trace/{traceID} endpoint #204

Merged
merged 4 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (r *Router) LnS(incomingOrPeer string) {
muxxer.HandleFunc("/alive", r.alive).Name("local health")
muxxer.HandleFunc("/panic", r.panic).Name("intentional panic")
muxxer.HandleFunc("/version", r.version).Name("report version info")
muxxer.HandleFunc("/getNode/{traceID}", r.getNode).Name("get node address for given trace ID")
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved

// require an auth header for events and batches
authedMuxxer := muxxer.PathPrefix("/1/").Methods("POST").Subrouter()
Expand Down Expand Up @@ -199,6 +200,12 @@ func (r *Router) version(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(fmt.Sprintf(`{"source":"refinery","version":"%s"}`, r.versionStr)))
}

func (r *Router) getNode(w http.ResponseWriter, req *http.Request) {
traceID := mux.Vars(req)["traceID"]
shard := r.Sharder.WhichShard(traceID)
w.Write([]byte(fmt.Sprintf(`{"traceID":"%s","node":"%s"}`, traceID, shard.GetAddress())))
}

// event is handler for /1/event/
func (r *Router) event(w http.ResponseWriter, req *http.Request) {
r.Metrics.IncrementCounter(r.incomingOrPeer + "_router_event")
Expand Down
34 changes: 34 additions & 0 deletions route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"testing"
"time"

"github.com/gorilla/mux"
"github.com/honeycombio/refinery/sharder"
"github.com/klauspost/compress/zstd"
"github.com/vmihailenco/msgpack/v4"
)
Expand Down Expand Up @@ -234,3 +236,35 @@ func TestUnmarshal(t *testing.T) {
t.Error("Expecting", now, "Received", b)
}
}

func TestGetNode(t *testing.T) {
req, _ := http.NewRequest("GET", "/getNode/123abcdef", nil)
req = mux.SetURLVars(req, map[string]string{"traceID": "123abcdef"})

rr := httptest.NewRecorder()
router := &Router{
Sharder: &TestSharder{},
}

router.getNode(rr, req)
if body := rr.Body.String(); body != `{"traceID":"123abcdef","node":"http://localhost:12345"}` {
t.Error(body)
}
}

type TestSharder struct{}

func (s *TestSharder) MyShard() sharder.Shard { return nil }

func (s *TestSharder) WhichShard(string) sharder.Shard {
return &TestShard{
addr: "http://localhost:12345",
}
}

type TestShard struct {
addr string
}

func (s *TestShard) Equals(other sharder.Shard) bool { return true }
func (s *TestShard) GetAddress() string { return s.addr }