| 1 |
|
| /* otel_sample.h - whether to record this trace. |
| 2 |
|
| * |
| 3 |
|
| * THE RULE THAT MATTERS: the ratio decision is derived from the TRACE ID, not |
| 4 |
|
| * from a random draw per service. |
| 5 |
|
| * |
| 6 |
|
| * Consider a request crossing three services, all sampling at 10%. With a |
| 7 |
|
| * coin flip per service, each independently keeps a tenth - so the chance all |
| 8 |
|
| * three keep the same trace is one in a thousand, and what a backend receives |
| 9 |
|
| * is a stream of one-span fragments with dangling parents. The dashboard says |
| 10 |
|
| * 10% sampling; what it actually has is 10% of spans and almost no complete |
| 11 |
|
| * traces. Every service having flipped a fair coin is no comfort at all. |
| 12 |
|
| * |
| 13 |
|
| * Deriving the decision from the trace id makes it the SAME decision |
| 14 |
|
| * everywhere the trace goes, with no coordination: the id is already |
| 15 |
|
| * propagated, and every service computes the same answer from it. 10% |
| 16 |
|
| * sampling then means 10% of traces, complete. |
| 17 |
|
| * |
| 18 |
|
| * The algorithm is the spec's: take the low 8 bytes of the trace id as a |
| 19 |
|
| * big-endian unsigned integer and keep the trace when it is below a |
| 20 |
|
| * threshold derived from the ratio. |
| 21 |
|
| * |
| 22 |
|
| * ParentBased wraps it: a trace that already carries a decision inherits it, |
| 23 |
|
| * because a service that re-decides mid-trace produces exactly the fragments |
| 24 |
|
| * this is here to prevent. Only a ROOT span consults the ratio. |
| 25 |
|
| */ |
| 26 |
|
| |
| 27 |
|
| #ifndef OTEL_SAMPLE_H |
| 28 |
|
| #define OTEL_SAMPLE_H |
| 29 |
|
| |
| 30 |
|
| /* the sampler kinds */ |
| 31 |
|
| #define OTEL_SAMPLER_PARENT_RATIO 0 /* ParentBased(TraceIdRatioBased) */ |
| 32 |
|
| #define OTEL_SAMPLER_ALWAYS_ON 1 |
| 33 |
|
| #define OTEL_SAMPLER_ALWAYS_OFF 2 |
| 34 |
|
| |
| 35 |
|
| /* W3C traceparent flags */ |
| 36 |
|
| #define OTEL_FLAG_SAMPLED 0x01 |
| 37 |
|
| |
| 38 |
|
| typedef struct { |
| 39 |
|
| int kind; |
| 40 |
|
| double ratio; /* 0.0 .. 1.0 */ |
| 41 |
|
| U64TYPE threshold; /* ratio scaled to the 64-bit id space */ |
| 42 |
|
| } otel_sampler; |
| 43 |
|
| |
| 44 |
114 |
| static void otel_sampler_init(otel_sampler *s, int kind, double ratio) { |
| 45 |
114 |
| s->kind = kind; |
| 46 |
114 |
▶ | if (ratio < 0.0) ratio = 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ratio < 0.0) ratio = 0.0; |
0 |
114 |
114 |
|
| 47 |
114 |
▶ | if (ratio > 1.0) ratio = 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ratio > 1.0) ratio = 1.0; |
0 |
114 |
114 |
|
| 48 |
114 |
| s->ratio = ratio; |
| 49 |
|
| /* 1.0 has to be exactly "everything": computing it as a scaled double |
| 50 |
|
| * risks landing one short of UINT64_MAX and silently dropping the one |
| 51 |
|
| * trace whose id is all ones. */ |
| 52 |
114 |
▶ | if (ratio >= 1.0) s->threshold = ~(U64TYPE)0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ratio >= 1.0) s->threshold = ~(U64TYPE)0; |
110 |
4 |
114 |
|
| 53 |
4 |
▶ | else if (ratio <= 0.0) s->threshold = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ratio <= 0.0) s->threshold = 0; |
2 |
2 |
4 |
|
| 54 |
2 |
| else s->threshold = (U64TYPE)(ratio * 18446744073709551616.0); |
| 55 |
114 |
| } |
| 56 |
|
| |
| 57 |
|
| /* The low 8 bytes of a trace id, big-endian. */ |
| 58 |
4000 |
| static U64TYPE otel_trace_id_low(const unsigned char *tid) { |
| 59 |
4000 |
| U64TYPE v = 0; |
| 60 |
|
| int i; |
| 61 |
36000 |
▶ | for (i = 8; i < 16; i++) v = (v << 8) | (U64TYPE)tid[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 8; i < 16; i++) v = (v << 8) | (U64TYPE)tid[i]; |
32000 |
4000 |
36000 |
|
| 62 |
4000 |
| return v; |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
|
| /* Would the ratio alone keep this trace? Deterministic in the trace id, so |
| 66 |
|
| * every service in the trace computes the same answer. */ |
| 67 |
4406 |
| static int otel_ratio_sampled(const otel_sampler *s, const unsigned char *tid) { |
| 68 |
4406 |
▶ | if (s->threshold == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->threshold == 0) return 0; |
200 |
4206 |
4406 |
|
| 69 |
4206 |
▶ | if (s->threshold == ~(U64TYPE)0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->threshold == ~(U64TYPE)0) return 1; |
206 |
4000 |
4206 |
|
| 70 |
4000 |
| return otel_trace_id_low(tid) < s->threshold; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| /* The decision. |
| 74 |
|
| * |
| 75 |
|
| * has_parent: a valid inbound trace context was extracted. |
| 76 |
|
| * parent_sampled: its sampled flag. |
| 77 |
|
| * |
| 78 |
|
| * ParentBased: an existing decision is INHERITED, including a decision not to |
| 79 |
|
| * sample. A service that overrides its parent produces a trace with holes in |
| 80 |
|
| * the middle, which is harder to read than no trace at all. */ |
| 81 |
7218 |
| static int otel_should_sample(const otel_sampler *s, const unsigned char *tid, |
| 82 |
|
| int has_parent, int parent_sampled) { |
| 83 |
7218 |
| switch (s->kind) { |
| 84 |
2745 |
| case OTEL_SAMPLER_ALWAYS_ON: return 1; |
| 85 |
4 |
| case OTEL_SAMPLER_ALWAYS_OFF: return 0; |
| 86 |
4469 |
| default: |
| 87 |
4469 |
▶ | if (has_parent) return parent_sampled ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_parent) return parent_sampled ? 1 : 0; |
63 |
4406 |
4469 |
|
| 88 |
4406 |
| return otel_ratio_sampled(s, tid); |
| 89 |
|
| } |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| #endif /* OTEL_SAMPLE_H */ |