| 1 |
|
| /* ft_obs.h - the outbound request observer registry (fetch_abi v2). |
| 2 |
|
| * |
| 3 |
|
| * Fetch had no way to see a request go out. `headers` is a per-request option |
| 4 |
|
| * the CALLER sets, which is no use to anything that wants to annotate a |
| 5 |
|
| * request the application wrote - a tracing layer adding a `traceparent`, an |
| 6 |
|
| * audit log recording what this process asked of whom, a metrics counter |
| 7 |
|
| * timing an upstream. All of those need to reach a request they did not |
| 8 |
|
| * build, and to be told how it ended. |
| 9 |
|
| * |
| 10 |
|
| * So: a start callback, fired once per HOP with the merged header list still |
| 11 |
|
| * mutable, which returns an opaque token; and a done callback, fired exactly |
| 12 |
|
| * once with that token when the request settles, however it settled. |
| 13 |
|
| * |
| 14 |
|
| * Registration is process-global, matching hm_abi's worker hook and Punk's |
| 15 |
|
| * pk_abi observers: an agent is a per-worker object and there may be many, |
| 16 |
|
| * while an observer is a property of the process. |
| 17 |
|
| * |
| 18 |
|
| * Needs ft_abi.h (the typedefs). Included before ft_ua.h, which fires it. |
| 19 |
|
| */ |
| 20 |
|
| |
| 21 |
|
| #ifndef FT_OBS_H |
| 22 |
|
| #define FT_OBS_H |
| 23 |
|
| |
| 24 |
|
| #include "fetch_abi.h" |
| 25 |
|
| |
| 26 |
|
| static struct { fetch_obs_start_cb start; fetch_obs_done_cb done; void *ud; } |
| 27 |
|
| FT_OBS[FETCH_ABI_MAX_OBSERVERS]; |
| 28 |
|
| static int FT_OBS_N = 0; |
| 29 |
|
| |
| 30 |
|
| /* The per-request tokens. Heap, because it has to outlive the call that made |
| 31 |
|
| * it and travel to the settle callback, and one allocation per instrumented |
| 32 |
|
| * request is the price of not having a fixed slot per observer on every |
| 33 |
|
| * request that is not instrumented at all. */ |
| 34 |
|
| typedef struct { void *tok[FETCH_ABI_MAX_OBSERVERS]; int n; } ft_obs_tokens; |
| 35 |
|
| |
| 36 |
5 |
| static int ft_obs_add(pTHX_ fetch_obs_start_cb start, fetch_obs_done_cb done, |
| 37 |
|
| void *ud) { |
| 38 |
|
| PERL_UNUSED_CONTEXT; |
| 39 |
5 |
▶ | if (!start || FT_OBS_N >= FETCH_ABI_MAX_OBSERVERS) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!start || FT_OBS_N >= FETCH_ABI_MAX_OBSERVERS) return 0; |
5 |
0 |
5 |
| if (!start || FT_OBS_N >= FETCH_ABI_MAX_OBSERVERS) return 0; |
0 |
5 |
5 |
|
| 40 |
5 |
| FT_OBS[FT_OBS_N].start = start; |
| 41 |
5 |
| FT_OBS[FT_OBS_N].done = done; |
| 42 |
5 |
| FT_OBS[FT_OBS_N].ud = ud; |
| 43 |
5 |
| FT_OBS_N++; |
| 44 |
5 |
| return 1; |
| 45 |
|
| } |
| 46 |
|
| |
| 47 |
|
| /* Fire the start half. `hav` is the merged header list, still mutable: an |
| 48 |
|
| * observer may push a name and a value onto it and the request will carry |
| 49 |
|
| * them. Returns the tokens, or NULL when nobody is listening. */ |
| 50 |
151 |
| static ft_obs_tokens *ft_obs_start(pTHX_ const char *method, STRLEN mlen, |
| 51 |
|
| const char *url, STRLEN ulen, AV *hav) { |
| 52 |
|
| ft_obs_tokens *t; |
| 53 |
|
| int i; |
| 54 |
151 |
▶ | if (!FT_OBS_N) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!FT_OBS_N) return NULL; |
138 |
13 |
151 |
|
| 55 |
13 |
| Newxz(t, 1, ft_obs_tokens); |
| 56 |
13 |
| t->n = FT_OBS_N; |
| 57 |
42 |
▶ | for (i = 0; i < FT_OBS_N; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < FT_OBS_N; i++) |
29 |
13 |
42 |
|
| 58 |
29 |
| t->tok[i] = FT_OBS[i].start(aTHX_ method, mlen, url, ulen, hav, |
| 59 |
|
| FT_OBS[i].ud); |
| 60 |
13 |
| return t; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| /* Fire the done half and free the tokens. res and err are borrowed; exactly |
| 64 |
|
| * one of them is non-NULL. */ |
| 65 |
13 |
| static void ft_obs_done(pTHX_ ft_obs_tokens *t, SV *res, SV *err) { |
| 66 |
|
| int i; |
| 67 |
13 |
▶ | if (!t) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return; |
0 |
13 |
13 |
|
| 68 |
42 |
▶ | for (i = 0; i < t->n && i < FT_OBS_N; i++) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < t->n && i < FT_OBS_N; i++) |
29 |
13 |
42 |
| for (i = 0; i < t->n && i < FT_OBS_N; i++) |
29 |
0 |
29 |
|
| 69 |
29 |
▶ | if (FT_OBS[i].done) FT_OBS[i].done(aTHX_ t->tok[i], res, err, |
Branch: 1 branch
| Branch | True | False | Total |
| if (FT_OBS[i].done) FT_OBS[i].done(aTHX_ t->tok[i], res, err, |
29 |
0 |
29 |
|
| 70 |
|
| FT_OBS[i].ud); |
| 71 |
13 |
| Safefree(t); |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| #endif /* FT_OBS_H */ |