Skip to content

Commit

Permalink
fix: implement default trace-id in response header
Browse files Browse the repository at this point in the history
  • Loading branch information
ProtozoaJr committed Apr 1, 2024
1 parent e9840fa commit 38d0a62
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
11 changes: 9 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ func WithFilter(filter func(r *http.Request) bool) Option {
}

// WithTraceResponseHeaderKey is used for changing response header key that contains trace id.
func WithTraceResponseHeaderKey(name string) Option {
// Instead of using a fixed name as the parameter, this function accepts a function
// that takes a string (the default trace header) and returns a string (the custom trace header).
// If the provided function is nil, the default trace header (X-Trace-ID) will be used.
func WithTraceResponseHeaderKey(headerKeyFunc func() string) Option {
return optionFunc(func(cfg *config) {
cfg.TraceResponseHeaderKey = name
if headerKeyFunc == nil {
cfg.TraceResponseHeaderKey = traceResponseHeaderKey // Default trace header
} else {
cfg.TraceResponseHeaderKey = headerKeyFunc()
}
})
}
4 changes: 2 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func (tw traceware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
)
defer span.End()

// put trace_id to response header
if span.SpanContext().HasTraceID() {
// put trace_id to response header only when WithTraceResponseHeaderKey is used
if tw.traceResponseHeaderKey != "" && span.SpanContext().HasTraceID() {
w.Header().Add(tw.traceResponseHeaderKey, span.SpanContext().TraceID().String())
}

Expand Down
9 changes: 6 additions & 3 deletions test/cases/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,16 @@ func TestSDKIntegrationWithOverrideHeaderKey(t *testing.T) {
provider := sdktrace.NewTracerProvider()
provider.RegisterSpanProcessor(sr)

customeHeaderKey := "X-Custom-Trace-ID"
// Define a function inline that transforms the default header name to a custom header name
customHeaderKeyFunc := func() string {
return "X-Custom-Trace-ID"
}

router := chi.NewRouter()
router.Use(otelchi.Middleware(
"foobar",
otelchi.WithTracerProvider(provider),
otelchi.WithTraceResponseHeaderKey(customeHeaderKey),
otelchi.WithTraceResponseHeaderKey(customHeaderKeyFunc),
))
router.HandleFunc("/user/{id:[0-9]+}", ok)
router.HandleFunc("/book/{title}", ok)
Expand All @@ -392,7 +395,7 @@ func TestSDKIntegrationWithOverrideHeaderKey(t *testing.T) {
attribute.String("http.target", "/user/123"),
attribute.String("http.route", "/user/{id:[0-9]+}"),
)
require.Equal(t, w.Header().Get(customeHeaderKey), sr.Ended()[0].SpanContext().TraceID().String())
require.Equal(t, w.Header().Get(customHeaderKeyFunc()), sr.Ended()[0].SpanContext().TraceID().String())
}

func assertSpan(t *testing.T, span sdktrace.ReadOnlySpan, name string, kind trace.SpanKind, attrs ...attribute.KeyValue) {
Expand Down

0 comments on commit 38d0a62

Please sign in to comment.