diff --git a/js/modules/k6/experimental/tracing/propagator.go b/js/modules/k6/experimental/tracing/propagator.go index 5d59f7f189b..3bacd128376 100644 --- a/js/modules/k6/experimental/tracing/propagator.go +++ b/js/modules/k6/experimental/tracing/propagator.go @@ -69,11 +69,11 @@ const ( // "0 value is valid and means “root span” (when not ignored)" JaegerRootSpanID = "0" - // JaegerSampledTraceFlag is the trace-flag value for an unsampled trace. - JaegerSampledTraceFlag = "0" + // JaegerUnsampledTraceFlag is the trace-flag value for an unsampled trace. + JaegerUnsampledTraceFlag = "0" - // JaegerUnsampledTraceFlag is the trace-flag value for a sampled trace. - JaegerUnsampledTraceFlag = "1" + // JaegerSampledTraceFlag is the trace-flag value for a sampled trace. + JaegerSampledTraceFlag = "1" ) // JaegerPropagator is a Propagator for the Jaeger trace context header diff --git a/js/modules/k6/experimental/tracing/propagator_test.go b/js/modules/k6/experimental/tracing/propagator_test.go new file mode 100644 index 00000000000..ebf795b5816 --- /dev/null +++ b/js/modules/k6/experimental/tracing/propagator_test.go @@ -0,0 +1,112 @@ +package tracing + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPropagate(t *testing.T) { + t.Parallel() + + traceID := "abc123" + + t.Run("W3C Propagator", func(t *testing.T) { + t.Parallel() + + sampler := mockSampler{decision: true} + propagator := NewW3CPropagator(sampler) + + gotHeader, gotErr := propagator.Propagate(traceID) + + assert.NoError(t, gotErr) + assert.Contains(t, gotHeader, W3CHeaderName) + + //nolint:staticcheck // as traceparent is not a canonical header + headerContent := gotHeader[W3CHeaderName][0] + assert.True(t, strings.HasPrefix(headerContent, W3CVersion+"-"+traceID+"-")) + }) + + t.Run("W3C propagator with sampled trace", func(t *testing.T) { + t.Parallel() + + sampler := mockSampler{decision: true} + propagator := NewW3CPropagator(sampler) + + gotHeader, gotErr := propagator.Propagate(traceID) + require.NoError(t, gotErr) + require.Contains(t, gotHeader, W3CHeaderName) + + //nolint:staticcheck // as traceparent is not a canonical header + assert.True(t, strings.HasSuffix(gotHeader[W3CHeaderName][0], "-01")) + }) + + t.Run("W3C propagator with unsampled trace", func(t *testing.T) { + t.Parallel() + + sampler := mockSampler{decision: false} + propagator := NewW3CPropagator(sampler) + + gotHeader, gotErr := propagator.Propagate(traceID) + require.NoError(t, gotErr) + require.Contains(t, gotHeader, W3CHeaderName) + + //nolint:staticcheck // as traceparent is not a canonical header + assert.True(t, strings.HasSuffix(gotHeader[W3CHeaderName][0], "-00")) + }) + + t.Run("Jaeger Propagator", func(t *testing.T) { + t.Parallel() + + sampler := mockSampler{decision: true} + propagator := NewJaegerPropagator(sampler) + + gotHeader, gotErr := propagator.Propagate(traceID) + + assert.NoError(t, gotErr) + assert.Contains(t, gotHeader, JaegerHeaderName) + + //nolint:staticcheck // as traceparent is not a canonical header + headerContent := gotHeader[JaegerHeaderName][0] + assert.True(t, strings.HasPrefix(headerContent, traceID+":")) + assert.True(t, strings.HasSuffix(headerContent, ":0:1")) + }) + + t.Run("Jaeger propagator with sampled trace", func(t *testing.T) { + t.Parallel() + + sampler := mockSampler{decision: true} + propagator := NewJaegerPropagator(sampler) + + gotHeader, gotErr := propagator.Propagate(traceID) + require.NoError(t, gotErr) + require.Contains(t, gotHeader, JaegerHeaderName) + + //nolint:staticcheck // as traceparent is not a canonical header + assert.True(t, strings.HasSuffix(gotHeader[JaegerHeaderName][0], ":1")) + }) + + t.Run("Jaeger propagator with unsampled trace", func(t *testing.T) { + t.Parallel() + + sampler := mockSampler{decision: false} + propagator := NewJaegerPropagator(sampler) + + gotHeader, gotErr := propagator.Propagate(traceID) + require.NoError(t, gotErr) + require.Contains(t, gotHeader, JaegerHeaderName) + + //nolint:staticcheck // as traceparent is not a canonical header + assert.True(t, strings.HasSuffix(gotHeader[JaegerHeaderName][0], ":0")) + }) +} + +type mockSampler struct { + decision bool +} + +func (m mockSampler) ShouldSample() bool { + return m.decision +}