Skip to content

Respecting -tracing.otel.sample-ratio configuration when enabling OpenTelemetry tracing with X-ray #4862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* [FEATURE] Storage/Bucket: Added `-*.s3.bucket-lookup-type` allowing to configure the s3 bucket lookup type. #4794
* [BUGFIX] Memberlist: Add join with no retrying when starting service. #4804
* [BUGFIX] Ruler: Fix /ruler/rule_groups returns YAML with extra fields. #4767
* [BUGFIX] Respecting `-tracing.otel.sample-ratio` configuration when enabling OpenTelemetry tracing with X-ray. #4862

## 1.13.0 2022-07-14

Expand Down
51 changes: 51 additions & 0 deletions pkg/tracing/sampler/sampling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package sampler

import (
"fmt"

sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

type randGenerator interface {
Float64() float64
}

type RandomRatioBased struct {
sdktrace.Sampler
rnd randGenerator
fraction float64
}

// NewRandomRatioBased crea
// fraction parameter should be between 0 and 1 where:
// fraction >= 1 it will always sample
// fraction <= 0 it will never sample
func NewRandomRatioBased(fraction float64, rnd randGenerator) sdktrace.Sampler {
if fraction >= 1 {
return sdktrace.AlwaysSample()
} else if fraction <= 0 {
return sdktrace.NeverSample()
}

return &RandomRatioBased{rnd: rnd, fraction: fraction}
}

func (s *RandomRatioBased) ShouldSample(p sdktrace.SamplingParameters) sdktrace.SamplingResult {
psc := trace.SpanContextFromContext(p.ParentContext)
shouldSample := s.rnd.Float64() < s.fraction
if shouldSample {
return sdktrace.SamplingResult{
Decision: sdktrace.RecordAndSample,
Tracestate: psc.TraceState(),
}
}
return sdktrace.SamplingResult{
Decision: sdktrace.Drop,
Tracestate: psc.TraceState(),
}
}

func (s *RandomRatioBased) Description() string {
return fmt.Sprintf("RandomRatioBased{%g}", s.fraction)
}
79 changes: 79 additions & 0 deletions pkg/tracing/sampler/sampling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package sampler

import (
"context"
"math/rand"
"testing"

"github.com/stretchr/testify/require"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

type mockGenerator struct {
mockedValue float64
}

func (g *mockGenerator) Float64() float64 {
return g.mockedValue
}

func Test_ShouldSample(t *testing.T) {
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
parentCtx := trace.ContextWithSpanContext(
context.Background(),
trace.NewSpanContext(trace.SpanContextConfig{
TraceState: trace.TraceState{},
}),
)

tests := []struct {
name string
samplingDecision sdktrace.SamplingDecision
fraction float64
generator randGenerator
}{
{
name: "should always sample",
samplingDecision: sdktrace.RecordAndSample,
fraction: 1,
generator: rand.New(rand.NewSource(rand.Int63())),
},
{
name: "should nerver sample",
samplingDecision: sdktrace.Drop,
fraction: 0,
generator: rand.New(rand.NewSource(rand.Int63())),
},
{
name: "should sample when fraction is above generated",
samplingDecision: sdktrace.RecordAndSample,
fraction: 0.5,
generator: &mockGenerator{0.2},
},
{
name: "should not sample when fraction is not above generated",
samplingDecision: sdktrace.Drop,
fraction: 0.5,
generator: &mockGenerator{0.8},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewRandomRatioBased(tt.fraction, tt.generator)
for i := 0; i < 100; i++ {

r := s.ShouldSample(
sdktrace.SamplingParameters{
ParentContext: parentCtx,
TraceID: traceID,
Name: "test",
Kind: trace.SpanKindServer,
})

require.Equal(t, tt.samplingDecision, r.Decision)
}
})
}
}
7 changes: 6 additions & 1 deletion pkg/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"flag"
"fmt"
"math/rand"
"strings"
"time"

"github.com/go-kit/log/level"
"github.com/pkg/errors"
Expand All @@ -22,6 +24,7 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"

"github.com/cortexproject/cortex/pkg/tracing/migration"
"github.com/cortexproject/cortex/pkg/tracing/sampler"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/cortexproject/cortex/pkg/util/tls"
)
Expand Down Expand Up @@ -118,13 +121,15 @@ func SetupTracing(ctx context.Context, name string, c Config) (func(context.Cont
func newTraceProvider(name string, c Config, exporter *otlptrace.Exporter) *sdktrace.TracerProvider {
options := []sdktrace.TracerProviderOption{
sdktrace.WithBatcher(exporter),
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Otel.SampleRatio))),
sdktrace.WithResource(newResource(name)),
}

switch strings.ToLower(c.Otel.ExporterType) {
case "awsxray":
options = append(options, sdktrace.WithIDGenerator(xray.NewIDGenerator()))
options = append(options, sdktrace.WithSampler(sdktrace.ParentBased(sampler.NewRandomRatioBased(c.Otel.SampleRatio, rand.New(rand.NewSource(time.Now().Unix()))))))
default:
options = append(options, sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Otel.SampleRatio))))
}

return sdktrace.NewTracerProvider(options...)
Expand Down