|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package x contains experimental trace features. |
| 5 | +package x |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/binary" |
| 9 | + "fmt" |
| 10 | + "math" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "go.opentelemetry.io/otel" |
| 15 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 16 | + "go.opentelemetry.io/otel/trace" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // defaultSamplingPrecision is the default precision for threshold encoding. |
| 21 | + defaultSamplingPrecision = 4 |
| 22 | + maxAdjustedCount = 1 << 56 |
| 23 | + // randomnessMask masks the least significant 56 bits of the trace ID per |
| 24 | + // W3C Trace Context Level 2 Random Trace ID Flag. |
| 25 | + // https://www.w3.org/TR/trace-context-2/#random-trace-id-flag |
| 26 | + randomnessMask = maxAdjustedCount - 1 |
| 27 | + |
| 28 | + probabilityZeroThreshold = 1 / float64(maxAdjustedCount) |
| 29 | +) |
| 30 | + |
| 31 | +// probabilitySampler is the sdktrace.Sampler implementation used by |
| 32 | +// ProbabilitySampler. |
| 33 | +type probabilitySampler struct { |
| 34 | + threshold uint64 |
| 35 | + thkv string |
| 36 | + description string |
| 37 | +} |
| 38 | + |
| 39 | +// ShouldSample implements sdktrace.Sampler. |
| 40 | +func (ps *probabilitySampler) ShouldSample(p sdktrace.SamplingParameters) sdktrace.SamplingResult { |
| 41 | + psc := trace.SpanContextFromContext(p.ParentContext) |
| 42 | + state := psc.TraceState() |
| 43 | + |
| 44 | + existingOtts := state.Get("ot") |
| 45 | + |
| 46 | + var randomness uint64 |
| 47 | + var hasRandomness bool |
| 48 | + if existingOtts != "" { |
| 49 | + randomness, hasRandomness = tracestateRandomness(existingOtts) |
| 50 | + } |
| 51 | + |
| 52 | + // When there is no explicit randomness, we use the trace ID. Trace ID is presumed to be random |
| 53 | + // even without the random flag set. |
| 54 | + if !hasRandomness { |
| 55 | + randomness = binary.BigEndian.Uint64(p.TraceID[8:16]) & randomnessMask |
| 56 | + } |
| 57 | + |
| 58 | + if ps.threshold > randomness { |
| 59 | + return sdktrace.SamplingResult{ |
| 60 | + Decision: sdktrace.Drop, |
| 61 | + Tracestate: state, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + newOtts := insertOrUpdateTraceStateThKeyValue(existingOtts, ps.thkv) |
| 66 | + |
| 67 | + if newOtts == "" { |
| 68 | + state = state.Delete("ot") |
| 69 | + return sdktrace.SamplingResult{Decision: sdktrace.RecordAndSample, Tracestate: state} |
| 70 | + } |
| 71 | + |
| 72 | + if existingOtts == newOtts { |
| 73 | + return sdktrace.SamplingResult{Decision: sdktrace.RecordAndSample, Tracestate: state} |
| 74 | + } |
| 75 | + |
| 76 | + combined, err := state.Insert("ot", newOtts) |
| 77 | + if err != nil { |
| 78 | + // This should never happen, but we handle it here for code hygiene. |
| 79 | + otel.Handle(fmt.Errorf("could not combine tracestate: %w", err)) |
| 80 | + return sdktrace.SamplingResult{Decision: sdktrace.RecordAndSample, Tracestate: state} |
| 81 | + } |
| 82 | + return sdktrace.SamplingResult{Decision: sdktrace.RecordAndSample, Tracestate: combined} |
| 83 | +} |
| 84 | + |
| 85 | +// Description implements sdktrace.Sampler. |
| 86 | +func (ps *probabilitySampler) Description() string { |
| 87 | + return ps.description |
| 88 | +} |
| 89 | + |
| 90 | +// ProbabilitySampler samples a trace with a given probability. Probabilities >= 1 will |
| 91 | +// always sample. Probabilities < 0 are treated as zero. To respect the parent |
| 92 | +// trace's SampledFlag, ProbabilitySampler should be used as a |
| 93 | +// delegate of a ParentBased sampler. |
| 94 | +// |
| 95 | +//nolint:revive // revive complains about stutter of `x.ProbabilitySampler` |
| 96 | +func ProbabilitySampler(probability float64) sdktrace.Sampler { |
| 97 | + const ( |
| 98 | + maxp = 14 |
| 99 | + defp = defaultSamplingPrecision |
| 100 | + hbits = 4 |
| 101 | + ) |
| 102 | + if probability >= 1.0 { |
| 103 | + return &probabilitySampler{ |
| 104 | + threshold: 0, |
| 105 | + thkv: "th:0", |
| 106 | + description: "ProbabilitySampler{1}", |
| 107 | + } |
| 108 | + } |
| 109 | + if math.IsNaN(probability) || probability < probabilityZeroThreshold { |
| 110 | + return sdktrace.NeverSample() |
| 111 | + } |
| 112 | + |
| 113 | + _, expF := math.Frexp(probability) |
| 114 | + _, expR := math.Frexp(1 - probability) |
| 115 | + precision := min(maxp, max(defp+expF/-hbits, defp+expR/-hbits)) |
| 116 | + |
| 117 | + scaled := uint64(math.Round(probability * float64(maxAdjustedCount))) |
| 118 | + threshold := maxAdjustedCount - scaled |
| 119 | + |
| 120 | + if shift := hbits * (maxp - precision); shift != 0 { |
| 121 | + half := uint64(1) << (shift - 1) |
| 122 | + threshold += half |
| 123 | + threshold >>= shift |
| 124 | + threshold <<= shift |
| 125 | + } |
| 126 | + |
| 127 | + tvalue := strings.TrimRight(strconv.FormatUint(maxAdjustedCount+threshold, 16)[1:], "0") |
| 128 | + return &probabilitySampler{ |
| 129 | + threshold: threshold, |
| 130 | + thkv: "th:" + tvalue, |
| 131 | + description: fmt.Sprintf("ProbabilitySampler{%g}", probability), |
| 132 | + } |
| 133 | +} |
0 commit comments