| 1 |
|
| /* punk_obs.h - the C ABI's observer registry and its firing points. |
| 2 |
|
| * |
| 3 |
|
| * Split out from pk_abi_impl.h and included EARLY, because the two places |
| 4 |
|
| * that fire an observer - punk_deliver (punk_context.h) and the dispatcher |
| 5 |
|
| * (punk_serve.h) - are compiled long before the accessor half of the table |
| 6 |
|
| * can be, and only the storage and the fire loops are needed there. |
| 7 |
|
| * |
| 8 |
|
| * Needs pk_abi.h (the callback typedefs) and nothing else of Punk's. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #ifndef PUNK_OBS_H |
| 12 |
|
| #define PUNK_OBS_H |
| 13 |
|
| |
| 14 |
|
| #include "pk_abi.h" |
| 15 |
|
| |
| 16 |
|
| static struct { pk_abi_req_cb cb; void *ud; } |
| 17 |
|
| PK_OBS_REQ[PK_ABI_MAX_OBSERVERS]; |
| 18 |
|
| static struct { pk_abi_res_cb cb; void *ud; } |
| 19 |
|
| PK_OBS_RES[PK_ABI_MAX_OBSERVERS]; |
| 20 |
|
| |
| 21 |
|
| static int PK_OBS_REQ_N = 0; |
| 22 |
|
| static int PK_OBS_RES_N = 0; |
| 23 |
|
| |
| 24 |
|
| /* The context of the dispatch frame running right now, or NULL. Borrowed: the |
| 25 |
|
| * frame that set it holds a reference for the whole time it is readable. |
| 26 |
|
| * |
| 27 |
|
| * Saved and restored around that frame, never merely cleared - which is the |
| 28 |
|
| * property the v5 note in pk_abi.h turns on. pk_cur_set is what the dispatcher |
| 29 |
|
| * calls once the context exists; pk_cur_restore is what the save stack calls |
| 30 |
|
| * on the way out, by either exit. */ |
| 31 |
|
| static SV *PK_CURRENT = NULL; |
| 32 |
|
| |
| 33 |
221477 |
| static void pk_cur_set(pTHX_ SV *c) { |
| 34 |
|
| PERL_UNUSED_CONTEXT; |
| 35 |
221477 |
| PK_CURRENT = c; |
| 36 |
221477 |
| } |
| 37 |
|
| |
| 38 |
60 |
| static SV *pk_cur_get(pTHX) { |
| 39 |
|
| PERL_UNUSED_CONTEXT; |
| 40 |
60 |
| return PK_CURRENT; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| /* The save-stack destructor. `p` is the value to put back, so a mounted |
| 44 |
|
| * application inside another restores the outer context rather than NULL. */ |
| 45 |
221542 |
| static void pk_cur_restore(pTHX_ void *p) { |
| 46 |
|
| PERL_UNUSED_CONTEXT; |
| 47 |
221542 |
| PK_CURRENT = (SV *)p; |
| 48 |
221542 |
| } |
| 49 |
|
| |
| 50 |
|
| /* Registering either kind makes the dispatcher build the context before |
| 51 |
|
| * routing, so a response observer is handed the same context the request |
| 52 |
|
| * observer saw whatever answered the request. One int to test on the hot |
| 53 |
|
| * path. */ |
| 54 |
|
| static int PK_OBS_ANY = 0; |
| 55 |
|
| |
| 56 |
14 |
| static int pk_obs_add_req(pTHX_ pk_abi_req_cb cb, void *ud) { |
| 57 |
|
| PERL_UNUSED_CONTEXT; |
| 58 |
|
| /* |
| 59 |
|
| # uncoverable branch true |
| 60 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 61 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 62 |
14 |
▶ | if (!cb || PK_OBS_REQ_N >= PK_ABI_MAX_OBSERVERS) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cb || PK_OBS_REQ_N >= PK_ABI_MAX_OBSERVERS) return 0; |
14 |
0 |
14 |
| if (!cb || PK_OBS_REQ_N >= PK_ABI_MAX_OBSERVERS) return 0; |
0 |
14 |
14 |
|
| 63 |
14 |
| PK_OBS_REQ[PK_OBS_REQ_N].cb = cb; |
| 64 |
14 |
| PK_OBS_REQ[PK_OBS_REQ_N].ud = ud; |
| 65 |
14 |
| PK_OBS_REQ_N++; |
| 66 |
14 |
| PK_OBS_ANY = 1; |
| 67 |
14 |
| return 1; |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
14 |
| static int pk_obs_add_res(pTHX_ pk_abi_res_cb cb, void *ud) { |
| 71 |
|
| PERL_UNUSED_CONTEXT; |
| 72 |
|
| /* |
| 73 |
|
| # uncoverable branch true |
| 74 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 75 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 76 |
14 |
▶ | if (!cb || PK_OBS_RES_N >= PK_ABI_MAX_OBSERVERS) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cb || PK_OBS_RES_N >= PK_ABI_MAX_OBSERVERS) return 0; |
14 |
0 |
14 |
| if (!cb || PK_OBS_RES_N >= PK_ABI_MAX_OBSERVERS) return 0; |
0 |
14 |
14 |
|
| 77 |
14 |
| PK_OBS_RES[PK_OBS_RES_N].cb = cb; |
| 78 |
14 |
| PK_OBS_RES[PK_OBS_RES_N].ud = ud; |
| 79 |
14 |
| PK_OBS_RES_N++; |
| 80 |
14 |
| PK_OBS_ANY = 1; |
| 81 |
14 |
| return 1; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| /* Fire, in registration order. A callback is C and is documented as unable to |
| 85 |
|
| * croak, so there is no eval frame here and no per-request cost for one. */ |
| 86 |
479 |
| static void pk_obs_fire_req(pTHX_ SV *c) { |
| 87 |
|
| int i; |
| 88 |
958 |
▶ | for (i = 0; i < PK_OBS_REQ_N; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PK_OBS_REQ_N; i++) |
479 |
479 |
958 |
|
| 89 |
479 |
| PK_OBS_REQ[i].cb(aTHX_ c, PK_OBS_REQ[i].ud); |
| 90 |
479 |
| } |
| 91 |
|
| |
| 92 |
479 |
| static void pk_obs_fire_res(pTHX_ SV *c, SV *response) { |
| 93 |
|
| int i; |
| 94 |
958 |
▶ | for (i = 0; i < PK_OBS_RES_N; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PK_OBS_RES_N; i++) |
479 |
479 |
958 |
|
| 95 |
479 |
| PK_OBS_RES[i].cb(aTHX_ c, response, PK_OBS_RES[i].ud); |
| 96 |
479 |
| } |
| 97 |
|
| |
| 98 |
|
| /* ---- v2: the shipped Punk::Model::DBI backend's statements -------------- */ |
| 99 |
|
| |
| 100 |
|
| static struct { pk_abi_query_cb start; pk_abi_query_done_cb done; void *ud; } |
| 101 |
|
| PK_OBS_QRY[PK_ABI_MAX_OBSERVERS]; |
| 102 |
|
| static int PK_OBS_QRY_N = 0; |
| 103 |
|
| |
| 104 |
|
| typedef struct { void *tok[PK_ABI_MAX_OBSERVERS]; int n; } pk_obs_qtokens; |
| 105 |
|
| |
| 106 |
2 |
| static int pk_obs_add_query(pTHX_ pk_abi_query_cb start, |
| 107 |
|
| pk_abi_query_done_cb done, void *ud) { |
| 108 |
|
| PERL_UNUSED_CONTEXT; |
| 109 |
|
| /* |
| 110 |
|
| # uncoverable branch true |
| 111 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 112 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 113 |
2 |
▶ | if (!start || PK_OBS_QRY_N >= PK_ABI_MAX_OBSERVERS) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!start || PK_OBS_QRY_N >= PK_ABI_MAX_OBSERVERS) return 0; |
2 |
0 |
2 |
| if (!start || PK_OBS_QRY_N >= PK_ABI_MAX_OBSERVERS) return 0; |
0 |
2 |
2 |
|
| 114 |
2 |
| PK_OBS_QRY[PK_OBS_QRY_N].start = start; |
| 115 |
2 |
| PK_OBS_QRY[PK_OBS_QRY_N].done = done; |
| 116 |
2 |
| PK_OBS_QRY[PK_OBS_QRY_N].ud = ud; |
| 117 |
2 |
| PK_OBS_QRY_N++; |
| 118 |
2 |
| return 1; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| /* Returns the tokens, or NULL when nobody is listening - the branch every |
| 122 |
|
| * uninstrumented statement takes, allocating nothing. */ |
| 123 |
17 |
| static void *pk_obs_query_start(pTHX_ SV *sql, int nbind) { |
| 124 |
|
| pk_obs_qtokens *t; |
| 125 |
17 |
| STRLEN sl = 0; |
| 126 |
|
| const char *sp; |
| 127 |
|
| int i; |
| 128 |
|
| /* SvOK DOES NOT APPLY GET MAGIC. A statement text read straight out of a |
| 129 |
|
| * DBI handle - $sth->{Statement} - has no value in it until magic has |
| 130 |
|
| * run, so testing SvOK first takes the "" branch and hands every observer |
| 131 |
|
| * an empty statement, while a copy of the same value through a lexical |
| 132 |
|
| * works. Punk::DBI now forces magic before it gets here, so this is the |
| 133 |
|
| * belt and not the braces; it stays because the guard is wrong without |
| 134 |
|
| * it and the next caller will not know that. */ |
| 135 |
|
| /* |
| 136 |
|
| # uncoverable branch false |
| 137 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 138 |
17 |
▶ | if (sql) SvGETMAGIC(sql); |
Branch: 1 branch
| Branch | True | False | Total |
| if (sql) SvGETMAGIC(sql); |
17 |
0 |
17 |
|
| 139 |
|
| /* |
| 140 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 141 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 142 |
34 |
▶ | sp = (sql && SvOK(sql)) ? SvPV_nomg_const(sql, sl) : ""; |
Branch: 2 branches
| Branch | True | False | Total |
| sp = (sql && SvOK(sql)) ? SvPV_nomg_const(sql, sl) : ""; |
17 |
0 |
17 |
| sp = (sql && SvOK(sql)) ? SvPV_nomg_const(sql, sl) : ""; |
17 |
0 |
17 |
|
| 143 |
|
| /* |
| 144 |
|
| # uncoverable branch true |
| 145 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 146 |
17 |
▶ | if (!PK_OBS_QRY_N) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PK_OBS_QRY_N) return NULL; |
0 |
17 |
17 |
|
| 147 |
17 |
| Newxz(t, 1, pk_obs_qtokens); |
| 148 |
17 |
| t->n = PK_OBS_QRY_N; |
| 149 |
34 |
▶ | for (i = 0; i < PK_OBS_QRY_N; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PK_OBS_QRY_N; i++) |
17 |
17 |
34 |
|
| 150 |
17 |
| t->tok[i] = PK_OBS_QRY[i].start(aTHX_ sp, sl, nbind, |
| 151 |
|
| PK_OBS_QRY[i].ud); |
| 152 |
17 |
| return t; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| /* `r` is what DBI's execute returned: a false or absent value is a failure. */ |
| 156 |
17 |
| static void pk_obs_query_done(pTHX_ void *tv, SV *r) { |
| 157 |
17 |
| pk_obs_qtokens *t = (pk_obs_qtokens *)tv; |
| 158 |
|
| /* |
| 159 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 160 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 161 |
17 |
▶ | int ok = (r && SvOK(r) && SvTRUE(r)) ? 1 : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| int ok = (r && SvOK(r) && SvTRUE(r)) ? 1 : 0; |
17 |
0 |
17 |
| int ok = (r && SvOK(r) && SvTRUE(r)) ? 1 : 0; |
17 |
0 |
17 |
| int ok = (r && SvOK(r) && SvTRUE(r)) ? 1 : 0; |
16 |
1 |
17 |
|
| 162 |
|
| int i; |
| 163 |
|
| /* |
| 164 |
|
| # uncoverable branch true |
| 165 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 166 |
17 |
▶ | if (!t) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return; |
0 |
17 |
17 |
|
| 167 |
34 |
▶ | for (i = 0; i < t->n && i < PK_OBS_QRY_N; i++) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < t->n && i < PK_OBS_QRY_N; i++) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
17 |
17 |
34 |
| for (i = 0; i < t->n && i < PK_OBS_QRY_N; i++) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
17 |
0 |
17 |
|
| 168 |
|
| /* |
| 169 |
|
| # uncoverable branch false |
| 170 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 171 |
17 |
▶ | if (PK_OBS_QRY[i].done) |
Branch: 1 branch
| Branch | True | False | Total |
| if (PK_OBS_QRY[i].done) |
17 |
0 |
17 |
|
| 172 |
17 |
| PK_OBS_QRY[i].done(aTHX_ t->tok[i], ok, PK_OBS_QRY[i].ud); |
| 173 |
17 |
| Safefree(t); |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
|
| /* ---- the log observer --------------------------------------------------- * |
| 177 |
|
| * A registration point for something that wants a COPY of every record: the |
| 178 |
|
| * logs signal of a telemetry layer, an audit sink, a test harness. |
| 179 |
|
| * |
| 180 |
|
| * A TAP, not a replacement. The line still goes wherever it was going - |
| 181 |
|
| * stderr, psgix.logger, a `to` coderef - and the observer gets a duplicate. |
| 182 |
|
| * The alternative, letting an observer take over the sink, means a telemetry |
| 183 |
|
| * layer silently redirects an operator's logs, and that when its collector is |
| 184 |
|
| * unreachable the logs simply vanish. Nobody should have to weigh "do I want |
| 185 |
|
| * my logs, or do I want them exported". |
| 186 |
|
| * |
| 187 |
|
| * The observer is C and must not croak: it runs inside the logger, which |
| 188 |
|
| * already promises that a failing sink never takes the request down. */ |
| 189 |
|
| typedef void (*pl_obs_cb)(pTHX_ const char *level, STRLEN llen, SV *msg, |
| 190 |
|
| HV *fields, void *ud); |
| 191 |
|
| |
| 192 |
|
| /* The same tap, plus the CONTEXT the line was logged against - which is what |
| 193 |
|
| * an observer needs to correlate a record with the request that produced it. |
| 194 |
|
| * Without it the only way to find the active span is a process-global, and on |
| 195 |
|
| * an event loop with two requests in flight that attributes one request's |
| 196 |
|
| * lines to the other. `c` is NULL for a line logged against the application |
| 197 |
|
| * rather than a request. */ |
| 198 |
|
| typedef void (*pl_obs_ctx_cb)(pTHX_ SV *c, const char *level, STRLEN llen, |
| 199 |
|
| SV *msg, HV *fields, void *ud); |
| 200 |
|
| |
| 201 |
|
| #define PL_OBS_MAX 4 |
| 202 |
|
| static struct { pl_obs_cb cb; void *ud; } PL_OBS[PL_OBS_MAX]; |
| 203 |
|
| static int PL_OBS_N = 0; |
| 204 |
|
| static struct { pl_obs_ctx_cb cb; void *ud; } PL_OBS_CTX[PL_OBS_MAX]; |
| 205 |
|
| static int PL_OBS_CTX_N = 0; |
| 206 |
|
| |
| 207 |
|
| /* |
| 208 |
|
| # uncoverable statement |
| 209 |
|
| # uncoverable subroutine */ |
| 210 |
0 |
| static int pl_observe(pTHX_ pl_obs_cb cb, void *ud) { |
| 211 |
|
| PERL_UNUSED_CONTEXT; |
| 212 |
|
| /* |
| 213 |
|
| # uncoverable statement |
| 214 |
|
| # uncoverable branch true |
| 215 |
|
| # uncoverable branch false |
| 216 |
|
| # uncoverable condition left count:1 |
| 217 |
|
| # uncoverable condition right count:1 |
| 218 |
|
| # uncoverable condition left count:2 |
| 219 |
|
| # uncoverable condition right count:2 */ |
| 220 |
0 |
▶ | if (!cb || PL_OBS_N >= PL_OBS_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cb || PL_OBS_N >= PL_OBS_MAX) return 0; |
0 |
0 |
0 |
| if (!cb || PL_OBS_N >= PL_OBS_MAX) return 0; |
0 |
0 |
0 |
|
| 221 |
0 |
| PL_OBS[PL_OBS_N].cb = cb; /* # uncoverable statement */ |
| 222 |
0 |
| PL_OBS[PL_OBS_N].ud = ud; /* # uncoverable statement */ |
| 223 |
0 |
| PL_OBS_N++; /* # uncoverable statement */ |
| 224 |
0 |
| return 1; /* # uncoverable statement */ |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
|
| /* |
| 228 |
|
| # uncoverable statement |
| 229 |
|
| # uncoverable subroutine */ |
| 230 |
0 |
| static int pl_observe_ctx(pTHX_ pl_obs_ctx_cb cb, void *ud) { |
| 231 |
|
| PERL_UNUSED_CONTEXT; |
| 232 |
|
| /* |
| 233 |
|
| # uncoverable statement |
| 234 |
|
| # uncoverable branch true |
| 235 |
|
| # uncoverable branch false |
| 236 |
|
| # uncoverable condition left count:1 |
| 237 |
|
| # uncoverable condition right count:1 |
| 238 |
|
| # uncoverable condition left count:2 |
| 239 |
|
| # uncoverable condition right count:2 */ |
| 240 |
0 |
▶ | if (!cb || PL_OBS_CTX_N >= PL_OBS_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cb || PL_OBS_CTX_N >= PL_OBS_MAX) return 0; |
0 |
0 |
0 |
| if (!cb || PL_OBS_CTX_N >= PL_OBS_MAX) return 0; |
0 |
0 |
0 |
|
| 241 |
0 |
| PL_OBS_CTX[PL_OBS_CTX_N].cb = cb; /* # uncoverable statement */ |
| 242 |
0 |
| PL_OBS_CTX[PL_OBS_CTX_N].ud = ud; /* # uncoverable statement */ |
| 243 |
0 |
| PL_OBS_CTX_N++; /* # uncoverable statement */ |
| 244 |
0 |
| return 1; /* # uncoverable statement */ |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| /* The guard the firing sites use, so "is anybody listening" is one load and |
| 248 |
|
| * one branch where a request would otherwise pay nothing at all. */ |
| 249 |
|
| #define PK_OBS_WANT_RES (PK_OBS_RES_N > 0) |
| 250 |
|
| #define PK_OBS_WANT_REQ (PK_OBS_REQ_N > 0) |
| 251 |
|
| #define PK_OBS_WANT_QUERY (PK_OBS_QRY_N > 0) |
| 252 |
|
| |
| 253 |
|
| #endif /* PUNK_OBS_H */ |