| 1 |
|
| /* otel_b3.h - B3, in both of its spellings. |
| 2 |
|
| * |
| 3 |
|
| * Zipkin's format, and still the most widely deployed non-W3C one. It exists |
| 4 |
|
| * twice: |
| 5 |
|
| * |
| 6 |
|
| * single: b3: {trace}-{span}-{sampled}-{parent} |
| 7 |
|
| * multi: X-B3-TraceId, X-B3-SpanId, X-B3-ParentSpanId, |
| 8 |
|
| * X-B3-Sampled, X-B3-Flags |
| 9 |
|
| * |
| 10 |
|
| * TWO THINGS BITE. |
| 11 |
|
| * |
| 12 |
|
| * 1. A B3 trace id may be 64-bit (16 hex) as well as 128-bit, and a short one |
| 13 |
|
| * is LEFT-padded. Get the side wrong and you produce a well-formed id of a |
| 14 |
|
| * completely different value - which is worse than rejecting it, because |
| 15 |
|
| * it looks correct and silently joins to nothing. Handled in |
| 16 |
|
| * otel_trace_id_flex, shared with Jaeger, which has the same trap. |
| 17 |
|
| * |
| 18 |
|
| * 2. `sampled` has five spellings across deployments - "1", "0", "true", |
| 19 |
|
| * "false" and "d" - and "d" is DEBUG, which is a distinct state that |
| 20 |
|
| * IMPLIES sampled rather than being a synonym for it. A debug trace that |
| 21 |
|
| * is treated as merely sampled loses the "always keep this one" signal |
| 22 |
|
| * that is the entire reason somebody set it. |
| 23 |
|
| */ |
| 24 |
|
| |
| 25 |
|
| #ifndef OTEL_B3_H |
| 26 |
|
| #define OTEL_B3_H |
| 27 |
|
| |
| 28 |
|
| #include "otel_ctx.h" |
| 29 |
|
| |
| 30 |
|
| /* -1 unknown, 0 not sampled, 1 sampled, 2 debug (which implies sampled) */ |
| 31 |
11 |
| static int otel_b3_sampled(const char *s, STRLEN n) { |
| 32 |
11 |
▶ | if (n == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 1) { |
9 |
2 |
11 |
|
| 33 |
9 |
▶ | if (s[0] == '1') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == '1') return 1; |
7 |
2 |
9 |
|
| 34 |
2 |
▶ | if (s[0] == '0') return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == '0') return 0; |
1 |
1 |
2 |
|
| 35 |
1 |
▶ | if (s[0] == 'd' || s[0] == 'D') return 2; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[0] == 'd' || s[0] == 'D') return 2; |
0 |
1 |
1 |
| if (s[0] == 'd' || s[0] == 'D') return 2; |
0 |
0 |
0 |
|
| 36 |
0 |
| return -1; |
| 37 |
|
| } |
| 38 |
2 |
▶ | if (n == 4 && (memEQ(s, "true", 4) || memEQ(s, "TRUE", 4))) return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 4 && (memEQ(s, "true", 4) || memEQ(s, "TRUE", 4))) return 1; |
1 |
1 |
2 |
| if (n == 4 && (memEQ(s, "true", 4) || memEQ(s, "TRUE", 4))) return 1; |
0 |
1 |
1 |
| if (n == 4 && (memEQ(s, "true", 4) || memEQ(s, "TRUE", 4))) return 1; |
0 |
0 |
0 |
|
| 39 |
1 |
▶ | if (n == 5 && (memEQ(s, "false", 5) || memEQ(s, "FALSE", 5))) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 5 && (memEQ(s, "false", 5) || memEQ(s, "FALSE", 5))) return 0; |
1 |
0 |
1 |
| if (n == 5 && (memEQ(s, "false", 5) || memEQ(s, "FALSE", 5))) return 0; |
0 |
1 |
1 |
| if (n == 5 && (memEQ(s, "false", 5) || memEQ(s, "FALSE", 5))) return 0; |
0 |
0 |
0 |
|
| 40 |
0 |
| return -1; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| /* b3: {trace}-{span}[-{sampled}[-{parent}]] |
| 44 |
|
| * |
| 45 |
|
| * Also accepts the two degenerate forms deployments really send: a bare "0" |
| 46 |
|
| * meaning "definitely do not sample", and a bare "d". */ |
| 47 |
10 |
| static int otel_b3_parse_single(const char *s, STRLEN len, otel_ctx *ctx) { |
| 48 |
10 |
| STRLEN i = 0, f[4], n[4]; |
| 49 |
10 |
| int fields = 0, samp; |
| 50 |
10 |
| otel_ctx_clear(ctx); |
| 51 |
10 |
▶ | if (!s || !len) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s || !len) return 0; |
10 |
0 |
10 |
| if (!s || !len) return 0; |
0 |
10 |
10 |
|
| 52 |
|
| |
| 53 |
10 |
▶ | if (len == 1) { /* the sampling-only forms */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 1) { /* the sampling-only forms */ |
0 |
10 |
10 |
|
| 54 |
0 |
| samp = otel_b3_sampled(s, 1); |
| 55 |
0 |
▶ | if (samp < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (samp < 0) return 0; |
0 |
0 |
0 |
|
| 56 |
0 |
| ctx->flags = samp ? OTEL_FLAG_SAMPLED : 0; |
| 57 |
0 |
| ctx->debug = (samp == 2); |
| 58 |
0 |
| return 0; /* a decision, but no ids */ |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
30 |
▶ | while (i <= len && fields < 4) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i <= len && fields < 4) { |
30 |
0 |
30 |
| while (i <= len && fields < 4) { |
30 |
0 |
30 |
|
| 62 |
30 |
| STRLEN start = i; |
| 63 |
511 |
▶ | while (i < len && s[i] != '-') i++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < len && s[i] != '-') i++; |
501 |
10 |
511 |
| while (i < len && s[i] != '-') i++; |
481 |
20 |
501 |
|
| 64 |
30 |
| f[fields] = start; |
| 65 |
30 |
| n[fields] = i - start; |
| 66 |
30 |
| fields++; |
| 67 |
30 |
▶ | if (i >= len) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i >= len) break; |
10 |
20 |
30 |
|
| 68 |
20 |
| i++; |
| 69 |
|
| } |
| 70 |
10 |
▶ | if (fields < 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fields < 2) return 0; |
0 |
10 |
10 |
|
| 71 |
10 |
▶ | if (!otel_trace_id_flex(s + f[0], n[0], ctx->trace_id)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_trace_id_flex(s + f[0], n[0], ctx->trace_id)) return 0; |
0 |
10 |
10 |
|
| 72 |
10 |
▶ | if (!otel_span_id_flex(s + f[1], n[1], ctx->span_id)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_span_id_flex(s + f[1], n[1], ctx->span_id)) return 0; |
0 |
10 |
10 |
|
| 73 |
|
| |
| 74 |
10 |
▶ | if (fields >= 3 && n[2]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fields >= 3 && n[2]) { |
10 |
0 |
10 |
| if (fields >= 3 && n[2]) { |
10 |
0 |
10 |
|
| 75 |
10 |
| samp = otel_b3_sampled(s + f[2], n[2]); |
| 76 |
10 |
▶ | if (samp > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (samp > 0) { |
8 |
2 |
10 |
|
| 77 |
8 |
| ctx->flags |= OTEL_FLAG_SAMPLED; |
| 78 |
8 |
| ctx->debug = (samp == 2); |
| 79 |
|
| } |
| 80 |
|
| } |
| 81 |
|
| /* field 4 is the parent span id, which OTel has no use for: a span's |
| 82 |
|
| * parent is the span id above, and B3's parent field describes Zipkin's |
| 83 |
|
| * own model. Parsed to validate the shape, then discarded. */ |
| 84 |
10 |
| ctx->valid = 1; |
| 85 |
10 |
| return 1; |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| /* The multi-header form. Any of the values may be NULL. */ |
| 89 |
2 |
| static int otel_b3_parse_multi(const char *tid, STRLEN tl, |
| 90 |
|
| const char *sid, STRLEN sl, |
| 91 |
|
| const char *smp, STRLEN pl, |
| 92 |
|
| const char *flg, STRLEN fl, |
| 93 |
|
| otel_ctx *ctx) { |
| 94 |
2 |
| otel_ctx_clear(ctx); |
| 95 |
2 |
▶ | if (!tid || !sid) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!tid || !sid) return 0; |
2 |
0 |
2 |
| if (!tid || !sid) return 0; |
0 |
2 |
2 |
|
| 96 |
2 |
▶ | if (!otel_trace_id_flex(tid, tl, ctx->trace_id)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_trace_id_flex(tid, tl, ctx->trace_id)) return 0; |
0 |
2 |
2 |
|
| 97 |
2 |
▶ | if (!otel_span_id_flex(sid, sl, ctx->span_id)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_span_id_flex(sid, sl, ctx->span_id)) return 0; |
0 |
2 |
2 |
|
| 98 |
|
| |
| 99 |
|
| /* X-B3-Flags: 1 is the multi-header spelling of debug, and debug implies |
| 100 |
|
| * sampled even when X-B3-Sampled says otherwise or is absent entirely */ |
| 101 |
2 |
▶ | if (flg && fl == 1 && flg[0] == '1') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (flg && fl == 1 && flg[0] == '1') { |
1 |
1 |
2 |
| if (flg && fl == 1 && flg[0] == '1') { |
1 |
0 |
1 |
| if (flg && fl == 1 && flg[0] == '1') { |
1 |
0 |
1 |
|
| 102 |
1 |
| ctx->debug = 1; |
| 103 |
1 |
| ctx->flags |= OTEL_FLAG_SAMPLED; |
| 104 |
|
| } |
| 105 |
1 |
▶ | else if (smp && pl) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (smp && pl) { |
1 |
0 |
1 |
| else if (smp && pl) { |
1 |
0 |
1 |
|
| 106 |
1 |
| int samp = otel_b3_sampled(smp, pl); |
| 107 |
1 |
▶ | if (samp > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (samp > 0) { |
1 |
0 |
1 |
|
| 108 |
1 |
| ctx->flags |= OTEL_FLAG_SAMPLED; |
| 109 |
1 |
| ctx->debug = (samp == 2); |
| 110 |
|
| } |
| 111 |
|
| } |
| 112 |
2 |
| ctx->valid = 1; |
| 113 |
2 |
| return 1; |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| /* Render the single-header form into `out` (needs 56 bytes). The single form |
| 117 |
|
| * is what the spec recommends emitting. */ |
| 118 |
1 |
| static void otel_b3_format_single(const otel_ctx *ctx, char *out) { |
| 119 |
1 |
| otel_bytes_to_hex(ctx->trace_id, 16, out); |
| 120 |
1 |
| out[32] = '-'; |
| 121 |
1 |
| otel_bytes_to_hex(ctx->span_id, 8, out + 33); |
| 122 |
1 |
| out[49] = '-'; |
| 123 |
1 |
▶ | out[50] = ctx->debug ? 'd' : ((ctx->flags & OTEL_FLAG_SAMPLED) ? '1' : '0'); |
Branch: 2 branches
| Branch | True | False | Total |
| out[50] = ctx->debug ? 'd' : ((ctx->flags & OTEL_FLAG_SAMPLED) ? '1' : '0'); |
1 |
0 |
1 |
| out[50] = ctx->debug ? 'd' : ((ctx->flags & OTEL_FLAG_SAMPLED) ? '1' : '0'); |
1 |
0 |
1 |
|
| 124 |
1 |
| out[51] = '\0'; |
| 125 |
1 |
| } |
| 126 |
|
| |
| 127 |
|
| #endif /* OTEL_B3_H */ |