Skip to content

Commit 76d4527

Browse files
basvanbeekpeterbourgon
authored andcommitted
set error tag on http error status codes (#683)
1 parent 00d061f commit 76d4527

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

tracing/zipkin/http.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ func HTTPServerTrace(tracer *zipkin.Tracer, options ...TracerOption) kithttp.Ser
193193
func(ctx context.Context, code int, r *http.Request) {
194194
if span := zipkin.SpanFromContext(ctx); span != nil {
195195
zipkin.TagHTTPStatusCode.Set(span, strconv.Itoa(code))
196+
if code > 399 {
197+
// set http status as error tag (if already set, this is a noop)
198+
zipkin.TagError.Set(span, http.StatusText(code))
199+
}
196200
if rs, ok := ctx.Value(kithttp.ContextKeyResponseSize).(int64); ok {
197201
zipkin.TagHTTPResponseSize.Set(span, strconv.FormatInt(rs, 10))
198202
}

tracing/zipkin/http_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package zipkin_test
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"net/http/httptest"
@@ -173,7 +174,7 @@ func TestHTTPServerTrace(t *testing.T) {
173174
handler := kithttp.NewServer(
174175
endpoint.Nop,
175176
func(context.Context, *http.Request) (interface{}, error) { return nil, nil },
176-
func(context.Context, http.ResponseWriter, interface{}) error { return nil },
177+
func(context.Context, http.ResponseWriter, interface{}) error { return errors.New("dummy") },
177178
zipkinkit.HTTPServerTrace(tr),
178179
)
179180

@@ -214,4 +215,8 @@ func TestHTTPServerTrace(t *testing.T) {
214215
if want, have := httpMethod, spans[0].Name; want != have {
215216
t.Errorf("incorrect span name, want %s, have %s", want, have)
216217
}
218+
219+
if want, have := http.StatusText(500), spans[0].Tags["error"]; want != have {
220+
t.Fatalf("incorrect error tag, want %s, have %s", want, have)
221+
}
217222
}

0 commit comments

Comments
 (0)