|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package consistent // import "go.opentelemetry.io/contrib/samplers/probability/consistent" |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + |
| 20 | + "go.opentelemetry.io/otel" |
| 21 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 22 | + "go.opentelemetry.io/otel/trace" |
| 23 | +) |
| 24 | + |
| 25 | +type ( |
| 26 | + parentProbabilitySampler struct { |
| 27 | + delegate sdktrace.Sampler |
| 28 | + } |
| 29 | +) |
| 30 | + |
| 31 | +// ParentProbabilityBased is an implementation of the OpenTelemetry |
| 32 | +// Trace Sampler interface that provides additional checks for tracestate |
| 33 | +// Probability Sampling fields. |
| 34 | +func ParentProbabilityBased(root sdktrace.Sampler, samplers ...sdktrace.ParentBasedSamplerOption) sdktrace.Sampler { |
| 35 | + return &parentProbabilitySampler{ |
| 36 | + delegate: sdktrace.ParentBased(root, samplers...), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// ShouldSample implements "go.opentelemetry.io/otel/sdk/trace".Sampler. |
| 41 | +func (p *parentProbabilitySampler) ShouldSample(params sdktrace.SamplingParameters) sdktrace.SamplingResult { |
| 42 | + psc := trace.SpanContextFromContext(params.ParentContext) |
| 43 | + |
| 44 | + // Note: We do not check psc.IsValid(), i.e., we repair the tracestate |
| 45 | + // with or without a parent TraceId and SpanId. |
| 46 | + state := psc.TraceState() |
| 47 | + |
| 48 | + otts, err := parseOTelTraceState(state.Get(traceStateKey), psc.IsSampled()) |
| 49 | + |
| 50 | + if err != nil { |
| 51 | + otel.Handle(err) |
| 52 | + value := otts.serialize() |
| 53 | + if len(value) > 0 { |
| 54 | + // Note: see the note in |
| 55 | + // "go.opentelemetry.io/otel/trace".TraceState.Insert(). The |
| 56 | + // error below is not a condition we're supposed to handle. |
| 57 | + state, _ = state.Insert(traceStateKey, value) |
| 58 | + } else { |
| 59 | + state = state.Delete(traceStateKey) |
| 60 | + } |
| 61 | + |
| 62 | + // Fix the broken tracestate before calling the delegate. |
| 63 | + params.ParentContext = trace.ContextWithSpanContext(params.ParentContext, psc.WithTraceState(state)) |
| 64 | + } |
| 65 | + |
| 66 | + return p.delegate.ShouldSample(params) |
| 67 | +} |
| 68 | + |
| 69 | +// Description returns the same description as the built-in |
| 70 | +// ParentBased sampler, with "ParentBased" replaced by |
| 71 | +// "ParentProbabilityBased". |
| 72 | +func (p *parentProbabilitySampler) Description() string { |
| 73 | + return "ParentProbabilityBased" + strings.TrimPrefix(p.delegate.Description(), "ParentBased") |
| 74 | +} |
0 commit comments