Skip to content

Commit

Permalink
set error tag on http error status codes (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
basvanbeek authored and peterbourgon committed Mar 18, 2018
1 parent 00d061f commit 76d4527
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tracing/zipkin/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ func HTTPServerTrace(tracer *zipkin.Tracer, options ...TracerOption) kithttp.Ser
func(ctx context.Context, code int, r *http.Request) {
if span := zipkin.SpanFromContext(ctx); span != nil {
zipkin.TagHTTPStatusCode.Set(span, strconv.Itoa(code))
if code > 399 {
// set http status as error tag (if already set, this is a noop)
zipkin.TagError.Set(span, http.StatusText(code))
}
if rs, ok := ctx.Value(kithttp.ContextKeyResponseSize).(int64); ok {
zipkin.TagHTTPResponseSize.Set(span, strconv.FormatInt(rs, 10))
}
Expand Down
7 changes: 6 additions & 1 deletion tracing/zipkin/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zipkin_test

import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -173,7 +174,7 @@ func TestHTTPServerTrace(t *testing.T) {
handler := kithttp.NewServer(
endpoint.Nop,
func(context.Context, *http.Request) (interface{}, error) { return nil, nil },
func(context.Context, http.ResponseWriter, interface{}) error { return nil },
func(context.Context, http.ResponseWriter, interface{}) error { return errors.New("dummy") },
zipkinkit.HTTPServerTrace(tr),
)

Expand Down Expand Up @@ -214,4 +215,8 @@ func TestHTTPServerTrace(t *testing.T) {
if want, have := httpMethod, spans[0].Name; want != have {
t.Errorf("incorrect span name, want %s, have %s", want, have)
}

if want, have := http.StatusText(500), spans[0].Tags["error"]; want != have {
t.Fatalf("incorrect error tag, want %s, have %s", want, have)
}
}

0 comments on commit 76d4527

Please sign in to comment.