include/otel_grpc.h

filter: Statementstmt 94.8% 110 / 116 Branchbran 69.0% 87 / 126 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.4% 197 / 242 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_jaeger.h ↑ Summary include/otel_expo.h »
1 /* otel_grpc.h - OTLP over gRPC.
2 *
3 * gRPC is not a framework here and is not a dependency. OTLP/gRPC is a narrow,
4 * fixed use of HTTP/2 that can be implemented directly:
5 *
6 * - POST over HTTP/2 to one fixed path per signal
7 * - content-type: application/grpc+proto, te: trailers
8 * - the body is LENGTH-PREFIXED FRAMES: one byte compressed flag, four
9 * bytes big-endian length, then the protobuf message from phase 2
10 * - the response is the same framing, and then the part that catches people
11 *
12 * THE STATUS IS IN THE TRAILERS.
13 *
14 * A gRPC call returns HTTP 200 even when it fails. The real outcome is in the
15 * HTTP/2 trailing headers: `grpc-status` (a number) and `grpc-message`. A
16 * transport that reads the HTTP status and stops will report every failure as
17 * a success - which is worse than not implementing gRPC at all, because it
18 * looks like it works.
19 *
20 * This needs Fetch 0.15 or newer. Before it, Fetch's HTTP/2 header callback
21 * filtered to NGHTTP2_HCAT_RESPONSE and discarded trailers - which arrive as
22 * a second HEADERS frame, category NGHTTP2_HCAT_HEADERS - so there was no
23 * grpc-status to read and a client could only ever report success. Fetch now
24 * captures them and exposes $res->trailers.
25 */
26  
27 #ifndef OTEL_GRPC_H
28 #define OTEL_GRPC_H
29  
30 /* gRPC status codes. The names matter because grpc-message is free text and
31 * the code is the only thing worth branching on. */
32 #define GRPC_OK 0
33 #define GRPC_CANCELLED 1
34 #define GRPC_UNKNOWN 2
35 #define GRPC_INVALID_ARGUMENT 3
36 #define GRPC_DEADLINE_EXCEEDED 4
37 #define GRPC_NOT_FOUND 5
38 #define GRPC_ALREADY_EXISTS 6
39 #define GRPC_PERMISSION_DENIED 7
40 #define GRPC_RESOURCE_EXHAUSTED 8
41 #define GRPC_FAILED_PRECONDITION 9
42 #define GRPC_ABORTED 10
43 #define GRPC_OUT_OF_RANGE 11
44 #define GRPC_UNIMPLEMENTED 12
45 #define GRPC_INTERNAL 13
46 #define GRPC_UNAVAILABLE 14
47 #define GRPC_DATA_LOSS 15
48 #define GRPC_UNAUTHENTICATED 16
49  
50 /* The OTLP spec's retryable set.
51 *
52 * RESOURCE_EXHAUSTED is the odd one: it is retryable ONLY when the server
53 * sent RetryInfo in the details. Without it, the server is saying "you are
54 * over quota", and retrying a quota refusal on a timer is how a client turns
55 * its own rate limit into an outage. */
56 24 static int otel_grpc_retryable(int code, int has_retry_info) {
57 24 switch (code) {
58 9 case GRPC_CANCELLED:
59 case GRPC_DEADLINE_EXCEEDED:
60 case GRPC_ABORTED:
61 case GRPC_OUT_OF_RANGE:
62 case GRPC_UNAVAILABLE:
63 case GRPC_DATA_LOSS:
64 9 return 1;
65 4 case GRPC_RESOURCE_EXHAUSTED:
66 4 return has_retry_info ? 1 : 0;
67 11 default:
68 11 return 0;
69 }
70 }
71  
72 /* The service path for a signal. Fixed, and part of the protocol rather than
73 * configuration - a gRPC endpoint is a host and port, and the path is derived
74 * from the proto package and method. */
75 4 static const char *otel_grpc_path(const char *signal) {
76 4 ▶ if (strEQ(signal, "traces"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(signal, "traces")) 1 3 4
77 1 return "/opentelemetry.proto.collector.trace.v1.TraceService/Export";
78 3 ▶ if (strEQ(signal, "metrics"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(signal, "metrics")) 1 2 3
79 1 return "/opentelemetry.proto.collector.metrics.v1.MetricsService/Export";
80 2 ▶ if (strEQ(signal, "logs"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(signal, "logs")) 1 1 2
81 1 return "/opentelemetry.proto.collector.logs.v1.LogsService/Export";
82 1 return NULL;
83 }
84  
85 /* ---- message framing ---------------------------------------------------- *
86 *
87 * byte 0 compressed flag (0 or 1)
88 * bytes 1..4 message length, BIG-endian
89 * bytes 5.. the protobuf message
90 *
91 * Big-endian, unlike every other length in this dist, which are protobuf
92 * varints or little-endian fixed64. Getting the byte order wrong here
93 * produces a length in the billions and a peer that closes the stream. */
94 9 static SV *otel_grpc_frame(pTHX_ SV *msg, int compressed) {
95 STRLEN len;
96 9 const char *p = SvPV_const(msg, len);
97 9 SV *out = newSV(len + 5);
98 char *d;
99 9 SvPOK_on(out);
100 9 d = SvPVX(out);
101 9 ▶ d[0] = (char)(compressed ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
d[0] = (char)(compressed ? 1 : 0); 2 7 9
102 9 d[1] = (char)((len >> 24) & 0xff);
103 9 d[2] = (char)((len >> 16) & 0xff);
104 9 d[3] = (char)((len >> 8) & 0xff);
105 9 d[4] = (char)( len & 0xff);
106 9 Copy(p, d + 5, len, char);
107 9 SvCUR_set(out, len + 5);
108 9 d[len + 5] = '\0';
109 9 return out;
110 }
111  
112 /* Read one frame. Returns 1 and fills *body / *blen / *compressed, or 0 when
113 * the buffer does not hold a whole frame yet - which is not an error, only
114 * "not yet". A length that would run past the buffer is treated the same way
115 * rather than trusted, because it arrived over a network. */
116 9 static int otel_grpc_unframe(const char *buf, STRLEN len, const char **body,
117 STRLEN *blen, int *compressed, STRLEN *consumed) {
118 U32 n;
119 9 ▶ if (!buf || len < 5) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || len < 5) return 0; 9 0 9
if (!buf || len < 5) return 0; 2 7 9
120 7 n = ((U32)(unsigned char)buf[1] << 24)
121 7 | ((U32)(unsigned char)buf[2] << 16)
122 7 | ((U32)(unsigned char)buf[3] << 8)
123 7 | (U32)(unsigned char)buf[4];
124 7 ▶ if (len < (STRLEN)n + 5) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < (STRLEN)n + 5) return 0; 2 5 7
125 5 ▶ if (compressed) *compressed = buf[0] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (compressed) *compressed = buf[0] ? 1 : 0; 5 0 5
126 5 ▶ if (body) *body = buf + 5;
Branch: 1 branch
BranchTrueFalseTotal
if (body) *body = buf + 5; 5 0 5
127 5 ▶ if (blen) *blen = (STRLEN)n;
Branch: 1 branch
BranchTrueFalseTotal
if (blen) *blen = (STRLEN)n; 5 0 5
128 5 ▶ if (consumed) *consumed = (STRLEN)n + 5;
Branch: 1 branch
BranchTrueFalseTotal
if (consumed) *consumed = (STRLEN)n + 5; 5 0 5
129 5 return 1;
130 }
131  
132 /* ---- RetryInfo ---------------------------------------------------------- *
133 * grpc-status-details-bin carries a base64 google.rpc.Status whose details
134 * hold a RetryInfo { retry_delay: Duration { seconds, nanos } }. When the
135 * server names a delay, it wins over any computed backoff: it knows when it
136 * will be ready and the client does not. */
137  
138 19 static int otel_b64_val(char c) {
139 19 ▶ if (c >= 'A' && c <= 'Z') return c - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') return c - 'A'; 17 2 19
if (c >= 'A' && c <= 'Z') return c - 'A'; 6 11 17
140 13 ▶ if (c >= 'a' && c <= 'z') return c - 'a' + 26;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 11 2 13
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 11 0 11
141 2 ▶ if (c >= '0' && c <= '9') return c - '0' + 52;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0' + 52; 1 1 2
if (c >= '0' && c <= '9') return c - '0' + 52; 1 0 1
142 1 ▶ if (c == '+' || c == '-') return 62; /* URL-safe alphabet too: the */
Branch: 2 branches
BranchTrueFalseTotal
if (c == '+' || c == '-') return 62; /* URL-safe alphabet too: the */ 1 0 1
if (c == '+' || c == '-') return 62; /* URL-safe alphabet too: the */ 0 1 1
143 1 ▶ if (c == '/' || c == '_') return 63; /* header is base64url in practice */
Branch: 2 branches
BranchTrueFalseTotal
if (c == '/' || c == '_') return 63; /* header is base64url in practice */ 1 0 1
if (c == '/' || c == '_') return 63; /* header is base64url in practice */ 0 1 1
144 1 return -1;
145 }
146  
147 2 static STRLEN otel_b64_decode(const char *s, STRLEN len, char *out,
148 STRLEN cap) {
149 2 STRLEN i, o = 0;
150 2 int acc = 0, bits = 0;
151 21 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 20 1 21
152 int v;
153 20 ▶ if (s[i] == '=' || s[i] == '\n' || s[i] == '\r') continue;
Branch: 3 branches
BranchTrueFalseTotal
if (s[i] == '=' || s[i] == '\n' || s[i] == '\r') continue; 19 1 20
if (s[i] == '=' || s[i] == '\n' || s[i] == '\r') continue; 19 0 19
if (s[i] == '=' || s[i] == '\n' || s[i] == '\r') continue; 0 19 19
154 19 v = otel_b64_val(s[i]);
155 19 ▶ if (v < 0) return 0; /* not base64: no details */
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) return 0; /* not base64: no details */ 1 18 19
156 18 acc = (acc << 6) | v;
157 18 bits += 6;
158 18 ▶ if (bits >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { 13 5 18
159 13 bits -= 8;
160 13 ▶ if (o >= cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (o >= cap) return 0; 0 13 13
161 13 out[o++] = (char)((acc >> bits) & 0xff);
162 }
163 }
164 1 return o;
165 }
166  
167 #define OTEL_GRPC_DETAILS_MAX 1024
168  
169 /* Find a RetryInfo delay in a grpc-status-details-bin value. Returns the
170 * delay in seconds, or -1 for none.
171 *
172 * This walks the protobuf generically rather than decoding google.rpc.Status
173 * properly: Status.details is an Any, whose type_url identifies RetryInfo and
174 * whose value holds the Duration. Walking for the shape is enough to answer
175 * the only question being asked - "did the server name a delay" - and avoids
176 * a second protobuf reader in a dist whose encoder is deliberately one-way. */
177 4 static double otel_grpc_retry_after(const char *b64, STRLEN len) {
178 char buf[OTEL_GRPC_DETAILS_MAX];
179 4 STRLEN n, pos = 0;
180 4 ▶ if (!b64 || !len) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!b64 || !len) return -1; 3 1 4
if (!b64 || !len) return -1; 1 2 3
181 2 n = otel_b64_decode(b64, len, buf, sizeof buf);
182 2 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 1 1 2
183  
184 /* look for the Any whose type_url mentions RetryInfo, then read the
185 * Duration inside its value */
186 1 ▶ while (pos + 1 < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos + 1 < n) { 1 0 1
187 1 ▶ if ((STRLEN)(pos + 9) <= n && memEQ(buf + pos, "RetryInfo", 9)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((STRLEN)(pos + 9) <= n && memEQ(buf + pos, "RetryInfo", 9)) { 1 0 1
if ((STRLEN)(pos + 9) <= n && memEQ(buf + pos, "RetryInfo", 9)) { 1 0 1
188 1 STRLEN q = pos + 9;
189 1 double secs = -1;
190 /* the Duration follows within the same Any value; its seconds is
191 * field 1 varint, nanos field 2 varint */
192 1 ▶ while (q + 1 < n && q < pos + 64) {
Branch: 2 branches
BranchTrueFalseTotal
while (q + 1 < n && q < pos + 64) { 1 0 1
while (q + 1 < n && q < pos + 64) { 1 0 1
193 1 unsigned char tag = (unsigned char)buf[q];
194 1 ▶ if ((tag >> 3) == 1 && (tag & 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((tag >> 3) == 1 && (tag & 7) == 0) { 1 0 1
if ((tag >> 3) == 1 && (tag & 7) == 0) { 1 0 1
195 1 UV v = 0;
196 1 int shift = 0;
197 1 q++;
198 1 ▶ while (q < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (q < n) { 1 0 1
199 1 unsigned char b = (unsigned char)buf[q++];
200 1 v |= (UV)(b & 0x7f) << shift;
201 1 ▶ if (!(b & 0x80)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!(b & 0x80)) break; 1 0 1
202 0 shift += 7;
203 0 ▶ if (shift > 56) break;
Branch: 1 branch
BranchTrueFalseTotal
if (shift > 56) break; 0 0 0
204 }
205 1 secs = (double)v;
206 1 break;
207 }
208 0 q++;
209 }
210 1 ▶ return secs >= 0 ? secs : 0; /* named, even if zero seconds */
Branch: 1 branch
BranchTrueFalseTotal
return secs >= 0 ? secs : 0; /* named, even if zero seconds */ 1 0 1
211 }
212 0 pos++;
213 }
214 0 return -1;
215 }
216  
217 /* ---- the classification a caller acts on -------------------------------- *
218 * Mirrors the HTTP transport's verdicts so a caller branches once:
219 * 0 ok, 1 retry, 2 permanent
220 *
221 * `have_status` is 0 when no grpc-status trailer was seen at all. That is NOT
222 * success: it means the stream ended without the server saying how it went,
223 * which is a transport failure and is retryable. Treating a missing status as
224 * OK is the specific bug that makes a broken gRPC client look healthy. */
225 12 static int otel_grpc_verdict(int have_status, int code, int has_retry_info) {
226 12 ▶ if (!have_status) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!have_status) return 1; 3 9 12
227 9 ▶ if (code == GRPC_OK) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (code == GRPC_OK) return 0; 2 7 9
228 7 ▶ return otel_grpc_retryable(code, has_retry_info) ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
return otel_grpc_retryable(code, has_retry_info) ? 1 : 2; 4 3 7
229 }
230  
231 /* ---- reading the status off a response ---------------------------------- *
232 *
233 * Fetch 0.15 exposes HTTP/2 trailers as $res->trailers
234 *
235 * TWO PLACES TO LOOK, and both are normal:
236 *
237 * - the TRAILERS, for an ordinary call: HEADERS, then DATA, then a second
238 * HEADERS carrying grpc-status.
239 * - the response HEADERS, for a TRAILERS-ONLY response: a server that fails
240 * before producing a body sends ONE HEADERS frame with :status, the
241 * grpc-status and END_STREAM, and no DATA at all. nghttp2 reports that
242 * first frame as HCAT_RESPONSE, so the status lands in the ordinary
243 * header list. A client that only looked at trailers would see no status
244 * and - if it treated that as success - would report every fast failure
245 * as a successful export.
246 *
247 * Found nowhere is NOT success; see otel_grpc_verdict. */
248 20 static SV *otel_grpc_hdr(pTHX_ SV *list, const char *want, STRLEN wlen) {
249 AV *av;
250 SSize_t i, n;
251 20 ▶ if (!list || !SvROK(list) || SvTYPE(SvRV(list)) != SVt_PVAV) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!list || !SvROK(list) || SvTYPE(SvRV(list)) != SVt_PVAV) return NULL; 20 0 20
if (!list || !SvROK(list) || SvTYPE(SvRV(list)) != SVt_PVAV) return NULL; 20 0 20
if (!list || !SvROK(list) || SvTYPE(SvRV(list)) != SVt_PVAV) return NULL; 0 20 20
252 20 av = (AV *)SvRV(list);
253 20 n = av_len(av) + 1;
254 34 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 20 14 34
255 20 SV **k = av_fetch(av, i, 0);
256 STRLEN kl;
257 const char *ks;
258 20 ▶ if (!(k && *k)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(k && *k)) continue; 20 0 20
if (!(k && *k)) continue; 0 20 20
259 20 ks = SvPV_const(*k, kl);
260 20 ▶ if (kl == wlen && strnEQ(ks, want, wlen)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == wlen && strnEQ(ks, want, wlen)) { 7 13 20
if (kl == wlen && strnEQ(ks, want, wlen)) { 6 1 7
261 6 SV **v = av_fetch(av, i + 1, 0);
262 6 ▶ return (v && *v) ? *v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (v && *v) ? *v : NULL; 6 0 6
return (v && *v) ? *v : NULL; 6 0 6
263 }
264 }
265 14 return NULL;
266 }
267  
268 /* Pull one named field from a response, looking in the trailers first and
269 * then the headers. `res` is a Fetch::Response or the raw simple_response
270 * hash; both are hashes with `headers` and possibly `trailers`. */
271 15 static SV *otel_grpc_field(pTHX_ SV *res, const char *want, STRLEN wlen) {
272 HV *h;
273 SV **e;
274 SV *found;
275 15 ▶ if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return NULL; 15 0 15
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return NULL; 15 0 15
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return NULL; 0 15 15
276 15 h = (HV *)SvRV(res);
277 15 e = hv_fetchs(h, "trailers", 0);
278 15 ▶ if (e && *e) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) { 9 6 15
if (e && *e) { 9 0 9
279 9 found = otel_grpc_hdr(aTHX_ *e, want, wlen);
280 9 ▶ if (found) return found;
Branch: 1 branch
BranchTrueFalseTotal
if (found) return found; 4 5 9
281 }
282 11 e = hv_fetchs(h, "headers", 0);
283 11 ▶ if (e && *e) return otel_grpc_hdr(aTHX_ *e, want, wlen);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) return otel_grpc_hdr(aTHX_ *e, want, wlen); 11 0 11
if (e && *e) return otel_grpc_hdr(aTHX_ *e, want, wlen); 11 0 11
284 0 return NULL;
285 }
286  
287 #endif /* OTEL_GRPC_H */
« include/otel_jaeger.h ↑ Summary include/otel_expo.h »