From 82e84911d5e1dbd9d09a4e44feb4ef85c8f27676 Mon Sep 17 00:00:00 2001 From: dengliming Date: Mon, 10 Aug 2020 22:03:50 +0800 Subject: [PATCH] Don't include the http.status_code when 2xx in server. --- middleware/http/server.go | 8 +++++--- middleware/http/server_test.go | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/middleware/http/server.go b/middleware/http/server.go index 9c36755..0b4c760 100644 --- a/middleware/http/server.go +++ b/middleware/http/server.go @@ -145,10 +145,12 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { code := ri.getStatusCode() sCode := strconv.Itoa(code) - if code > 399 { - h.errHandler(sp, nil, code) + if code < 200 || code > 299 { + zipkin.TagHTTPStatusCode.Set(sp, sCode) + if code > 399 { + h.errHandler(sp, nil, code) + } } - zipkin.TagHTTPStatusCode.Set(sp, sCode) if h.tagResponseSize && atomic.LoadUint64(&ri.size) > 0 { zipkin.TagHTTPResponseSize.Set(sp, ri.getResponseSize()) } diff --git a/middleware/http/server_test.go b/middleware/http/server_test.go index 64d4034..eb84caf 100644 --- a/middleware/http/server_test.go +++ b/middleware/http/server_test.go @@ -179,6 +179,7 @@ func TestHTTPDefaultSpanName(t *testing.T) { httpRecorder = httptest.NewRecorder() requestBuf = bytes.NewBufferString("incoming data") methodType = "POST" + code = "" ) request, err := http.NewRequest(methodType, "/test", requestBuf) @@ -203,6 +204,10 @@ func TestHTTPDefaultSpanName(t *testing.T) { if want, have := methodType, span.Name; want != have { t.Errorf("Expected span name %s, got %s", want, have) } + + if want, have := code, span.Tags["http.status_code"]; want != have { + t.Errorf("Expected span status code %s, got %s", want, have) + } } func TestHTTPRequestSampler(t *testing.T) {