| 1 |
|
| MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::Meter |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| SV * |
| 6 |
|
| new(class, ...) |
| 7 |
|
| SV *class |
| 8 |
|
| CODE: |
| 9 |
|
| { |
| 10 |
58 |
| otel_meter *m = otel_meter_new(aTHX); |
| 11 |
|
| int i; |
| 12 |
145 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
87 |
58 |
145 |
|
| 13 |
87 |
| const char *k = SvPV_nolen(ST(i)); |
| 14 |
87 |
| SV *v = ST(i + 1); |
| 15 |
87 |
▶ | if (strEQ(k, "scope_name")) m->scope_name = newSVsv(v); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(k, "scope_name")) m->scope_name = newSVsv(v); |
46 |
41 |
87 |
|
| 16 |
41 |
▶ | else if (strEQ(k, "scope_version")) m->scope_version = newSVsv(v); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "scope_version")) m->scope_version = newSVsv(v); |
0 |
41 |
41 |
|
| 17 |
41 |
▶ | else if (strEQ(k, "temporality")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "temporality")) |
23 |
18 |
41 |
|
| 18 |
23 |
| m->temporality = strEQ(SvPV_nolen(v), "delta") |
| 19 |
23 |
▶ | ? OTEL_TEMP_DELTA : OTEL_TEMP_CUMULATIVE; |
Branch: 1 branch
| Branch | True | False | Total |
| ? OTEL_TEMP_DELTA : OTEL_TEMP_CUMULATIVE; |
4 |
19 |
23 |
|
| 20 |
18 |
▶ | else if (strEQ(k, "resource") |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "resource") |
18 |
0 |
18 |
|
| 21 |
18 |
▶ | && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) { |
18 |
0 |
18 |
| && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) { |
18 |
0 |
18 |
|
| 22 |
18 |
| HV *src = (HV *)SvRV(v); |
| 23 |
|
| HE *he; |
| 24 |
18 |
| hv_iterinit(src); |
| 25 |
186 |
▶ | while ((he = hv_iternext(src))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) |
168 |
18 |
186 |
|
| 26 |
168 |
| (void)hv_store_ent(m->resource, hv_iterkeysv(he), |
| 27 |
|
| newSVsv(HeVAL(he)), 0); |
| 28 |
|
| } |
| 29 |
|
| } |
| 30 |
58 |
| RETVAL = sv_bless(newRV_noinc(newSViv(PTR2IV(m))), |
| 31 |
|
| gv_stashpv("Punk::OpenTelemetry::Meter", GV_ADD)); |
| 32 |
|
| } |
| 33 |
|
| OUTPUT: |
| 34 |
|
| RETVAL |
| 35 |
|
| |
| 36 |
|
| void |
| 37 |
|
| DESTROY(self) |
| 38 |
|
| SV *self |
| 39 |
|
| CODE: |
| 40 |
58 |
▶ | if (SvROK(self) && SvIOK(SvRV(self))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(self) && SvIOK(SvRV(self))) |
58 |
0 |
58 |
| if (SvROK(self) && SvIOK(SvRV(self))) |
58 |
0 |
58 |
|
| 41 |
58 |
| otel_meter_free(aTHX_ INT2PTR(otel_meter *, SvIV(SvRV(self)))); |
| 42 |
|
| |
| 43 |
|
| # view(match => 'http.*', name => ..., aggregation => 'drop'|'sum'| |
| 44 |
|
| # 'last_value'|'histogram'|'exponential', keys => [...], bounds => [...]) |
| 45 |
|
| # |
| 46 |
|
| # Dropping attribute KEYS is the primary tool against cardinality, and is |
| 47 |
|
| # worth reaching for before the hard cap has to do its job. |
| 48 |
|
| IV |
| 49 |
|
| view(self, ...) |
| 50 |
|
| SV *self |
| 51 |
|
| CODE: |
| 52 |
|
| { |
| 53 |
12 |
| otel_meter *m = INT2PTR(otel_meter *, SvIV(SvRV(self))); |
| 54 |
|
| otel_view *v; |
| 55 |
|
| int i; |
| 56 |
12 |
▶ | if (m->nviews >= OTEL_MAX_VIEWS) XSRETURN_IV(0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->nviews >= OTEL_MAX_VIEWS) XSRETURN_IV(0); |
0 |
12 |
12 |
|
| 57 |
12 |
| v = &m->views[m->nviews]; |
| 58 |
12 |
| Zero(v, 1, otel_view); |
| 59 |
12 |
| v->aggregation = -1; |
| 60 |
36 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
24 |
12 |
36 |
|
| 61 |
24 |
| const char *k = SvPV_nolen(ST(i)); |
| 62 |
24 |
| SV *val = ST(i + 1); |
| 63 |
24 |
▶ | if (strEQ(k, "match")) v->match = newSVsv(val); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(k, "match")) v->match = newSVsv(val); |
12 |
12 |
24 |
|
| 64 |
12 |
▶ | else if (strEQ(k, "name")) v->name = newSVsv(val); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "name")) v->name = newSVsv(val); |
2 |
10 |
12 |
|
| 65 |
10 |
▶ | else if (strEQ(k, "aggregation")) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "aggregation")) { |
6 |
4 |
10 |
|
| 66 |
6 |
| const char *a = SvPV_nolen(val); |
| 67 |
6 |
| v->aggregation = strEQ(a, "drop") ? OTEL_AGG_DROP |
| 68 |
8 |
▶ | : strEQ(a, "sum") ? OTEL_AGG_SUM |
Branch: 1 branch
| Branch | True | False | Total |
| : strEQ(a, "sum") ? OTEL_AGG_SUM |
2 |
4 |
6 |
|
| 69 |
4 |
▶ | : strEQ(a, "last_value") ? OTEL_AGG_LASTVALUE |
Branch: 1 branch
| Branch | True | False | Total |
| : strEQ(a, "last_value") ? OTEL_AGG_LASTVALUE |
2 |
0 |
2 |
|
| 70 |
4 |
▶ | : strEQ(a, "exponential") ? OTEL_AGG_EXPO |
Branch: 1 branch
| Branch | True | False | Total |
| : strEQ(a, "exponential") ? OTEL_AGG_EXPO |
2 |
0 |
2 |
|
| 71 |
2 |
▶ | : OTEL_AGG_HISTOGRAM; |
Branch: 1 branch
| Branch | True | False | Total |
| : OTEL_AGG_HISTOGRAM; |
2 |
0 |
2 |
|
| 72 |
|
| } |
| 73 |
4 |
▶ | else if (strEQ(k, "keys") |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "keys") |
2 |
2 |
4 |
|
| 74 |
4 |
▶ | && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
2 |
0 |
2 |
| && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
2 |
0 |
2 |
|
| 75 |
2 |
| AV *av = (AV *)SvRV(val); |
| 76 |
2 |
| SSize_t j, n = av_len(av) + 1; |
| 77 |
2 |
| v->keys = newHV(); |
| 78 |
2 |
| v->has_keys = 1; |
| 79 |
4 |
▶ | for (j = 0; j < n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < n; j++) { |
2 |
2 |
4 |
|
| 80 |
2 |
| SV **e = av_fetch(av, j, 0); |
| 81 |
2 |
▶ | if (e && *e) (void)hv_store_ent(v->keys, *e, newSViv(1), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && *e) (void)hv_store_ent(v->keys, *e, newSViv(1), 0); |
2 |
0 |
2 |
| if (e && *e) (void)hv_store_ent(v->keys, *e, newSViv(1), 0); |
2 |
0 |
2 |
|
| 82 |
|
| } |
| 83 |
|
| } |
| 84 |
2 |
▶ | else if (strEQ(k, "bounds") |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "bounds") |
2 |
0 |
2 |
|
| 85 |
2 |
▶ | && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
2 |
0 |
2 |
| && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
2 |
0 |
2 |
|
| 86 |
2 |
| AV *av = (AV *)SvRV(val); |
| 87 |
2 |
| SSize_t j, n = av_len(av) + 1; |
| 88 |
2 |
▶ | if (n > OTEL_MAX_BOUNDS) n = OTEL_MAX_BOUNDS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > OTEL_MAX_BOUNDS) n = OTEL_MAX_BOUNDS; |
0 |
2 |
2 |
|
| 89 |
8 |
▶ | for (j = 0; j < n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < n; j++) { |
6 |
2 |
8 |
|
| 90 |
6 |
| SV **e = av_fetch(av, j, 0); |
| 91 |
6 |
▶ | v->bounds[j] = (e && *e) ? SvNV(*e) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| v->bounds[j] = (e && *e) ? SvNV(*e) : 0; |
6 |
0 |
6 |
| v->bounds[j] = (e && *e) ? SvNV(*e) : 0; |
6 |
0 |
6 |
|
| 92 |
|
| } |
| 93 |
2 |
| v->nbounds = (int)n; |
| 94 |
|
| } |
| 95 |
|
| } |
| 96 |
12 |
▶ | if (!v->match) v->match = newSVpvs("*"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v->match) v->match = newSVpvs("*"); |
0 |
12 |
12 |
|
| 97 |
12 |
| m->nviews++; |
| 98 |
12 |
▶ | RETVAL = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 1; |
0 |
12 |
12 |
|
| 99 |
|
| } |
| 100 |
|
| OUTPUT: |
| 101 |
|
| RETVAL |
| 102 |
|
| |
| 103 |
|
| # record($name, $kind, $value, \%attributes, $span) |
| 104 |
|
| # |
| 105 |
|
| # $kind: 1 counter, 2 updowncounter, 3 histogram, 4 gauge. |
| 106 |
|
| # $span is optional and is what makes an exemplar possible: the default |
| 107 |
|
| # filter is trace_based, so with no sampled span there is no exemplar, an |
| 108 |
|
| # exemplar pointing at a trace nobody recorded being a pointer to nothing. |
| 109 |
|
| void |
| 110 |
|
| record(self, name, kind, value, attrs = &PL_sv_undef, span = &PL_sv_undef) |
| 111 |
|
| SV *self |
| 112 |
|
| SV *name |
| 113 |
|
| IV kind |
| 114 |
|
| double value |
| 115 |
|
| SV *attrs |
| 116 |
|
| SV *span |
| 117 |
|
| CODE: |
| 118 |
|
| { |
| 119 |
5384 |
| otel_meter *m = INT2PTR(otel_meter *, SvIV(SvRV(self))); |
| 120 |
|
| otel_instrument *in; |
| 121 |
5384 |
▶ | HV *a = (SvROK(attrs) && SvTYPE(SvRV(attrs)) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| HV *a = (SvROK(attrs) && SvTYPE(SvRV(attrs)) == SVt_PVHV) |
5384 |
0 |
5384 |
|
| 122 |
10768 |
▶ | ? (HV *)SvRV(attrs) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(attrs) : NULL; |
5384 |
0 |
5384 |
|
| 123 |
4 |
▶ | otel_span *sp = (SvROK(span) && SvIOK(SvRV(span)) && SvIV(SvRV(span))) |
Branch: 2 branches
| Branch | True | False | Total |
| otel_span *sp = (SvROK(span) && SvIOK(SvRV(span)) && SvIV(SvRV(span))) |
4 |
0 |
4 |
| otel_span *sp = (SvROK(span) && SvIOK(SvRV(span)) && SvIV(SvRV(span))) |
4 |
0 |
4 |
|
| 124 |
5388 |
▶ | ? INT2PTR(otel_span *, SvIV(SvRV(span))) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? INT2PTR(otel_span *, SvIV(SvRV(span))) : NULL; |
4 |
5380 |
5384 |
|
| 125 |
5384 |
| otel_meter_check_fork(aTHX_ m); |
| 126 |
5384 |
| in = otel_meter_instrument(aTHX_ m, name, (int)kind, NULL, NULL); |
| 127 |
5384 |
| a = otel_view_filter(aTHX_ m, name, a); |
| 128 |
5384 |
| otel_instr_record(aTHX_ in, value, a, sp); |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| # Collect everything as an OTLP metrics payload, or undef when nothing has |
| 132 |
|
| # been recorded. Under DELTA the accumulators reset and the next interval |
| 133 |
|
| # starts now; under CUMULATIVE they do not, and each series keeps its original |
| 134 |
|
| # start timestamp - which is the whole distinction and what a backend notices |
| 135 |
|
| # when it is wrong. |
| 136 |
|
| SV * |
| 137 |
|
| collect(self) |
| 138 |
|
| SV *self |
| 139 |
|
| CODE: |
| 140 |
|
| { |
| 141 |
49 |
| otel_meter *m = INT2PTR(otel_meter *, SvIV(SvRV(self))); |
| 142 |
49 |
| otel_meter_check_fork(aTHX_ m); |
| 143 |
49 |
| RETVAL = otel_meter_collect(aTHX_ m); |
| 144 |
49 |
▶ | if (!RETVAL) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) XSRETURN_UNDEF; |
4 |
45 |
49 |
|
| 145 |
|
| } |
| 146 |
|
| OUTPUT: |
| 147 |
|
| RETVAL |
| 148 |
|
| |
| 149 |
|
| # conflicts: streams that ended up with the same name but a different unit, |
| 150 |
|
| # kind or aggregation. A backend resolves that silently, by storing whichever |
| 151 |
|
| # arrived last, so it is counted here and reported rather than ignored. |
| 152 |
|
| IV |
| 153 |
|
| conflicts(self) |
| 154 |
|
| SV *self |
| 155 |
|
| CODE: |
| 156 |
|
| { |
| 157 |
4 |
| otel_meter *m = INT2PTR(otel_meter *, SvIV(SvRV(self))); |
| 158 |
4 |
▶ | RETVAL = m->conflicts; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = m->conflicts; |
0 |
4 |
4 |
|
| 159 |
|
| } |
| 160 |
|
| OUTPUT: |
| 161 |
|
| RETVAL |
| 162 |
|
| |
| 163 |
|
| # (instruments, series, overflow) - overflow being attribute sets past the |
| 164 |
|
| # cardinality cap, which is the number that says a metric has stopped being |
| 165 |
|
| # useful before memory says it more loudly. |
| 166 |
|
| void |
| 167 |
|
| stats(self) |
| 168 |
|
| SV *self |
| 169 |
|
| PPCODE: |
| 170 |
|
| { |
| 171 |
4 |
| otel_meter *m = INT2PTR(otel_meter *, SvIV(SvRV(self))); |
| 172 |
|
| otel_instrument *in; |
| 173 |
4 |
| IV ni = 0, ns = 0, ov = 0; |
| 174 |
8 |
▶ | for (in = m->instruments; in; in = in->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (in = m->instruments; in; in = in->next) { |
4 |
4 |
8 |
|
| 175 |
4 |
| ni++; |
| 176 |
4 |
| ns += in->npoints; |
| 177 |
4 |
| ov += in->overflow; |
| 178 |
|
| } |
| 179 |
4 |
▶ | EXTEND(SP, 6); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 6); |
0 |
4 |
4 |
|
| 180 |
4 |
| mPUSHp("instruments", 11); mPUSHi(ni); |
| 181 |
4 |
| mPUSHp("series", 6); mPUSHi(ns); |
| 182 |
4 |
| mPUSHp("overflow", 8); mPUSHi(ov); |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::Expo |
| 186 |
|
| |
| 187 |
|
| PROTOTYPES: DISABLE |
| 188 |
|
| |
| 189 |
|
| # The exponential histogram, reachable directly. It is the one piece of |
| 190 |
|
| # numeric code here where being slightly wrong produces a plausible number |
| 191 |
|
| # rather than an obvious failure, so the tests drive it as property tests |
| 192 |
|
| # rather than through a metric that happens to look right. |
| 193 |
|
| |
| 194 |
|
| SV * |
| 195 |
|
| new(class, scale = 20) |
| 196 |
|
| SV *class |
| 197 |
|
| int scale |
| 198 |
|
| CODE: |
| 199 |
|
| { |
| 200 |
|
| otel_expo *h; |
| 201 |
20 |
| Newxz(h, 1, otel_expo); |
| 202 |
20 |
| otel_expo_init(h, scale); |
| 203 |
20 |
| RETVAL = sv_bless(newRV_noinc(newSViv(PTR2IV(h))), |
| 204 |
|
| gv_stashpv("Punk::OpenTelemetry::Expo", GV_ADD)); |
| 205 |
|
| } |
| 206 |
|
| OUTPUT: |
| 207 |
|
| RETVAL |
| 208 |
|
| |
| 209 |
|
| void |
| 210 |
|
| DESTROY(self) |
| 211 |
|
| SV *self |
| 212 |
|
| CODE: |
| 213 |
20 |
▶ | if (SvROK(self) && SvIOK(SvRV(self))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(self) && SvIOK(SvRV(self))) |
20 |
0 |
20 |
| if (SvROK(self) && SvIOK(SvRV(self))) |
20 |
0 |
20 |
|
| 214 |
20 |
| Safefree(INT2PTR(otel_expo *, SvIV(SvRV(self)))); |
| 215 |
|
| |
| 216 |
|
| void |
| 217 |
|
| record(self, v) |
| 218 |
|
| SV *self |
| 219 |
|
| double v |
| 220 |
|
| CODE: |
| 221 |
4612 |
| otel_expo_record(INT2PTR(otel_expo *, SvIV(SvRV(self))), v); |
| 222 |
|
| |
| 223 |
|
| void |
| 224 |
|
| downscale(self, by) |
| 225 |
|
| SV *self |
| 226 |
|
| int by |
| 227 |
|
| CODE: |
| 228 |
2 |
| otel_expo_downscale(INT2PTR(otel_expo *, SvIV(SvRV(self))), by); |
| 229 |
|
| |
| 230 |
|
| void |
| 231 |
|
| merge(self, other) |
| 232 |
|
| SV *self |
| 233 |
|
| SV *other |
| 234 |
|
| CODE: |
| 235 |
2 |
| otel_expo_merge(INT2PTR(otel_expo *, SvIV(SvRV(self))), |
| 236 |
2 |
| INT2PTR(otel_expo *, SvIV(SvRV(other)))); |
| 237 |
|
| |
| 238 |
|
| # (count, sum, scale, zero_count, total, pos_len, neg_len, pos_offset) |
| 239 |
|
| # |
| 240 |
|
| # `total` is every bucket plus the zeros: the invariant a downscale or a merge |
| 241 |
|
| # must never change. A downscale that loses a count is a silently wrong |
| 242 |
|
| # percentile, which is the worst thing a histogram can be - it still looks |
| 243 |
|
| # like data. |
| 244 |
|
| void |
| 245 |
|
| state(self) |
| 246 |
|
| SV *self |
| 247 |
|
| PPCODE: |
| 248 |
|
| { |
| 249 |
30 |
| otel_expo *h = INT2PTR(otel_expo *, SvIV(SvRV(self))); |
| 250 |
30 |
▶ | EXTEND(SP, 16); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 16); |
0 |
30 |
30 |
|
| 251 |
30 |
| mPUSHp("count", 5); mPUSHi(h->count); |
| 252 |
30 |
| mPUSHp("sum", 3); mPUSHn(h->sum); |
| 253 |
30 |
| mPUSHp("scale", 5); mPUSHi(h->scale); |
| 254 |
30 |
| mPUSHp("zero_count", 10);mPUSHi(h->zero_count); |
| 255 |
30 |
| mPUSHp("total", 5); mPUSHi(otel_expo_total(h)); |
| 256 |
30 |
| mPUSHp("pos_len", 7); mPUSHi(h->pos.len); |
| 257 |
30 |
| mPUSHp("neg_len", 7); mPUSHi(h->neg.len); |
| 258 |
30 |
| mPUSHp("pos_offset", 10);mPUSHi(h->pos.offset); |
| 259 |
|
| } |