Skip to content

Commit

Permalink
test: control span context in trace response headers option to ensure…
Browse files Browse the repository at this point in the history
… the value of X-Trace-Sampled is correct;
  • Loading branch information
riandyrn committed Sep 17, 2024
1 parent 519fb83 commit 0cfb59f
Showing 1 changed file with 65 additions and 31 deletions.
96 changes: 65 additions & 31 deletions test/cases/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package otelchi_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -430,58 +431,91 @@ func TestSDKIntegrationWithDefaultLegacyHeaderKey(t *testing.T) {
}

func TestSDKIntegrationWithTraceResponseHeaders(t *testing.T) {
// prepare both sampled & non-sampled span context
spanCtxSampled := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{1},
SpanID: [8]byte{1},
Remote: true,
TraceFlags: trace.FlagsSampled,
})
spanCtxNotSampled := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{2},
SpanID: [8]byte{2},
Remote: true,
TraceFlags: 0,
})

// define test cases
testCases := []struct {
Name string
TraceHeaderConfig otelchi.TraceHeaderConfig
ExpTraceResponseIDKey string
ExpTraceResponseSampledKey string
Name string
TraceHeaderConfig otelchi.TraceHeaderConfig
SpanContext trace.SpanContext
ExpTraceResponseIDKey string
ExpTraceResponseSampledKey string
ExpTraceResponseSampledKeyVal bool
}{
{
Name: "Default Trace Config",
TraceHeaderConfig: otelchi.TraceHeaderConfig{},
ExpTraceResponseIDKey: otelchi.DefaultTraceIDResponseHeaderKey,
ExpTraceResponseSampledKey: otelchi.DefaultTraceSampledResponseHeaderKey,
Name: "Default Trace Config, Trace Sampled",
TraceHeaderConfig: otelchi.TraceHeaderConfig{},
SpanContext: spanCtxSampled,
ExpTraceResponseIDKey: otelchi.DefaultTraceIDResponseHeaderKey,
ExpTraceResponseSampledKey: otelchi.DefaultTraceSampledResponseHeaderKey,
ExpTraceResponseSampledKeyVal: true,
},
{
Name: "Default Trace Config, Trace Not Sampled",
TraceHeaderConfig: otelchi.TraceHeaderConfig{},
SpanContext: spanCtxNotSampled,
ExpTraceResponseIDKey: otelchi.DefaultTraceIDResponseHeaderKey,
ExpTraceResponseSampledKey: otelchi.DefaultTraceSampledResponseHeaderKey,
ExpTraceResponseSampledKeyVal: false,
},
{
Name: "Custom Trace Config, Trace Sampled",
TraceHeaderConfig: otelchi.TraceHeaderConfig{
TraceIDHeader: "X-Custom-Trace-ID",
TraceSampledHeader: "X-Custom-Trace-Sampled",
},
SpanContext: spanCtxSampled,
ExpTraceResponseIDKey: "X-Custom-Trace-ID",
ExpTraceResponseSampledKey: "X-Custom-Trace-Sampled",
ExpTraceResponseSampledKeyVal: true,
},
{
Name: "Custom Trace Config",
Name: "Custom Trace Config, Trace Not Sampled",
TraceHeaderConfig: otelchi.TraceHeaderConfig{
TraceIDHeader: "X-Custom-Trace-ID",
TraceSampledHeader: "X-Custom-Trace-Sampled",
},
ExpTraceResponseIDKey: "X-Custom-Trace-ID",
ExpTraceResponseSampledKey: "X-Custom-Trace-Sampled",
SpanContext: spanCtxNotSampled,
ExpTraceResponseIDKey: "X-Custom-Trace-ID",
ExpTraceResponseSampledKey: "X-Custom-Trace-Sampled",
ExpTraceResponseSampledKeyVal: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
router, sr := newSDKTestRouter(
"foobar",
true,
otelchi.WithTraceResponseHeaders(testCase.TraceHeaderConfig),
// configure router
router := chi.NewRouter()
router.Use(
otelchi.Middleware(
"foobar",
otelchi.WithChiRoutes(router),
otelchi.WithTraceResponseHeaders(testCase.TraceHeaderConfig),
),
)
router.HandleFunc("/user/{id:[0-9]+}", ok)
router.HandleFunc("/book/{title}", ok)

// execute requests
r0 := httptest.NewRequest("GET", "/user/123", nil)
r0 = r0.WithContext(trace.ContextWithRemoteSpanContext(context.Background(), testCase.SpanContext))
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))
// check response headers
require.Equal(t, testCase.SpanContext.TraceID().String(), w.Header().Get(testCase.ExpTraceResponseIDKey))
require.Equal(t, fmt.Sprintf("%v", testCase.ExpTraceResponseSampledKeyVal), w.Header().Get(testCase.ExpTraceResponseSampledKey))
})
}
}
Expand Down

0 comments on commit 0cfb59f

Please sign in to comment.