|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// # TraceState representation |
| 5 | +// |
| 6 | +// A [W3CTraceState] object parses and stores the OpenTelemetry |
| 7 | +// tracestate field and any other fields that are present in the |
| 8 | +// W3C tracestate header, part of the [W3C tracecontext specification]. |
| 9 | +// |
| 10 | +// An [OpenTelemetryTraceState] object parses and stores fields of |
| 11 | +// the OpenTelemetry-specific tracestate field, including those recognized |
| 12 | +// for probability sampling and any other fields that are present. The |
| 13 | +// syntax of the OpenTelemetry field is specified in [Tracestate handling]. |
| 14 | +// |
| 15 | +// The probability sampling-specific fields used here are specified in |
| 16 | +// [OTEP 235]. The principal named fields are: |
| 17 | +// |
| 18 | +// - T-value: The sampling rejection threshold, expresses a 56-bit |
| 19 | +// hexadecimal number of traces that will be rejected by sampling. |
| 20 | +// - R-value: The sampling randomness value can be implicit in a TraceID, |
| 21 | +// otherwise it is explicitly encoded as an R-value. |
| 22 | +// |
| 23 | +// # Low-level types |
| 24 | +// |
| 25 | +// The three key data types implemented in this package represent sampling |
| 26 | +// decisions. |
| 27 | +// |
| 28 | +// - [Threshold]: Represents an exact sampling probability. |
| 29 | +// - [Randomness]: Randomness used for sampling decisions. |
| 30 | +// - [Threshold.Probability]: a float64 in the range [MinSamplingProbability, 1.0]. |
| 31 | +// |
| 32 | +// # Example use-case |
| 33 | +// |
| 34 | +// To configure a consistent tail sampler in an OpenTelemetry |
| 35 | +// Collector using a fixed probability for all traces in an |
| 36 | +// "equalizing" arrangement, where the effect of sampling is |
| 37 | +// conditioned on how much sampling has already taken place, use the |
| 38 | +// following pseudocode. |
| 39 | +// |
| 40 | +// func Setup() { |
| 41 | +// // Get a fixed probability value from the configuration, in |
| 42 | +// // the range (0, 1]. |
| 43 | +// probability := *FLAG_probability |
| 44 | +// |
| 45 | +// // Calculate the sampling threshold from probability using 3 |
| 46 | +// // hex digits of precision. |
| 47 | +// fixedThreshold, err = ProbabilityToThresholdWithPrecision(probability, 3) |
| 48 | +// if err != nil { |
| 49 | +// // error case: Probability is not valid. |
| 50 | +// } |
| 51 | +// } |
| 52 | +// |
| 53 | +// func MakeDecision(tracestate string, tid TraceID) bool { |
| 54 | +// // Parse the incoming tracestate |
| 55 | +// ts, err := NewW3CTraceState(tracestate) |
| 56 | +// if err != nil { |
| 57 | +// // error case: Tracestate is ill-formed. |
| 58 | +// } |
| 59 | +// // For an absolute probability sample, we check the incoming |
| 60 | +// // tracestate to see whether it was already sampled enough. |
| 61 | +// if len(ts.OTelValue().TValue()) != 0 { |
| 62 | +// // If the incoming tracestate was already sampled at |
| 63 | +// // least as much as our threshold implies, then its |
| 64 | +// // (rejection) threshold is higher. If so, then no |
| 65 | +// // further sampling is called for. |
| 66 | +// if ThresholdGreater(ts.OTelValue().TValueThreshold(), fixedThreshold) { |
| 67 | +// return true |
| 68 | +// } |
| 69 | +// } |
| 70 | +// var rnd Randomness |
| 71 | +// // If the R-value is present, use it. If not, rely on TraceID |
| 72 | +// // randomness. Note that OTLP v1.1.0 introduces a new Span flag |
| 73 | +// // to convey trace randomness correctly, and if the context has |
| 74 | +// // neither the randomness bit set or the R-value set, we need a |
| 75 | +// // fallback, which can be to synthesize an R-value or to assume |
| 76 | +// // the TraceID has sufficient randomness. This detail is left |
| 77 | +// // out of scope. |
| 78 | +// if rval, hasRval := ts.OTelValue().RValueRandomness(); hasRv { |
| 79 | +// rnd = rval |
| 80 | +// } else { |
| 81 | +// rnd = TraceIDToRandomness(tid) |
| 82 | +// } |
| 83 | +// |
| 84 | +// return fixedThreshold.ShouldSample(rnd) |
| 85 | +// } |
| 86 | +// |
| 87 | +// [W3C tracecontext specification]: https://www.w3.org/TR/trace-context/#tracestate-header |
| 88 | +// [Tracestate handling]: https://opentelemetry.io/docs/specs/otel/trace/tracestate-handling/ |
| 89 | +package sampling // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/sampling" |
0 commit comments