| 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 |
350 |
| static void pm_init(pTHX) { |
| 68 |
350 |
▶ | if (!PM_TOTAL) PM_TOTAL = newHV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PM_TOTAL) PM_TOTAL = newHV(); |
2 |
348 |
350 |
|
| 69 |
350 |
▶ | if (!PM_HIST) PM_HIST = newHV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PM_HIST) PM_HIST = newHV(); |
2 |
348 |
350 |
|
| 70 |
350 |
▶ | if (!PM_SKIP) PM_SKIP = newHV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PM_SKIP) PM_SKIP = newHV(); |
2 |
348 |
350 |
|
| 71 |
350 |
| } |
| 72 |
|
| |
| 73 |
|
| /* Is this one of the scrape endpoints? */ |
| 74 |
397 |
| static int pm_is_scrape(pTHX_ SV *path) { |
| 75 |
|
| /* |
| 76 |
|
| # uncoverable branch true |
| 77 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 78 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 79 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 80 |
397 |
▶ | if (!PM_SKIP || !path || !SvOK(path)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 81 |
397 |
| return hv_exists_ent(PM_SKIP, path, 0) ? 1 : 0; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| /* A label value, escaped. Prometheus needs backslash, quote and newline out |
| 85 |
|
| * of the way; a route pattern will not contain them, but a `collect` name or |
| 86 |
|
| * an operation id is not this plugin's to vouch for. */ |
| 87 |
227 |
| static void pm_label(pTHX_ SV *out, const char *s, STRLEN len) { |
| 88 |
|
| STRLEN i; |
| 89 |
1160 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
933 |
227 |
1160 |
|
| 90 |
933 |
| char ch = s[i]; |
| 91 |
|
| /* |
| 92 |
|
| # uncoverable branch true |
| 93 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 94 |
933 |
▶ | if (ch == '\\') sv_catpvs(out, "\\\\"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '\\') sv_catpvs(out, "\\\\"); |
0 |
933 |
933 |
|
| 95 |
|
| /* |
| 96 |
|
| # uncoverable branch true |
| 97 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 98 |
933 |
▶ | else if (ch == '"') sv_catpvs(out, "\\\""); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ch == '"') sv_catpvs(out, "\\\""); |
0 |
933 |
933 |
|
| 99 |
|
| /* |
| 100 |
|
| # uncoverable branch true |
| 101 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 102 |
933 |
▶ | else if (ch == '\n') sv_catpvs(out, "\\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ch == '\n') sv_catpvs(out, "\\n"); |
0 |
933 |
933 |
|
| 103 |
933 |
| else sv_catpvn(out, &ch, 1); |
| 104 |
|
| } |
| 105 |
227 |
| } |
| 106 |
|
| |
| 107 |
|
| /* A metric name Prometheus will accept: [a-zA-Z_:][a-zA-Z0-9_:]* |
| 108 |
|
| * |
| 109 |
|
| * Checked rather than trusted, because ONE bad name does not break one |
| 110 |
|
| * series - it makes the whole scrape unparseable, and every metric the |
| 111 |
|
| * application has disappears at once. */ |
| 112 |
17 |
| static int pm_name_ok(const char *s, STRLEN len) { |
| 113 |
|
| STRLEN i; |
| 114 |
|
| /* |
| 115 |
|
| # uncoverable branch true |
| 116 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 117 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 118 |
17 |
▶ | if (!s || !len) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s || !len) return 0; |
17 |
0 |
17 |
| if (!s || !len) return 0; |
0 |
17 |
17 |
|
| 119 |
|
| /* |
| 120 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 121 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 122 |
17 |
▶ | if (!(isALPHA(s[0]) || s[0] == '_' || s[0] == ':')) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 123 |
139 |
▶ | for (i = 1; i < len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < len; i++) |
124 |
15 |
139 |
|
| 124 |
|
| /* |
| 125 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 126 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 127 |
124 |
▶ | if (!(isALNUM(s[i]) || s[i] == '_' || s[i] == ':')) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 128 |
15 |
| return 1; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| /* The route to label this request with. |
| 132 |
|
| * |
| 133 |
|
| * The declared pattern when there is one; the OpenAPI operationId for an api |
| 134 |
|
| * mount, which is bounded the same way; and <other> for everything else. A |
| 135 |
|
| * 404 has no route by definition, and giving it the request path is precisely |
| 136 |
|
| * how the bounded dimension becomes unbounded. */ |
| 137 |
302 |
| static SV *pm_route_of(pTHX_ SV *c) { |
| 138 |
302 |
| SV *r = pk_abi_route_pattern_of(aTHX_ c); |
| 139 |
|
| /* |
| 140 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 141 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 142 |
302 |
▶ | if (r && SvOK(r) && SvCUR(r)) return r; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 143 |
232 |
| r = pk_abi_operation_of(aTHX_ c); |
| 144 |
|
| /* |
| 145 |
|
| # uncoverable branch true |
| 146 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 147 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 148 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 149 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 150 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 151 |
232 |
▶ | if (r && SvOK(r) && SvCUR(r)) return r; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 152 |
232 |
| return NULL; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| /* REQUEST_METHOD as a label value, or "-". |
| 156 |
|
| * |
| 157 |
|
| * A known method, or `<other>`. The method is whatever the client put on the |
| 158 |
|
| * request line, any bytes at all, and an unknown one on any path is still a |
| 159 |
|
| * counted 404: left as it came, every spelling was a new series in every |
| 160 |
|
| * worker and in whatever scrapes them, without end. The route label is |
| 161 |
|
| * bounded the same way and for the same reason. */ |
| 162 |
302 |
| static const char *pm_method_label(const char *m, STRLEN *len) { |
| 163 |
|
| static const char *const known[] = { "GET", "HEAD", "POST", "PUT", |
| 164 |
|
| "DELETE", "PATCH", "OPTIONS", "CONNECT", "TRACE", NULL }; |
| 165 |
|
| int i; |
| 166 |
2169 |
▶ | for (i = 0; known[i]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; known[i]; i++) |
1966 |
203 |
2169 |
|
| 167 |
1966 |
▶ | if (strlen(known[i]) == *len && memEQ(m, known[i], *len)) return m; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 168 |
203 |
| *len = 7; |
| 169 |
203 |
| return "<other>"; |
| 170 |
|
| } |
| 171 |
|
| |
| 172 |
302 |
| static const char *pm_method_of(pTHX_ SV *c, STRLEN *len) { |
| 173 |
302 |
| SV *env = pk_abi_env_of(aTHX_ c); |
| 174 |
|
| SV **m; |
| 175 |
|
| /* |
| 176 |
|
| # uncoverable branch false |
| 177 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 178 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 179 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 180 |
|
| # uncoverable condition right count:4 class:ignore_covered_err |
| 181 |
|
| # uncoverable condition right count:5 class:ignore_covered_err |
| 182 |
|
| # uncoverable condition right count:6 class:ignore_covered_err |
| 183 |
|
| # uncoverable condition right count:7 class:ignore_covered_err |
| 184 |
|
| # uncoverable condition right count:8 class:ignore_covered_err */ |
| 185 |
302 |
▶ | if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 186 |
302 |
▶ | && (m = hv_fetchs((HV *)SvRV(env), "REQUEST_METHOD", 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| && (m = hv_fetchs((HV *)SvRV(env), "REQUEST_METHOD", 0)) |
302 |
0 |
302 |
|
| 187 |
302 |
▶ | && m && *m && SvOK(*m) && SvCUR(*m)) { |
Branch: 4 branches
| Branch | True | False | Total |
| && 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 |
|
| 188 |
302 |
| const char *p = SvPV_const(*m, *len); |
| 189 |
302 |
| return pm_method_label(p, len); |
| 190 |
|
| } |
| 191 |
0 |
| *len = 1; /* # uncoverable statement */ |
| 192 |
0 |
| return "-"; /* # uncoverable statement */ |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
302 |
| static void pm_bump_total(pTHX_ const char *k, STRLEN kl) { |
| 196 |
302 |
| SV **e = hv_fetch(PM_TOTAL, k, (I32)kl, 1); |
| 197 |
|
| /* |
| 198 |
|
| # uncoverable branch false |
| 199 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 200 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 201 |
302 |
▶ | if (e && *e) sv_setuv(*e, (SvOK(*e) ? SvUV(*e) : 0) + 1); |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 202 |
302 |
| } |
| 203 |
|
| |
| 204 |
302 |
| static void pm_bump_hist(pTHX_ const char *k, STRLEN kl, double secs) { |
| 205 |
302 |
| SV **e = hv_fetch(PM_HIST, k, (I32)kl, 1); |
| 206 |
|
| AV *h; |
| 207 |
|
| int i; |
| 208 |
|
| /* |
| 209 |
|
| # uncoverable branch true |
| 210 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 211 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 212 |
302 |
▶ | if (!(e && *e)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(e && *e)) return; |
302 |
0 |
302 |
| if (!(e && *e)) return; |
0 |
302 |
302 |
|
| 213 |
302 |
▶ | if (!(SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
271 |
31 |
302 |
| if (!(SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
271 |
271 |
|
| 214 |
31 |
| AV *fresh = newAV(); |
| 215 |
31 |
| av_extend(fresh, 2 + PM_NBUCKETS); |
| 216 |
31 |
| av_store(fresh, 0, newSVuv(0)); |
| 217 |
31 |
| av_store(fresh, 1, newSVnv(0.0)); |
| 218 |
465 |
▶ | for (i = 0; i < PM_NBUCKETS; i++) av_store(fresh, 2 + i, newSVuv(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PM_NBUCKETS; i++) av_store(fresh, 2 + i, newSVuv(0)); |
434 |
31 |
465 |
|
| 219 |
31 |
| sv_setsv(*e, sv_2mortal(newRV_noinc((SV *)fresh))); |
| 220 |
|
| } |
| 221 |
302 |
| h = (AV *)SvRV(*e); |
| 222 |
|
| { |
| 223 |
302 |
| SV **n = av_fetch(h, 0, 0); |
| 224 |
302 |
| SV **s = av_fetch(h, 1, 0); |
| 225 |
|
| /* |
| 226 |
|
| # uncoverable branch false |
| 227 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 228 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 229 |
302 |
▶ | if (n && *n) sv_setuv(*n, SvUV(*n) + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| if (n && *n) sv_setuv(*n, SvUV(*n) + 1); |
302 |
0 |
302 |
| if (n && *n) sv_setuv(*n, SvUV(*n) + 1); |
302 |
0 |
302 |
|
| 230 |
|
| /* |
| 231 |
|
| # uncoverable branch false |
| 232 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 233 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 234 |
302 |
▶ | if (s && *s) sv_setnv(*s, SvNV(*s) + secs); |
Branch: 2 branches
| Branch | True | False | Total |
| if (s && *s) sv_setnv(*s, SvNV(*s) + secs); |
302 |
0 |
302 |
| if (s && *s) sv_setnv(*s, SvNV(*s) + secs); |
302 |
0 |
302 |
|
| 235 |
|
| } |
| 236 |
|
| /* Cumulative, which is what a Prometheus histogram means: a request lands |
| 237 |
|
| * in its own bucket AND every wider one. */ |
| 238 |
4530 |
▶ | for (i = 0; i < PM_NBUCKETS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PM_NBUCKETS; i++) { |
4228 |
302 |
4530 |
|
| 239 |
4228 |
▶ | if (secs <= PM_BUCKETS[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (secs <= PM_BUCKETS[i]) { |
4225 |
3 |
4228 |
|
| 240 |
4225 |
| SV **b = av_fetch(h, 2 + i, 0); |
| 241 |
|
| /* |
| 242 |
|
| # uncoverable branch false |
| 243 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 244 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 245 |
4225 |
▶ | if (b && *b) sv_setuv(*b, SvUV(*b) + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| if (b && *b) sv_setuv(*b, SvUV(*b) + 1); |
4225 |
0 |
4225 |
| if (b && *b) sv_setuv(*b, SvUV(*b) + 1); |
4225 |
0 |
4225 |
|
| 246 |
|
| } |
| 247 |
|
| } |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
|
| /* ---- the observers --------------------------------------------------------- |
| 251 |
|
| * |
| 252 |
|
| * MUST NOT croak: they run inside somebody else's request, and an exporter |
| 253 |
|
| * that can take the application down with it is worse than no exporter. */ |
| 254 |
|
| /* The context's own slot for the start time: one av_store, where the stash |
| 255 |
|
| * hash cost a hash to create and two stores per request. */ |
| 256 |
654 |
| static AV *pm_ctx_av(pTHX_ SV *c) { |
| 257 |
|
| /* |
| 258 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 259 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 260 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 261 |
654 |
▶ | return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVAV) ? (AV *)SvRV(c) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
327 |
| static void pm_on_request(pTHX_ SV *c, void *ud) { |
| 265 |
327 |
| AV *cav = pm_ctx_av(aTHX_ c); |
| 266 |
|
| PERL_UNUSED_ARG(ud); |
| 267 |
|
| |
| 268 |
|
| /* Is this the scrape itself? Decided HERE, at the one moment it can be |
| 269 |
|
| * decided symmetrically, and recorded for the response side. |
| 270 |
|
| * |
| 271 |
|
| * Routing has not happened yet, so there is no pattern to compare - only |
| 272 |
|
| * the path. That is the whole reason for the stash flag: if the two sides |
| 273 |
|
| * decided independently they could disagree, and the in-flight gauge |
| 274 |
|
| * would then drift by one every time they did. |
| 275 |
|
| * |
| 276 |
|
| * It matters because a scrape is in flight while it renders. Counting it |
| 277 |
|
| * gives an idle server a permanent floor of one, on every dashboard, for |
| 278 |
|
| * ever. */ |
| 279 |
|
| { |
| 280 |
327 |
| SV *env = pk_abi_env_of(aTHX_ c); |
| 281 |
327 |
▶ | SV **p = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 282 |
654 |
▶ | ? hv_fetchs((HV *)SvRV(env), "PATH_INFO", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? hv_fetchs((HV *)SvRV(env), "PATH_INFO", 0) : NULL; |
327 |
0 |
327 |
|
| 283 |
|
| /* |
| 284 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 285 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 286 |
327 |
▶ | if (p && *p && pm_is_scrape(aTHX_ *p)) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 287 |
|
| /* |
| 288 |
|
| # uncoverable branch false |
| 289 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 290 |
25 |
▶ | if (cav) (void)av_store(cav, PCX_T0, newSVnv(-1.0)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cav) (void)av_store(cav, PCX_T0, newSVnv(-1.0)); |
25 |
0 |
25 |
|
| 291 |
25 |
| return; |
| 292 |
|
| } |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
302 |
| PM_INFLIGHT++; |
| 296 |
|
| /* |
| 297 |
|
| # uncoverable branch false |
| 298 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 299 |
302 |
▶ | if (cav) (void)av_store(cav, PCX_T0, newSVnv(pc_now(aTHX))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cav) (void)av_store(cav, PCX_T0, newSVnv(pc_now(aTHX))); |
302 |
0 |
302 |
|
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
327 |
| static void pm_on_response(pTHX_ SV *c, SV *response, void *ud) { |
| 303 |
|
| SV *route; |
| 304 |
327 |
| AV *cav = pm_ctx_av(aTHX_ c); |
| 305 |
|
| const char *mp; STRLEN ml; |
| 306 |
|
| IV status; |
| 307 |
327 |
| double t0 = 0.0, secs; |
| 308 |
|
| SV *key; |
| 309 |
|
| PERL_UNUSED_ARG(ud); |
| 310 |
|
| |
| 311 |
|
| /* |
| 312 |
|
| # uncoverable branch false |
| 313 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 314 |
327 |
▶ | if (cav) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cav) { |
327 |
0 |
327 |
|
| 315 |
327 |
| SV **t = av_fetch(cav, PCX_T0, 0); |
| 316 |
|
| /* |
| 317 |
|
| # uncoverable branch false |
| 318 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 319 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 320 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 321 |
327 |
▶ | if (t && *t && SvOK(*t)) t0 = SvNV(*t); |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 322 |
|
| } |
| 323 |
|
| /* The request side already decided, so the two cannot disagree: it never |
| 324 |
|
| * incremented the gauge for this one, and this never decrements it. */ |
| 325 |
327 |
▶ | if (t0 < 0.0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t0 < 0.0) return; |
25 |
302 |
327 |
|
| 326 |
|
| |
| 327 |
|
| /* |
| 328 |
|
| # uncoverable branch false |
| 329 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 330 |
302 |
▶ | if (PM_INFLIGHT > 0) PM_INFLIGHT--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (PM_INFLIGHT > 0) PM_INFLIGHT--; |
302 |
0 |
302 |
|
| 331 |
302 |
| pm_init(aTHX); |
| 332 |
|
| |
| 333 |
302 |
| route = pm_route_of(aTHX_ c); |
| 334 |
|
| |
| 335 |
|
| /* And the same exclusion by route, for a scrape reached through a mount |
| 336 |
|
| * where the path the request carried was not the path this was told. */ |
| 337 |
|
| /* |
| 338 |
|
| # uncoverable branch true |
| 339 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 340 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 341 |
302 |
▶ | if (route && pm_is_scrape(aTHX_ route)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (route && pm_is_scrape(aTHX_ route)) return; |
70 |
232 |
302 |
| if (route && pm_is_scrape(aTHX_ route)) return; |
0 |
70 |
70 |
|
| 342 |
|
| |
| 343 |
302 |
| mp = pm_method_of(aTHX_ c, &ml); |
| 344 |
302 |
| status = pk_abi_status_of(aTHX_ response); |
| 345 |
|
| |
| 346 |
302 |
▶ | secs = t0 > 0.0 ? pc_now(aTHX) - t0 : 0.0; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| secs = t0 > 0.0 ? pc_now(aTHX) - t0 : 0.0; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
302 |
0 |
302 |
|
| 347 |
|
| /* |
| 348 |
|
| # uncoverable branch true |
| 349 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 350 |
302 |
▶ | if (secs < 0.0) secs = 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (secs < 0.0) secs = 0.0; |
0 |
302 |
302 |
|
| 351 |
|
| |
| 352 |
302 |
| key = sv_2mortal(newSVpvn(mp, ml)); |
| 353 |
302 |
| sv_catpvs(key, "\x1f"); |
| 354 |
302 |
▶ | if (route) sv_catsv(key, route); |
Branch: 1 branch
| Branch | True | False | Total |
| if (route) sv_catsv(key, route); |
70 |
232 |
302 |
|
| 355 |
232 |
| else sv_catpvs(key, "<other>"); |
| 356 |
|
| |
| 357 |
302 |
| pm_bump_hist(aTHX_ SvPVX(key), SvCUR(key), secs); |
| 358 |
|
| |
| 359 |
|
| /* A streaming or detached response has no status to read; -1 rather than |
| 360 |
|
| * a guess, so nobody graphs a 200 that never happened. */ |
| 361 |
302 |
| sv_catpvs(key, "\x1f"); |
| 362 |
302 |
▶ | if (status >= 0) sv_catpvf(key, "%" IVdf, status); |
Branch: 1 branch
| Branch | True | False | Total |
| if (status >= 0) sv_catpvf(key, "%" IVdf, status); |
301 |
1 |
302 |
|
| 363 |
1 |
| else sv_catpvs(key, "-"); |
| 364 |
302 |
| pm_bump_total(aTHX_ SvPVX(key), SvCUR(key)); |
| 365 |
|
| } |
| 366 |
|
| |
| 367 |
|
| /* ---- rendering ------------------------------------------------------------ */ |
| 368 |
|
| |
| 369 |
|
| /* Split a "\x1f"-joined key. */ |
| 370 |
215 |
| static const char *pm_field(const char *k, STRLEN kl, int n, STRLEN *len) { |
| 371 |
215 |
| const char *p = k, *end = k + kl; |
| 372 |
215 |
| int i = 0; |
| 373 |
1070 |
▶ | while (i < n && p < end) { if (*p++ == '\x1f') i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < n && p < end) { if (*p++ == '\x1f') i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
172 |
683 |
855 |
| while (i < n && p < end) { if (*p++ == '\x1f') i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
855 |
215 |
1070 |
| while (i < n && p < end) { if (*p++ == '\x1f') i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
855 |
0 |
855 |
|
| 374 |
215 |
| { const char *q = p; |
| 375 |
1064 |
▶ | while (q < end && *q != '\x1f') q++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < end && *q != '\x1f') q++; |
978 |
86 |
1064 |
| while (q < end && *q != '\x1f') q++; |
849 |
129 |
978 |
|
| 376 |
215 |
| *len = (STRLEN)(q - p); |
| 377 |
215 |
| return p; } |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
98 |
| static void pm_worker(pTHX_ SV *out) { |
| 381 |
98 |
| sv_catpvf(out, ",worker=\"%" IVdf "\"", (IV)PerlProc_getpid()); |
| 382 |
98 |
| } |
| 383 |
|
| |
| 384 |
|
| /* The sorted keys of an HV, so a scrape is byte-stable between calls and a |
| 385 |
|
| * diff of two shows what changed rather than what moved. */ |
| 386 |
76 |
| static AV *pm_sorted_keys(pTHX_ HV *h) { |
| 387 |
76 |
| AV *keys = (AV *)sv_2mortal((SV *)newAV()); |
| 388 |
|
| HE *ent; |
| 389 |
76 |
| hv_iterinit(h); |
| 390 |
168 |
▶ | while ((ent = hv_iternext(h))) av_push(keys, newSVsv(hv_iterkeysv(ent))); |
Branch: 1 branch
| Branch | True | False | Total |
| while ((ent = hv_iternext(h))) av_push(keys, newSVsv(hv_iterkeysv(ent))); |
92 |
76 |
168 |
|
| 391 |
76 |
| sortsv(AvARRAY(keys), (SSize_t)(av_len(keys) + 1), Perl_sv_cmp); |
| 392 |
76 |
| return keys; |
| 393 |
|
| } |
| 394 |
|
| |
| 395 |
|
| /* Anything an application wants to add, as gauges. This is where the queue's |
| 396 |
|
| * depth and a server's own counters go: reaching into another distribution |
| 397 |
|
| * from here would couple this plugin to versions of things it cannot test |
| 398 |
|
| * against, and an application already has both in scope. */ |
| 399 |
26 |
| static void pm_collect(pTHX_ SV *app, SV *out) { |
| 400 |
26 |
| HV *h = app_hv(aTHX_ app); |
| 401 |
26 |
▶ | SV *cb = h ? app_get(aTHX_ h, "metrics_collect") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *cb = h ? app_get(aTHX_ h, "metrics_collect") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
26 |
0 |
26 |
|
| 402 |
26 |
| SV *got = NULL; |
| 403 |
|
| HV *gh; |
| 404 |
|
| AV *names; |
| 405 |
|
| SSize_t i, n; |
| 406 |
|
| |
| 407 |
|
| /* |
| 408 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 409 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 410 |
26 |
▶ | if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 411 |
|
| |
| 412 |
|
| { |
| 413 |
24 |
| dSP; int count; |
| 414 |
24 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
24 |
24 |
|
| 415 |
24 |
| count = call_sv(cb, G_SCALAR | G_EVAL); |
| 416 |
24 |
| SPAGAIN; |
| 417 |
24 |
▶ | if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
24 |
0 |
24 |
| if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
23 |
1 |
24 |
| if (!SvTRUE(ERRSV) && count > 0) got = newSVsv(POPs); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
23 |
0 |
23 |
|
| 418 |
|
| /* |
| 419 |
|
| # uncoverable branch false |
| 420 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 421 |
1 |
▶ | else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) (void)POPs; |
1 |
0 |
1 |
|
| 422 |
24 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
24 |
0 |
24 |
|
| 423 |
|
| } |
| 424 |
24 |
▶ | if (!got) return; /* it died: the scrape goes on */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!got) return; /* it died: the scrape goes on */ |
1 |
23 |
24 |
|
| 425 |
23 |
| sv_2mortal(got); |
| 426 |
|
| /* |
| 427 |
|
| # uncoverable branch true |
| 428 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 429 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 430 |
23 |
▶ | if (!(SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV)) return; |
23 |
0 |
23 |
| if (!(SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV)) return; |
0 |
23 |
23 |
|
| 431 |
23 |
| gh = (HV *)SvRV(got); |
| 432 |
|
| |
| 433 |
23 |
| names = pm_sorted_keys(aTHX_ gh); |
| 434 |
23 |
| n = av_len(names) + 1; |
| 435 |
28 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
5 |
23 |
28 |
|
| 436 |
5 |
| SV *nm = *av_fetch(names, i, 0); |
| 437 |
5 |
| HE *he = hv_fetch_ent(gh, nm, 0, 0); |
| 438 |
5 |
▶ | SV *v = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *v = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
5 |
0 |
5 |
|
| 439 |
5 |
| STRLEN nl; const char *np = SvPV_const(nm, nl); |
| 440 |
5 |
▶ | if (!pm_name_ok(np, nl)) continue; /* see pm_name_ok */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pm_name_ok(np, nl)) continue; /* see pm_name_ok */ |
2 |
3 |
5 |
|
| 441 |
|
| /* |
| 442 |
|
| # uncoverable branch true |
| 443 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 444 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 445 |
3 |
▶ | if (!(v && SvOK(v))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(v && SvOK(v))) continue; |
3 |
0 |
3 |
| if (!(v && SvOK(v))) continue; |
0 |
3 |
3 |
|
| 446 |
3 |
| sv_catpvf(out, "# TYPE %s gauge\n%s{", np, np); |
| 447 |
|
| /* the leading comma of pm_worker would be wrong as the first label */ |
| 448 |
6 |
| sv_catpvf(out, "worker=\"%" IVdf "\"} %s\n", |
| 449 |
|
| (IV)PerlProc_getpid(), SvPV_nolen(sv_2mortal(newSVnv(SvNV(v))))); |
| 450 |
|
| } |
| 451 |
|
| } |
| 452 |
|
| |
| 453 |
|
| /* Whatever each cache is willing to say about itself, as gauges. In-dist, so |
| 454 |
|
| * it can be kept in step. |
| 455 |
|
| * |
| 456 |
|
| * `app->{cache}` is a HASH of name => cache object, not a cache - so every |
| 457 |
|
| * configured cache is reported, labelled with its name. The label set is |
| 458 |
|
| * bounded by the config, which is the same rule the route label follows. */ |
| 459 |
26 |
| static void pm_cache(pTHX_ SV *app, SV *out) { |
| 460 |
26 |
| HV *h = app_hv(aTHX_ app); |
| 461 |
26 |
▶ | SV *caches = h ? app_get(aTHX_ h, "cache") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *caches = h ? app_get(aTHX_ h, "cache") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
26 |
0 |
26 |
|
| 462 |
|
| HV *ch; |
| 463 |
|
| AV *names; |
| 464 |
|
| SSize_t i, n; |
| 465 |
|
| |
| 466 |
|
| /* |
| 467 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 468 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 469 |
26 |
▶ | if (!(caches && SvROK(caches) && SvTYPE(SvRV(caches)) == SVt_PVHV)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 470 |
1 |
| ch = (HV *)SvRV(caches); |
| 471 |
|
| |
| 472 |
1 |
| names = pm_sorted_keys(aTHX_ ch); |
| 473 |
1 |
| n = av_len(names) + 1; |
| 474 |
2 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
1 |
1 |
2 |
|
| 475 |
1 |
| SV *nm = *av_fetch(names, i, 0); |
| 476 |
1 |
| HE *he = hv_fetch_ent(ch, nm, 0, 0); |
| 477 |
1 |
▶ | SV *cache = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *cache = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
1 |
0 |
1 |
|
| 478 |
1 |
| STRLEN nml; const char *nmp = SvPV_const(nm, nml); |
| 479 |
|
| SSize_t j, got; |
| 480 |
|
| |
| 481 |
|
| /* |
| 482 |
|
| # uncoverable branch true |
| 483 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 484 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 485 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 486 |
1 |
▶ | if (!(cache && SvROK(cache) && SvOBJECT(SvRV(cache)))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 487 |
|
| |
| 488 |
|
| /* stats() returns a flat LIST, so it has to be called in list |
| 489 |
|
| * context - pcx_call_meth only offers scalar or void. */ |
| 490 |
|
| { |
| 491 |
1 |
| dSP; int count, k; |
| 492 |
1 |
| AV *flat = (AV *)sv_2mortal((SV *)newAV()); |
| 493 |
1 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
1 |
1 |
|
| 494 |
1 |
▶ | EXTEND(SP, 1); PUSHs(cache); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(cache); PUTBACK; |
0 |
1 |
1 |
|
| 495 |
1 |
| count = call_method("stats", G_ARRAY | G_EVAL); |
| 496 |
1 |
| SPAGAIN; |
| 497 |
|
| /* |
| 498 |
|
| # uncoverable branch true |
| 499 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 500 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 501 |
1 |
▶ | if (SvTRUE(ERRSV)) count = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) count = 0; |
1 |
0 |
1 |
| if (SvTRUE(ERRSV)) count = 0; |
0 |
1 |
1 |
|
| 502 |
25 |
▶ | for (k = 0; k < count; k++) av_unshift(flat, 1), av_store(flat, 0, newSVsv(POPs)); |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < count; k++) av_unshift(flat, 1), av_store(flat, 0, newSVsv(POPs)); |
24 |
1 |
25 |
|
| 503 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 504 |
|
| |
| 505 |
1 |
| got = av_len(flat) + 1; |
| 506 |
13 |
▶ | for (j = 0; j + 1 < got; j += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j + 1 < got; j += 2) { |
12 |
1 |
13 |
|
| 507 |
12 |
| SV **k2 = av_fetch(flat, j, 0); |
| 508 |
12 |
| SV **v = av_fetch(flat, j + 1, 0); |
| 509 |
|
| STRLEN kl; const char *kp; |
| 510 |
|
| /* |
| 511 |
|
| # uncoverable branch true |
| 512 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 513 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 514 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 515 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 516 |
|
| # uncoverable condition left count:5 class:ignore_covered_err */ |
| 517 |
12 |
▶ | if (!(k2 && *k2 && v && *v && SvOK(*v))) continue; |
Branch: 5 branches
| Branch | True | False | Total |
| 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 |
|
| 518 |
12 |
| kp = SvPV_const(*k2, kl); |
| 519 |
|
| /* |
| 520 |
|
| # uncoverable branch true |
| 521 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 522 |
12 |
▶ | if (!pm_name_ok(kp, kl)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pm_name_ok(kp, kl)) continue; |
0 |
12 |
12 |
|
| 523 |
12 |
| sv_catpvf(out, "punk_cache_%.*s{cache=\"", (int)kl, kp); |
| 524 |
12 |
| pm_label(aTHX_ out, nmp, nml); |
| 525 |
12 |
| sv_catpvs(out, "\""); |
| 526 |
12 |
| pm_worker(aTHX_ out); |
| 527 |
|
| /* NVgf, not a bare "g", and (NV) to match it. |
| 528 |
|
| * |
| 529 |
|
| * sv_catpvf is not C printf. Its float conversions take |
| 530 |
|
| * whatever width the format's length modifier names, and a |
| 531 |
|
| * bare %g names `double` - so on a long-double or quadmath |
| 532 |
|
| * perl an NV argument is read as the wrong type off the |
| 533 |
|
| * varargs and every cache counter here came out as |
| 534 |
|
| * 8.094771541e-320. NVgf expands to the modifier that perl's |
| 535 |
|
| * own NV needs, on each of them. */ |
| 536 |
12 |
| sv_catpvf(out, "} %.10" NVgf "\n", (NV)SvNV(*v)); |
| 537 |
|
| } |
| 538 |
|
| } |
| 539 |
|
| } |
| 540 |
|
| } |
| 541 |
|
| |
| 542 |
|
| /* The whole document. */ |
| 543 |
26 |
| static SV *pm_render(pTHX_ SV *app) { |
| 544 |
26 |
| SV *out = sv_2mortal(newSVpvs("")); |
| 545 |
|
| AV *keys; |
| 546 |
|
| SSize_t i, n; |
| 547 |
|
| |
| 548 |
26 |
| pm_init(aTHX); |
| 549 |
|
| |
| 550 |
26 |
| sv_catpvs(out, |
| 551 |
|
| "# HELP http_requests_total Requests, by method, declared route and " |
| 552 |
|
| "status.\n" |
| 553 |
|
| "# TYPE http_requests_total counter\n"); |
| 554 |
26 |
| keys = pm_sorted_keys(aTHX_ PM_TOTAL); |
| 555 |
26 |
| n = av_len(keys) + 1; |
| 556 |
69 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
43 |
26 |
69 |
|
| 557 |
43 |
| SV *k = *av_fetch(keys, i, 0); |
| 558 |
43 |
| STRLEN kl; const char *kp = SvPV_const(k, kl); |
| 559 |
|
| STRLEN ml, rl, sl; |
| 560 |
43 |
| const char *mp = pm_field(kp, kl, 0, &ml); |
| 561 |
43 |
| const char *rp = pm_field(kp, kl, 1, &rl); |
| 562 |
43 |
| const char *sp = pm_field(kp, kl, 2, &sl); |
| 563 |
43 |
| HE *he = hv_fetch_ent(PM_TOTAL, k, 0, 0); |
| 564 |
43 |
| sv_catpvs(out, "http_requests_total{method=\""); |
| 565 |
43 |
| pm_label(aTHX_ out, mp, ml); |
| 566 |
43 |
| sv_catpvs(out, "\",route=\""); |
| 567 |
43 |
| pm_label(aTHX_ out, rp, rl); |
| 568 |
43 |
| sv_catpvs(out, "\",status=\""); |
| 569 |
43 |
| pm_label(aTHX_ out, sp, sl); |
| 570 |
43 |
| sv_catpvs(out, "\""); |
| 571 |
43 |
| pm_worker(aTHX_ out); |
| 572 |
43 |
▶ | sv_catpvf(out, "} %" UVuf "\n", he ? SvUV(HeVAL(he)) : (UV)0); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catpvf(out, "} %" UVuf "\n", he ? SvUV(HeVAL(he)) : (UV)0); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
43 |
0 |
43 |
|
| 573 |
|
| } |
| 574 |
|
| |
| 575 |
26 |
| sv_catpvs(out, |
| 576 |
|
| "# HELP http_request_duration_seconds Request duration.\n" |
| 577 |
|
| "# TYPE http_request_duration_seconds histogram\n"); |
| 578 |
26 |
| keys = pm_sorted_keys(aTHX_ PM_HIST); |
| 579 |
26 |
| n = av_len(keys) + 1; |
| 580 |
69 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
43 |
26 |
69 |
|
| 581 |
43 |
| SV *k = *av_fetch(keys, i, 0); |
| 582 |
43 |
| STRLEN kl; const char *kp = SvPV_const(k, kl); |
| 583 |
|
| STRLEN ml, rl; |
| 584 |
43 |
| const char *mp = pm_field(kp, kl, 0, &ml); |
| 585 |
43 |
| const char *rp = pm_field(kp, kl, 1, &rl); |
| 586 |
43 |
| HE *he = hv_fetch_ent(PM_HIST, k, 0, 0); |
| 587 |
43 |
▶ | AV *hst = (he && SvROK(HeVAL(he)) |
Branch: 1 branch
| Branch | True | False | Total |
| AV *hst = (he && SvROK(HeVAL(he)) |
43 |
0 |
43 |
|
| 588 |
43 |
▶ | && SvTYPE(SvRV(HeVAL(he))) == SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(HeVAL(he))) == SVt_PVAV) |
43 |
0 |
43 |
|
| 589 |
86 |
▶ | ? (AV *)SvRV(HeVAL(he)) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(HeVAL(he)) : NULL; |
43 |
0 |
43 |
|
| 590 |
43 |
| SV *labels = sv_2mortal(newSVpvs("method=\"")); |
| 591 |
|
| int b; |
| 592 |
|
| /* |
| 593 |
|
| # uncoverable branch true |
| 594 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 595 |
43 |
▶ | if (!hst) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hst) continue; |
0 |
43 |
43 |
|
| 596 |
43 |
| pm_label(aTHX_ labels, mp, ml); |
| 597 |
43 |
| sv_catpvs(labels, "\",route=\""); |
| 598 |
43 |
| pm_label(aTHX_ labels, rp, rl); |
| 599 |
43 |
| sv_catpvs(labels, "\""); |
| 600 |
43 |
| pm_worker(aTHX_ labels); |
| 601 |
|
| |
| 602 |
645 |
▶ | for (b = 0; b < PM_NBUCKETS; b++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (b = 0; b < PM_NBUCKETS; b++) { |
602 |
43 |
645 |
|
| 603 |
602 |
| SV **cnt = av_fetch(hst, 2 + b, 0); |
| 604 |
1204 |
▶ | sv_catpvf(out, "http_request_duration_seconds_bucket{%s,le=\"%" |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 605 |
|
| NVgf "\"} %" UVuf "\n", |
| 606 |
|
| SvPV_nolen(labels), (NV)PM_BUCKETS[b], |
| 607 |
|
| cnt && *cnt ? SvUV(*cnt) : (UV)0); |
| 608 |
|
| } |
| 609 |
|
| { |
| 610 |
43 |
| SV **num = av_fetch(hst, 0, 0); |
| 611 |
43 |
| SV **sum = av_fetch(hst, 1, 0); |
| 612 |
|
| /* |
| 613 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 614 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 615 |
43 |
▶ | UV nn = num && *num ? SvUV(*num) : (UV)0; |
Branch: 2 branches
| Branch | True | False | Total |
| UV nn = num && *num ? SvUV(*num) : (UV)0; |
43 |
0 |
43 |
| UV nn = num && *num ? SvUV(*num) : (UV)0; |
43 |
0 |
43 |
|
| 616 |
43 |
| sv_catpvf(out, "http_request_duration_seconds_bucket{%s,le=\"+Inf\"} " |
| 617 |
|
| "%" UVuf "\n", SvPV_nolen(labels), nn); |
| 618 |
86 |
▶ | sv_catpvf(out, "http_request_duration_seconds_sum{%s} %.6" NVff |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 619 |
|
| "\n", |
| 620 |
|
| SvPV_nolen(labels), sum && *sum ? (NV)SvNV(*sum) : (NV)0); |
| 621 |
43 |
| sv_catpvf(out, "http_request_duration_seconds_count{%s} %" UVuf "\n", |
| 622 |
|
| SvPV_nolen(labels), nn); |
| 623 |
|
| } |
| 624 |
|
| } |
| 625 |
|
| |
| 626 |
26 |
| sv_catpvs(out, |
| 627 |
|
| "# HELP http_requests_in_flight Requests being served right now.\n" |
| 628 |
|
| "# TYPE http_requests_in_flight gauge\n"); |
| 629 |
26 |
| sv_catpvf(out, "http_requests_in_flight{worker=\"%" IVdf "\"} %" IVdf "\n", |
| 630 |
|
| (IV)PerlProc_getpid(), PM_INFLIGHT); |
| 631 |
|
| |
| 632 |
26 |
| pm_cache(aTHX_ app, out); |
| 633 |
26 |
| pm_collect(aTHX_ app, out); |
| 634 |
|
| |
| 635 |
26 |
| return newSVsv(out); |
| 636 |
|
| } |
| 637 |
|
| |
| 638 |
|
| XS_INTERNAL(pm_serve_cb); |
| 639 |
25 |
| XS_INTERNAL(pm_serve_cb) { |
| 640 |
25 |
| dXSARGS; |
| 641 |
25 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 642 |
25 |
▶ | SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
25 |
0 |
25 |
|
| 643 |
25 |
| AV *extra = (AV *)sv_2mortal((SV *)newAV()); |
| 644 |
|
| PERL_UNUSED_VAR(items); |
| 645 |
25 |
| av_push(extra, newSVpvs("Cache-Control")); |
| 646 |
25 |
| av_push(extra, newSVpvs("no-store")); |
| 647 |
25 |
| ST(0) = sv_2mortal(punk_triplet(aTHX_ 200, |
| 648 |
|
| sv_2mortal(newSVpvs("text/plain; version=0.0.4; charset=utf-8")), |
| 649 |
|
| pm_render(aTHX_ app), extra)); |
| 650 |
25 |
| XSRETURN(1); |
| 651 |
|
| } |
| 652 |
|
| |
| 653 |
|
| #endif /* PUNK_METRICS_H */ |