From cf810828b8bfc4d76e87c22e3c04ae264802c08a Mon Sep 17 00:00:00 2001 From: Shazi <42436533+shayyxi@users.noreply.github.com> Date: Thu, 4 May 2023 13:56:17 +0200 Subject: [PATCH] Traces sampler env var (#6306) * Issue#5947 OTEL_TRACES_SAMPLER env var Signed-off-by: shayyxi * Test correction Signed-off-by: shayyxi * doc failure correction. parse float argument correction. Signed-off-by: shayyxi * added the changelog. Signed-off-by: shayyxi * ran make docs to fix the build failure. Signed-off-by: shayyxi * corrected the incorrect change in tools.md Signed-off-by: shayyxi * fixed review comments. Signed-off-by: shayyxi --------- Signed-off-by: shayyxi Signed-off-by: Shazi <42436533+shayyxi@users.noreply.github.com> Co-authored-by: shayyxi --- CHANGELOG.md | 1 + docs/tracing.md | 2 ++ pkg/tracing/otlp/config_yaml.go | 2 ++ pkg/tracing/otlp/otlp.go | 31 +++++++++++++++++++++++++++---- pkg/tracing/otlp/otlp_test.go | 3 ++- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3126c1bf6a..afee8458a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re - [#6222](https://github.com/thanos-io/thanos/pull/6222) mixin(Receive): Fix tenant series received charts. - [#6218](https://github.com/thanos-io/thanos/pull/6218) mixin(Store): handle ResourceExhausted as a non-server error. As a consequence, this error won't contribute to Store's grpc errors alerts. - [#6271](https://github.com/thanos-io/thanos/pull/6271) Receive: Fix segfault in `LabelValues` during head compaction. +- [#6306](https://github.com/thanos-io/thanos/pull/6306) Tracing: tracing in OTLP utilize the OTEL_TRACES_SAMPLER env variable - [#6330](https://github.com/thanos-io/thanos/pull/6330) Store: Fix inconsistent error for series limits. ### Changed diff --git a/docs/tracing.md b/docs/tracing.md index 8e55672871..dd8cf998e4 100644 --- a/docs/tracing.md +++ b/docs/tracing.md @@ -97,6 +97,8 @@ config: key_file: "" server_name: "" insecure_skip_verify: false + sampler_type: "" + sampler_param: "" ``` ### Jaeger diff --git a/pkg/tracing/otlp/config_yaml.go b/pkg/tracing/otlp/config_yaml.go index caa9ceb5ba..b8aa6f9dc8 100644 --- a/pkg/tracing/otlp/config_yaml.go +++ b/pkg/tracing/otlp/config_yaml.go @@ -31,6 +31,8 @@ type Config struct { RetryConfig retryConfig `yaml:"retry_config"` Headers map[string]string `yaml:"headers"` TLSConfig exthttp.TLSConfig `yaml:"tls_config"` + SamplerType string `yaml:"sampler_type"` + SamplerParam string `yaml:"sampler_param"` } func traceGRPCOptions(config Config) []otlptracegrpc.Option { diff --git a/pkg/tracing/otlp/otlp.go b/pkg/tracing/otlp/otlp.go index 7f0890a790..1fa251670b 100644 --- a/pkg/tracing/otlp/otlp.go +++ b/pkg/tracing/otlp/otlp.go @@ -5,6 +5,7 @@ package otlp import ( "context" + "strconv" "strings" "github.com/thanos-io/thanos/pkg/tracing/migration" @@ -25,6 +26,9 @@ import ( const ( TracingClientGRPC string = "grpc" TracingClientHTTP string = "http" + AlwaysSample string = "alwayssample" + NeverSample string = "neversample" + RatioBasedSample string = "traceidratiobased" ) // NewOTELTracer returns an OTLP exporter based tracer. @@ -59,12 +63,16 @@ func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tr } processor := tracesdk.NewBatchSpanProcessor(exporter) - tp := newTraceProvider(ctx, processor, logger, config.ServiceName) + sampler, err := getSampler(config) + if err != nil { + logger.Log(err) + } + tp := newTraceProvider(ctx, processor, logger, config.ServiceName, sampler) return tp, nil } -func newTraceProvider(ctx context.Context, processor tracesdk.SpanProcessor, logger log.Logger, serviceName string) *tracesdk.TracerProvider { +func newTraceProvider(ctx context.Context, processor tracesdk.SpanProcessor, logger log.Logger, serviceName string, sampler tracesdk.Sampler) *tracesdk.TracerProvider { resource, err := resource.New( ctx, resource.WithAttributes(semconv.ServiceNameKey.String(serviceName)), @@ -73,8 +81,6 @@ func newTraceProvider(ctx context.Context, processor tracesdk.SpanProcessor, log level.Warn(logger).Log("msg", "jaeger: detecting resources for tracing provider failed", "err", err) } - sampler := tracesdk.ParentBased(tracesdk.TraceIDRatioBased(1.0)) - tp := tracesdk.NewTracerProvider( tracesdk.WithSpanProcessor(processor), tracesdk.WithResource(resource), @@ -86,3 +92,20 @@ func newTraceProvider(ctx context.Context, processor tracesdk.SpanProcessor, log ) return tp } + +func getSampler(config Config) (tracesdk.Sampler, error) { + switch strings.ToLower(config.SamplerType) { + case AlwaysSample: + return tracesdk.ParentBased(tracesdk.AlwaysSample()), nil + case NeverSample: + return tracesdk.ParentBased(tracesdk.NeverSample()), nil + case RatioBasedSample: + arg, err := strconv.ParseFloat(config.SamplerParam, 64) + if err != nil { + return tracesdk.ParentBased(tracesdk.TraceIDRatioBased(1.0)), err + } + return tracesdk.ParentBased(tracesdk.TraceIDRatioBased(arg)), nil + } + + return tracesdk.ParentBased(tracesdk.TraceIDRatioBased(1.0)), nil +} diff --git a/pkg/tracing/otlp/otlp_test.go b/pkg/tracing/otlp/otlp_test.go index 979942d85b..1f651e9c87 100644 --- a/pkg/tracing/otlp/otlp_test.go +++ b/pkg/tracing/otlp/otlp_test.go @@ -24,7 +24,8 @@ func TestContextTracing_ClientEnablesTracing(t *testing.T) { context.Background(), tracesdk.NewSimpleSpanProcessor(exp), log.NewNopLogger(), - "thanos") + "thanos", + tracesdk.AlwaysSample()) tracer, _ := migration.Bridge(tracerOtel, log.NewNopLogger()) clientRoot, _ := tracing.StartSpan(tracing.ContextWithTracer(context.Background(), tracer), "a")