| 1 |
|
| /* otel_jaeger.h - Jaeger's uber-trace-id. |
| 2 |
|
| * |
| 3 |
|
| * uber-trace-id: {trace-id}:{span-id}:{parent-span-id}:{flags} |
| 4 |
|
| * |
| 5 |
|
| * Three quirks, all of which show up in real deployments: |
| 6 |
|
| * |
| 7 |
|
| * 1. The trace id is frequently written SHORT - leading zeroes trimmed, so |
| 8 |
|
| * "abc" rather than 32 characters - and must be left-padded. Same trap as |
| 9 |
|
| * B3, same shared helper. |
| 10 |
|
| * |
| 11 |
|
| * 2. The parent field is deprecated and is usually "0". It is parsed to |
| 12 |
|
| * validate the shape and then discarded, because OTel's parent is the span |
| 13 |
|
| * id above it. |
| 14 |
|
| * |
| 15 |
|
| * 3. The WHOLE VALUE is sometimes percent-encoded, because proxies and client |
| 16 |
|
| * libraries treat it as a URL component and helpfully escape the colons. |
| 17 |
|
| * A parser that only accepts the raw form silently drops context from |
| 18 |
|
| * every request that passed through one of those, which looks like an |
| 19 |
|
| * intermittent tracing bug and is really a decoding one. |
| 20 |
|
| * |
| 21 |
|
| * flags is a bitfield: 1 sampled, 2 debug. Debug implies sampled, exactly as |
| 22 |
|
| * in B3. |
| 23 |
|
| */ |
| 24 |
|
| |
| 25 |
|
| #ifndef OTEL_JAEGER_H |
| 26 |
|
| #define OTEL_JAEGER_H |
| 27 |
|
| |
| 28 |
|
| #include "otel_ctx.h" |
| 29 |
|
| |
| 30 |
|
| #define OTEL_JAEGER_MAX 256 /* a bound, so no allocation follows the input */ |
| 31 |
|
| |
| 32 |
|
| /* Percent-decode in place into `out` (bounded by OTEL_JAEGER_MAX). Returns |
| 33 |
|
| * the decoded length, or 0 when it will not fit - never a partial decode. */ |
| 34 |
13 |
| static STRLEN otel_pct_decode(const char *s, STRLEN len, char *out, |
| 35 |
|
| STRLEN cap) { |
| 36 |
13 |
| STRLEN i = 0, o = 0; |
| 37 |
228 |
▶ | while (i < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < len) { |
215 |
13 |
228 |
|
| 38 |
215 |
▶ | if (o >= cap) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (o >= cap) return 0; |
0 |
215 |
215 |
|
| 39 |
215 |
▶ | if (s[i] == '%' && i + 2 < len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[i] == '%' && i + 2 < len) { |
7 |
208 |
215 |
| if (s[i] == '%' && i + 2 < len) { |
7 |
0 |
7 |
|
| 40 |
7 |
| int hi = otel_nib(s[i + 1]), lo = otel_nib(s[i + 2]); |
| 41 |
7 |
▶ | if (hi >= 0 && lo >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (hi >= 0 && lo >= 0) { |
7 |
0 |
7 |
| if (hi >= 0 && lo >= 0) { |
7 |
0 |
7 |
|
| 42 |
7 |
| out[o++] = (char)((hi << 4) | lo); |
| 43 |
7 |
| i += 3; |
| 44 |
7 |
| continue; |
| 45 |
|
| } |
| 46 |
|
| } |
| 47 |
208 |
| out[o++] = s[i++]; |
| 48 |
|
| } |
| 49 |
13 |
| return o; |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
6 |
| static int otel_jaeger_parse(const char *s, STRLEN len, otel_ctx *ctx) { |
| 53 |
|
| char buf[OTEL_JAEGER_MAX]; |
| 54 |
6 |
| STRLEN i = 0, f[4], n[4]; |
| 55 |
6 |
| int fields = 0; |
| 56 |
6 |
| otel_ctx_clear(ctx); |
| 57 |
6 |
▶ | if (!s || !len || len >= OTEL_JAEGER_MAX) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!s || !len || len >= OTEL_JAEGER_MAX) return 0; |
6 |
0 |
6 |
| if (!s || !len || len >= OTEL_JAEGER_MAX) return 0; |
6 |
0 |
6 |
| if (!s || !len || len >= OTEL_JAEGER_MAX) return 0; |
1 |
5 |
6 |
|
| 58 |
|
| |
| 59 |
|
| /* Decode unconditionally: a value with no percent escapes comes back |
| 60 |
|
| * unchanged, so this costs one bounded copy and buys every request that |
| 61 |
|
| * came through a proxy which escaped the colons. */ |
| 62 |
5 |
| len = otel_pct_decode(s, len, buf, sizeof buf); |
| 63 |
5 |
▶ | if (!len) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) return 0; |
0 |
5 |
5 |
|
| 64 |
5 |
| s = buf; |
| 65 |
|
| |
| 66 |
17 |
▶ | while (i <= len && fields < 4) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i <= len && fields < 4) { |
17 |
0 |
17 |
| while (i <= len && fields < 4) { |
17 |
0 |
17 |
|
| 67 |
17 |
| STRLEN start = i; |
| 68 |
182 |
▶ | while (i < len && s[i] != ':') i++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < len && s[i] != ':') i++; |
177 |
5 |
182 |
| while (i < len && s[i] != ':') i++; |
165 |
12 |
177 |
|
| 69 |
17 |
| f[fields] = start; |
| 70 |
17 |
| n[fields] = i - start; |
| 71 |
17 |
| fields++; |
| 72 |
17 |
▶ | if (i >= len) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i >= len) break; |
5 |
12 |
17 |
|
| 73 |
12 |
| i++; |
| 74 |
|
| } |
| 75 |
5 |
▶ | if (fields < 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fields < 2) return 0; |
1 |
4 |
5 |
|
| 76 |
|
| |
| 77 |
|
| /* a short trace id is left-padded; see otel_trace_id_flex */ |
| 78 |
4 |
▶ | if (n[0] > 32) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n[0] > 32) return 0; |
0 |
4 |
4 |
|
| 79 |
4 |
▶ | if (n[0] == 32 || n[0] == 16) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n[0] == 32 || n[0] == 16) { |
1 |
3 |
4 |
| if (n[0] == 32 || n[0] == 16) { |
0 |
1 |
1 |
|
| 80 |
3 |
▶ | 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 |
3 |
3 |
|
| 81 |
|
| } |
| 82 |
|
| else { |
| 83 |
|
| char pad[32]; |
| 84 |
1 |
| STRLEN want = 32, j; |
| 85 |
1 |
▶ | if (n[0] == 0 || (n[0] % 2)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n[0] == 0 || (n[0] % 2)) { |
1 |
0 |
1 |
| if (n[0] == 0 || (n[0] % 2)) { |
1 |
0 |
1 |
|
| 86 |
|
| /* an odd-length id is legal here: Jaeger trims leading zeroes |
| 87 |
|
| * without regard for byte boundaries */ |
| 88 |
1 |
▶ | if (n[0] == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n[0] == 0) return 0; |
0 |
1 |
1 |
|
| 89 |
|
| } |
| 90 |
30 |
▶ | for (j = 0; j < want - n[0]; j++) pad[j] = '0'; |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < want - n[0]; j++) pad[j] = '0'; |
29 |
1 |
30 |
|
| 91 |
1 |
| Copy(s + f[0], pad + (want - n[0]), n[0], char); |
| 92 |
1 |
▶ | if (!otel_trace_id_flex(pad, want, ctx->trace_id)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_trace_id_flex(pad, want, ctx->trace_id)) return 0; |
0 |
1 |
1 |
|
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
4 |
▶ | if (n[1] == 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n[1] == 16) { |
3 |
1 |
4 |
|
| 96 |
3 |
▶ | 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 |
3 |
3 |
|
| 97 |
|
| } |
| 98 |
|
| else { |
| 99 |
|
| char pad[16]; |
| 100 |
1 |
| STRLEN want = 16, j; |
| 101 |
1 |
▶ | if (n[1] == 0 || n[1] > 16) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n[1] == 0 || n[1] > 16) return 0; |
1 |
0 |
1 |
| if (n[1] == 0 || n[1] > 16) return 0; |
0 |
1 |
1 |
|
| 102 |
14 |
▶ | for (j = 0; j < want - n[1]; j++) pad[j] = '0'; |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < want - n[1]; j++) pad[j] = '0'; |
13 |
1 |
14 |
|
| 103 |
1 |
| Copy(s + f[1], pad + (want - n[1]), n[1], char); |
| 104 |
1 |
▶ | if (!otel_span_id_flex(pad, want, ctx->span_id)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_span_id_flex(pad, want, ctx->span_id)) return 0; |
0 |
1 |
1 |
|
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
4 |
▶ | if (fields >= 4 && n[3]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fields >= 4 && n[3]) { |
4 |
0 |
4 |
| if (fields >= 4 && n[3]) { |
4 |
0 |
4 |
|
| 108 |
4 |
| int v = 0; |
| 109 |
|
| STRLEN j; |
| 110 |
8 |
▶ | for (j = 0; j < n[3] && j < 8; j++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < n[3] && j < 8; j++) { |
4 |
4 |
8 |
| for (j = 0; j < n[3] && j < 8; j++) { |
4 |
0 |
4 |
|
| 111 |
4 |
| int d = otel_nib(s[f[3] + j]); |
| 112 |
4 |
▶ | if (d < 0) { v = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (d < 0) { v = 0; break; } |
0 |
4 |
4 |
|
| 113 |
4 |
| v = (v << 4) | d; |
| 114 |
|
| } |
| 115 |
4 |
▶ | if (v & 0x2) { ctx->debug = 1; ctx->flags |= OTEL_FLAG_SAMPLED; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (v & 0x2) { ctx->debug = 1; ctx->flags |= OTEL_FLAG_SAMPLED; } |
1 |
3 |
4 |
|
| 116 |
3 |
▶ | else if (v & 0x1) ctx->flags |= OTEL_FLAG_SAMPLED; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (v & 0x1) ctx->flags |= OTEL_FLAG_SAMPLED; |
3 |
0 |
3 |
|
| 117 |
|
| } |
| 118 |
4 |
| ctx->valid = 1; |
| 119 |
4 |
| return 1; |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| /* Render into `out` (needs 56 bytes). The parent field is emitted as 0, |
| 123 |
|
| * which is what Jaeger itself does now that it is deprecated. */ |
| 124 |
1 |
| static void otel_jaeger_format(const otel_ctx *ctx, char *out) { |
| 125 |
1 |
| otel_bytes_to_hex(ctx->trace_id, 16, out); |
| 126 |
1 |
| out[32] = ':'; |
| 127 |
1 |
| otel_bytes_to_hex(ctx->span_id, 8, out + 33); |
| 128 |
1 |
| out[49] = ':'; |
| 129 |
1 |
| out[50] = '0'; |
| 130 |
1 |
| out[51] = ':'; |
| 131 |
1 |
▶ | out[52] = ctx->debug ? '3' : ((ctx->flags & OTEL_FLAG_SAMPLED) ? '1' : '0'); |
Branch: 2 branches
| Branch | True | False | Total |
| out[52] = ctx->debug ? '3' : ((ctx->flags & OTEL_FLAG_SAMPLED) ? '1' : '0'); |
1 |
0 |
1 |
| out[52] = ctx->debug ? '3' : ((ctx->flags & OTEL_FLAG_SAMPLED) ? '1' : '0'); |
1 |
0 |
1 |
|
| 132 |
1 |
| out[53] = '\0'; |
| 133 |
1 |
| } |
| 134 |
|
| |
| 135 |
|
| #endif /* OTEL_JAEGER_H */ |