|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { |
| 17 | + Context, |
| 18 | + SpanKind, |
| 19 | + Attributes, |
| 20 | + Link, |
| 21 | + TraceState, |
| 22 | + trace, |
| 23 | +} from '@opentelemetry/api'; |
| 24 | +import { TraceState as CoreTraceState } from '@opentelemetry/core'; |
| 25 | +import { |
| 26 | + Sampler, |
| 27 | + SamplingDecision, |
| 28 | + SamplingResult, |
| 29 | +} from '@opentelemetry/sdk-trace-base'; |
| 30 | +import { ComposableSampler } from './types'; |
| 31 | +import { parseOtelTraceState, serializeTraceState } from './tracestate'; |
| 32 | +import { |
| 33 | + INVALID_THRESHOLD, |
| 34 | + isValidRandomValue, |
| 35 | + isValidThreshold, |
| 36 | +} from './util'; |
| 37 | + |
| 38 | +class CompositeSampler implements Sampler { |
| 39 | + constructor(private readonly delegate: ComposableSampler) {} |
| 40 | + |
| 41 | + shouldSample( |
| 42 | + context: Context, |
| 43 | + traceId: string, |
| 44 | + spanName: string, |
| 45 | + spanKind: SpanKind, |
| 46 | + attributes: Attributes, |
| 47 | + links: Link[] |
| 48 | + ): SamplingResult { |
| 49 | + const spanContext = trace.getSpanContext(context); |
| 50 | + |
| 51 | + const traceState = spanContext?.traceState; |
| 52 | + let otTraceState = parseOtelTraceState(traceState); |
| 53 | + |
| 54 | + const intent = this.delegate.getSamplingIntent( |
| 55 | + context, |
| 56 | + traceId, |
| 57 | + spanName, |
| 58 | + spanKind, |
| 59 | + attributes, |
| 60 | + links |
| 61 | + ); |
| 62 | + |
| 63 | + let adjustedCountCorrect = false; |
| 64 | + let sampled = false; |
| 65 | + if (isValidThreshold(intent.threshold)) { |
| 66 | + adjustedCountCorrect = intent.thresholdReliable; |
| 67 | + let randomness: bigint; |
| 68 | + if (isValidRandomValue(otTraceState.randomValue)) { |
| 69 | + randomness = otTraceState.randomValue; |
| 70 | + } else { |
| 71 | + // Use last 56 bits of trace_id as randomness. |
| 72 | + randomness = BigInt(`0x${traceId.slice(-14)}`); |
| 73 | + } |
| 74 | + sampled = intent.threshold <= randomness; |
| 75 | + } |
| 76 | + |
| 77 | + const decision = sampled |
| 78 | + ? SamplingDecision.RECORD_AND_SAMPLED |
| 79 | + : SamplingDecision.NOT_RECORD; |
| 80 | + if (sampled && adjustedCountCorrect) { |
| 81 | + otTraceState = { |
| 82 | + ...otTraceState, |
| 83 | + threshold: intent.threshold, |
| 84 | + }; |
| 85 | + } else { |
| 86 | + otTraceState = { |
| 87 | + ...otTraceState, |
| 88 | + threshold: INVALID_THRESHOLD, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + const otts = serializeTraceState(otTraceState); |
| 93 | + |
| 94 | + let newTraceState: TraceState | undefined; |
| 95 | + if (traceState) { |
| 96 | + newTraceState = traceState; |
| 97 | + if (intent.updateTraceState) { |
| 98 | + newTraceState = intent.updateTraceState(newTraceState); |
| 99 | + } |
| 100 | + } |
| 101 | + if (otts) { |
| 102 | + if (!newTraceState) { |
| 103 | + newTraceState = new CoreTraceState(); |
| 104 | + } |
| 105 | + newTraceState = newTraceState.set('ot', otts); |
| 106 | + } |
| 107 | + |
| 108 | + return { |
| 109 | + decision, |
| 110 | + attributes: intent.attributes, |
| 111 | + traceState: newTraceState, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + toString(): string { |
| 116 | + return this.delegate.toString(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Returns a composite sampler that uses a composable sampler to make its |
| 122 | + * sampling decisions while handling tracestate. |
| 123 | + */ |
| 124 | +export function createCompositeSampler(delegate: ComposableSampler): Sampler { |
| 125 | + return new CompositeSampler(delegate); |
| 126 | +} |
0 commit comments