| 1 |
|
| /* otel_export.h - the OTLP/HTTP transport, in C. |
| 2 |
|
| * |
| 3 |
|
| * Punk::OpenTelemetry::Exporter: endpoint resolution, the response |
| 4 |
|
| * classification, the partial-success readers, the Retry-After parse and the |
| 5 |
|
| * backoff policy. lib/Punk/OpenTelemetry/Exporter.pm is documentation. |
| 6 |
|
| * |
| 7 |
|
| * The object stays a blessed HASH with the same keys it always had - endpoint, |
| 8 |
|
| * endpoints, protocol, headers, timeout, compression, max_retries, ua, stats - |
| 9 |
|
| * because they are part of the interface: an application reads $e->{protocol} |
| 10 |
|
| * and bumps $e->{stats}{dropped}, and a port that quietly turned those into |
| 11 |
|
| * opaque slots would be a rewrite wearing a port's clothes. |
| 12 |
|
| * |
| 13 |
|
| * Needs otel_trace.h (otel_h / otel_h_hv, and the encoder this dispatches to) |
| 14 |
|
| * and otel_json.h. otel_frj() comes from the root XS file. |
| 15 |
|
| */ |
| 16 |
|
| |
| 17 |
|
| #ifndef OTEL_EXPORT_H |
| 18 |
|
| #define OTEL_EXPORT_H |
| 19 |
|
| |
| 20 |
|
| /* The signal paths appended to a GENERAL endpoint. A per-signal endpoint is |
| 21 |
|
| * used exactly as given; see otel_exp_endpoint_for. */ |
| 22 |
14 |
| static const char *otel_exp_signal_path(const char *sig, STRLEN len) { |
| 23 |
14 |
▶ | if (len == 6 && memEQ(sig, "traces", 6)) return "/v1/traces"; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 6 && memEQ(sig, "traces", 6)) return "/v1/traces"; |
7 |
7 |
14 |
| if (len == 6 && memEQ(sig, "traces", 6)) return "/v1/traces"; |
7 |
0 |
7 |
|
| 24 |
7 |
▶ | if (len == 7 && memEQ(sig, "metrics", 7)) return "/v1/metrics"; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 7 && memEQ(sig, "metrics", 7)) return "/v1/metrics"; |
4 |
3 |
7 |
| if (len == 7 && memEQ(sig, "metrics", 7)) return "/v1/metrics"; |
4 |
0 |
4 |
|
| 25 |
3 |
▶ | if (len == 4 && memEQ(sig, "logs", 4)) return "/v1/logs"; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 4 && memEQ(sig, "logs", 4)) return "/v1/logs"; |
3 |
0 |
3 |
| if (len == 4 && memEQ(sig, "logs", 4)) return "/v1/logs"; |
3 |
0 |
3 |
|
| 26 |
0 |
| return ""; |
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
|
| /* application/x-protobuf or application/json, or NULL for a protocol that is |
| 30 |
|
| * neither - which is how new() knows to refuse it. */ |
| 31 |
46 |
| static const char *otel_exp_content_type(const char *p, STRLEN len) { |
| 32 |
46 |
▶ | if (len == 13 && memEQ(p, "http/protobuf", 13)) return "application/x-protobuf"; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 13 && memEQ(p, "http/protobuf", 13)) return "application/x-protobuf"; |
39 |
7 |
46 |
| if (len == 13 && memEQ(p, "http/protobuf", 13)) return "application/x-protobuf"; |
39 |
0 |
39 |
|
| 33 |
7 |
▶ | if (len == 9 && memEQ(p, "http/json", 9)) return "application/json"; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 9 && memEQ(p, "http/json", 9)) return "application/json"; |
5 |
2 |
7 |
| if (len == 9 && memEQ(p, "http/json", 9)) return "application/json"; |
5 |
0 |
5 |
|
| 34 |
2 |
| return NULL; |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
24 |
| static int otel_exp_is_json(pTHX_ HV *self) { |
| 38 |
24 |
| SV *p = otel_h(aTHX_ self, "protocol"); |
| 39 |
|
| STRLEN l; |
| 40 |
|
| const char *s; |
| 41 |
24 |
▶ | if (!p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return 0; |
0 |
24 |
24 |
|
| 42 |
24 |
| s = SvPV_const(p, l); |
| 43 |
24 |
▶ | return (l == 9 && memEQ(s, "http/json", 9)); |
Branch: 2 branches
| Branch | True | False | Total |
| return (l == 9 && memEQ(s, "http/json", 9)); |
4 |
20 |
24 |
| return (l == 9 && memEQ(s, "http/json", 9)); |
4 |
0 |
4 |
|
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| /* The HTTP statuses OTLP says to retry. Everything else is permanent: drop it, |
| 47 |
|
| * count it, and do not spend the next hour asking a collector that has already |
| 48 |
|
| * said no. */ |
| 49 |
19 |
| static int otel_exp_retryable(IV status) { |
| 50 |
19 |
▶ | return status == 429 || status == 502 || status == 503 || status == 504; |
Branch: 4 branches
| Branch | True | False | Total |
| return status == 429 || status == 502 || status == 503 || status == 504; |
17 |
2 |
19 |
| return status == 429 || status == 502 || status == 503 || status == 504; |
16 |
1 |
17 |
| return status == 429 || status == 502 || status == 503 || status == 504; |
12 |
4 |
16 |
| return status == 429 || status == 502 || status == 503 || status == 504; |
1 |
11 |
12 |
|
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| /* ---- the endpoint ------------------------------------------------------- * |
| 54 |
|
| * |
| 55 |
|
| * The asymmetry here is in the spec and surprises everybody exactly once: a |
| 56 |
|
| * GENERAL endpoint has the signal path appended, while a PER-SIGNAL endpoint |
| 57 |
|
| * is used exactly as given - including its path, and including a path that |
| 58 |
|
| * does not end in /v1/traces. Appending to a per-signal endpoint would break |
| 59 |
|
| * every collector deployed behind a path prefix. Returns a mortal SV, or NULL |
| 60 |
|
| * when nothing is configured. */ |
| 61 |
17 |
| static SV *otel_exp_endpoint_for(pTHX_ HV *self, SV *signal) { |
| 62 |
|
| STRLEN sl; |
| 63 |
34 |
▶ | const char *sp = SvOK(signal) ? SvPV_const(signal, sl) : ""; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *sp = SvOK(signal) ? SvPV_const(signal, sl) : ""; |
17 |
0 |
17 |
|
| 64 |
|
| HV *eps; |
| 65 |
|
| SV *base; |
| 66 |
|
| STRLEN bl; |
| 67 |
|
| const char *bp; |
| 68 |
|
| SV *out; |
| 69 |
17 |
▶ | if (!SvOK(signal)) sl = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(signal)) sl = 0; |
0 |
17 |
17 |
|
| 70 |
|
| |
| 71 |
17 |
| eps = otel_h_hv(aTHX_ self, "endpoints"); |
| 72 |
17 |
▶ | if (eps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eps) { |
17 |
0 |
17 |
|
| 73 |
17 |
| SV **e = hv_fetch(eps, sp, (I32)sl, 0); |
| 74 |
|
| /* truthiness, as the Perl did: an empty per-signal endpoint is not a |
| 75 |
|
| * configured one and falls through to the general endpoint */ |
| 76 |
17 |
▶ | if (e && *e && SvTRUE(*e)) return sv_2mortal(newSVsv(*e)); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvTRUE(*e)) return sv_2mortal(newSVsv(*e)); |
1 |
16 |
17 |
| if (e && *e && SvTRUE(*e)) return sv_2mortal(newSVsv(*e)); |
1 |
0 |
1 |
| if (e && *e && SvTRUE(*e)) return sv_2mortal(newSVsv(*e)); |
1 |
0 |
1 |
|
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
16 |
| base = otel_h(aTHX_ self, "endpoint"); |
| 80 |
16 |
▶ | if (!base) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!base) return NULL; |
2 |
14 |
16 |
|
| 81 |
14 |
| bp = SvPV_const(base, bl); |
| 82 |
14 |
▶ | if (!bl) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bl) return NULL; |
0 |
14 |
14 |
|
| 83 |
15 |
▶ | while (bl > 0 && bp[bl - 1] == '/') bl--; /* s{/+$}{} */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (bl > 0 && bp[bl - 1] == '/') bl--; /* s{/+$}{} */ |
15 |
0 |
15 |
| while (bl > 0 && bp[bl - 1] == '/') bl--; /* s{/+$}{} */ |
1 |
14 |
15 |
|
| 84 |
14 |
| out = sv_2mortal(newSVpvn(bp, bl)); |
| 85 |
14 |
| sv_catpv(out, otel_exp_signal_path(sp, sl)); |
| 86 |
14 |
| return out; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| /* ---- partial success, protobuf ------------------------------------------ * |
| 90 |
|
| * |
| 91 |
|
| * A minimal protobuf reader for ONE message shape, because there is no general |
| 92 |
|
| * decoder in this dist - the encoder only writes - and this shape is three |
| 93 |
|
| * fields that will not change: |
| 94 |
|
| * |
| 95 |
|
| * ExportTraceServiceResponse { partial_success = 1 } |
| 96 |
|
| * ExportTracePartialSuccess { rejected_spans = 1, error_message = 2 } |
| 97 |
|
| * |
| 98 |
|
| * Every malformed or truncated input stops the walk and yields whatever was |
| 99 |
|
| * gathered before it, exactly as the Perl did. That is not sloppiness: a |
| 100 |
|
| * collector's response is not a place to start dying, and a body we cannot |
| 101 |
|
| * read means "no partial success reported", which is the safe reading. */ |
| 102 |
|
| typedef struct { |
| 103 |
|
| int have1, have2; |
| 104 |
|
| UV v1; /* field 1 as a varint */ |
| 105 |
|
| const char *b1; STRLEN b1l;/* field 1 as bytes */ |
| 106 |
|
| const char *b2; STRLEN b2l;/* field 2 as bytes */ |
| 107 |
|
| } otel_pbr; |
| 108 |
|
| |
| 109 |
6 |
| static void otel_pbr_walk(const char *buf, STRLEN len, otel_pbr *out) { |
| 110 |
6 |
| STRLEN pos = 0; |
| 111 |
6 |
| Zero(out, 1, otel_pbr); |
| 112 |
10 |
▶ | while (pos < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pos < len) { |
6 |
4 |
10 |
|
| 113 |
6 |
| UV tag = 0, field, wire; |
| 114 |
6 |
| int shift = 0; |
| 115 |
3 |
| for (;;) { |
| 116 |
|
| unsigned char b; |
| 117 |
9 |
▶ | if (pos >= len) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos >= len) return; |
1 |
8 |
9 |
|
| 118 |
8 |
| b = (unsigned char)buf[pos++]; |
| 119 |
8 |
| tag |= ((UV)(b & 0x7f)) << shift; |
| 120 |
8 |
▶ | if (!(b & 0x80)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(b & 0x80)) break; |
5 |
3 |
8 |
|
| 121 |
3 |
| shift += 7; |
| 122 |
3 |
▶ | if (shift > 63) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift > 63) return; |
0 |
3 |
3 |
|
| 123 |
|
| } |
| 124 |
5 |
| field = tag >> 3; |
| 125 |
5 |
| wire = tag & 7; |
| 126 |
5 |
▶ | if (wire == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wire == 0) { |
1 |
4 |
5 |
|
| 127 |
1 |
| UV v = 0; |
| 128 |
1 |
| int s = 0; |
| 129 |
0 |
| for (;;) { |
| 130 |
|
| unsigned char b; |
| 131 |
1 |
▶ | if (pos >= len) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos >= len) return; |
0 |
1 |
1 |
|
| 132 |
1 |
| b = (unsigned char)buf[pos++]; |
| 133 |
1 |
| v |= ((UV)(b & 0x7f)) << s; |
| 134 |
1 |
▶ | if (!(b & 0x80)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(b & 0x80)) break; |
1 |
0 |
1 |
|
| 135 |
0 |
| s += 7; |
| 136 |
0 |
▶ | if (s > 63) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s > 63) return; |
0 |
0 |
0 |
|
| 137 |
|
| } |
| 138 |
1 |
▶ | if (field == 1) { out->have1 = 1; out->v1 = v; out->b1 = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (field == 1) { out->have1 = 1; out->v1 = v; out->b1 = NULL; } |
1 |
0 |
1 |
|
| 139 |
|
| } |
| 140 |
4 |
▶ | else if (wire == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (wire == 2) { |
4 |
0 |
4 |
|
| 141 |
4 |
| UV dl = 0; |
| 142 |
4 |
| int s = 0; |
| 143 |
1 |
| for (;;) { |
| 144 |
|
| unsigned char b; |
| 145 |
5 |
▶ | if (pos >= len) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos >= len) return; |
1 |
4 |
5 |
|
| 146 |
4 |
| b = (unsigned char)buf[pos++]; |
| 147 |
4 |
| dl |= ((UV)(b & 0x7f)) << s; |
| 148 |
4 |
▶ | if (!(b & 0x80)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(b & 0x80)) break; |
3 |
1 |
4 |
|
| 149 |
1 |
| s += 7; |
| 150 |
1 |
▶ | if (s > 63) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s > 63) return; |
0 |
1 |
1 |
|
| 151 |
|
| } |
| 152 |
3 |
▶ | if (dl > (UV)(len - pos)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dl > (UV)(len - pos)) return; |
0 |
3 |
3 |
|
| 153 |
3 |
▶ | if (field == 1) { out->have1 = 1; out->b1 = buf + pos; out->b1l = (STRLEN)dl; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (field == 1) { out->have1 = 1; out->b1 = buf + pos; out->b1l = (STRLEN)dl; } |
2 |
1 |
3 |
|
| 154 |
1 |
▶ | else if (field == 2) { out->have2 = 1; out->b2 = buf + pos; out->b2l = (STRLEN)dl; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (field == 2) { out->have2 = 1; out->b2 = buf + pos; out->b2l = (STRLEN)dl; } |
1 |
0 |
1 |
|
| 155 |
3 |
| pos += (STRLEN)dl; |
| 156 |
|
| } |
| 157 |
0 |
▶ | else if (wire == 1) { if (len - pos < 8) return; pos += 8; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (wire == 1) { if (len - pos < 8) return; pos += 8; } |
0 |
0 |
0 |
| else if (wire == 1) { if (len - pos < 8) return; pos += 8; } |
0 |
0 |
0 |
|
| 158 |
0 |
▶ | else if (wire == 5) { if (len - pos < 4) return; pos += 4; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (wire == 5) { if (len - pos < 4) return; pos += 4; } |
0 |
0 |
0 |
| else if (wire == 5) { if (len - pos < 4) return; pos += 4; } |
0 |
0 |
0 |
|
| 159 |
0 |
| else return; |
| 160 |
|
| } |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
|
| /* { rejected => N, message => $s } as a mortal hashref, or NULL when the body |
| 164 |
|
| * carries no partial_success at all. */ |
| 165 |
9 |
| static SV *otel_exp_parse_partial(pTHX_ SV *body) { |
| 166 |
|
| STRLEN len; |
| 167 |
|
| const char *buf; |
| 168 |
|
| otel_pbr top, ps; |
| 169 |
|
| HV *out; |
| 170 |
9 |
▶ | if (!body || !SvOK(body)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!body || !SvOK(body)) return NULL; |
9 |
0 |
9 |
| if (!body || !SvOK(body)) return NULL; |
1 |
8 |
9 |
|
| 171 |
8 |
| buf = SvPV_const(body, len); |
| 172 |
8 |
▶ | if (!len) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) return NULL; |
4 |
4 |
8 |
|
| 173 |
4 |
| otel_pbr_walk(buf, len, &top); |
| 174 |
4 |
▶ | if (!top.have1 || !top.b1) return NULL; /* no partial_success */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!top.have1 || !top.b1) return NULL; /* no partial_success */ |
2 |
2 |
4 |
| if (!top.have1 || !top.b1) return NULL; /* no partial_success */ |
0 |
2 |
2 |
|
| 175 |
2 |
| otel_pbr_walk(top.b1, top.b1l, &ps); |
| 176 |
2 |
| out = newHV(); |
| 177 |
2 |
▶ | (void)hv_stores(out, "rejected", |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_stores(out, "rejected", |
1 |
1 |
2 |
| (void)hv_stores(out, "rejected", |
1 |
0 |
1 |
|
| 178 |
|
| newSVuv(ps.have1 && !ps.b1 ? ps.v1 : 0)); |
| 179 |
2 |
▶ | (void)hv_stores(out, "message", |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(out, "message", |
1 |
1 |
2 |
|
| 180 |
|
| ps.have2 ? newSVpvn(ps.b2, ps.b2l) : newSV(0)); |
| 181 |
2 |
| return sv_2mortal(newRV_noinc((SV *)out)); |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
|
| /* The same, for OTLP/JSON: { partialSuccess: { rejectedSpans, errorMessage } }. |
| 185 |
|
| * A body that does not decode is not an error here either - same reasoning. */ |
| 186 |
1 |
| static SV *otel_exp_parse_partial_json(pTHX_ SV *body) { |
| 187 |
|
| STRLEN len; |
| 188 |
|
| const char *buf; |
| 189 |
|
| SV *decoded; |
| 190 |
|
| HV *top, *ps; |
| 191 |
|
| SV **e; |
| 192 |
|
| HV *out; |
| 193 |
1 |
▶ | if (!body || !SvOK(body)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!body || !SvOK(body)) return NULL; |
1 |
0 |
1 |
| if (!body || !SvOK(body)) return NULL; |
0 |
1 |
1 |
|
| 194 |
1 |
| buf = SvPV_const(body, len); |
| 195 |
1 |
▶ | if (!len) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) return NULL; |
0 |
1 |
1 |
|
| 196 |
1 |
| decoded = otel_frj(aTHX)->decode(aTHX_ buf, len, NULL); |
| 197 |
1 |
▶ | if (!decoded) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!decoded) return NULL; |
0 |
1 |
1 |
|
| 198 |
1 |
| sv_2mortal(decoded); |
| 199 |
1 |
| top = otel_hv_of(aTHX_ decoded); |
| 200 |
1 |
▶ | if (!top) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!top) return NULL; |
0 |
1 |
1 |
|
| 201 |
1 |
| ps = otel_h_hv(aTHX_ top, "partialSuccess"); |
| 202 |
1 |
▶ | if (!ps) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ps) return NULL; |
0 |
1 |
1 |
|
| 203 |
1 |
| out = newHV(); |
| 204 |
1 |
| e = hv_fetchs(ps, "rejectedSpans", 0); |
| 205 |
1 |
▶ | (void)hv_stores(out, "rejected", |
Branch: 3 branches
| Branch | True | False | Total |
| (void)hv_stores(out, "rejected", |
1 |
0 |
1 |
| (void)hv_stores(out, "rejected", |
1 |
0 |
1 |
| (void)hv_stores(out, "rejected", |
1 |
0 |
1 |
|
| 206 |
|
| newSVuv((e && *e && SvOK(*e)) ? SvUV(*e) : 0)); |
| 207 |
1 |
| e = hv_fetchs(ps, "errorMessage", 0); |
| 208 |
1 |
▶ | (void)hv_stores(out, "message", |
Branch: 3 branches
| Branch | True | False | Total |
| (void)hv_stores(out, "message", |
1 |
0 |
1 |
| (void)hv_stores(out, "message", |
1 |
0 |
1 |
| (void)hv_stores(out, "message", |
1 |
0 |
1 |
|
| 209 |
|
| (e && *e && SvOK(*e)) ? newSVsv(*e) : newSV(0)); |
| 210 |
1 |
| return sv_2mortal(newRV_noinc((SV *)out)); |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| /* ---- Retry-After -------------------------------------------------------- * |
| 214 |
|
| * |
| 215 |
|
| * Either a delta in seconds or an HTTP date. A collector under load is telling |
| 216 |
|
| * us when to come back; ignoring it is how a fleet turns a slow collector into |
| 217 |
|
| * a dead one. Returns a mortal SV, or NULL for "no usable value". */ |
| 218 |
14 |
| static SV *otel_exp_retry_after(pTHX_ SV *headers) { |
| 219 |
14 |
| SV *v = NULL; |
| 220 |
|
| STRLEN l; |
| 221 |
|
| const char *s; |
| 222 |
|
| STRLEN i; |
| 223 |
14 |
| int all_digits = 0; |
| 224 |
|
| |
| 225 |
14 |
▶ | if (!headers || !SvROK(headers)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!headers || !SvROK(headers)) return NULL; |
14 |
0 |
14 |
| if (!headers || !SvROK(headers)) return NULL; |
1 |
13 |
14 |
|
| 226 |
13 |
▶ | if (SvTYPE(SvRV(headers)) == SVt_PVAV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(SvRV(headers)) == SVt_PVAV) { |
12 |
1 |
13 |
|
| 227 |
12 |
| AV *av = (AV *)SvRV(headers); |
| 228 |
12 |
| SSize_t j, n = av_len(av) + 1; |
| 229 |
13 |
▶ | for (j = 0; j + 1 < n; j += 2) { /* first match wins */ |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j + 1 < n; j += 2) { /* first match wins */ |
7 |
6 |
13 |
|
| 230 |
7 |
| SV **k = av_fetch(av, j, 0); |
| 231 |
|
| STRLEN kl; |
| 232 |
|
| const char *kp; |
| 233 |
7 |
▶ | if (!(k && *k && SvOK(*k))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(k && *k && SvOK(*k))) continue; |
7 |
0 |
7 |
| if (!(k && *k && SvOK(*k))) continue; |
7 |
0 |
7 |
| if (!(k && *k && SvOK(*k))) continue; |
0 |
7 |
7 |
|
| 234 |
7 |
| kp = SvPV_const(*k, kl); |
| 235 |
7 |
▶ | if (kl == 11 && strnEQ(kp, "retry-after", 11)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 11 && strnEQ(kp, "retry-after", 11)) { |
6 |
1 |
7 |
| if (kl == 11 && strnEQ(kp, "retry-after", 11)) { |
1 |
5 |
6 |
|
| 236 |
1 |
| SV **vv = av_fetch(av, j + 1, 0); |
| 237 |
1 |
▶ | if (vv && *vv) v = *vv; |
Branch: 2 branches
| Branch | True | False | Total |
| if (vv && *vv) v = *vv; |
1 |
0 |
1 |
| if (vv && *vv) v = *vv; |
1 |
0 |
1 |
|
| 238 |
6 |
| break; |
| 239 |
|
| } |
| 240 |
6 |
▶ | else if (kl == 11) { /* case-insensitively */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (kl == 11) { /* case-insensitively */ |
5 |
1 |
6 |
|
| 241 |
|
| STRLEN c; |
| 242 |
5 |
| int same = 1; |
| 243 |
60 |
▶ | for (c = 0; c < 11; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < 11; c++) |
55 |
5 |
60 |
|
| 244 |
55 |
▶ | if (toLOWER(kp[c]) != "retry-after"[c]) { same = 0; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (toLOWER(kp[c]) != "retry-after"[c]) { same = 0; break; } |
10 |
45 |
55 |
| if (toLOWER(kp[c]) != "retry-after"[c]) { same = 0; break; } |
0 |
55 |
55 |
|
| 245 |
5 |
▶ | if (same) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (same) { |
5 |
0 |
5 |
|
| 246 |
5 |
| SV **vv = av_fetch(av, j + 1, 0); |
| 247 |
5 |
▶ | if (vv && *vv) v = *vv; |
Branch: 2 branches
| Branch | True | False | Total |
| if (vv && *vv) v = *vv; |
5 |
0 |
5 |
| if (vv && *vv) v = *vv; |
5 |
0 |
5 |
|
| 248 |
5 |
| break; |
| 249 |
|
| } |
| 250 |
|
| } |
| 251 |
|
| } |
| 252 |
|
| } |
| 253 |
1 |
▶ | else if (SvTYPE(SvRV(headers)) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvTYPE(SvRV(headers)) == SVt_PVHV) { |
1 |
0 |
1 |
|
| 254 |
1 |
| HV *hv = (HV *)SvRV(headers); |
| 255 |
1 |
| SV **e = hv_fetchs(hv, "retry-after", 0); |
| 256 |
1 |
▶ | if (!(e && *e && SvOK(*e))) e = hv_fetchs(hv, "Retry-After", 0); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(e && *e && SvOK(*e))) e = hv_fetchs(hv, "Retry-After", 0); |
1 |
0 |
1 |
| if (!(e && *e && SvOK(*e))) e = hv_fetchs(hv, "Retry-After", 0); |
1 |
0 |
1 |
| if (!(e && *e && SvOK(*e))) e = hv_fetchs(hv, "Retry-After", 0); |
0 |
1 |
1 |
|
| 257 |
1 |
▶ | if (e && *e) v = *e; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && *e) v = *e; |
1 |
0 |
1 |
| if (e && *e) v = *e; |
1 |
0 |
1 |
|
| 258 |
|
| } |
| 259 |
13 |
▶ | if (!v || !SvOK(v)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !SvOK(v)) return NULL; |
7 |
6 |
13 |
| if (!v || !SvOK(v)) return NULL; |
0 |
7 |
7 |
|
| 260 |
|
| |
| 261 |
|
| /* ^\s*\d+\s*$ - digits only, no sign and no decimal, as the Perl was */ |
| 262 |
7 |
| s = SvPV_const(v, l); |
| 263 |
|
| { |
| 264 |
7 |
| STRLEN a = 0, b = l; |
| 265 |
8 |
▶ | while (a < b && isSPACE(s[a])) a++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (a < b && isSPACE(s[a])) a++; |
8 |
0 |
8 |
| while (a < b && isSPACE(s[a])) a++; |
1 |
7 |
8 |
|
| 266 |
8 |
▶ | while (b > a && isSPACE(s[b - 1])) b--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (b > a && isSPACE(s[b - 1])) b--; |
8 |
0 |
8 |
| while (b > a && isSPACE(s[b - 1])) b--; |
1 |
7 |
8 |
|
| 267 |
7 |
▶ | if (b > a) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b > a) { |
7 |
0 |
7 |
|
| 268 |
7 |
| all_digits = 1; |
| 269 |
12 |
▶ | for (i = a; i < b; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = a; i < b; i++) |
8 |
4 |
12 |
|
| 270 |
8 |
▶ | if (!isDIGIT(s[i])) { all_digits = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!isDIGIT(s[i])) { all_digits = 0; break; } |
3 |
5 |
8 |
|
| 271 |
|
| } |
| 272 |
7 |
▶ | if (all_digits) return sv_2mortal(newSVnv(Atof(s + a))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (all_digits) return sv_2mortal(newSVnv(Atof(s + a))); |
4 |
3 |
7 |
|
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
|
| /* an HTTP date, through HTTP::Date if it is there */ |
| 276 |
|
| { |
| 277 |
3 |
| dSP; int count; SV *t = NULL; |
| 278 |
3 |
| eval_pv("require HTTP::Date;", FALSE); |
| 279 |
3 |
| SPAGAIN; |
| 280 |
3 |
▶ | if (SvTRUE(ERRSV)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) return NULL; |
3 |
0 |
3 |
| if (SvTRUE(ERRSV)) return NULL; |
0 |
3 |
3 |
|
| 281 |
3 |
| ENTER; SAVETMPS; |
| 282 |
3 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(v); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(v); PUTBACK; |
0 |
3 |
3 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(v); PUTBACK; |
0 |
3 |
3 |
|
| 283 |
3 |
| count = call_pv("HTTP::Date::str2time", G_SCALAR | G_EVAL); |
| 284 |
3 |
| SPAGAIN; |
| 285 |
3 |
▶ | if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) t = newSVsv(r); } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) t = newSVsv(r); } |
3 |
0 |
3 |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) t = newSVsv(r); } |
3 |
0 |
3 |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) t = newSVsv(r); } |
3 |
0 |
3 |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) t = newSVsv(r); } |
2 |
1 |
3 |
|
| 286 |
0 |
▶ | else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 287 |
3 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
3 |
0 |
3 |
|
| 288 |
3 |
▶ | if (!t) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return NULL; |
1 |
2 |
3 |
|
| 289 |
2 |
| sv_2mortal(t); |
| 290 |
2 |
▶ | if (!SvTRUE(t)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvTRUE(t)) return NULL; |
0 |
2 |
2 |
|
| 291 |
|
| { |
| 292 |
2 |
| NV d = SvNV(t) - (NV)time(NULL); |
| 293 |
2 |
▶ | return sv_2mortal(newSVnv(d > 0 ? d : 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| return sv_2mortal(newSVnv(d > 0 ? d : 0)); |
1 |
1 |
2 |
|
| 294 |
|
| } |
| 295 |
|
| } |
| 296 |
|
| } |
| 297 |
|
| |
| 298 |
|
| /* ---- backoff ------------------------------------------------------------ * |
| 299 |
|
| * |
| 300 |
|
| * Exponential from one second to a thirty second ceiling, with FULL JITTER. |
| 301 |
|
| * The jitter is not decoration: a fleet of workers that all failed at the same |
| 302 |
|
| * moment and all back off by the same amount retries in a thundering herd, |
| 303 |
|
| * which is how a collector that was briefly slow stays down. |
| 304 |
|
| * |
| 305 |
|
| * A Retry-After from the server wins outright - including one of 0, which is a |
| 306 |
|
| * collector saying "come back now", not a missing value. */ |
| 307 |
602 |
| static NV otel_exp_backoff(pTHX_ IV attempt, SV *retry_after) { |
| 308 |
|
| NV exp; |
| 309 |
602 |
▶ | if (retry_after && SvOK(retry_after)) return SvNV(retry_after); |
Branch: 2 branches
| Branch | True | False | Total |
| if (retry_after && SvOK(retry_after)) return SvNV(retry_after); |
602 |
0 |
602 |
| if (retry_after && SvOK(retry_after)) return SvNV(retry_after); |
2 |
600 |
602 |
|
| 310 |
600 |
| exp = Perl_pow(2.0, (NV)(attempt - 1)); |
| 311 |
600 |
▶ | if (!(exp <= 30.0)) exp = 30.0; /* also catches a NaN attempt */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(exp <= 30.0)) exp = 30.0; /* also catches a NaN attempt */ |
350 |
250 |
600 |
|
| 312 |
600 |
| return Drand01() * exp; |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
|
| #endif /* OTEL_EXPORT_H */ |