-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2: migrate contribs to new instrumentation api (3) (#2819)
- Loading branch information
1 parent
f376fa4
commit f231f23
Showing
52 changed files
with
913 additions
and
571 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,12 @@ import ( | |
"sync" | ||
"testing" | ||
|
||
pappsec "github.com/DataDog/dd-trace-go/v2/appsec" | ||
"github.com/DataDog/dd-trace-go/v2/appsec" | ||
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext" | ||
"github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer" | ||
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer" | ||
"github.com/DataDog/dd-trace-go/v2/internal/appsec" | ||
"github.com/DataDog/dd-trace-go/v2/internal/contrib/namingschematest" | ||
"github.com/DataDog/dd-trace-go/v2/internal/globalconfig" | ||
"github.com/DataDog/dd-trace-go/v2/internal/normalizer" | ||
"github.com/DataDog/dd-trace-go/v2/instrumentation" | ||
"github.com/DataDog/dd-trace-go/v2/instrumentation/testutils" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/stretchr/testify/assert" | ||
|
@@ -279,9 +277,7 @@ func TestAnalyticsSettings(t *testing.T) { | |
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
rate := globalconfig.AnalyticsRate() | ||
defer globalconfig.SetAnalyticsRate(rate) | ||
globalconfig.SetAnalyticsRate(0.4) | ||
testutils.SetGlobalAnalyticsRate(t, 0.4) | ||
|
||
assertRate(t, mt, 0.4) | ||
}) | ||
|
@@ -304,9 +300,7 @@ func TestAnalyticsSettings(t *testing.T) { | |
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
rate := globalconfig.AnalyticsRate() | ||
defer globalconfig.SetAnalyticsRate(rate) | ||
globalconfig.SetAnalyticsRate(0.4) | ||
testutils.SetGlobalAnalyticsRate(t, 0.4) | ||
|
||
assertRate(t, mt, 0.23, WithAnalyticsRate(0.23)) | ||
}) | ||
|
@@ -342,10 +336,9 @@ func TestIgnoreRequest(t *testing.T) { | |
} | ||
|
||
func TestAppSec(t *testing.T) { | ||
appsec.Start() | ||
defer appsec.Stop() | ||
testutils.StartAppSec(t) | ||
|
||
if !appsec.Enabled() { | ||
if !instr.AppSecEnabled() { | ||
t.Skip("appsec disabled") | ||
} | ||
|
||
|
@@ -360,7 +353,7 @@ func TestAppSec(t *testing.T) { | |
require.NoError(t, err) | ||
}) | ||
router.HandleFunc("/body", func(w http.ResponseWriter, r *http.Request) { | ||
pappsec.MonitorParsedHTTPBody(r.Context(), "$globals") | ||
appsec.MonitorParsedHTTPBody(r.Context(), "$globals") | ||
_, err := w.Write([]byte("Hello Body!\n")) | ||
require.NoError(t, err) | ||
}) | ||
|
@@ -478,90 +471,63 @@ func TestWithHeaderTags(t *testing.T) { | |
assert := assert.New(t) | ||
assert.Equal(len(spans), 1) | ||
s := spans[0] | ||
for _, arg := range htArgs { | ||
_, tag := normalizer.HeaderTag(arg) | ||
|
||
instrumentation.NewHeaderTags(htArgs).Iter(func(_ string, tag string) { | ||
assert.NotContains(s.Tags(), tag) | ||
} | ||
}) | ||
}) | ||
|
||
t.Run("integration", func(t *testing.T) { | ||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
htArgs := []string{"[email protected]*r", "2header:tag"} | ||
r := setupReq(WithHeaderTags(htArgs)) | ||
_ = setupReq(WithHeaderTags(htArgs)) | ||
spans := mt.FinishedSpans() | ||
assert := assert.New(t) | ||
assert.Equal(len(spans), 1) | ||
s := spans[0] | ||
|
||
for _, arg := range htArgs { | ||
header, tag := normalizer.HeaderTag(arg) | ||
assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag]) | ||
} | ||
assert.Equal("val,val2", s.Tags()["http.request.headers.h_e_a-d_e_r"]) | ||
assert.Equal("2val", s.Tags()["tag"]) | ||
assert.NotContains(s.Tags(), "http.headers.x-datadog-header") | ||
}) | ||
|
||
t.Run("global", func(t *testing.T) { | ||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
header, tag := normalizer.HeaderTag("3header") | ||
globalconfig.SetHeaderTag(header, tag) | ||
testutils.SetGlobalHeaderTags(t, "3header") | ||
|
||
r := setupReq() | ||
_ = setupReq() | ||
spans := mt.FinishedSpans() | ||
assert := assert.New(t) | ||
assert.Equal(len(spans), 1) | ||
s := spans[0] | ||
|
||
assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag]) | ||
assert.Equal("3val", s.Tags()["http.request.headers.3header"]) | ||
assert.NotContains(s.Tags(), "http.request.headers.other") | ||
assert.NotContains(s.Tags(), "http.headers.x-datadog-header") | ||
}) | ||
|
||
t.Run("override", func(t *testing.T) { | ||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
globalH, globalT := normalizer.HeaderTag("3header") | ||
globalconfig.SetHeaderTag(globalH, globalT) | ||
testutils.SetGlobalHeaderTags(t, "3header") | ||
|
||
htArgs := []string{"[email protected]*r", "2header:tag"} | ||
r := setupReq(WithHeaderTags(htArgs)) | ||
_ = setupReq(WithHeaderTags(htArgs)) | ||
spans := mt.FinishedSpans() | ||
assert := assert.New(t) | ||
assert.Equal(len(spans), 1) | ||
s := spans[0] | ||
|
||
for _, arg := range htArgs { | ||
header, tag := normalizer.HeaderTag(arg) | ||
assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag]) | ||
} | ||
assert.Equal("val,val2", s.Tags()["http.request.headers.h_e_a-d_e_r"]) | ||
assert.Equal("2val", s.Tags()["tag"]) | ||
assert.NotContains(s.Tags(), "http.headers.x-datadog-header") | ||
assert.NotContains(s.Tags(), globalT) | ||
}) | ||
} | ||
func TestNamingSchema(t *testing.T) { | ||
genSpans := namingschematest.GenSpansFn(func(t *testing.T, serviceOverride string) []*mocktracer.Span { | ||
var opts []Option | ||
if serviceOverride != "" { | ||
opts = append(opts, WithService(serviceOverride)) | ||
} | ||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
mux := chi.NewRouter().With(Middleware(opts...)) | ||
mux.HandleFunc("/200", func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte("ok")) | ||
require.NoError(t, err) | ||
}) | ||
r := httptest.NewRequest("GET", "/200", nil) | ||
w := httptest.NewRecorder() | ||
mux.ServeHTTP(w, r) | ||
|
||
return mt.FinishedSpans() | ||
assert.NotContains(s.Tags(), "http.request.headers.3header") | ||
}) | ||
namingschematest.NewHTTPServerTest(genSpans, "chi.router")(t) | ||
} | ||
|
||
func TestCustomResourceName(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.