include/punk/punk_obs.h

filter: Statementstmt 82.0% 55 / 67 Branchbran 52.0% 25 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.5% 80 / 115 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_plural.h ↑ Summary include/punk/punk_auth.h »
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 219426 static void pk_cur_set(pTHX_ SV *c) {
34 PERL_UNUSED_CONTEXT;
35 219426 PK_CURRENT = c;
36 219426 }
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 219525 static void pk_cur_restore(pTHX_ void *p) {
46 PERL_UNUSED_CONTEXT;
47 219525 PK_CURRENT = (SV *)p;
48 219525 }
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 8 static int pk_obs_add_req(pTHX_ pk_abi_req_cb cb, void *ud) {
57 PERL_UNUSED_CONTEXT;
58 8 ▶ if (!cb || PK_OBS_REQ_N >= PK_ABI_MAX_OBSERVERS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!cb || PK_OBS_REQ_N >= PK_ABI_MAX_OBSERVERS) return 0; 8 0 8
if (!cb || PK_OBS_REQ_N >= PK_ABI_MAX_OBSERVERS) return 0; 0 8 8
59 8 PK_OBS_REQ[PK_OBS_REQ_N].cb = cb;
60 8 PK_OBS_REQ[PK_OBS_REQ_N].ud = ud;
61 8 PK_OBS_REQ_N++;
62 8 PK_OBS_ANY = 1;
63 8 return 1;
64 }
65  
66 8 static int pk_obs_add_res(pTHX_ pk_abi_res_cb cb, void *ud) {
67 PERL_UNUSED_CONTEXT;
68 8 ▶ if (!cb || PK_OBS_RES_N >= PK_ABI_MAX_OBSERVERS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!cb || PK_OBS_RES_N >= PK_ABI_MAX_OBSERVERS) return 0; 8 0 8
if (!cb || PK_OBS_RES_N >= PK_ABI_MAX_OBSERVERS) return 0; 0 8 8
69 8 PK_OBS_RES[PK_OBS_RES_N].cb = cb;
70 8 PK_OBS_RES[PK_OBS_RES_N].ud = ud;
71 8 PK_OBS_RES_N++;
72 8 PK_OBS_ANY = 1;
73 8 return 1;
74 }
75  
76 /* Fire, in registration order. A callback is C and is documented as unable to
77 * croak, so there is no eval frame here and no per-request cost for one. */
78 419 static void pk_obs_fire_req(pTHX_ SV *c) {
79 int i;
80 838 ▶ for (i = 0; i < PK_OBS_REQ_N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PK_OBS_REQ_N; i++) 419 419 838
81 419 PK_OBS_REQ[i].cb(aTHX_ c, PK_OBS_REQ[i].ud);
82 419 }
83  
84 419 static void pk_obs_fire_res(pTHX_ SV *c, SV *response) {
85 int i;
86 838 ▶ for (i = 0; i < PK_OBS_RES_N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PK_OBS_RES_N; i++) 419 419 838
87 419 PK_OBS_RES[i].cb(aTHX_ c, response, PK_OBS_RES[i].ud);
88 419 }
89  
90 /* ---- v2: the shipped Punk::Model::DBI backend's statements -------------- */
91  
92 static struct { pk_abi_query_cb start; pk_abi_query_done_cb done; void *ud; }
93 PK_OBS_QRY[PK_ABI_MAX_OBSERVERS];
94 static int PK_OBS_QRY_N = 0;
95  
96 typedef struct { void *tok[PK_ABI_MAX_OBSERVERS]; int n; } pk_obs_qtokens;
97  
98 2 static int pk_obs_add_query(pTHX_ pk_abi_query_cb start,
99 pk_abi_query_done_cb done, void *ud) {
100 PERL_UNUSED_CONTEXT;
101 2 ▶ if (!start || PK_OBS_QRY_N >= PK_ABI_MAX_OBSERVERS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
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
102 2 PK_OBS_QRY[PK_OBS_QRY_N].start = start;
103 2 PK_OBS_QRY[PK_OBS_QRY_N].done = done;
104 2 PK_OBS_QRY[PK_OBS_QRY_N].ud = ud;
105 2 PK_OBS_QRY_N++;
106 2 return 1;
107 }
108  
109 /* Returns the tokens, or NULL when nobody is listening - the branch every
110 * uninstrumented statement takes, allocating nothing. */
111 17 static void *pk_obs_query_start(pTHX_ SV *sql, int nbind) {
112 pk_obs_qtokens *t;
113 17 STRLEN sl = 0;
114 const char *sp;
115 int i;
116 /* SvOK DOES NOT APPLY GET MAGIC. A statement text read straight out of a
117 * DBI handle - $sth->{Statement} - has no value in it until magic has
118 * run, so testing SvOK first takes the "" branch and hands every observer
119 * an empty statement, while a copy of the same value through a lexical
120 * works. Punk::DBI now forces magic before it gets here, so this is the
121 * belt and not the braces; it stays because the guard is wrong without
122 * it and the next caller will not know that. */
123 17 ▶ if (sql) SvGETMAGIC(sql);
Branch: 1 branch
BranchTrueFalseTotal
if (sql) SvGETMAGIC(sql); 17 0 17
124 34 ▶ sp = (sql && SvOK(sql)) ? SvPV_nomg_const(sql, sl) : "";
Branch: 2 branches
BranchTrueFalseTotal
sp = (sql && SvOK(sql)) ? SvPV_nomg_const(sql, sl) : ""; 17 0 17
sp = (sql && SvOK(sql)) ? SvPV_nomg_const(sql, sl) : ""; 17 0 17
125 17 ▶ if (!PK_OBS_QRY_N) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!PK_OBS_QRY_N) return NULL; 0 17 17
126 17 Newxz(t, 1, pk_obs_qtokens);
127 17 t->n = PK_OBS_QRY_N;
128 34 ▶ for (i = 0; i < PK_OBS_QRY_N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PK_OBS_QRY_N; i++) 17 17 34
129 17 t->tok[i] = PK_OBS_QRY[i].start(aTHX_ sp, sl, nbind,
130 PK_OBS_QRY[i].ud);
131 17 return t;
132 }
133  
134 /* `r` is what DBI's execute returned: a false or absent value is a failure. */
135 17 static void pk_obs_query_done(pTHX_ void *tv, SV *r) {
136 17 pk_obs_qtokens *t = (pk_obs_qtokens *)tv;
137 17 ▶ int ok = (r && SvOK(r) && SvTRUE(r)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
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
138 int i;
139 17 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 17 17
140 34 ▶ for (i = 0; i < t->n && i < PK_OBS_QRY_N; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < t->n && i < PK_OBS_QRY_N; i++) 17 17 34
for (i = 0; i < t->n && i < PK_OBS_QRY_N; i++) 17 0 17
141 17 ▶ if (PK_OBS_QRY[i].done)
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_QRY[i].done) 17 0 17
142 17 PK_OBS_QRY[i].done(aTHX_ t->tok[i], ok, PK_OBS_QRY[i].ud);
143 17 Safefree(t);
144 }
145  
146 /* ---- the log observer --------------------------------------------------- *
147 * A registration point for something that wants a COPY of every record: the
148 * logs signal of a telemetry layer, an audit sink, a test harness.
149 *
150 * A TAP, not a replacement. The line still goes wherever it was going -
151 * stderr, psgix.logger, a `to` coderef - and the observer gets a duplicate.
152 * The alternative, letting an observer take over the sink, means a telemetry
153 * layer silently redirects an operator's logs, and that when its collector is
154 * unreachable the logs simply vanish. Nobody should have to weigh "do I want
155 * my logs, or do I want them exported".
156 *
157 * The observer is C and must not croak: it runs inside the logger, which
158 * already promises that a failing sink never takes the request down. */
159 typedef void (*pl_obs_cb)(pTHX_ const char *level, STRLEN llen, SV *msg,
160 HV *fields, void *ud);
161  
162 /* The same tap, plus the CONTEXT the line was logged against - which is what
163 * an observer needs to correlate a record with the request that produced it.
164 * Without it the only way to find the active span is a process-global, and on
165 * an event loop with two requests in flight that attributes one request's
166 * lines to the other. `c` is NULL for a line logged against the application
167 * rather than a request. */
168 typedef void (*pl_obs_ctx_cb)(pTHX_ SV *c, const char *level, STRLEN llen,
169 SV *msg, HV *fields, void *ud);
170  
171 #define PL_OBS_MAX 4
172 static struct { pl_obs_cb cb; void *ud; } PL_OBS[PL_OBS_MAX];
173 static int PL_OBS_N = 0;
174 static struct { pl_obs_ctx_cb cb; void *ud; } PL_OBS_CTX[PL_OBS_MAX];
175 static int PL_OBS_CTX_N = 0;
176  
177 0 static int pl_observe(pTHX_ pl_obs_cb cb, void *ud) {
178 PERL_UNUSED_CONTEXT;
179 0 ▶ if (!cb || PL_OBS_N >= PL_OBS_MAX) return 0;
Branch: 2 branches
BranchTrueFalseTotal
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
180 0 PL_OBS[PL_OBS_N].cb = cb;
181 0 PL_OBS[PL_OBS_N].ud = ud;
182 0 PL_OBS_N++;
183 0 return 1;
184 }
185  
186 0 static int pl_observe_ctx(pTHX_ pl_obs_ctx_cb cb, void *ud) {
187 PERL_UNUSED_CONTEXT;
188 0 ▶ if (!cb || PL_OBS_CTX_N >= PL_OBS_MAX) return 0;
Branch: 2 branches
BranchTrueFalseTotal
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
189 0 PL_OBS_CTX[PL_OBS_CTX_N].cb = cb;
190 0 PL_OBS_CTX[PL_OBS_CTX_N].ud = ud;
191 0 PL_OBS_CTX_N++;
192 0 return 1;
193 }
194  
195 /* The guard the firing sites use, so "is anybody listening" is one load and
196 * one branch where a request would otherwise pay nothing at all. */
197 #define PK_OBS_WANT_RES (PK_OBS_RES_N > 0)
198 #define PK_OBS_WANT_REQ (PK_OBS_REQ_N > 0)
199 #define PK_OBS_WANT_QUERY (PK_OBS_QRY_N > 0)
200  
201 #endif /* PUNK_OBS_H */
« include/punk/punk_plural.h ↑ Summary include/punk/punk_auth.h »