Skip to content

Commit

Permalink
fix: make X-Trace-Sampled also appears if `WithTraceIDResponseHeade…
Browse files Browse the repository at this point in the history
…r` enabled;
  • Loading branch information
riandyrn committed Sep 17, 2024
1 parent 5dcc278 commit a52731b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
23 changes: 12 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

// These defaults are used in `TraceHeaderConfig`.
const (
defaultTraceIDResponseHeaderKey = "X-Trace-Id"
defaultTraceSampledResponseHeaderKey = "X-Trace-Sampled"
DefaultTraceIDResponseHeaderKey = "X-Trace-Id"
DefaultTraceSampledResponseHeaderKey = "X-Trace-Sampled"
)

// config is used to configure the mux middleware.
Expand Down Expand Up @@ -103,13 +103,14 @@ func WithFilter(filter Filter) Option {
//
// Deprecated: use `WithTraceResponseHeaders` instead.
func WithTraceIDResponseHeader(headerKeyFunc func() string) Option {
return optionFunc(func(cfg *config) {
if headerKeyFunc == nil {
cfg.TraceIDResponseHeaderKey = defaultTraceIDResponseHeaderKey // use default trace header
} else {
cfg.TraceIDResponseHeaderKey = headerKeyFunc()
}
})
cfg := TraceHeaderConfig{
TraceIDHeader: "",
TraceSampledHeader: "",
}
if headerKeyFunc != nil {
cfg.TraceIDHeader = headerKeyFunc()
}
return WithTraceResponseHeaders(cfg)
}

// TraceHeaderConfig is configuration for trace headers in the response.
Expand All @@ -126,12 +127,12 @@ func WithTraceResponseHeaders(cfg TraceHeaderConfig) Option {
return optionFunc(func(c *config) {
c.TraceIDResponseHeaderKey = cfg.TraceIDHeader
if c.TraceIDResponseHeaderKey == "" {
c.TraceIDResponseHeaderKey = defaultTraceIDResponseHeaderKey
c.TraceIDResponseHeaderKey = DefaultTraceIDResponseHeaderKey
}

c.TraceSampledResponseHeaderKey = cfg.TraceSampledHeader
if c.TraceSampledResponseHeaderKey == "" {
c.TraceSampledResponseHeaderKey = defaultTraceSampledResponseHeaderKey
c.TraceSampledResponseHeaderKey = DefaultTraceSampledResponseHeaderKey
}
})
}
Expand Down
15 changes: 9 additions & 6 deletions test/cases/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ func TestSDKIntegrationWithDefaultLegacyHeaderKey(t *testing.T) {
),
},
})
require.Equal(t, recordedSpans[0].SpanContext().TraceID().String(), w.Header().Get("X-Trace-ID"))
require.Equal(t, recordedSpans[0].SpanContext().TraceID().String(), w.Header().Get(otelchi.DefaultTraceIDResponseHeaderKey))
require.Equal(t, "true", w.Header().Get(otelchi.DefaultTraceSampledResponseHeaderKey))
}

func TestSDKIntegrationWithTraceResponseHeaders(t *testing.T) {
Expand Down Expand Up @@ -455,11 +456,11 @@ func TestSDKIntegrationWithTraceResponseHeaders(t *testing.T) {
),
},
})
require.Equal(t, recordedSpans[0].SpanContext().TraceID().String(), w.Header().Get("X-Trace-ID"))
require.Equal(t, "true", w.Header().Get("X-Trace-Sampled"))
require.Equal(t, recordedSpans[0].SpanContext().TraceID().String(), w.Header().Get(otelchi.DefaultTraceIDResponseHeaderKey))
require.Equal(t, "true", w.Header().Get(otelchi.DefaultTraceSampledResponseHeaderKey))
}

func TestSDKIntegrationWithOverrideHeaderKey(t *testing.T) {
func TestSDKIntegrationWithOverrideLegacyHeaderKey(t *testing.T) {
// Define a function inline that transforms the default
// header name to a custom header name
customHeaderKeyFunc := func() string {
Expand Down Expand Up @@ -493,9 +494,10 @@ func TestSDKIntegrationWithOverrideHeaderKey(t *testing.T) {
},
})
require.Equal(t, w.Header().Get(customHeaderKeyFunc()), recordedSpans[0].SpanContext().TraceID().String())
require.Equal(t, "true", w.Header().Get(otelchi.DefaultTraceSampledResponseHeaderKey))
}

func TestSDKIntegrationWithoutOverrideHeaderKey(t *testing.T) {
func TestSDKIntegrationWithoutOverrideLegacyHeaderKey(t *testing.T) {
router, sr := newSDKTestRouter("foobar", true)

router.HandleFunc("/user/{id:[0-9]+}", ok)
Expand All @@ -521,7 +523,8 @@ func TestSDKIntegrationWithoutOverrideHeaderKey(t *testing.T) {
},
})

require.Empty(t, w.Header().Get("X-Trace-ID"))
require.Empty(t, w.Header().Get(otelchi.DefaultTraceIDResponseHeaderKey))
require.Empty(t, w.Header().Get(otelchi.DefaultTraceSampledResponseHeaderKey))
}

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

0 comments on commit a52731b

Please sign in to comment.