Skip to content

Commit

Permalink
Merge pull request #114 from instana/mark_http500_errors
Browse files Browse the repository at this point in the history
Mark HTTP entry spans as errors if the response status is 500 and above
  • Loading branch information
Andrew Slotin authored May 4, 2020
2 parents 8aefa21 + c10ae70 commit 7bd19c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func TracingHandlerFunc(sensor *Sensor, name string, handler http.HandlerFunc) h
w3ctrace.TracingHandlerFunc(handler)(wrapped, req.WithContext(ctx))

if wrapped.Status > 0 {
if wrapped.Status > http.StatusInternalServerError {
span.SetTag("http.error", http.StatusText(wrapped.Status))
}

span.SetTag("http.status", wrapped.Status)
}
}
Expand Down
37 changes: 34 additions & 3 deletions instrumentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func TestTracingHandlerFunc_WriteHeaders(t *testing.T) {
s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{}, recorder))

h := instana.TracingHandlerFunc(s, "test-handler", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
w.WriteHeader(http.StatusNotFound)
})

rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/test?q=classified", nil))

assert.Equal(t, http.StatusNotImplemented, rec.Code)
assert.Equal(t, http.StatusNotFound, rec.Code)

spans := recorder.GetQueuedSpans()
require.Len(t, spans, 1)
Expand All @@ -76,7 +76,38 @@ func TestTracingHandlerFunc_WriteHeaders(t *testing.T) {
data := span.Data.(instana.HTTPSpanData)

assert.Equal(t, instana.HTTPSpanTags{
Status: http.StatusNotImplemented,
Status: http.StatusNotFound,
Method: "GET",
Host: "example.com",
Path: "/test",
}, data.Tags)
}

func TestTracingHandlerFunc_Error(t *testing.T) {
recorder := instana.NewTestRecorder()
s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{}, recorder))

h := instana.TracingHandlerFunc(s, "test-handler", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "something went wrong", http.StatusInternalServerError)
})

rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/test", nil))

assert.Equal(t, http.StatusInternalServerError, rec.Code)

spans := recorder.GetQueuedSpans()
require.Len(t, spans, 1)

span := spans[0]
assert.Equal(t, 0, span.Ec)
assert.EqualValues(t, instana.EntrySpanKind, span.Kind)

require.IsType(t, instana.HTTPSpanData{}, span.Data)
data := span.Data.(instana.HTTPSpanData)

assert.Equal(t, instana.HTTPSpanTags{
Status: http.StatusInternalServerError,
Method: "GET",
Host: "example.com",
Path: "/test",
Expand Down

0 comments on commit 7bd19c5

Please sign in to comment.