Skip to content

Instantly share code, notes, and snippets.

@cartermp
Last active November 6, 2021 00:59
Show Gist options
  • Save cartermp/2e2f32735709fb264f6d9e3797aa5be3 to your computer and use it in GitHub Desktop.
Save cartermp/2e2f32735709fb264f6d9e3797aa5be3 to your computer and use it in GitHub Desktop.
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