From aaa56d8668f11229fd7168d47b56c738c31f04bc Mon Sep 17 00:00:00 2001 From: Jon Day Date: Mon, 30 Oct 2023 06:09:14 -0400 Subject: [PATCH] contrib/net/http: honor WithHeaderTags in WrapHandler (#2288) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dario Castañé --- contrib/net/http/http.go | 3 ++- contrib/net/http/http_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/contrib/net/http/http.go b/contrib/net/http/http.go index 963483c2db..b95bbffe41 100644 --- a/contrib/net/http/http.go +++ b/contrib/net/http/http.go @@ -85,8 +85,9 @@ func WrapHandler(h http.Handler, service, resource string, opts ...Option) http. if r := cfg.resourceNamer(req); r != "" { resc = r } - so := make([]ddtrace.StartSpanOption, len(cfg.spanOpts)) + so := make([]ddtrace.StartSpanOption, len(cfg.spanOpts), len(cfg.spanOpts)+1) copy(so, cfg.spanOpts) + so = append(so, httptrace.HeaderTagsFromRequest(req, cfg.headerTags)) TraceAndServe(h, w, req, &ServeConfig{ Service: service, Resource: resc, diff --git a/contrib/net/http/http_test.go b/contrib/net/http/http_test.go index 0a6bba960c..5024884755 100644 --- a/contrib/net/http/http_test.go +++ b/contrib/net/http/http_test.go @@ -104,6 +104,41 @@ func TestWithHeaderTags(t *testing.T) { assert.NotContains(s.Tags(), "http.headers.x-datadog-header") assert.NotContains(s.Tags(), globalT) }) + + t.Run("wrap-handler", func(t *testing.T) { + mt := mocktracer.Start() + defer mt.Stop() + htArgs := []string{"h!e@a-d.e*r", "2header", "3header"} + + handler := WrapHandler(http.HandlerFunc(handler200), "my-service", "my-resource", + WithHeaderTags(htArgs), + ) + + url := "/" + r := httptest.NewRequest("GET", url, nil) + r.Header.Set("h!e@a-d.e*r", "val") + r.Header.Add("h!e@a-d.e*r", "val2") + r.Header.Set("2header", "2val") + r.Header.Set("3header", "3val") + r.Header.Set("x-datadog-header", "value") + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) + + assert := assert.New(t) + assert.Equal(200, w.Code) + assert.Equal("OK\n", w.Body.String()) + + spans := mt.FinishedSpans() + assert.Equal(1, len(spans)) + + s := spans[0] + assert.Equal("http.request", s.OperationName()) + + for _, arg := range htArgs { + header, tag := normalizer.HeaderTag(arg) + assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag]) + } + }) } func TestHttpTracer200(t *testing.T) {