Skip to content

Commit

Permalink
contrib/net/http: honor WithHeaderTags in WrapHandler (#2288)
Browse files Browse the repository at this point in the history
Co-authored-by: Dario Castañé <[email protected]>
  • Loading branch information
sudolibre and darccio authored Oct 30, 2023
1 parent cb50734 commit aaa56d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion contrib/net/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions contrib/net/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{"[email protected]*r", "2header", "3header"}

handler := WrapHandler(http.HandlerFunc(handler200), "my-service", "my-resource",
WithHeaderTags(htArgs),
)

url := "/"
r := httptest.NewRequest("GET", url, nil)
r.Header.Set("[email protected]*r", "val")
r.Header.Add("[email protected]*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) {
Expand Down

0 comments on commit aaa56d8

Please sign in to comment.