include/punk/punk_metrics.h

filter: Statementstmt 99.1% 241 / 243 Branchbran 63.2% 172 / 272 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.1% 413 / 515 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/stencil.xs ↑ Summary xs/router.xs »
1 /* punk_metrics.h - a Prometheus /metrics endpoint.
2 *
3 * NOT A SUBSTITUTE FOR Punk::OpenTelemetry: the difference is push against
4 * pull. OpenTelemetry pushes OTLP to a collector somebody has to run;
5 * Prometheus scrapes an endpoint, which needs nothing deployed beside the
6 * application. They coexist - the same request feeds both, and an application
7 * that later adopts a collector keeps its dashboards.
8 *
9 * CARDINALITY. A counter labelled with the REQUEST path is the classic
10 * monitoring outage: /users/1, /users/2 and a million more each become their
11 * own time series, and the scrape target takes the monitoring system down
12 * with it. Punk cannot make that mistake, because the compiled route table IS
13 * the label set: every pattern is known at to_app, the set is bounded, and
14 * /users/:id is one series however many ids exist. Anything with no route to
15 * name - a 404, a mount - is labelled once as <other>.
16 *
17 * THE PREFORK TRAP. A scrape hits ONE worker, so a naive exporter reports one
18 * Nth of the traffic and a different Nth every scrape - every graph wrong in
19 * a way that looks like noise. Two ways out: aggregate through the shared
20 * arena, which is correct and costs an atomic per request on a shared
21 * cacheline against a request path of about 4.4us; or export per-worker
22 * series with a `worker` label and let Prometheus sum them, with no shared
23 * state and no contention.
24 *
25 * THIS DOES THE SECOND, and says so in the documentation rather than letting
26 * an operator discover it from a graph. `sum by (route) (...)` is the cost.
27 *
28 * Must be included after punk_context.h, pk_abi_impl.h (the accessors),
29 * punk_obs.h (the registry), punk_response.h and punk_static.h.
30 */
31  
32 #ifndef PUNK_METRICS_H
33 #define PUNK_METRICS_H
34  
35 /* The buckets, chosen ONCE and documented, because changing them later
36 * invalidates every histogram already recorded.
37 *
38 * Not Prometheus's defaults, which start at 5ms. Punk's own dispatch is about
39 * 4.4us, so a first bucket of 5ms would put essentially every request into
40 * it and the histogram would answer no question anybody asks. These start at
41 * half a millisecond and keep the familiar tail. */
42 static const double PM_BUCKETS[] = {
43 0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05,
44 0.1, 0.25, 0.5, 1, 2.5, 5, 10
45 };
46 #define PM_NBUCKETS ((int)(sizeof(PM_BUCKETS) / sizeof(PM_BUCKETS[0])))
47  
48 /* Per PROCESS, not per application: a worker exports its own counters and
49 * Prometheus sums them. Two applications in one process share these, which is
50 * what a process-wide exporter means. */
51 static HV *PM_TOTAL = NULL; /* "METHOD\x1Froute\x1Fstatus" => count */
52 static HV *PM_HIST = NULL; /* "METHOD\x1Froute" => [n, sum, b0..bN-1] */
53 static IV PM_INFLIGHT = 0;
54 static int PM_OBSERVING = 0; /* the observers are registered once */
55 /* EVERY metrics path registered in this process, not the latest one.
56 *
57 * It was a single SV, and that was a bug with a very quiet symptom: a second
58 * application registering a different path REPLACED it, after which the first
59 * application's scrape endpoint silently began counting its own scrapes -
60 * for ever, and looking exactly like real traffic. A set, because the answer
61 * to "is this a scrape" is about the process, not about the last caller. */
62 static HV *PM_SKIP = NULL; /* path => 1, never counted */
63  
64 #define PM_T0_KEY "punk.metrics.t0"
65 #define PM_SKIP_KEY "punk.metrics.skip"
66  
67 349 static void pm_init(pTHX) {
68 349 ▶ if (!PM_TOTAL) PM_TOTAL = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PM_TOTAL) PM_TOTAL = newHV(); 1 348 349
69 349 ▶ if (!PM_HIST) PM_HIST = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PM_HIST) PM_HIST = newHV(); 1 348 349
70 349 ▶ if (!PM_SKIP) PM_SKIP = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PM_SKIP) PM_SKIP = newHV(); 1 348 349
71 349 }
72  
73 /* Is this one of the scrape endpoints? */
74 397 static int pm_is_scrape(pTHX_ SV *path) {
75 397 ▶ if (!PM_SKIP || !path || !SvOK(path)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!PM_SKIP || !path || !SvOK(path)) return 0; 397 0 397
if (!PM_SKIP || !path || !SvOK(path)) return 0; 397 0 397
if (!PM_SKIP || !path || !SvOK(path)) return 0; 0 397 397
76 397 return hv_exists_ent(PM_SKIP, path, 0) ? 1 : 0;
77 }
78  
79 /* A label value, escaped. Prometheus needs backslash, quote and newline out
80 * of the way; a route pattern will not contain them, but a `collect` name or
81 * an operation id is not this plugin's to vouch for. */
82 227 static void pm_label(pTHX_ SV *out, const char *s, STRLEN len) {
83 STRLEN i;
84 1160 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 933 227 1160
85 933 char ch = s[i];
86 933 ▶ if (ch == '\\') sv_catpvs(out, "\\\\");
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\\') sv_catpvs(out, "\\\\"); 0 933 933
87 933 ▶ else if (ch == '"') sv_catpvs(out, "\\\"");
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '"') sv_catpvs(out, "\\\""); 0 933 933
88 933 ▶ else if (ch == '\n') sv_catpvs(out, "\\n");
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '\n') sv_catpvs(out, "\\n"); 0 933 933
89 933 else sv_catpvn(out, &ch, 1);
90 }
91 227 }
92  
93 /* A metric name Prometheus will accept: [a-zA-Z_:][a-zA-Z0-9_:]*
94 *
95 * Checked rather than trusted, because ONE bad name does not break one
96 * series - it makes the whole scrape unparseable, and every metric the
97 * application has disappears at once. */
98 17 static int pm_name_ok(const char *s, STRLEN len) {
99 STRLEN i;
100 17 ▶ if (!s || !len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !len) return 0; 17 0 17
if (!s || !len) return 0; 0 17 17
101 17 ▶ if (!(isALPHA(s[0]) || s[0] == '_' || s[0] == ':')) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(isALPHA(s[0]) || s[0] == '_' || s[0] == ':')) return 0; 1 16 17
if (!(isALPHA(s[0]) || s[0] == '_' || s[0] == ':')) return 0; 1 0 1
if (!(isALPHA(s[0]) || s[0] == '_' || s[0] == ':')) return 0; 1 0 1
102 139 ▶ for (i = 1; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) 124 15 139
103 124 ▶ if (!(isALNUM(s[i]) || s[i] == '_' || s[i] == ':')) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(isALNUM(s[i]) || s[i] == '_' || s[i] == ':')) return 0; 1 123 124
if (!(isALNUM(s[i]) || s[i] == '_' || s[i] == ':')) return 0; 1 0 1
if (!(isALNUM(s[i]) || s[i] == '_' || s[i] == ':')) return 0; 1 0 1
104 15 return 1;
105 }
106  
107 /* The route to label this request with.
108 *
109 * The declared pattern when there is one; the OpenAPI operationId for an api
110 * mount, which is bounded the same way; and <other> for everything else. A
111 * 404 has no route by definition, and giving it the request path is precisely
112 * how the bounded dimension becomes unbounded. */
113 302 static SV *pm_route_of(pTHX_ SV *c) {
114 302 SV *r = pk_abi_route_pattern_of(aTHX_ c);
115 302 ▶ if (r && SvOK(r) && SvCUR(r)) return r;
Branch: 3 branches
BranchTrueFalseTotal
if (r && SvOK(r) && SvCUR(r)) return r; 70 232 302
if (r && SvOK(r) && SvCUR(r)) return r; 70 0 70
if (r && SvOK(r) && SvCUR(r)) return r; 70 0 70
116 232 r = pk_abi_operation_of(aTHX_ c);
117 232 ▶ if (r && SvOK(r) && SvCUR(r)) return r;
Branch: 3 branches
BranchTrueFalseTotal
if (r && SvOK(r) && SvCUR(r)) return r; 0 232 232
if (r && SvOK(r) && SvCUR(r)) return r; 0 0 0
if (r && SvOK(r) && SvCUR(r)) return r; 0 0 0
118 232 return NULL;
119 }
120  
121 /* REQUEST_METHOD as a label value, or "-".
122 *
123 * A known method, or `<other>`. The method is whatever the client put on the
124 * request line, any bytes at all, and an unknown one on any path is still a
125 * counted 404: left as it came, every spelling was a new series in every
126 * worker and in whatever scrapes them, without end. The route label is
127 * bounded the same way and for the same reason. */
128 302 static const char *pm_method_label(const char *m, STRLEN *len) {
129 static const char *const known[] = { "GET", "HEAD", "POST", "PUT",
130 "DELETE", "PATCH", "OPTIONS", "CONNECT", "TRACE", NULL };
131 int i;
132 2169 ▶ for (i = 0; known[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; known[i]; i++) 1966 203 2169
133 1966 ▶ if (strlen(known[i]) == *len && memEQ(m, known[i], *len)) return m;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(known[i]) == *len && memEQ(m, known[i], *len)) return m; 489 1477 1966
if (strlen(known[i]) == *len && memEQ(m, known[i], *len)) return m; 99 390 489
134 203 *len = 7;
135 203 return "<other>";
136 }
137  
138 302 static const char *pm_method_of(pTHX_ SV *c, STRLEN *len) {
139 302 SV *env = pk_abi_env_of(aTHX_ c);
140 SV **m;
141 302 ▶ if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV
Branch: 3 branches
BranchTrueFalseTotal
if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV 302 0 302
if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV 302 0 302
if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV 302 0 302
142 302 ▶ && (m = hv_fetchs((HV *)SvRV(env), "REQUEST_METHOD", 0))
Branch: 1 branch
BranchTrueFalseTotal
&& (m = hv_fetchs((HV *)SvRV(env), "REQUEST_METHOD", 0)) 302 0 302
143 302 ▶ && m && *m && SvOK(*m) && SvCUR(*m)) {
Branch: 4 branches
BranchTrueFalseTotal
&& m && *m && SvOK(*m) && SvCUR(*m)) { 302 0 302
&& m && *m && SvOK(*m) && SvCUR(*m)) { 302 0 302
&& m && *m && SvOK(*m) && SvCUR(*m)) { 302 0 302
&& m && *m && SvOK(*m) && SvCUR(*m)) { 302 0 302
144 302 const char *p = SvPV_const(*m, *len);
145 302 return pm_method_label(p, len);
146 }
147 0 *len = 1;
148 0 return "-";
149 }
150  
151 302 static void pm_bump_total(pTHX_ const char *k, STRLEN kl) {
152 302 SV **e = hv_fetch(PM_TOTAL, k, (I32)kl, 1);
153 302 ▶ if (e && *e) sv_setuv(*e, (SvOK(*e) ? SvUV(*e) : 0) + 1);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e) sv_setuv(*e, (SvOK(*e) ? SvUV(*e) : 0) + 1); 302 0 302
if (e && *e) sv_setuv(*e, (SvOK(*e) ? SvUV(*e) : 0) + 1); 302 0 302
if (e && *e) sv_setuv(*e, (SvOK(*e) ? SvUV(*e) : 0) + 1); 271 31 302
154 302 }
155  
156 302 static void pm_bump_hist(pTHX_ const char *k, STRLEN kl, double secs) {
157 302 SV **e = hv_fetch(PM_HIST, k, (I32)kl, 1);
158 AV *h;
159 int i;
160 302 ▶ if (!(e && *e)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) return; 302 0 302
if (!(e && *e)) return; 0 302 302
161 302 ▶ if (!(SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) { 271 31 302
if (!(SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) { 0 271 271
162 31 AV *fresh = newAV();
163 31 av_extend(fresh, 2 + PM_NBUCKETS);
164 31 av_store(fresh, 0, newSVuv(0));
165 31 av_store(fresh, 1, newSVnv(0.0));
166 465 ▶ for (i = 0; i < PM_NBUCKETS; i++) av_store(fresh, 2 + i, newSVuv(0));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PM_NBUCKETS; i++) av_store(fresh, 2 + i, newSVuv(0)); 434 31 465
167 31 sv_setsv(*e, sv_2mortal(newRV_noinc((SV *)fresh)));
168 }
169 302 h = (AV *)SvRV(*e);
170 {
171 302 SV **n = av_fetch(h, 0, 0);
172 302 SV **s = av_fetch(h, 1, 0);
173 302 ▶ if (n && *n) sv_setuv(*n, SvUV(*n) + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (n && *n) sv_setuv(*n, SvUV(*n) + 1); 302 0 302
if (n && *n) sv_setuv(*n, SvUV(*n) + 1); 302 0 302
174 302 ▶ if (s && *s) sv_setnv(*s, SvNV(*s) + secs);
Branch: 2 branches
BranchTrueFalseTotal
if (s && *s) sv_setnv(*s, SvNV(*s) + secs); 302 0 302
if (s && *s) sv_setnv(*s, SvNV(*s) + secs); 302 0 302
175 }
176 /* Cumulative, which is what a Prometheus histogram means: a request lands
177 * in its own bucket AND every wider one. */
178 4530 ▶ for (i = 0; i < PM_NBUCKETS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PM_NBUCKETS; i++) { 4228 302 4530
179 4228 ▶ if (secs <= PM_BUCKETS[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (secs <= PM_BUCKETS[i]) { 4225 3 4228
180 4225 SV **b = av_fetch(h, 2 + i, 0);
181 4225 ▶ if (b && *b) sv_setuv(*b, SvUV(*b) + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (b && *b) sv_setuv(*b, SvUV(*b) + 1); 4225 0 4225
if (b && *b) sv_setuv(*b, SvUV(*b) + 1); 4225 0 4225
182 }
183 }
184 }
185  
186 /* ---- the observers ---------------------------------------------------------
187 *
188 * MUST NOT croak: they run inside somebody else's request, and an exporter
189 * that can take the application down with it is worse than no exporter. */
190 /* The context's own slot for the start time: one av_store, where the stash
191 * hash cost a hash to create and two stores per request. */
192 654 static AV *pm_ctx_av(pTHX_ SV *c) {
193 654 ▶ return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVAV) ? (AV *)SvRV(c) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVAV) ? (AV *)SvRV(c) : NULL; 654 0 654
return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVAV) ? (AV *)SvRV(c) : NULL; 654 0 654
return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVAV) ? (AV *)SvRV(c) : NULL; 654 0 654
194 }
195  
196 327 static void pm_on_request(pTHX_ SV *c, void *ud) {
197 327 AV *cav = pm_ctx_av(aTHX_ c);
198 PERL_UNUSED_ARG(ud);
199  
200 /* Is this the scrape itself? Decided HERE, at the one moment it can be
201 * decided symmetrically, and recorded for the response side.
202 *
203 * Routing has not happened yet, so there is no pattern to compare - only
204 * the path. That is the whole reason for the stash flag: if the two sides
205 * decided independently they could disagree, and the in-flight gauge
206 * would then drift by one every time they did.
207 *
208 * It matters because a scrape is in flight while it renders. Counting it
209 * gives an idle server a permanent floor of one, on every dashboard, for
210 * ever. */
211 {
212 327 SV *env = pk_abi_env_of(aTHX_ c);
213 327 ▶ SV **p = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
SV **p = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) 327 0 327
SV **p = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) 327 0 327
214 654 ▶ ? hv_fetchs((HV *)SvRV(env), "PATH_INFO", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(env), "PATH_INFO", 0) : NULL; 327 0 327
215 327 ▶ if (p && *p && pm_is_scrape(aTHX_ *p)) {
Branch: 3 branches
BranchTrueFalseTotal
if (p && *p && pm_is_scrape(aTHX_ *p)) { 327 0 327
if (p && *p && pm_is_scrape(aTHX_ *p)) { 327 0 327
if (p && *p && pm_is_scrape(aTHX_ *p)) { 25 302 327
216 25 ▶ if (cav) (void)av_store(cav, PCX_T0, newSVnv(-1.0));
Branch: 1 branch
BranchTrueFalseTotal
if (cav) (void)av_store(cav, PCX_T0, newSVnv(-1.0)); 25 0 25
217 25 return;
218 }
219 }
220  
221 302 PM_INFLIGHT++;
222 302 ▶ if (cav) (void)av_store(cav, PCX_T0, newSVnv(pc_now(aTHX)));
Branch: 1 branch
BranchTrueFalseTotal
if (cav) (void)av_store(cav, PCX_T0, newSVnv(pc_now(aTHX))); 302 0 302
223 }
224  
225 327 static void pm_on_response(pTHX_ SV *c, SV *response, void *ud) {
226 SV *route;
227 327 AV *cav = pm_ctx_av(aTHX_ c);
228 const char *mp; STRLEN ml;
229 IV status;
230 327 double t0 = 0.0, secs;
231 SV *key;
232 PERL_UNUSED_ARG(ud);
233  
234 327 ▶ if (cav) {
Branch: 1 branch
BranchTrueFalseTotal
if (cav) { 327 0 327
235 327 SV **t = av_fetch(cav, PCX_T0, 0);
236 327 ▶ if (t && *t && SvOK(*t)) t0 = SvNV(*t);
Branch: 3 branches
BranchTrueFalseTotal
if (t && *t && SvOK(*t)) t0 = SvNV(*t); 327 0 327
if (t && *t && SvOK(*t)) t0 = SvNV(*t); 327 0 327
if (t && *t && SvOK(*t)) t0 = SvNV(*t); 327 0 327
237 }
238 /* The request side already decided, so the two cannot disagree: it never
239 * incremented the gauge for this one, and this never decrements it. */
240 327 ▶ if (t0 < 0.0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (t0 < 0.0) return; 25 302 327
241  
242 302 ▶ if (PM_INFLIGHT > 0) PM_INFLIGHT--;
Branch: 1 branch
BranchTrueFalseTotal
if (PM_INFLIGHT > 0) PM_INFLIGHT--; 302 0 302
243 302 pm_init(aTHX);
244  
245 302 route = pm_route_of(aTHX_ c);
246  
247 /* And the same exclusion by route, for a scrape reached through a mount
248 * where the path the request carried was not the path this was told. */
249 302 ▶ if (route && pm_is_scrape(aTHX_ route)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (route && pm_is_scrape(aTHX_ route)) return; 70 232 302
if (route && pm_is_scrape(aTHX_ route)) return; 0 70 70
250  
251 302 mp = pm_method_of(aTHX_ c, &ml);
252 302 status = pk_abi_status_of(aTHX_ response);
253  
254 302 ▶ secs = t0 > 0.0 ? pc_now(aTHX) - t0 : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
secs = t0 > 0.0 ? pc_now(aTHX) - t0 : 0.0; 302 0 302
255 302 ▶ if (secs < 0.0) secs = 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if (secs < 0.0) secs = 0.0; 0 302 302
256  
257 302 key = sv_2mortal(newSVpvn(mp, ml));
258 302 sv_catpvs(key, "\x1f");
259 302 ▶ if (route) sv_catsv(key, route);
Branch: 1 branch
BranchTrueFalseTotal
if (route) sv_catsv(key, route); 70 232 302
260 232 else sv_catpvs(key, "<other>");
261  
262 302 pm_bump_hist(aTHX_ SvPVX(key), SvCUR(key), secs);
263  
264 /* A streaming or detached response has no status to read; -1 rather than
265 * a guess, so nobody graphs a 200 that never happened. */
266 302 sv_catpvs(key, "\x1f");
267 302 ▶ if (status >= 0) sv_catpvf(key, "%" IVdf, status);
Branch: 1 branch
BranchTrueFalseTotal
if (status >= 0) sv_catpvf(key, "%" IVdf, status); 301 1 302
268 1 else sv_catpvs(key, "-");
269 302 pm_bump_total(aTHX_ SvPVX(key), SvCUR(key));
270 }
271  
272 /* ---- rendering ------------------------------------------------------------ */
273  
274 /* Split a "\x1f"-joined key. */
275 215 static const char *pm_field(const char *k, STRLEN kl, int n, STRLEN *len) {
276 215 const char *p = k, *end = k + kl;
277 215 int i = 0;
278 1070 ▶ while (i < n && p < end) { if (*p++ == '\x1f') i++; }
Branch: 3 branches
BranchTrueFalseTotal
while (i < n && p < end) { if (*p++ == '\x1f') i++; } 172 683 855
while (i < n && p < end) { if (*p++ == '\x1f') i++; } 855 215 1070
while (i < n && p < end) { if (*p++ == '\x1f') i++; } 855 0 855
279 215 { const char *q = p;
280 1064 ▶ while (q < end && *q != '\x1f') q++;
Branch: 2 branches
BranchTrueFalseTotal
while (q < end && *q != '\x1f') q++; 978 86 1064
while (q < end && *q != '\x1f') q++; 849 129 978
281 215 *len = (STRLEN)(q - p);
282 215 return p; }
283 }
284  
285 98 static void pm_worker(pTHX_ SV *out) {
286 98 sv_catpvf(out, ",worker=\"%" IVdf "\"", (IV)PerlProc_getpid());
287 98 }
288  
289 /* The sorted keys of an HV, so a scrape is byte-stable between calls and a
290 * diff of two shows what changed rather than what moved. */
291 76 static AV *pm_sorted_keys(pTHX_ HV *h) {
292 76 AV *keys = (AV *)sv_2mortal((SV *)newAV());
293 HE *ent;
294 76 hv_iterinit(h);
295 168 ▶ while ((ent = hv_iternext(h))) av_push(keys, newSVsv(hv_iterkeysv(ent)));
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext(h))) av_push(keys, newSVsv(hv_iterkeysv(ent))); 92 76 168
296 76 sortsv(AvARRAY(keys), (SSize_t)(av_len(keys) + 1), Perl_sv_cmp);
297 76 return keys;
298 }
299  
300 /* Anything an application wants to add, as gauges. This is where the queue's
301 * depth and a server's own counters go: reaching into another distribution
302 * from here would couple this plugin to versions of things it cannot test
303 * against, and an application already has both in scope. */
304 26 static void pm_collect(pTHX_ SV *app, SV *out) {
305 26 HV *h = app_hv(aTHX_ app);
306 26 ▶ SV *cb = h ? app_get(aTHX_ h, "metrics_collect") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *cb = h ? app_get(aTHX_ h, "metrics_collect") : NULL; 26 0 26
307 26 SV *got = NULL;
308 HV *gh;
309 AV *names;
310 SSize_t i, n;
311  
312 26 ▶ if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return; 24 2 26
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return; 24 0 24
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return; 0 24 24
313  
314 {
315 24 dSP; int count;
316 24 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 24 24
317 24 count = call_sv(cb, G_SCALAR | G_EVAL);
318 24 SPAGAIN;
319 24 ▶ if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs);
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs); 24 0 24
if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs); 23 1 24
if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs); 23 0 23
320 1 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 1 0 1
321 24 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 24 0 24
322 }
323 24 ▶ if (!got) return; /* it died: the scrape goes on */
Branch: 1 branch
BranchTrueFalseTotal
if (!got) return; /* it died: the scrape goes on */ 1 23 24
324 23 sv_2mortal(got);
325 23 ▶ if (!(SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV)) return; 23 0 23
if (!(SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV)) return; 0 23 23
326 23 gh = (HV *)SvRV(got);
327  
328 23 names = pm_sorted_keys(aTHX_ gh);
329 23 n = av_len(names) + 1;
330 28 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 23 28
331 5 SV *nm = *av_fetch(names, i, 0);
332 5 HE *he = hv_fetch_ent(gh, nm, 0, 0);
333 5 ▶ SV *v = he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = he ? HeVAL(he) : NULL; 5 0 5
334 5 STRLEN nl; const char *np = SvPV_const(nm, nl);
335 5 ▶ if (!pm_name_ok(np, nl)) continue; /* see pm_name_ok */
Branch: 1 branch
BranchTrueFalseTotal
if (!pm_name_ok(np, nl)) continue; /* see pm_name_ok */ 2 3 5
336 3 ▶ if (!(v && SvOK(v))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(v && SvOK(v))) continue; 3 0 3
if (!(v && SvOK(v))) continue; 0 3 3
337 3 sv_catpvf(out, "# TYPE %s gauge\n%s{", np, np);
338 /* the leading comma of pm_worker would be wrong as the first label */
339 6 sv_catpvf(out, "worker=\"%" IVdf "\"} %s\n",
340 (IV)PerlProc_getpid(), SvPV_nolen(sv_2mortal(newSVnv(SvNV(v)))));
341 }
342 }
343  
344 /* Whatever each cache is willing to say about itself, as gauges. In-dist, so
345 * it can be kept in step.
346 *
347 * `app->{cache}` is a HASH of name => cache object, not a cache - so every
348 * configured cache is reported, labelled with its name. The label set is
349 * bounded by the config, which is the same rule the route label follows. */
350 26 static void pm_cache(pTHX_ SV *app, SV *out) {
351 26 HV *h = app_hv(aTHX_ app);
352 26 ▶ SV *caches = h ? app_get(aTHX_ h, "cache") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *caches = h ? app_get(aTHX_ h, "cache") : NULL; 26 0 26
353 HV *ch;
354 AV *names;
355 SSize_t i, n;
356  
357 26 ▶ if (!(caches && SvROK(caches) && SvTYPE(SvRV(caches)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(caches && SvROK(caches) && SvTYPE(SvRV(caches)) == SVt_PVHV)) return; 1 25 26
if (!(caches && SvROK(caches) && SvTYPE(SvRV(caches)) == SVt_PVHV)) return; 1 0 1
if (!(caches && SvROK(caches) && SvTYPE(SvRV(caches)) == SVt_PVHV)) return; 0 1 1
358 1 ch = (HV *)SvRV(caches);
359  
360 1 names = pm_sorted_keys(aTHX_ ch);
361 1 n = av_len(names) + 1;
362 2 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 1 2
363 1 SV *nm = *av_fetch(names, i, 0);
364 1 HE *he = hv_fetch_ent(ch, nm, 0, 0);
365 1 ▶ SV *cache = he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *cache = he ? HeVAL(he) : NULL; 1 0 1
366 1 STRLEN nml; const char *nmp = SvPV_const(nm, nml);
367 SSize_t j, got;
368  
369 1 ▶ if (!(cache && SvROK(cache) && SvOBJECT(SvRV(cache)))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cache && SvROK(cache) && SvOBJECT(SvRV(cache)))) continue; 1 0 1
if (!(cache && SvROK(cache) && SvOBJECT(SvRV(cache)))) continue; 1 0 1
if (!(cache && SvROK(cache) && SvOBJECT(SvRV(cache)))) continue; 0 1 1
370  
371 /* stats() returns a flat LIST, so it has to be called in list
372 * context - pcx_call_meth only offers scalar or void. */
373 {
374 1 dSP; int count, k;
375 1 AV *flat = (AV *)sv_2mortal((SV *)newAV());
376 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
377 1 ▶ EXTEND(SP, 1); PUSHs(cache); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(cache); PUTBACK; 0 1 1
378 1 count = call_method("stats", G_ARRAY | G_EVAL);
379 1 SPAGAIN;
380 1 ▶ if (SvTRUE(ERRSV)) count = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) count = 0; 1 0 1
if (SvTRUE(ERRSV)) count = 0; 0 1 1
381 25 ▶ for (k = 0; k < count; k++) av_unshift(flat, 1), av_store(flat, 0, newSVsv(POPs));
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < count; k++) av_unshift(flat, 1), av_store(flat, 0, newSVsv(POPs)); 24 1 25
382 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
383  
384 1 got = av_len(flat) + 1;
385 13 ▶ for (j = 0; j + 1 < got; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < got; j += 2) { 12 1 13
386 12 SV **k2 = av_fetch(flat, j, 0);
387 12 SV **v = av_fetch(flat, j + 1, 0);
388 STRLEN kl; const char *kp;
389 12 ▶ if (!(k2 && *k2 && v && *v && SvOK(*v))) continue;
Branch: 5 branches
BranchTrueFalseTotal
if (!(k2 && *k2 && v && *v && SvOK(*v))) continue; 12 0 12
if (!(k2 && *k2 && v && *v && SvOK(*v))) continue; 12 0 12
if (!(k2 && *k2 && v && *v && SvOK(*v))) continue; 12 0 12
if (!(k2 && *k2 && v && *v && SvOK(*v))) continue; 12 0 12
if (!(k2 && *k2 && v && *v && SvOK(*v))) continue; 0 12 12
390 12 kp = SvPV_const(*k2, kl);
391 12 ▶ if (!pm_name_ok(kp, kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pm_name_ok(kp, kl)) continue; 0 12 12
392 12 sv_catpvf(out, "punk_cache_%.*s{cache=\"", (int)kl, kp);
393 12 pm_label(aTHX_ out, nmp, nml);
394 12 sv_catpvs(out, "\"");
395 12 pm_worker(aTHX_ out);
396 /* NVgf, not a bare "g", and (NV) to match it.
397 *
398 * sv_catpvf is not C printf. Its float conversions take
399 * whatever width the format's length modifier names, and a
400 * bare %g names `double` - so on a long-double or quadmath
401 * perl an NV argument is read as the wrong type off the
402 * varargs and every cache counter here came out as
403 * 8.094771541e-320. NVgf expands to the modifier that perl's
404 * own NV needs, on each of them. */
405 12 sv_catpvf(out, "} %.10" NVgf "\n", (NV)SvNV(*v));
406 }
407 }
408 }
409 }
410  
411 /* The whole document. */
412 26 static SV *pm_render(pTHX_ SV *app) {
413 26 SV *out = sv_2mortal(newSVpvs(""));
414 AV *keys;
415 SSize_t i, n;
416  
417 26 pm_init(aTHX);
418  
419 26 sv_catpvs(out,
420 "# HELP http_requests_total Requests, by method, declared route and "
421 "status.\n"
422 "# TYPE http_requests_total counter\n");
423 26 keys = pm_sorted_keys(aTHX_ PM_TOTAL);
424 26 n = av_len(keys) + 1;
425 69 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 43 26 69
426 43 SV *k = *av_fetch(keys, i, 0);
427 43 STRLEN kl; const char *kp = SvPV_const(k, kl);
428 STRLEN ml, rl, sl;
429 43 const char *mp = pm_field(kp, kl, 0, &ml);
430 43 const char *rp = pm_field(kp, kl, 1, &rl);
431 43 const char *sp = pm_field(kp, kl, 2, &sl);
432 43 HE *he = hv_fetch_ent(PM_TOTAL, k, 0, 0);
433 43 sv_catpvs(out, "http_requests_total{method=\"");
434 43 pm_label(aTHX_ out, mp, ml);
435 43 sv_catpvs(out, "\",route=\"");
436 43 pm_label(aTHX_ out, rp, rl);
437 43 sv_catpvs(out, "\",status=\"");
438 43 pm_label(aTHX_ out, sp, sl);
439 43 sv_catpvs(out, "\"");
440 43 pm_worker(aTHX_ out);
441 43 ▶ sv_catpvf(out, "} %" UVuf "\n", he ? SvUV(HeVAL(he)) : (UV)0);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(out, "} %" UVuf "\n", he ? SvUV(HeVAL(he)) : (UV)0); 43 0 43
442 }
443  
444 26 sv_catpvs(out,
445 "# HELP http_request_duration_seconds Request duration.\n"
446 "# TYPE http_request_duration_seconds histogram\n");
447 26 keys = pm_sorted_keys(aTHX_ PM_HIST);
448 26 n = av_len(keys) + 1;
449 69 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 43 26 69
450 43 SV *k = *av_fetch(keys, i, 0);
451 43 STRLEN kl; const char *kp = SvPV_const(k, kl);
452 STRLEN ml, rl;
453 43 const char *mp = pm_field(kp, kl, 0, &ml);
454 43 const char *rp = pm_field(kp, kl, 1, &rl);
455 43 HE *he = hv_fetch_ent(PM_HIST, k, 0, 0);
456 43 ▶ AV *hst = (he && SvROK(HeVAL(he))
Branch: 1 branch
BranchTrueFalseTotal
AV *hst = (he && SvROK(HeVAL(he)) 43 0 43
457 43 ▶ && SvTYPE(SvRV(HeVAL(he))) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(HeVAL(he))) == SVt_PVAV) 43 0 43
458 86 ▶ ? (AV *)SvRV(HeVAL(he)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(HeVAL(he)) : NULL; 43 0 43
459 43 SV *labels = sv_2mortal(newSVpvs("method=\""));
460 int b;
461 43 ▶ if (!hst) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hst) continue; 0 43 43
462 43 pm_label(aTHX_ labels, mp, ml);
463 43 sv_catpvs(labels, "\",route=\"");
464 43 pm_label(aTHX_ labels, rp, rl);
465 43 sv_catpvs(labels, "\"");
466 43 pm_worker(aTHX_ labels);
467  
468 645 ▶ for (b = 0; b < PM_NBUCKETS; b++) {
Branch: 1 branch
BranchTrueFalseTotal
for (b = 0; b < PM_NBUCKETS; b++) { 602 43 645
469 602 SV **cnt = av_fetch(hst, 2 + b, 0);
470 1204 ▶ sv_catpvf(out, "http_request_duration_seconds_bucket{%s,le=\"%"
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(out, "http_request_duration_seconds_bucket{%s,le=\"%" 602 0 602
sv_catpvf(out, "http_request_duration_seconds_bucket{%s,le=\"%" 602 0 602
471 NVgf "\"} %" UVuf "\n",
472 SvPV_nolen(labels), (NV)PM_BUCKETS[b],
473 cnt && *cnt ? SvUV(*cnt) : (UV)0);
474 }
475 {
476 43 SV **num = av_fetch(hst, 0, 0);
477 43 SV **sum = av_fetch(hst, 1, 0);
478 43 ▶ UV nn = num && *num ? SvUV(*num) : (UV)0;
Branch: 2 branches
BranchTrueFalseTotal
UV nn = num && *num ? SvUV(*num) : (UV)0; 43 0 43
UV nn = num && *num ? SvUV(*num) : (UV)0; 43 0 43
479 43 sv_catpvf(out, "http_request_duration_seconds_bucket{%s,le=\"+Inf\"} "
480 "%" UVuf "\n", SvPV_nolen(labels), nn);
481 86 ▶ sv_catpvf(out, "http_request_duration_seconds_sum{%s} %.6" NVff
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(out, "http_request_duration_seconds_sum{%s} %.6" NVff 43 0 43
sv_catpvf(out, "http_request_duration_seconds_sum{%s} %.6" NVff 43 0 43
482 "\n",
483 SvPV_nolen(labels), sum && *sum ? (NV)SvNV(*sum) : (NV)0);
484 43 sv_catpvf(out, "http_request_duration_seconds_count{%s} %" UVuf "\n",
485 SvPV_nolen(labels), nn);
486 }
487 }
488  
489 26 sv_catpvs(out,
490 "# HELP http_requests_in_flight Requests being served right now.\n"
491 "# TYPE http_requests_in_flight gauge\n");
492 26 sv_catpvf(out, "http_requests_in_flight{worker=\"%" IVdf "\"} %" IVdf "\n",
493 (IV)PerlProc_getpid(), PM_INFLIGHT);
494  
495 26 pm_cache(aTHX_ app, out);
496 26 pm_collect(aTHX_ app, out);
497  
498 26 return newSVsv(out);
499 }
500  
501 XS_INTERNAL(pm_serve_cb);
502 25 XS_INTERNAL(pm_serve_cb) {
503 25 dXSARGS;
504 25 AV *cap = punk_clos_cap(aTHX_ cv);
505 25 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; 25 0 25
506 25 AV *extra = (AV *)sv_2mortal((SV *)newAV());
507 PERL_UNUSED_VAR(items);
508 25 av_push(extra, newSVpvs("Cache-Control"));
509 25 av_push(extra, newSVpvs("no-store"));
510 25 ST(0) = sv_2mortal(punk_triplet(aTHX_ 200,
511 sv_2mortal(newSVpvs("text/plain; version=0.0.4; charset=utf-8")),
512 pm_render(aTHX_ app), extra));
513 25 XSRETURN(1);
514 }
515  
516 #endif /* PUNK_METRICS_H */
« xs/stencil.xs ↑ Summary xs/router.xs »