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