| 1 |
|
| MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::GRPC |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| # OTLP over gRPC: the parts that are protocol rather than transport. |
| 6 |
|
| # |
| 7 |
|
| # Everything here is pure and testable with no network. The transport half |
| 8 |
|
| # needs trailers out of HTTP/2, which Fetch does not yet expose - see the POD. |
| 9 |
|
| |
| 10 |
|
| # The fixed service path for a signal. Part of the protocol, not |
| 11 |
|
| # configuration: a gRPC endpoint is a host and port, and the path is derived |
| 12 |
|
| # from the proto package and method. |
| 13 |
|
| SV * |
| 14 |
|
| path(signal) |
| 15 |
|
| SV *signal |
| 16 |
|
| CODE: |
| 17 |
|
| { |
| 18 |
4 |
| const char *p = otel_grpc_path(SvPV_nolen(signal)); |
| 19 |
4 |
▶ | if (!p) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) XSRETURN_UNDEF; |
1 |
3 |
4 |
|
| 20 |
3 |
| RETVAL = newSVpv(p, 0); |
| 21 |
|
| } |
| 22 |
|
| OUTPUT: |
| 23 |
|
| RETVAL |
| 24 |
|
| |
| 25 |
|
| # Wrap a message in the gRPC length prefix: one byte compressed flag, four |
| 26 |
|
| # bytes BIG-endian length, then the message. Big-endian, unlike every other |
| 27 |
|
| # length in this dist - getting the byte order wrong produces a length in the |
| 28 |
|
| # billions and a peer that closes the stream. |
| 29 |
|
| SV * |
| 30 |
|
| frame(msg, compressed = 0) |
| 31 |
|
| SV *msg |
| 32 |
|
| int compressed |
| 33 |
|
| CODE: |
| 34 |
9 |
| RETVAL = otel_grpc_frame(aTHX_ msg, compressed); |
| 35 |
|
| OUTPUT: |
| 36 |
|
| RETVAL |
| 37 |
|
| |
| 38 |
|
| # Read one frame back. Returns (body, compressed, consumed), or an empty list |
| 39 |
|
| # when the buffer does not hold a whole frame yet - which means "not yet", |
| 40 |
|
| # and is not a failure. A length running past the buffer is treated the same |
| 41 |
|
| # way rather than trusted, because it arrived over a network. |
| 42 |
|
| void |
| 43 |
|
| unframe(buf) |
| 44 |
|
| SV *buf |
| 45 |
|
| PPCODE: |
| 46 |
|
| { |
| 47 |
|
| STRLEN len; |
| 48 |
9 |
| const char *p = SvPV_const(buf, len); |
| 49 |
|
| const char *body; |
| 50 |
|
| STRLEN blen, consumed; |
| 51 |
|
| int compressed; |
| 52 |
9 |
▶ | if (!otel_grpc_unframe(p, len, &body, &blen, &compressed, &consumed)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_grpc_unframe(p, len, &body, &blen, &compressed, &consumed)) |
4 |
5 |
9 |
|
| 53 |
4 |
| XSRETURN_EMPTY; |
| 54 |
5 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
5 |
5 |
|
| 55 |
5 |
| mPUSHp(body, blen); |
| 56 |
5 |
| mPUSHi(compressed); |
| 57 |
5 |
| mPUSHu((UV)consumed); |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
|
| # Is this status one the OTLP spec says to retry? |
| 61 |
|
| # |
| 62 |
|
| # RESOURCE_EXHAUSTED is the odd one: retryable ONLY with RetryInfo in the |
| 63 |
|
| # details. Without it the server is refusing a quota, and retrying a quota |
| 64 |
|
| # refusal on a timer is how a client turns its own rate limit into an outage. |
| 65 |
|
| IV |
| 66 |
|
| retryable(code, has_retry_info = 0) |
| 67 |
|
| int code |
| 68 |
|
| int has_retry_info |
| 69 |
|
| CODE: |
| 70 |
17 |
▶ | RETVAL = otel_grpc_retryable(code, has_retry_info); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = otel_grpc_retryable(code, has_retry_info); |
14 |
3 |
17 |
|
| 71 |
|
| OUTPUT: |
| 72 |
|
| RETVAL |
| 73 |
|
| |
| 74 |
|
| # The verdict a caller acts on: 0 ok, 1 retry, 2 permanent. Mirrors the HTTP |
| 75 |
|
| # transport's, so a caller branches once. |
| 76 |
|
| # |
| 77 |
|
| # A MISSING grpc-status is not success. It means the stream ended without the |
| 78 |
|
| # server saying how it went, which is a transport failure and is retryable. |
| 79 |
|
| # Treating it as OK is the specific bug that makes a broken gRPC client look |
| 80 |
|
| # perfectly healthy. |
| 81 |
|
| IV |
| 82 |
|
| verdict(have_status, code = 0, has_retry_info = 0) |
| 83 |
|
| int have_status |
| 84 |
|
| int code |
| 85 |
|
| int has_retry_info |
| 86 |
|
| CODE: |
| 87 |
7 |
▶ | RETVAL = otel_grpc_verdict(have_status, code, has_retry_info); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = otel_grpc_verdict(have_status, code, has_retry_info); |
0 |
7 |
7 |
|
| 88 |
|
| OUTPUT: |
| 89 |
|
| RETVAL |
| 90 |
|
| |
| 91 |
|
| # The retry delay a server named in grpc-status-details-bin, in seconds. |
| 92 |
|
| # Undef when there is none. When the server names one it wins over any computed backoff: |
| 93 |
|
| # it knows when it will be ready and the client does not. |
| 94 |
|
| SV * |
| 95 |
|
| retry_after(details) |
| 96 |
|
| SV *details |
| 97 |
|
| CODE: |
| 98 |
|
| { |
| 99 |
|
| STRLEN l; |
| 100 |
7 |
▶ | const char *s = SvOK(details) ? SvPV_const(details, l) : (l = 0, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *s = SvOK(details) ? SvPV_const(details, l) : (l = 0, NULL); |
3 |
1 |
4 |
|
| 101 |
4 |
| double d = otel_grpc_retry_after(s, l); |
| 102 |
4 |
▶ | if (d < 0) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d < 0) XSRETURN_UNDEF; |
3 |
1 |
4 |
|
| 103 |
1 |
| RETVAL = newSVnv(d); |
| 104 |
|
| } |
| 105 |
|
| OUTPUT: |
| 106 |
|
| RETVAL |
| 107 |
|
| |
| 108 |
|
| # The default port for a protocol. The single most common OTLP |
| 109 |
|
| # misconfiguration is sending gRPC to 4318 or HTTP to 4317, so this is stated |
| 110 |
|
| # rather than assumed, and the boot diagnostic prints which was chosen. |
| 111 |
|
| IV |
| 112 |
|
| default_port(protocol) |
| 113 |
|
| SV *protocol |
| 114 |
|
| CODE: |
| 115 |
|
| { |
| 116 |
3 |
| const char *p = SvPV_nolen(protocol); |
| 117 |
3 |
▶ | RETVAL = strEQ(p, "grpc") ? 4317 : 4318; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = strEQ(p, "grpc") ? 4317 : 4318; |
1 |
2 |
3 |
| RETVAL = strEQ(p, "grpc") ? 4317 : 4318; |
0 |
3 |
3 |
|
| 118 |
|
| } |
| 119 |
|
| OUTPUT: |
| 120 |
|
| RETVAL |
| 121 |
|
| |
| 122 |
|
| # The headers a gRPC request must carry. `te: trailers` is not optional: it is |
| 123 |
|
| # how a client tells the server it will read the trailing metadata, and the |
| 124 |
|
| # status lives there. |
| 125 |
|
| void |
| 126 |
|
| headers(compressed = 0) |
| 127 |
|
| int compressed |
| 128 |
|
| PPCODE: |
| 129 |
|
| { |
| 130 |
2 |
▶ | EXTEND(SP, 8); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 8); |
0 |
2 |
2 |
|
| 131 |
2 |
| mPUSHp("content-type", 12); |
| 132 |
2 |
| mPUSHp("application/grpc+proto", 22); |
| 133 |
2 |
| mPUSHp("te", 2); |
| 134 |
2 |
| mPUSHp("trailers", 8); |
| 135 |
2 |
▶ | if (compressed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (compressed) { |
1 |
1 |
2 |
|
| 136 |
|
| /* gzip lives in the FRAME flag and in grpc-encoding - NOT in |
| 137 |
|
| * HTTP's content-encoding. Two mechanisms with similar names, and |
| 138 |
|
| * using the HTTP one produces a request the collector rejects |
| 139 |
|
| * with a confusing message. */ |
| 140 |
1 |
| mPUSHp("grpc-encoding", 13); |
| 141 |
1 |
| mPUSHp("gzip", 4); |
| 142 |
|
| } |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| # ---- the transport half ------------------------------------------------------ |
| 146 |
|
| # Made possible by Fetch 0.15, which captures HTTP/2 trailers. Before it, the |
| 147 |
|
| # grpc-status could not be read at all. |
| 148 |
|
| |
| 149 |
|
| # classify($res) -> (verdict, code, message, retry_after) |
| 150 |
|
| # |
| 151 |
|
| # Looks for grpc-status in the TRAILERS first, then in the response HEADERS - |
| 152 |
|
| # and both are normal. An ordinary call puts it in the trailers; a |
| 153 |
|
| # TRAILERS-ONLY response, which is what a server sends when it fails before |
| 154 |
|
| # producing a body, puts it in the one and only HEADERS frame. A client that |
| 155 |
|
| # looked only at trailers would find no status on exactly the responses that |
| 156 |
|
| # failed fastest. |
| 157 |
|
| void |
| 158 |
|
| classify(res) |
| 159 |
|
| SV *res |
| 160 |
|
| PPCODE: |
| 161 |
|
| { |
| 162 |
5 |
| SV *st = otel_grpc_field(aTHX_ res, "grpc-status", 11); |
| 163 |
5 |
| SV *msg = otel_grpc_field(aTHX_ res, "grpc-message", 12); |
| 164 |
5 |
| SV *det = otel_grpc_field(aTHX_ res, "grpc-status-details-bin", 23); |
| 165 |
5 |
▶ | int have = st && SvOK(st) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| int have = st && SvOK(st) ? 1 : 0; |
4 |
1 |
5 |
| int have = st && SvOK(st) ? 1 : 0; |
4 |
0 |
4 |
|
| 166 |
5 |
▶ | int code = have ? (int)SvIV(st) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int code = have ? (int)SvIV(st) : 0; |
4 |
1 |
5 |
|
| 167 |
5 |
| double after = -1; |
| 168 |
5 |
| int has_ri = 0; |
| 169 |
5 |
▶ | if (det && SvOK(det)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (det && SvOK(det)) { |
0 |
5 |
5 |
| if (det && SvOK(det)) { |
0 |
0 |
0 |
|
| 170 |
|
| STRLEN dl; |
| 171 |
0 |
| const char *ds = SvPV_const(det, dl); |
| 172 |
0 |
| after = otel_grpc_retry_after(ds, dl); |
| 173 |
0 |
| has_ri = after >= 0; |
| 174 |
|
| } |
| 175 |
5 |
▶ | EXTEND(SP, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 4); |
0 |
5 |
5 |
|
| 176 |
5 |
| mPUSHi(otel_grpc_verdict(have, code, has_ri)); |
| 177 |
5 |
▶ | mPUSHi(have ? code : -1); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHi(have ? code : -1); |
4 |
1 |
5 |
|
| 178 |
5 |
▶ | if (msg && SvOK(msg)) mPUSHs(newSVsv(msg)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (msg && SvOK(msg)) mPUSHs(newSVsv(msg)); |
2 |
3 |
5 |
| if (msg && SvOK(msg)) mPUSHs(newSVsv(msg)); |
2 |
0 |
2 |
|
| 179 |
3 |
| else PUSHs(&PL_sv_undef); |
| 180 |
5 |
▶ | if (after >= 0) mPUSHn(after); |
Branch: 1 branch
| Branch | True | False | Total |
| if (after >= 0) mPUSHn(after); |
0 |
5 |
5 |
|
| 181 |
5 |
| else PUSHs(&PL_sv_undef); |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
|
| # send($ua, $endpoint, $signal, $bytes, $timeout) -> the ua's future. |
| 185 |
|
| # |
| 186 |
|
| # The same protobuf the HTTP transport sends, in a gRPC frame, over HTTP/2 to |
| 187 |
|
| # the fixed service path. The endpoint is a scheme://host:port with NO path - |
| 188 |
|
| # the path is protocol, not configuration. |
| 189 |
|
| SV * |
| 190 |
|
| send(ua, endpoint, signal, bytes, timeout = 10) |
| 191 |
|
| SV *ua |
| 192 |
|
| SV *endpoint |
| 193 |
|
| SV *signal |
| 194 |
|
| SV *bytes |
| 195 |
|
| double timeout |
| 196 |
|
| CODE: |
| 197 |
|
| { |
| 198 |
0 |
| const char *path = otel_grpc_path(SvPV_nolen(signal)); |
| 199 |
|
| SV *url, *body; |
| 200 |
|
| AV *hdrs; |
| 201 |
0 |
▶ | if (!path) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!path) |
0 |
0 |
0 |
|
| 202 |
0 |
| croak("Punk::OpenTelemetry::GRPC::send: unknown signal '%s'", |
| 203 |
|
| SvPV_nolen(signal)); |
| 204 |
|
| { |
| 205 |
|
| STRLEN el; |
| 206 |
0 |
| const char *es = SvPV_const(endpoint, el); |
| 207 |
0 |
▶ | while (el && es[el - 1] == '/') el--; /* no double slash */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (el && es[el - 1] == '/') el--; /* no double slash */ |
0 |
0 |
0 |
| while (el && es[el - 1] == '/') el--; /* no double slash */ |
0 |
0 |
0 |
|
| 208 |
0 |
| url = sv_2mortal(newSVpvn(es, el)); |
| 209 |
0 |
| sv_catpv(url, path); |
| 210 |
|
| } |
| 211 |
0 |
| body = sv_2mortal(otel_grpc_frame(aTHX_ bytes, 0)); |
| 212 |
|
| |
| 213 |
0 |
| hdrs = (AV *)sv_2mortal((SV *)newAV()); |
| 214 |
0 |
| av_push(hdrs, newSVpvs("content-type")); |
| 215 |
0 |
| av_push(hdrs, newSVpvs("application/grpc+proto")); |
| 216 |
|
| /* te: trailers is how a client says it will read the trailing |
| 217 |
|
| * metadata, and the status lives there */ |
| 218 |
0 |
| av_push(hdrs, newSVpvs("te")); |
| 219 |
0 |
| av_push(hdrs, newSVpvs("trailers")); |
| 220 |
|
| |
| 221 |
|
| { |
| 222 |
0 |
| dSP; int count; SV *f = NULL; |
| 223 |
0 |
| ENTER; SAVETMPS; |
| 224 |
0 |
▶ | PUSHMARK(SP); EXTEND(SP, 9); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 9); |
0 |
0 |
0 |
| PUSHMARK(SP); EXTEND(SP, 9); |
0 |
0 |
0 |
|
| 225 |
0 |
| PUSHs(ua); |
| 226 |
0 |
| PUSHs(sv_2mortal(newSVpvs("POST"))); |
| 227 |
0 |
| PUSHs(url); |
| 228 |
0 |
| PUSHs(sv_2mortal(newSVpvs("headers"))); |
| 229 |
0 |
| PUSHs(sv_2mortal(newRV_inc((SV *)hdrs))); |
| 230 |
0 |
| PUSHs(sv_2mortal(newSVpvs("body"))); |
| 231 |
0 |
| PUSHs(body); |
| 232 |
0 |
| PUSHs(sv_2mortal(newSVpvs("timeout"))); |
| 233 |
0 |
| PUSHs(sv_2mortal(newSVnv(timeout))); |
| 234 |
0 |
| PUTBACK; |
| 235 |
0 |
| count = call_method("request", G_SCALAR); |
| 236 |
0 |
| SPAGAIN; |
| 237 |
0 |
▶ | if (count > 0) f = newSVsv(POPs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) f = newSVsv(POPs); |
0 |
0 |
0 |
|
| 238 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 239 |
0 |
▶ | if (!f) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) XSRETURN_UNDEF; |
0 |
0 |
0 |
|
| 240 |
0 |
| RETVAL = f; |
| 241 |
|
| } |
| 242 |
|
| } |
| 243 |
|
| OUTPUT: |
| 244 |
|
| RETVAL |