| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Copyright (c) 2021 The Jaeger Authors. |
| 5 |
// Copyright (c) 2017 Uber Technologies, Inc. |
| 6 |
// |
| 7 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
// you may not use this file except in compliance with the License. |
| 9 |
// You may obtain a copy of the License at |
| 10 |
// |
| 11 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
// |
| 13 |
// Unless required by applicable law or agreed to in writing, software |
| 14 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
// See the License for the specific language governing permissions and |
| 17 |
// limitations under the License. |
| 18 |
|
| 19 |
package jaegerremote // import "go.opentelemetry.io/contrib/samplers/jaegerremote" |
| 20 |
|
| 21 |
import ( |
| 22 |
"fmt" |
| 23 |
"math" |
| 24 |
"sync" |
| 25 |
|
| 26 |
jaeger_api_v2 "github.com/jaegertracing/jaeger-idl/proto-gen/api_v2" |
| 27 |
"go.opentelemetry.io/otel/attribute" |
| 28 |
"go.opentelemetry.io/otel/sdk/trace" |
| 29 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 30 |
|
| 31 |
"go.opentelemetry.io/contrib/samplers/jaegerremote/internal/ratelimiter" |
| 32 |
) |
| 33 |
|
| 34 |
const ( |
| 35 |
defaultMaxOperations = 2000 |
| 36 |
) |
| 37 |
|
| 38 |
const ( |
| 39 |
samplerTypeKey = "jaeger.sampler.type" |
| 40 |
samplerParamKey = "jaeger.sampler.param" |
| 41 |
samplerTypeValueProbabilistic = "probabilistic" |
| 42 |
samplerTypeValueRateLimiting = "ratelimiting" |
| 43 |
) |
| 44 |
|
| 45 |
// ----------------------- |
| 46 |
|
| 47 |
// probabilisticSampler is a sampler that randomly samples a certain percentage |
| 48 |
// of traces. |
| 49 |
type probabilisticSampler struct { |
| 50 |
samplingRate float64 |
| 51 |
sampler trace.Sampler |
| 52 |
attributes []attribute.KeyValue |
| 53 |
attributesDisabled bool |
| 54 |
} |
| 55 |
|
| 56 |
// newProbabilisticSampler creates a sampler that randomly samples a certain percentage of traces specified by the |
| 57 |
// samplingRate, in the range between 0.0 and 1.0. it utilizes the SDK `trace.TraceIDRatioBased` sampler. |
| 58 |
func newProbabilisticSampler(samplingRate float64, attributesDisabled bool) *probabilisticSampler { |
| 59 |
s := &probabilisticSampler{ |
| 60 |
attributesDisabled: attributesDisabled, |
| 61 |
} |
| 62 |
return s.init(samplingRate) |
| 63 |
} |
| 64 |
|
| 65 |
func (s *probabilisticSampler) init(samplingRate float64) *probabilisticSampler { |
| 66 |
s.samplingRate = math.Max(0.0, math.Min(samplingRate, 1.0)) |
| 67 |
s.sampler = trace.TraceIDRatioBased(s.samplingRate) |
| 68 |
if s.attributesDisabled { |
| 69 |
return s |
| 70 |
} |
| 71 |
s.attributes = []attribute.KeyValue{attribute.String(samplerTypeKey, samplerTypeValueProbabilistic), attribute.Float64(samplerParamKey, s.samplingRate)} |
| 72 |
return s |
| 73 |
} |
| 74 |
|
| 75 |
// SamplingRate returns the sampling probability this sampled was constructed with. |
| 76 |
func (s *probabilisticSampler) SamplingRate() float64 { |
| 77 |
return s.samplingRate |
| 78 |
} |
| 79 |
|
| 80 |
func (s *probabilisticSampler) ShouldSample(p trace.SamplingParameters) trace.SamplingResult { |
| 81 |
r := s.sampler.ShouldSample(p) |
| 82 |
if r.Decision == trace.Drop { |
| 83 |
return r |
| 84 |
} |
| 85 |
r.Attributes = s.attributes |
| 86 |
return r |
| 87 |
} |
| 88 |
|
| 89 |
// Equal compares with another sampler. |
| 90 |
func (s *probabilisticSampler) Equal(other trace.Sampler) bool { |
| 91 |
if o, ok := other.(*probabilisticSampler); ok { |
| 92 |
return math.Abs(s.samplingRate-o.samplingRate) < 1e-9 // consider equal if within 0.000001% |
| 93 |
} |
| 94 |
return false |
| 95 |
} |
| 96 |
|
| 97 |
// Update modifies in-place the sampling rate. Locking must be done externally. |
| 98 |
func (s *probabilisticSampler) Update(samplingRate float64) error { |
| 99 |
if samplingRate < 0.0 || samplingRate > 1.0 { |
| 100 |
return fmt.Errorf("sampling rate must be between 0.0 and 1.0, received %f", samplingRate) |
| 101 |
} |
| 102 |
s.init(samplingRate) |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
func (s *probabilisticSampler) Description() string { |
| 107 |
return s.sampler.Description() |
| 108 |
} |
| 109 |
|
| 110 |
// ----------------------- |
| 111 |
|
| 112 |
// rateLimitingSampler samples at most maxTracesPerSecond. The distribution of sampled traces follows |
| 113 |
// burstiness of the service, i.e. a service with uniformly distributed requests will have those |
| 114 |
// requests sampled uniformly as well, but if requests are bursty, especially sub-second, then a |
| 115 |
// number of sequential requests can be sampled each second. |
| 116 |
type rateLimitingSampler struct { |
| 117 |
maxTracesPerSecond float64 |
| 118 |
rateLimiter *ratelimiter.RateLimiter |
| 119 |
attributes []attribute.KeyValue |
| 120 |
attributesDisabled bool |
| 121 |
} |
| 122 |
|
| 123 |
// newRateLimitingSampler creates new rateLimitingSampler. |
| 124 |
func newRateLimitingSampler(maxTracesPerSecond float64, attributesDisabled bool) *rateLimitingSampler { |
| 125 |
s := &rateLimitingSampler{ |
| 126 |
attributesDisabled: attributesDisabled, |
| 127 |
} |
| 128 |
|
| 129 |
return s.init(maxTracesPerSecond) |
| 130 |
} |
| 131 |
|
| 132 |
func (s *rateLimitingSampler) init(maxTracesPerSecond float64) *rateLimitingSampler { |
| 133 |
if s.rateLimiter == nil { |
| 134 |
s.rateLimiter = ratelimiter.NewRateLimiter(maxTracesPerSecond, math.Max(maxTracesPerSecond, 1.0)) |
| 135 |
} else { |
| 136 |
s.rateLimiter.Update(maxTracesPerSecond, math.Max(maxTracesPerSecond, 1.0)) |
| 137 |
} |
| 138 |
s.maxTracesPerSecond = maxTracesPerSecond |
| 139 |
if s.attributesDisabled { |
| 140 |
return s |
| 141 |
} |
| 142 |
s.attributes = []attribute.KeyValue{attribute.String(samplerTypeKey, samplerTypeValueRateLimiting), attribute.Float64(samplerParamKey, s.maxTracesPerSecond)} |
| 143 |
return s |
| 144 |
} |
| 145 |
|
| 146 |
func (s *rateLimitingSampler) ShouldSample(p trace.SamplingParameters) trace.SamplingResult { |
| 147 |
psc := oteltrace.SpanContextFromContext(p.ParentContext) |
| 148 |
if s.rateLimiter.CheckCredit(1.0) { |
| 149 |
return trace.SamplingResult{ |
| 150 |
Decision: trace.RecordAndSample, |
| 151 |
Tracestate: psc.TraceState(), |
| 152 |
Attributes: s.attributes, |
| 153 |
} |
| 154 |
} |
| 155 |
return trace.SamplingResult{ |
| 156 |
Decision: trace.Drop, |
| 157 |
Tracestate: psc.TraceState(), |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Update reconfigures the rate limiter, while preserving its accumulated balance. |
| 162 |
// Locking must be done externally. |
| 163 |
func (s *rateLimitingSampler) Update(maxTracesPerSecond float64) { |
| 164 |
if s.maxTracesPerSecond != maxTracesPerSecond { |
| 165 |
s.init(maxTracesPerSecond) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Equal compares with another sampler. |
| 170 |
func (s *rateLimitingSampler) Equal(other trace.Sampler) bool { |
| 171 |
if o, ok := other.(*rateLimitingSampler); ok { |
| 172 |
return s.maxTracesPerSecond == o.maxTracesPerSecond |
| 173 |
} |
| 174 |
return false |
| 175 |
} |
| 176 |
|
| 177 |
func (*rateLimitingSampler) Description() string { |
| 178 |
return "rateLimitingSampler{}" |
| 179 |
} |
| 180 |
|
| 181 |
// ----------------------- |
| 182 |
|
| 183 |
// guaranteedThroughputProbabilisticSampler is a sampler that leverages both probabilisticSampler and |
| 184 |
// rateLimitingSampler. The rateLimitingSampler is used as a guaranteed lower bound sampler such that |
| 185 |
// every operation is sampled at least once in a time interval defined by the lowerBound. ie a lowerBound |
| 186 |
// of 1.0 / (60 * 10) will sample an operation at least once every 10 minutes. |
| 187 |
// |
| 188 |
// The probabilisticSampler is given higher priority when tags are emitted, ie. if IsSampled() for both |
| 189 |
// samplers return true, the tags for probabilisticSampler will be used. |
| 190 |
type guaranteedThroughputProbabilisticSampler struct { |
| 191 |
probabilisticSampler *probabilisticSampler |
| 192 |
lowerBoundSampler *rateLimitingSampler |
| 193 |
samplingRate float64 |
| 194 |
lowerBound float64 |
| 195 |
attributesDisabled bool |
| 196 |
} |
| 197 |
|
| 198 |
func newGuaranteedThroughputProbabilisticSampler(lowerBound, samplingRate float64, attributesDisabled bool) *guaranteedThroughputProbabilisticSampler { |
| 199 |
s := &guaranteedThroughputProbabilisticSampler{ |
| 200 |
lowerBoundSampler: newRateLimitingSampler(lowerBound, attributesDisabled), |
| 201 |
lowerBound: lowerBound, |
| 202 |
attributesDisabled: attributesDisabled, |
| 203 |
} |
| 204 |
s.setProbabilisticSampler(samplingRate) |
| 205 |
return s |
| 206 |
} |
| 207 |
|
| 208 |
func (s *guaranteedThroughputProbabilisticSampler) setProbabilisticSampler(samplingRate float64) { |
| 209 |
if s.probabilisticSampler == nil { |
| 210 |
s.probabilisticSampler = newProbabilisticSampler(samplingRate, s.attributesDisabled) |
| 211 |
} else if s.samplingRate != samplingRate { |
| 212 |
s.probabilisticSampler.init(samplingRate) |
| 213 |
} |
| 214 |
// since we don't validate samplingRate, sampler may have clamped it to [0, 1] interval |
| 215 |
s.samplingRate = s.probabilisticSampler.SamplingRate() |
| 216 |
} |
| 217 |
|
| 218 |
func (s *guaranteedThroughputProbabilisticSampler) ShouldSample(p trace.SamplingParameters) trace.SamplingResult { |
| 219 |
if result := s.probabilisticSampler.ShouldSample(p); result.Decision == trace.RecordAndSample { |
| 220 |
s.lowerBoundSampler.ShouldSample(p) |
| 221 |
return result |
| 222 |
} |
| 223 |
result := s.lowerBoundSampler.ShouldSample(p) |
| 224 |
return result |
| 225 |
} |
| 226 |
|
| 227 |
// this function should only be called while holding a Write lock. |
| 228 |
func (s *guaranteedThroughputProbabilisticSampler) update(lowerBound, samplingRate float64) { |
| 229 |
s.setProbabilisticSampler(samplingRate) |
| 230 |
if s.lowerBound != lowerBound { |
| 231 |
s.lowerBoundSampler.Update(lowerBound) |
| 232 |
s.lowerBound = lowerBound |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
func (*guaranteedThroughputProbabilisticSampler) Description() string { |
| 237 |
return "guaranteedThroughputProbabilisticSampler{}" |
| 238 |
} |
| 239 |
|
| 240 |
// ----------------------- |
| 241 |
|
| 242 |
// perOperationSampler is a delegating sampler that applies guaranteedThroughputProbabilisticSampler |
| 243 |
// on a per-operation basis. |
| 244 |
type perOperationSampler struct { |
| 245 |
sync.RWMutex |
| 246 |
|
| 247 |
samplers map[string]*guaranteedThroughputProbabilisticSampler |
| 248 |
defaultSampler *probabilisticSampler |
| 249 |
lowerBound float64 |
| 250 |
maxOperations int |
| 251 |
|
| 252 |
// see description in perOperationSamplerParams |
| 253 |
operationNameLateBinding bool |
| 254 |
attributesDisabled bool |
| 255 |
} |
| 256 |
|
| 257 |
// perOperationSamplerParams defines parameters when creating perOperationSampler. |
| 258 |
type perOperationSamplerParams struct { |
| 259 |
// Max number of operations that will be tracked. Other operations will be given default strategy. |
| 260 |
MaxOperations int |
| 261 |
|
| 262 |
// Opt-in feature for applications that require late binding of span name via explicit call to SetOperationName. |
| 263 |
// When this feature is enabled, the sampler will return retryable=true from OnCreateSpan(), thus leaving |
| 264 |
// the sampling decision as non-final (and the span as writeable). This may lead to degraded performance |
| 265 |
// in applications that always provide the correct span name on oteltrace creation. |
| 266 |
// |
| 267 |
// For backwards compatibility this option is off by default. |
| 268 |
OperationNameLateBinding bool |
| 269 |
|
| 270 |
// Initial configuration of the sampling strategies (usually retrieved from the backend by Remote Sampler). |
| 271 |
Strategies *jaeger_api_v2.PerOperationSamplingStrategies |
| 272 |
} |
| 273 |
|
| 274 |
// newPerOperationSampler returns a new perOperationSampler. |
| 275 |
func newPerOperationSampler(params perOperationSamplerParams, attributesDisabled bool) *perOperationSampler { |
| 276 |
if params.MaxOperations <= 0 { |
| 277 |
params.MaxOperations = defaultMaxOperations |
| 278 |
} |
| 279 |
samplers := make(map[string]*guaranteedThroughputProbabilisticSampler) |
| 280 |
for _, strategy := range params.Strategies.PerOperationStrategies { |
| 281 |
sampler := newGuaranteedThroughputProbabilisticSampler( |
| 282 |
params.Strategies.DefaultLowerBoundTracesPerSecond, |
| 283 |
strategy.ProbabilisticSampling.SamplingRate, |
| 284 |
attributesDisabled, |
| 285 |
) |
| 286 |
samplers[strategy.Operation] = sampler |
| 287 |
} |
| 288 |
return &perOperationSampler{ |
| 289 |
samplers: samplers, |
| 290 |
defaultSampler: newProbabilisticSampler(params.Strategies.DefaultSamplingProbability, attributesDisabled), |
| 291 |
lowerBound: params.Strategies.DefaultLowerBoundTracesPerSecond, |
| 292 |
maxOperations: params.MaxOperations, |
| 293 |
operationNameLateBinding: params.OperationNameLateBinding, |
| 294 |
attributesDisabled: attributesDisabled, |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func (s *perOperationSampler) ShouldSample(p trace.SamplingParameters) trace.SamplingResult { |
| 299 |
sampler := s.getSamplerForOperation(p.Name) |
| 300 |
return sampler.ShouldSample(p) |
| 301 |
} |
| 302 |
|
| 303 |
func (s *perOperationSampler) getSamplerForOperation(operation string) trace.Sampler { |
| 304 |
s.RLock() |
| 305 |
sampler, ok := s.samplers[operation] |
| 306 |
if ok { |
| 307 |
defer s.RUnlock() |
| 308 |
return sampler |
| 309 |
} |
| 310 |
s.RUnlock() |
| 311 |
s.Lock() |
| 312 |
defer s.Unlock() |
| 313 |
|
| 314 |
// Check if sampler has already been created |
| 315 |
sampler, ok = s.samplers[operation] |
| 316 |
if ok { |
| 317 |
return sampler |
| 318 |
} |
| 319 |
// Store only up to maxOperations of unique ops. |
| 320 |
if len(s.samplers) >= s.maxOperations { |
| 321 |
return s.defaultSampler |
| 322 |
} |
| 323 |
newSampler := newGuaranteedThroughputProbabilisticSampler(s.lowerBound, s.defaultSampler.SamplingRate(), s.attributesDisabled) |
| 324 |
s.samplers[operation] = newSampler |
| 325 |
return newSampler |
| 326 |
} |
| 327 |
|
| 328 |
func (*perOperationSampler) Description() string { |
| 329 |
return "perOperationSampler{}" |
| 330 |
} |
| 331 |
|
| 332 |
func (s *perOperationSampler) update(strategies *jaeger_api_v2.PerOperationSamplingStrategies) { |
| 333 |
s.Lock() |
| 334 |
defer s.Unlock() |
| 335 |
newSamplers := map[string]*guaranteedThroughputProbabilisticSampler{} |
| 336 |
for _, strategy := range strategies.PerOperationStrategies { |
| 337 |
operation := strategy.Operation |
| 338 |
samplingRate := strategy.ProbabilisticSampling.SamplingRate |
| 339 |
lowerBound := strategies.DefaultLowerBoundTracesPerSecond |
| 340 |
if sampler, ok := s.samplers[operation]; ok { |
| 341 |
sampler.update(lowerBound, samplingRate) |
| 342 |
newSamplers[operation] = sampler |
| 343 |
} else { |
| 344 |
sampler := newGuaranteedThroughputProbabilisticSampler( |
| 345 |
lowerBound, |
| 346 |
samplingRate, |
| 347 |
s.attributesDisabled, |
| 348 |
) |
| 349 |
newSamplers[operation] = sampler |
| 350 |
} |
| 351 |
} |
| 352 |
s.lowerBound = strategies.DefaultLowerBoundTracesPerSecond |
| 353 |
if s.defaultSampler.SamplingRate() != strategies.DefaultSamplingProbability { |
| 354 |
s.defaultSampler = newProbabilisticSampler(strategies.DefaultSamplingProbability, s.attributesDisabled) |
| 355 |
} |
| 356 |
s.samplers = newSamplers |
| 357 |
} |