Skip to content

Commit

Permalink
contrib/internal/httptrace: set http.host tag on request Host not URL…
Browse files Browse the repository at this point in the history
….host (#1327)
  • Loading branch information
ajgajg1134 committed Jun 6, 2022
1 parent b14a582 commit 5915aae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions contrib/internal/httptrace/httptrace.go
Expand Up @@ -29,9 +29,9 @@ func StartRequestSpan(r *http.Request, opts ...ddtrace.StartSpanOption) (tracer.
tracer.Tag(ext.HTTPUserAgent, r.UserAgent()),
tracer.Measured(),
}, opts...)
if r.URL.Host != "" {
if r.Host != "" {
opts = append([]ddtrace.StartSpanOption{
tracer.Tag("http.host", r.URL.Host),
tracer.Tag("http.host", r.Host),
}, opts...)
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header)); err == nil {
Expand Down
29 changes: 29 additions & 0 deletions contrib/internal/httptrace/httptrace_test.go
@@ -0,0 +1,29 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

package httptrace

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
)

func TestStartRequestSpan(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
r := httptest.NewRequest(http.MethodGet, "/somePath", nil)
s, _ := StartRequestSpan(r)
s.Finish()
spans := mt.FinishedSpans()

require.Len(t, spans, 1)
assert.Equal(t, "example.com", spans[0].Tag("http.host"))
}

0 comments on commit 5915aae

Please sign in to comment.