Last active
November 6, 2021 00:59
-
-
Save cartermp/2e2f32735709fb264f6d9e3797aa5be3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"go.opentelemetry.io/otel/attribute" | |
sdktrace "go.opentelemetry.io/otel/sdk/trace" | |
) | |
var ( | |
ErrInvalidSampleRate = errors.New("sample rate must be >= 1") | |
) | |
type deterministicSampler struct { | |
sampleRate int | |
traceIDRatioSampler sdktrace.Sampler | |
} | |
func DeterministicSampler(sampleRate int) (*deterministicSampler, error) { | |
if sampleRate < 1 { | |
return nil, ErrInvalidSampleRate | |
} | |
return &deterministicSampler{ | |
sampleRate: sampleRate, | |
traceIDRatioSampler: sdktrace.TraceIDRatioBased(1.0 / float64(sampleRate)), | |
}, nil | |
} | |
func (ds *deterministicSampler) ShouldSample(p sdktrace.SamplingParameters) sdktrace.SamplingResult { | |
attrs := []attribute.KeyValue{ | |
attribute.Int("SampleRate", int(ds.sampleRate)), | |
} | |
delegatedResult := ds.traceIDRatioSampler.ShouldSample(p) | |
return sdktrace.SamplingResult{ | |
Decision: delegatedResult.Decision, | |
Attributes: attrs, | |
Tracestate: delegatedResult.Tracestate, | |
} | |
} | |
func (ds *deterministicSampler) Description() string { | |
return "HoneycombDeterministicSampler" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment