diff --git a/CHANGELOG.md b/CHANGELOG.md index b0de1d27ddc..91a959e6050 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * [CHANGE] Alertmanager: Local file disclosure vulnerability in OpsGenie configuration has been fixed. #5045 * [CHANGE] Rename oltp_endpoint to otlp_endpoint to match opentelemetry spec and lib name. #5067 * [CHANGE] Distributor/Ingester: Log warn level on push requests when they have status code 4xx. Do not log if status is 429. #5103 +* [CHANGE] Tracing: Use the default OTEL trace sampler when `-tracing.otel.exporter-type` is set to `awsxray`. #5141 * [ENHANCEMENT] Update Go version to 1.19.3. #4988 * [ENHANCEMENT] Querier: limit series query to only ingesters if `start` param is not specified. #4976 * [ENHANCEMENT] Query-frontend/scheduler: add a new limit `frontend.max-outstanding-requests-per-tenant` for configuring queue size per tenant. Started deprecating two flags `-query-scheduler.max-outstanding-requests-per-tenant` and `-querier.max-outstanding-requests-per-tenant`, and change their value default to 0. Now if both the old flag and new flag are specified, the old flag's queue size will be picked. #5005 diff --git a/pkg/tracing/sampler/sampling.go b/pkg/tracing/sampler/sampling.go deleted file mode 100644 index 068f48a1e83..00000000000 --- a/pkg/tracing/sampler/sampling.go +++ /dev/null @@ -1,55 +0,0 @@ -package sampler - -import ( - "encoding/binary" - "fmt" - "math" - - sdktrace "go.opentelemetry.io/otel/sdk/trace" - "go.opentelemetry.io/otel/trace" -) - -type xrayTraceIDRatioBased struct { - sdktrace.Sampler - max uint64 -} - -// NewXrayTraceIDRatioBased creates a sampler based on random number. -// fraction parameter should be between 0 and 1 where: -// fraction >= 1 it will always sample -// fraction <= 0 it will never sample -func NewXrayTraceIDRatioBased(fraction float64) sdktrace.Sampler { - if fraction >= 1 { - return sdktrace.AlwaysSample() - } else if fraction <= 0 { - return sdktrace.NeverSample() - } - - return &xrayTraceIDRatioBased{max: uint64(fraction * math.MaxUint64)} -} - -func (s *xrayTraceIDRatioBased) ShouldSample(p sdktrace.SamplingParameters) sdktrace.SamplingResult { - // The default otel sampler pick the first 8 bytes to make the sampling decision and this is a problem to - // xray case as the first 4 bytes on the xray traceId is the time of the original request and the random part are - // the 12 last bytes, and so, this sampler pick the last 8 bytes to make the sampling decision. - // Xray Trace format: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html - // Xray Id Generator: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/54f0bc5c0fd347cd6db9b7bc14c9f0c00dfcb36b/propagators/aws/xray/idgenerator.go#L58-L63 - // Ref: https://github.com/open-telemetry/opentelemetry-go/blob/7a60bc785d669fa6ad26ba70e88151d4df631d90/sdk/trace/sampling.go#L82-L95 - val := binary.BigEndian.Uint64(p.TraceID[8:16]) - psc := trace.SpanContextFromContext(p.ParentContext) - shouldSample := val < s.max - if shouldSample { - return sdktrace.SamplingResult{ - Decision: sdktrace.RecordAndSample, - Tracestate: psc.TraceState(), - } - } - return sdktrace.SamplingResult{ - Decision: sdktrace.Drop, - Tracestate: psc.TraceState(), - } -} - -func (s *xrayTraceIDRatioBased) Description() string { - return fmt.Sprintf("xrayTraceIDRatioBased{%v}", s.max) -} diff --git a/pkg/tracing/sampler/sampling_test.go b/pkg/tracing/sampler/sampling_test.go deleted file mode 100644 index 892392ab2f6..00000000000 --- a/pkg/tracing/sampler/sampling_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package sampler - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" - "go.opentelemetry.io/contrib/propagators/aws/xray" - sdktrace "go.opentelemetry.io/otel/sdk/trace" - "go.opentelemetry.io/otel/trace" -) - -func Test_ShouldSample(t *testing.T) { - parentCtx := trace.ContextWithSpanContext( - context.Background(), - trace.NewSpanContext(trace.SpanContextConfig{ - TraceState: trace.TraceState{}, - }), - ) - - generator := xray.NewIDGenerator() - - tests := []struct { - name string - fraction float64 - }{ - { - name: "should always sample", - fraction: 1, - }, - { - name: "should nerver sample", - fraction: 0, - }, - { - name: "should sample 50%", - fraction: 0.5, - }, - { - name: "should sample 10%", - fraction: 0.1, - }, - } - - totalIterations := 10000 - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - totalSampled := 0 - s := NewXrayTraceIDRatioBased(tt.fraction) - for i := 0; i < totalIterations; i++ { - traceID, _ := generator.NewIDs(context.Background()) - r := s.ShouldSample( - sdktrace.SamplingParameters{ - ParentContext: parentCtx, - TraceID: traceID, - Name: "test", - Kind: trace.SpanKindServer, - }) - if r.Decision == sdktrace.RecordAndSample { - totalSampled++ - } - } - - tolerance := 0.1 - expected := tt.fraction * float64(totalIterations) - require.InDelta(t, expected, totalSampled, expected*tolerance) - }) - } -} diff --git a/pkg/tracing/tracing.go b/pkg/tracing/tracing.go index b9cb069062b..33f90ee5bd2 100644 --- a/pkg/tracing/tracing.go +++ b/pkg/tracing/tracing.go @@ -22,7 +22,6 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.17.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" ) @@ -147,12 +146,12 @@ func newTraceProvider(r *resource.Resource, c Config, exporter *otlptrace.Export switch strings.ToLower(c.Otel.ExporterType) { case "awsxray": options = append(options, sdktrace.WithIDGenerator(xray.NewIDGenerator())) - options = append(options, sdktrace.WithSampler(sdktrace.ParentBased(sampler.NewXrayTraceIDRatioBased(c.Otel.SampleRatio)))) propagator = xray.Propagator{} default: - options = append(options, sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Otel.SampleRatio)))) } + options = append(options, sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Otel.SampleRatio)))) + return propagator, sdktrace.NewTracerProvider(options...) }