Skip to content

Commit 27dd649

Browse files
meobilivangrichardcase
authored andcommitted
add sampling rate
1 parent dbf66f7 commit 27dd649

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ var (
8484
leaderElectionRenewDeadline time.Duration
8585
leaderElectionRetryPeriod time.Duration
8686
enableTracing bool
87+
samplingRate float64
8788
)
8889

8990
// Add RBAC for the authorized diagnostics endpoint.
@@ -165,7 +166,7 @@ func main() {
165166
ctx := ctrl.SetupSignalHandler()
166167

167168
if enableTracing {
168-
if err := ot.RegisterTracing(ctx, setupLog); err != nil {
169+
if err := ot.RegisterTracing(ctx, samplingRate, setupLog); err != nil {
169170
setupLog.Error(err, "unable to set up tracing")
170171
os.Exit(1)
171172
}
@@ -389,6 +390,12 @@ func initFlags(fs *pflag.FlagSet) {
389390
"Enable collecting and sending traces to opentelemetry-collector service",
390391
)
391392

393+
fs.Float64Var(&samplingRate,
394+
"trace-sampling-rate",
395+
0.6,
396+
"The fraction of all the traces will be sample",
397+
)
398+
392399
flags.AddManagerOptions(fs, &managerOptions)
393400

394401
feature.MutableGates.AddFlag(fs)

pkg/otel/tracing.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
2121
)
2222

23-
func RegisterTracing(ctx context.Context, log logr.Logger) error {
23+
func RegisterTracing(ctx context.Context, samplingRate float64, log logr.Logger) error {
2424

25-
tracerProvider, err := SetUpTracing(ctx)
25+
tracerProvider, err := SetUpTracing(ctx, samplingRate)
2626
if err != nil {
2727
return err
2828
}
@@ -64,7 +64,7 @@ func newExporter(ctx context.Context) (*otlptrace.Exporter, error) {
6464
return traceExporter, nil
6565
}
6666

67-
func SetUpTracing(ctx context.Context) (*trace.TracerProvider, error) {
67+
func SetUpTracing(ctx context.Context, samplingRate float64) (*trace.TracerProvider, error) {
6868

6969
traceExporter, err := newExporter(ctx)
7070

@@ -89,9 +89,8 @@ func SetUpTracing(ctx context.Context) (*trace.TracerProvider, error) {
8989
traceProvider := trace.NewTracerProvider(
9090
trace.WithBatcher(traceExporter),
9191
trace.WithResource(resource),
92-
// TODO: dynamic sampling rate?
93-
// sampling rate based on parent span = 60%
94-
trace.WithSampler(trace.ParentBased(trace.TraceIDRatioBased(0.6))),
92+
// 0 < samplingRate <= 1 (< 0 -> be treated as 0; >= 1 -> always sample)
93+
trace.WithSampler(trace.ParentBased(trace.TraceIDRatioBased(samplingRate))),
9594
)
9695

9796
otel.SetTracerProvider(traceProvider)

0 commit comments

Comments
 (0)