Skip to content

Commit

Permalink
Fix tracing module's Jaeger sampling flag (#3251)
Browse files Browse the repository at this point in the history
* Fix tracing module's inverted jaeger sampling flags

* Add tests asserting the tracing module's propagators flags are correct
  • Loading branch information
oleiade authored Aug 7, 2023
1 parent 5b5c5cc commit fcb19fb
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 4 deletions.
8 changes: 4 additions & 4 deletions js/modules/k6/experimental/tracing/propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 112 additions & 0 deletions js/modules/k6/experimental/tracing/propagator_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fcb19fb

Please sign in to comment.