xs/metrics.xs

filter: Statementstmt 100% 39 / 39 Branchbran 65.6% 21 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.5% 60 / 71 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_request.h ↑ Summary include/punk/punk_names.h »
1 MODULE = Punk PACKAGE = Punk::Plugin::Metrics
2  
3 PROTOTYPES: DISABLE
4  
5 SV *
6 new(class)
7 SV *class
8 CODE:
9 6 RETVAL = sv_bless(newRV_noinc(newSViv(0)),
10 gv_stashpv(SvPV_nolen(class), GV_ADD));
11 OUTPUT:
12 RETVAL
13  
14 # register($app, \%opts)
15 void
16 register(self, app, opts = &PL_sv_undef)
17 SV *self
18 SV *app
19 SV *opts
20 CODE:
21 {
22 6 HV *h = app_hv(aTHX_ app);
23 6 ▶ HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 6 0 6
24 12 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 6 0 6
25 SV **v;
26 6 SV *path = NULL;
27 PERL_UNUSED_VAR(self);
28  
29 6 ▶ if (!h) croak("Punk::Plugin::Metrics: no application");
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Punk::Plugin::Metrics: no application"); 0 6 6
30  
31 6 ▶ if (o && (v = hv_fetchs(o, "path", 0)) && SvOK(*v) && SvCUR(*v))
Branch: 4 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "path", 0)) && SvOK(*v) && SvCUR(*v)) 6 0 6
if (o && (v = hv_fetchs(o, "path", 0)) && SvOK(*v) && SvCUR(*v)) 4 2 6
if (o && (v = hv_fetchs(o, "path", 0)) && SvOK(*v) && SvCUR(*v)) 4 0 4
if (o && (v = hv_fetchs(o, "path", 0)) && SvOK(*v) && SvCUR(*v)) 4 0 4
32 4 path = newSVsv(*v);
33 else
34 2 path = newSVpvs("/metrics");
35  
36 6 ▶ if (o && (v = hv_fetchs(o, "collect", 0)) && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "collect", 0)) && SvOK(*v)) { 6 0 6
if (o && (v = hv_fetchs(o, "collect", 0)) && SvOK(*v)) { 3 3 6
if (o && (v = hv_fetchs(o, "collect", 0)) && SvOK(*v)) { 3 0 3
37 3 ▶ if (!(SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVCV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVCV)) 2 1 3
if (!(SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVCV)) 0 2 2
38 1 croak("Punk::Plugin::Metrics: `collect` takes a coderef "
39 "returning a hashref of name => number");
40 2 (void)hv_stores(h, "metrics_collect", newSVsv(*v));
41 }
42  
43 5 (void)hv_stores(h, "metrics_path", newSVsv(path));
44  
45 5 pm_init(aTHX);
46  
47 /* This scrape endpoint, ADDED to the set rather than replacing it -
48 * see the note on PM_SKIP. Every metrics path in the process is
49 * excluded, whichever application declared it. */
50 5 (void)hv_store_ent(PM_SKIP, path, newSViv(1), 0);
51  
52 /* Registered ONCE per process. The registry is a static array with no
53 * removal, so registering per application would count every request
54 * twice in a test file that builds two apps - and silently, since
55 * both numbers look plausible. */
56 5 ▶ if (!PM_OBSERVING) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PM_OBSERVING) { 1 4 5
57 1 PM_OBSERVING = 1;
58 1 (void)pk_obs_add_req(aTHX_ pm_on_request, NULL);
59 1 (void)pk_obs_add_res(aTHX_ pm_on_response, NULL);
60 }
61  
62 {
63 SV *argv[5];
64 5 AV *cap = newAV();
65 SV *r;
66 5 av_push(cap, newSVsv(app));
67 5 argv[0] = sv_2mortal(newSVpvs("GET"));
68 5 argv[1] = sv_2mortal(path);
69 5 argv[2] = sv_2mortal(punk_closure(aTHX_ pm_serve_cb, cap));
70 5 argv[3] = &PL_sv_undef;
71 { /* out of the sitemap: a scrape target is not a page */
72 5 HV *ro = newHV();
73 5 (void)hv_stores(ro, K_SITEMAP, newSViv(0));
74 5 argv[4] = sv_2mortal(newRV_noinc((SV *)ro));
75 }
76 5 r = pcx_call_meth(aTHX_ app, "route", argv, 5, 1);
77 5 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 5 0 5
78 }
79 }
80  
81 # The exposition document, without going through a request. Private.
82 SV *
83 _render(class, app)
84 SV *class
85 SV *app
86 CODE:
87 PERL_UNUSED_VAR(class);
88 1 RETVAL = pm_render(aTHX_ app);
89 OUTPUT:
90 RETVAL
91  
92 # The bucket boundaries, so a test asserts the documented set rather than
93 # whatever the code happens to hold.
94 void
95 buckets(class)
96 SV *class
97 PPCODE:
98 {
99 int i;
100 PERL_UNUSED_VAR(class);
101 2 ▶ EXTEND(SP, PM_NBUCKETS);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, PM_NBUCKETS); 0 2 2
102 30 ▶ for (i = 0; i < PM_NBUCKETS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PM_NBUCKETS; i++) 28 2 30
103 28 PUSHs(sv_2mortal(newSVnv(PM_BUCKETS[i])));
104 }
105  
106 # Drop everything counted so far. Private: the counters are process-wide, so
107 # a test asserting a number needs to know where it started from.
108 void
109 _reset(class)
110 SV *class
111 CODE:
112 PERL_UNUSED_VAR(class);
113 16 pm_init(aTHX);
114 16 hv_clear(PM_TOTAL);
115 16 hv_clear(PM_HIST);
116 16 PM_INFLIGHT = 0;
« include/punk/punk_request.h ↑ Summary include/punk/punk_names.h »