Skip to content

Commit

Permalink
fix(tracing): ignore health endpoints (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
inigohu authored Aug 21, 2020
1 parent 7c73623 commit 6dcd69d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tracing/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tracing

import (
"net/http"
"strings"

opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
Expand All @@ -13,6 +14,12 @@ func (t *Tracer) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.Ha
var span opentracing.Span
opName := r.URL.Path

// Omit health endpoints
if strings.HasPrefix(opName, "/health/") {
next(rw, r)
return
}

// It's very possible that Hydra is fronted by a proxy which could have initiated a trace.
// If so, we should attempt to join it.
remoteContext, err := opentracing.GlobalTracer().Extract(
Expand Down
32 changes: 32 additions & 0 deletions tracing/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,35 @@ func TestShouldContinueTraceIfAlreadyPresent(t *testing.T) {

assert.Equal(t, parentSpan.SpanContext.SpanID, span.ParentID)
}

func TestShouldNotTraceHealthEndpoint(t *testing.T) {
testCases := []struct {
path string
testDescription string
}{
{
path: "health/ready",
testDescription: "ready",
},
{
path: "health/alive",
testDescription: "alive",
},
}
for _, test := range testCases {
t.Run(test.testDescription, func(t *testing.T) {
defer mockedTracer.Reset()
request := httptest.NewRequest(http.MethodGet, "https://apis.somecompany.com/"+test.path, nil)

next := func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusOK)
}

tracer.ServeHTTP(negroni.NewResponseWriter(httptest.NewRecorder()), request, next)

spans := mockedTracer.FinishedSpans()
assert.Len(t, spans, 0)

})
}
}

0 comments on commit 6dcd69d

Please sign in to comment.