xs/health.xs

filter: Statementstmt 97.6% 84 / 86 Branchbran 63.8% 69 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.8% 153 / 194 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/blob.xs ↑ Summary xs/abi.xs »
1 MODULE = Punk PACKAGE = Punk::Plugin::Health
2  
3 PROTOTYPES: DISABLE
4  
5 SV *
6 new(class)
7 SV *class
8 CODE:
9 13 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 #
16 # Two routes, and the difference between them is the plugin:
17 #
18 # /healthz liveness - checks NOTHING, because failing it restarts the
19 # worker, and restarting a worker does not fix a
20 # database.
21 # /readyz readiness - runs the checks, and failing it only takes this
22 # worker out of the pool.
23 #
24 # Both default to bare `{"status":"ok"}`. Health output names an
25 # application's internal dependencies, and the endpoints are unauthenticated
26 # because a probe cannot hold a credential - so the detail is behind an
27 # option rather than on by default.
28 void
29 register(self, app, opts = &PL_sv_undef)
30 SV *self
31 SV *app
32 SV *opts
33 CODE:
34 {
35 13 HV *h = app_hv(aTHX_ app);
36 13 ▶ HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 13 0 13
37 26 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 13 0 13
38 SV **v;
39 13 SV *live_path = NULL, *ready_path = NULL;
40 13 int have_live = 1, have_ready = 1;
41 PERL_UNUSED_VAR(self);
42  
43 13 ▶ if (!h) croak("Punk::Plugin::Health: no application");
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Punk::Plugin::Health: no application"); 0 13 13
44  
45 /* Time::HiRes for the budget and the ms figures. A prerequisite of
46 * perl itself, but `require`d rather than assumed loaded. */
47 13 (void)pk_require_once(aTHX_ "Time::HiRes", TRUE);
48  
49 /* Paths. An explicit undef DISABLES that endpoint - an application
50 * whose platform only probes one of them should be able to say so,
51 * rather than serving an endpoint nobody asked for. */
52 13 ▶ if (o && (v = hv_fetchs(o, "liveness", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "liveness", 0))) { 13 0 13
if (o && (v = hv_fetchs(o, "liveness", 0))) { 1 12 13
53 1 ▶ if (SvOK(*v) && SvCUR(*v)) live_path = newSVsv(*v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(*v) && SvCUR(*v)) live_path = newSVsv(*v); 1 0 1
if (SvOK(*v) && SvCUR(*v)) live_path = newSVsv(*v); 1 0 1
54 0 else have_live = 0;
55 }
56 13 ▶ if (o && (v = hv_fetchs(o, "readiness", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "readiness", 0))) { 13 0 13
if (o && (v = hv_fetchs(o, "readiness", 0))) { 1 12 13
57 1 ▶ if (SvOK(*v) && SvCUR(*v)) ready_path = newSVsv(*v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(*v) && SvCUR(*v)) ready_path = newSVsv(*v); 0 1 1
if (SvOK(*v) && SvCUR(*v)) ready_path = newSVsv(*v); 0 0 0
58 1 else have_ready = 0;
59 }
60 13 ▶ if (have_live && !live_path) live_path = newSVpvs("/healthz");
Branch: 2 branches
BranchTrueFalseTotal
if (have_live && !live_path) live_path = newSVpvs("/healthz"); 13 0 13
if (have_live && !live_path) live_path = newSVpvs("/healthz"); 12 1 13
61 13 ▶ if (have_ready && !ready_path) ready_path = newSVpvs("/readyz");
Branch: 2 branches
BranchTrueFalseTotal
if (have_ready && !ready_path) ready_path = newSVpvs("/readyz"); 12 1 13
if (have_ready && !ready_path) ready_path = newSVpvs("/readyz"); 12 0 12
62  
63 13 ▶ if (o && (v = hv_fetchs(o, "detail", 0)))
Branch: 2 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "detail", 0))) 13 0 13
if (o && (v = hv_fetchs(o, "detail", 0))) 4 9 13
64 4 (void)hv_stores(h, "health_detail", newSViv(SvTRUE(*v) ? 1 : 0));
65 13 ▶ if (o && (v = hv_fetchs(o, "version", 0)) && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "version", 0)) && SvOK(*v)) 13 0 13
if (o && (v = hv_fetchs(o, "version", 0)) && SvOK(*v)) 5 8 13
if (o && (v = hv_fetchs(o, "version", 0)) && SvOK(*v)) 5 0 5
66 5 (void)hv_stores(h, "health_version", newSVsv(*v));
67  
68 /* ttl: how long a readiness answer may be reused. Zero disables the
69 * cache, which is a real choice for an application whose checks are
70 * free, and 0.1s of staleness is not what makes a probe wrong. */
71 {
72 13 double ttl = PH_DEFAULT_TTL;
73 13 ▶ if (o && (v = hv_fetchs(o, "ttl", 0)) && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "ttl", 0)) && SvOK(*v)) { 13 0 13
if (o && (v = hv_fetchs(o, "ttl", 0)) && SvOK(*v)) { 7 6 13
if (o && (v = hv_fetchs(o, "ttl", 0)) && SvOK(*v)) { 7 0 7
74 7 ttl = SvNV(*v);
75 7 ▶ if (ttl < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ttl < 0) 1 6 7
76 1 croak("Punk::Plugin::Health: `ttl` cannot be negative");
77 }
78 12 (void)hv_stores(h, "health_ttl", newSVnv(ttl));
79 }
80  
81 /* timeout: the budget for the whole readiness pass. NOT a timeout in
82 * the sense of interrupting anything - nothing here can unblock a
83 * check already waiting in a driver, and the POD says so. It stops
84 * further checks being STARTED once the time is spent. */
85 {
86 12 double budget = PH_DEFAULT_BUDGET;
87 12 ▶ if (o && (v = hv_fetchs(o, "timeout", 0)) && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "timeout", 0)) && SvOK(*v)) { 12 0 12
if (o && (v = hv_fetchs(o, "timeout", 0)) && SvOK(*v)) { 2 10 12
if (o && (v = hv_fetchs(o, "timeout", 0)) && SvOK(*v)) { 2 0 2
88 2 budget = SvNV(*v);
89 2 ▶ if (budget <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (budget <= 0) 1 1 2
90 1 croak("Punk::Plugin::Health: `timeout` must be positive "
91 "- it is the budget for the readiness checks, and "
92 "a budget of zero would report unready always");
93 }
94 11 (void)hv_stores(h, "health_budget", newSVnv(budget));
95 }
96  
97 11 ▶ if (o && (v = hv_fetchs(o, "checks", 0)) && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (o && (v = hv_fetchs(o, "checks", 0)) && SvOK(*v)) { 11 0 11
if (o && (v = hv_fetchs(o, "checks", 0)) && SvOK(*v)) { 9 2 11
if (o && (v = hv_fetchs(o, "checks", 0)) && SvOK(*v)) { 9 0 9
98 HV *ch;
99 HE *ent;
100 9 ▶ if (!(SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVHV)) 9 0 9
if (!(SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVHV)) 0 9 9
101 0 croak("Punk::Plugin::Health: `checks` takes a hashref of "
102 "name => sub { ... }");
103 9 ch = (HV *)SvRV(*v);
104 9 hv_iterinit(ch);
105 20 ▶ while ((ent = hv_iternext(ch))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext(ch))) { 12 8 20
106 12 SV *cb = HeVAL(ent);
107 12 ▶ if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) 12 0 12
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) 11 1 12
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) 0 11 11
108 1 croak("Punk::Plugin::Health: the `%" SVf "` check is not "
109 "a coderef", SVfARG(hv_iterkeysv(ent)));
110 }
111 8 (void)hv_stores(h, "health_checks", newSVsv(*v));
112 }
113  
114 /* The paths this plugin owns, so anything counting requests can skip
115 * them. Punk logs no access line of its own, so there is nothing here
116 * to exclude from - but a probe every second will dominate any
117 * metric an observer keeps, and an observer can only skip what it can
118 * name. */
119 {
120 10 AV *own = newAV();
121 10 ▶ if (live_path) av_push(own, newSVsv(live_path));
Branch: 1 branch
BranchTrueFalseTotal
if (live_path) av_push(own, newSVsv(live_path)); 10 0 10
122 10 ▶ if (ready_path) av_push(own, newSVsv(ready_path));
Branch: 1 branch
BranchTrueFalseTotal
if (ready_path) av_push(own, newSVsv(ready_path)); 9 1 10
123 10 (void)hv_stores(h, "health_paths", newRV_noinc((SV *)own));
124 }
125  
126 {
127 SV *argv[5];
128 AV *cap;
129 SV *r;
130  
131 10 ▶ if (live_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (live_path) { 10 0 10
132 10 cap = newAV();
133 10 av_push(cap, newSVsv(app));
134 10 argv[0] = sv_2mortal(newSVpvs("GET"));
135 10 argv[1] = sv_2mortal(live_path);
136 10 argv[2] = sv_2mortal(punk_closure(aTHX_ ph_live_cb, cap));
137 10 argv[3] = &PL_sv_undef;
138 { /* out of the sitemap: a probe endpoint is not a page, and
139 * a crawler following one is pure noise. */
140 10 HV *ro = newHV();
141 10 (void)hv_stores(ro, K_SITEMAP, newSViv(0));
142 10 argv[4] = sv_2mortal(newRV_noinc((SV *)ro));
143 }
144 10 r = pcx_call_meth(aTHX_ app, "route", argv, 5, 1);
145 10 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 10 0 10
146 }
147  
148 10 ▶ if (ready_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (ready_path) { 9 1 10
149 9 cap = newAV();
150 9 av_push(cap, newSVsv(app));
151 9 argv[0] = sv_2mortal(newSVpvs("GET"));
152 9 argv[1] = sv_2mortal(ready_path);
153 9 argv[2] = sv_2mortal(punk_closure(aTHX_ ph_ready_cb, cap));
154 9 argv[3] = &PL_sv_undef;
155 {
156 9 HV *ro = newHV();
157 9 (void)hv_stores(ro, K_SITEMAP, newSViv(0));
158 9 argv[4] = sv_2mortal(newRV_noinc((SV *)ro));
159 }
160 9 r = pcx_call_meth(aTHX_ app, "route", argv, 5, 1);
161 9 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 9 0 9
162 }
163 }
164 }
165  
166 # The paths the plugin serves, for an observer that wants to skip them.
167 void
168 paths(class, app)
169 SV *class
170 SV *app
171 PPCODE:
172 {
173 2 HV *h = app_hv(aTHX_ app);
174 2 ▶ SV *own = h ? app_get(aTHX_ h, "health_paths") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *own = h ? app_get(aTHX_ h, "health_paths") : NULL; 2 0 2
175 2 ▶ AV *av = (own && SvROK(own) && SvTYPE(SvRV(own)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
AV *av = (own && SvROK(own) && SvTYPE(SvRV(own)) == SVt_PVAV) 2 0 2
AV *av = (own && SvROK(own) && SvTYPE(SvRV(own)) == SVt_PVAV) 2 0 2
176 4 ▶ ? (AV *)SvRV(own) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(own) : NULL; 2 0 2
177 2 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 2 0 2
178 PERL_UNUSED_VAR(class);
179 2 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 2 0 2
EXTEND(SP, n); 0 2 2
180 5 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 3 2 5
181 3 PUSHs(sv_2mortal(newSVsv(*av_fetch(av, i, 0))));
182 }
183  
184 # _ready($app, $c) -> (ready, detail_json)
185 #
186 # The readiness pass with the response and the cache taken off it, so a test
187 # can assert what the checks decided rather than parsing a body. Private.
188 void
189 _ready(class, app, c = &PL_sv_undef)
190 SV *class
191 SV *app
192 SV *c
193 PPCODE:
194 {
195 5 SV *det = sv_2mortal(newSVpvs(""));
196 int ready;
197 PERL_UNUSED_VAR(class);
198 5 ▶ ready = ph_readiness(aTHX_ app, SvOK(c) ? c : NULL, det);
Branch: 1 branch
BranchTrueFalseTotal
ready = ph_readiness(aTHX_ app, SvOK(c) ? c : NULL, det); 0 5 5
199 /* ph_readiness runs Perl, which may have reallocated the stack. */
200 5 SP = PL_stack_base + ax - 1;
201 5 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 5 5
202 5 PUSHs(sv_2mortal(newSViv(ready)));
203 5 PUSHs(sv_2mortal(newSVsv(det)));
204 }
205  
206 # Drop a cached readiness answer, so a test does not have to sleep out a ttl.
207 void
208 _uncache(class, app)
209 SV *class
210 SV *app
211 CODE:
212 {
213 2 HV *h = app_hv(aTHX_ app);
214 PERL_UNUSED_VAR(class);
215 2 ▶ if (h) (void)hv_delete(h, "health_cached",
Branch: 1 branch
BranchTrueFalseTotal
if (h) (void)hv_delete(h, "health_cached", 2 0 2
216 (I32)(sizeof("health_cached") - 1), G_DISCARD);
217 }
« xs/blob.xs ↑ Summary xs/abi.xs »