Skip to content

Commit

Permalink
test: add testing for custom trace header keys;
Browse files Browse the repository at this point in the history
  • Loading branch information
riandyrn committed Sep 17, 2024
1 parent a52731b commit 519fb83
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions test/cases/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,34 +430,60 @@ func TestSDKIntegrationWithDefaultLegacyHeaderKey(t *testing.T) {
}

func TestSDKIntegrationWithTraceResponseHeaders(t *testing.T) {
router, sr := newSDKTestRouter(
"foobar",
true,
otelchi.WithTraceResponseHeaders(otelchi.TraceHeaderConfig{}),
)
router.HandleFunc("/user/{id:[0-9]+}", ok)
router.HandleFunc("/book/{title}", ok)

r0 := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r0)

recordedSpans := sr.Ended()
require.Len(t, recordedSpans, 1)
checkSpans(t, recordedSpans, []spanValueCheck{
testCases := []struct {
Name string
TraceHeaderConfig otelchi.TraceHeaderConfig
ExpTraceResponseIDKey string
ExpTraceResponseSampledKey string
}{
{
Name: "/user/{id:[0-9]+}",
Kind: trace.SpanKindServer,
Attributes: getSemanticAttributes(
"foobar",
http.StatusOK,
"GET",
"/user/{id:[0-9]+}",
),
Name: "Default Trace Config",
TraceHeaderConfig: otelchi.TraceHeaderConfig{},
ExpTraceResponseIDKey: otelchi.DefaultTraceIDResponseHeaderKey,
ExpTraceResponseSampledKey: otelchi.DefaultTraceSampledResponseHeaderKey,
},
})
require.Equal(t, recordedSpans[0].SpanContext().TraceID().String(), w.Header().Get(otelchi.DefaultTraceIDResponseHeaderKey))
require.Equal(t, "true", w.Header().Get(otelchi.DefaultTraceSampledResponseHeaderKey))
{
Name: "Custom Trace Config",
TraceHeaderConfig: otelchi.TraceHeaderConfig{
TraceIDHeader: "X-Custom-Trace-ID",
TraceSampledHeader: "X-Custom-Trace-Sampled",
},
ExpTraceResponseIDKey: "X-Custom-Trace-ID",
ExpTraceResponseSampledKey: "X-Custom-Trace-Sampled",
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
router, sr := newSDKTestRouter(
"foobar",
true,
otelchi.WithTraceResponseHeaders(testCase.TraceHeaderConfig),
)
router.HandleFunc("/user/{id:[0-9]+}", ok)
router.HandleFunc("/book/{title}", ok)

r0 := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r0)

recordedSpans := sr.Ended()
require.Len(t, recordedSpans, 1)
checkSpans(t, recordedSpans, []spanValueCheck{
{
Name: "/user/{id:[0-9]+}",
Kind: trace.SpanKindServer,
Attributes: getSemanticAttributes(
"foobar",
http.StatusOK,
"GET",
"/user/{id:[0-9]+}",
),
},
})
require.Equal(t, recordedSpans[0].SpanContext().TraceID().String(), w.Header().Get(testCase.ExpTraceResponseIDKey))
require.Equal(t, "true", w.Header().Get(testCase.ExpTraceResponseSampledKey))
})
}
}

func TestSDKIntegrationWithOverrideLegacyHeaderKey(t *testing.T) {
Expand Down

0 comments on commit 519fb83

Please sign in to comment.