Skip to content

Commit

Permalink
Merge pull request #272 from netlify/fix-staus-in-traces
Browse files Browse the repository at this point in the history
Set status code as string on spans
  • Loading branch information
Kelsey authored Dec 9, 2020
2 parents 6636bf7 + 9fba9e8 commit c535614
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion api/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"strconv"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
Expand Down Expand Up @@ -47,7 +48,11 @@ func tracer(next http.Handler) http.Handler {
next.ServeHTTP(trw, r.WithContext(traceCtx))

status := trw.statusCode
ext.HTTPStatusCode.Set(span, uint16(status))

// Setting the status as an int doesn't propogate for use in datadog dashboards,
// so we convert to a string.
span.SetTag(string(ext.HTTPStatusCode), strconv.Itoa(status))

if status >= 500 && status < 600 {
ext.Error.Set(span, true)
span.SetTag("error.type", fmt.Sprintf("%d: %s", status, http.StatusText(status)))
Expand Down
4 changes: 2 additions & 2 deletions api/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func (ts *TracerTestSuite) TestTracer_Spans() {
assert.Equal(ts.T(), "POST", spans[0].Tag("http.method"))
assert.Equal(ts.T(), "/something1", spans[0].Tag("http.url"))
assert.Equal(ts.T(), "POST /something1", spans[0].Tag("resource.name"))
assert.Equal(ts.T(), uint16(http.StatusNotFound), spans[0].Tag("http.status_code"))
assert.Equal(ts.T(), "404", spans[0].Tag("http.status_code"))
assert.NotEmpty(ts.T(), spans[0].Tag("http.request_id"))

assert.Equal(ts.T(), "GET", spans[1].Tag("http.method"))
assert.Equal(ts.T(), "/something2", spans[1].Tag("http.url"))
assert.Equal(ts.T(), "GET /something2", spans[1].Tag("resource.name"))
assert.Equal(ts.T(), uint16(http.StatusNotFound), spans[1].Tag("http.status_code"))
assert.Equal(ts.T(), "404", spans[1].Tag("http.status_code"))
assert.NotEmpty(ts.T(), spans[1].Tag("http.request_id"))
}
}

0 comments on commit c535614

Please sign in to comment.