From becd6a2eddee3e8c6a2b76f6ce77079d950f23a2 Mon Sep 17 00:00:00 2001 From: Mike Goldsmith Date: Tue, 19 Jan 2021 16:12:43 +0000 Subject: [PATCH] Add /debug/trace/{traceID} endpoint (#204) Add a diagnostics HTTP GET endpoint that returns the debug trace information, including the assigned node address using the configured sharding algorithm. The endpoint can be called like this: http://localhost:9000/debug/trace/123abc The response is a JSON object two two properties and looks like the following: {"traceID":"123abc","node":"http://localhost:12345"} --- route/route.go | 7 +++++++ route/route_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/route/route.go b/route/route.go index 9f17268c42..3dbdcf93ff 100644 --- a/route/route.go +++ b/route/route.go @@ -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("/debug/trace/{traceID}", r.debugTrace).Name("get debug information for given trace ID") // require an auth header for events and batches authedMuxxer := muxxer.PathPrefix("/1/").Methods("POST").Subrouter() @@ -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) debugTrace(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") diff --git a/route/route_test.go b/route/route_test.go index ee26527a98..c4e75a94ca 100644 --- a/route/route_test.go +++ b/route/route_test.go @@ -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" ) @@ -234,3 +236,35 @@ func TestUnmarshal(t *testing.T) { t.Error("Expecting", now, "Received", b) } } + +func TestDebugTrace(t *testing.T) { + req, _ := http.NewRequest("GET", "/debug/trace/123abcdef", nil) + req = mux.SetURLVars(req, map[string]string{"traceID": "123abcdef"}) + + rr := httptest.NewRecorder() + router := &Router{ + Sharder: &TestSharder{}, + } + + router.debugTrace(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 }