Skip to content

Commit d969c36

Browse files
committed
Store both the raw and scaled probability on spans
1 parent 62d4170 commit d969c36

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

packages/core/lib/batch-processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class BatchProcessor<C extends Configuration> implements Processor {
132132
const probability = this.sampler.spanProbability
133133

134134
for (const span of this.batch) {
135-
if (span.samplingProbability < probability) {
135+
if (span.samplingProbability.raw < probability.raw) {
136136
span.samplingProbability = probability
137137
}
138138

packages/core/lib/sampler.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { type SpanEnded, type SpanProbability } from './span'
1+
import { type SpanEnded, type ScaledProbability, type SpanProbability } from './span'
22

33
// sampling rates are stored as a number between 0 and 2^32 - 1 (i.e. they are
44
// u32s) so we need to scale the probability value to match this range as they
55
// are stored as values between 0 and 1
6-
function scaleProbabilityToMatchSamplingRate (probability: number): SpanProbability {
7-
return Math.floor(probability * 0xffffffff) as SpanProbability
6+
function scaleProbabilityToMatchSamplingRate (probability: number): ScaledProbability {
7+
return Math.floor(probability * 0xffffffff) as ScaledProbability
88
}
99

1010
interface ReadonlySampler {
@@ -25,7 +25,7 @@ class Sampler {
2525
*
2626
* @see scaleProbabilityToMatchSamplingRate
2727
*/
28-
private scaledProbability: SpanProbability
28+
private scaledProbability: ScaledProbability
2929

3030
constructor (initialProbability: number) {
3131
// we could just do 'this.probability = initialProbability' but TypeScript
@@ -57,11 +57,14 @@ class Sampler {
5757
* @see scaleProbabilityToMatchSamplingRate
5858
*/
5959
get spanProbability (): SpanProbability {
60-
return this.scaledProbability
60+
return {
61+
raw: this._probability,
62+
scaled: this.scaledProbability
63+
}
6164
}
6265

6366
sample (span: SpanEnded): boolean {
64-
return span.samplingRate <= span.samplingProbability
67+
return span.samplingRate <= span.samplingProbability.scaled
6568
}
6669
}
6770

packages/core/lib/span.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ export const enum Kind {
2626
// this prevents the wrong kind of number being assigned to the span's
2727
// samplingProbability
2828
// this exists only in the type system; at runtime it's a regular number
29-
declare const validSpanProbability: unique symbol
30-
export type SpanProbability = number & { [validSpanProbability]: true }
29+
declare const validScaledProbability: unique symbol
30+
export type ScaledProbability = number & { [validScaledProbability]: true }
31+
32+
export interface SpanProbability {
33+
readonly scaled: ScaledProbability
34+
readonly raw: number
35+
}
3136

3237
export interface SpanEnded {
3338
readonly id: string // 64 bit random string

packages/test-utilities/lib/create-span.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
type SpanEnded,
3-
type SpanProbability,
3+
type ScaledProbability,
44
SpanAttributes,
55
traceIdToSamplingRate,
66
SpanEvents
@@ -20,7 +20,10 @@ export function createEndedSpan (overrides: Partial<SpanEnded> = {}): SpanEnded
2020
traceId,
2121
samplingRate: traceIdToSamplingRate(traceId),
2222
endTime: 23456,
23-
samplingProbability: Math.floor(0.5 * 0xffffffff) as SpanProbability,
23+
samplingProbability: {
24+
raw: 0.5,
25+
scaled: Math.floor(0.5 * 0xffffffff) as ScaledProbability
26+
},
2427
...overrides
2528
}
2629
}

0 commit comments

Comments
 (0)