Skip to content

Commit 31e8ef9

Browse files
authored
[tracegen] Allow use of adaptive sampling (#5718)
## Which problem is this PR solving? - Part of #5717 ## Description of the changes - Add a new flag to tracegen to allow it to use `jaegerremotesampling` contrib module with OTEL SDK ## How was this change tested? - It's not actually working at the moment, the backend does not recognize the spans without sampling tags Signed-off-by: Yuri Shkuro <[email protected]>
1 parent 13c715b commit 31e8ef9

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

cmd/tracegen/main.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import (
1919
"errors"
2020
"flag"
2121
"fmt"
22+
"time"
2223

2324
"github.com/go-logr/zapr"
25+
"go.opentelemetry.io/contrib/samplers/jaegerremote"
2426
"go.opentelemetry.io/otel"
2527
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
2628
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
@@ -39,6 +41,8 @@ import (
3941
"github.com/jaegertracing/jaeger/pkg/version"
4042
)
4143

44+
var flagAdaptiveSamplingEndpoint string
45+
4246
func main() {
4347
zc := zap.NewDevelopmentConfig()
4448
zc.Level = zap.NewAtomicLevelAt(zapcore.Level(-8)) // level used by OTEL's Debug()
@@ -51,6 +55,14 @@ func main() {
5155
fs := flag.CommandLine
5256
cfg := new(tracegen.Config)
5357
cfg.Flags(fs)
58+
fs.StringVar(
59+
&flagAdaptiveSamplingEndpoint,
60+
"adaptive-sampling",
61+
"",
62+
"HTTP endpoint to use to retrieve sampling strategies, "+
63+
"e.g. http://localhost:14268/api/sampling. "+
64+
"When not specified a standard SDK sampler will be used "+
65+
"(see OTEL_TRACES_SAMPLER env var in OTEL docs)")
5466
flag.Parse()
5567

5668
logger.Info(version.Get().String())
@@ -94,10 +106,21 @@ func createTracers(cfg *tracegen.Config, logger *zap.Logger) ([]trace.Tracer, fu
94106
logger.Sugar().Fatalf("resource creation failed: %s", err)
95107
}
96108

97-
tp := sdktrace.NewTracerProvider(
109+
opts := []sdktrace.TracerProviderOption{
98110
sdktrace.WithBatcher(exp, sdktrace.WithBlocking()),
99111
sdktrace.WithResource(res),
100-
)
112+
}
113+
if flagAdaptiveSamplingEndpoint != "" {
114+
jaegerRemoteSampler := jaegerremote.New(
115+
svc,
116+
jaegerremote.WithSamplingServerURL(flagAdaptiveSamplingEndpoint),
117+
jaegerremote.WithSamplingRefreshInterval(5*time.Second),
118+
jaegerremote.WithInitialSampler(sdktrace.TraceIDRatioBased(0.5)),
119+
)
120+
opts = append(opts, sdktrace.WithSampler(jaegerRemoteSampler))
121+
logger.Sugar().Infof("using adaptive sampling URL: %s", flagAdaptiveSamplingEndpoint)
122+
}
123+
tp := sdktrace.NewTracerProvider(opts...)
101124
tracers = append(tracers, tp.Tracer(cfg.Service))
102125
shutdown = append(shutdown, tp.Shutdown)
103126
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ require (
7171
go.opentelemetry.io/collector/receiver/otlpreceiver v0.104.0
7272
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0
7373
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0
74+
go.opentelemetry.io/contrib/samplers/jaegerremote v0.22.0
7475
go.opentelemetry.io/otel v1.28.0
7576
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0
7677
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 h1:4K4tsIX
489489
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0/go.mod h1:jjdQuTGVsXV4vSs+CJ2qYDeDPf9yIJV23qlIzBm73Vg=
490490
go.opentelemetry.io/contrib/propagators/b3 v1.27.0 h1:IjgxbomVrV9za6bRi8fWCNXENs0co37SZedQilP2hm0=
491491
go.opentelemetry.io/contrib/propagators/b3 v1.27.0/go.mod h1:Dv9obQz25lCisDvvs4dy28UPh974CxkahRDUPsY7y9E=
492+
go.opentelemetry.io/contrib/samplers/jaegerremote v0.22.0 h1:OYxqumWcd1yaV/qvCt1B7Sru9OeUNGjeXq/oldx3AGk=
493+
go.opentelemetry.io/contrib/samplers/jaegerremote v0.22.0/go.mod h1:2tZTRqCbvx7nG57wUwd5NQpNVujOWnR84iPLllIH0Ok=
492494
go.opentelemetry.io/contrib/zpages v0.52.0 h1:MPgkMy0Cp3O5EdfVXP0ss3ujhEibysTM4eszx7E7d+E=
493495
go.opentelemetry.io/contrib/zpages v0.52.0/go.mod h1:fqG5AFdoYru3A3DnhibVuaaEfQV2WKxE7fYE1jgDRwk=
494496
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -euf -o pipefail
4+
5+
# This script is currently a placeholder.
6+
7+
# Commands to run integration test:
8+
# SAMPLING_STORAGE_TYPE=memory SAMPLING_CONFIG_TYPE=adaptive go run -tags=ui ./cmd/all-in-one --log-level=debug
9+
# go run ./cmd/tracegen -adaptive-sampling=http://localhost:14268/api/sampling -pause=10ms -duration=60m
10+
11+
# Check how strategy is changing
12+
# curl 'http://localhost:14268/api/sampling?service=tracegen' | jq .
13+
14+
# Issues
15+
# - SDK does not report sampling probability in the tags the way Jaeger SDKs did
16+
# - Server probably does not recognize spans as having adaptive sampling without sampler info
17+
# - There is no way to modify target traces-per-second dynamically, must restart collector.

0 commit comments

Comments
 (0)