xs/tracer.xs

filter: Statementstmt 74.5% 123 / 165 Branchbran 43.6% 110 / 252 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.8% 233 / 417 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/grpc.xs ↑ Summary xs/schema.xs »
1 MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::Tracer
2  
3 PROTOTYPES: DISABLE
4  
5 # The tracer. One per process; spans are C structs and an unsampled span is
6 # never built at all.
7  
8 SV *
9 new(class, ...)
10 SV *class
11 CODE:
12 {
13 50 otel_tracer *t = otel_tracer_new(aTHX);
14 int i;
15 206 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 156 50 206
16 156 const char *k = SvPV_nolen(ST(i));
17 156 SV *v = ST(i + 1);
18 156 ▶ if (strEQ(k, "scope_name")) t->scope_name = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "scope_name")) t->scope_name = newSVsv(v); 23 133 156
19 133 ▶ else if (strEQ(k, "scope_version")) t->scope_version = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "scope_version")) t->scope_version = newSVsv(v); 16 117 133
20 117 ▶ else if (strEQ(k, "schema_url")) t->schema_url = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "schema_url")) t->schema_url = newSVsv(v); 16 101 117
21 101 ▶ else if (strEQ(k, "scope_schema_url"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "scope_schema_url")) 16 85 101
22 16 t->scope_schema_url = newSVsv(v);
23 85 ▶ else if (strEQ(k, "resource")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "resource")) { 21 64 85
24 21 ▶ if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) { 21 0 21
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) { 21 0 21
25 21 HV *src = (HV *)SvRV(v);
26 HE *he;
27 21 hv_iterinit(src);
28 202 ▶ while ((he = hv_iternext(src)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) 181 21 202
29 181 (void)hv_store_ent(t->resource, hv_iterkeysv(he),
30 newSVsv(HeVAL(he)), 0);
31 }
32 }
33 64 ▶ else if (strEQ(k, "sampler")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "sampler")) { 43 21 64
34 43 const char *s = SvPV_nolen(v);
35 86 int kind = strEQ(s, "always_on") ? OTEL_SAMPLER_ALWAYS_ON
36 61 ▶ : strEQ(s, "always_off") ? OTEL_SAMPLER_ALWAYS_OFF
Branch: 1 branch
BranchTrueFalseTotal
: strEQ(s, "always_off") ? OTEL_SAMPLER_ALWAYS_OFF 18 25 43
37 18 ▶ : OTEL_SAMPLER_PARENT_RATIO;
Branch: 1 branch
BranchTrueFalseTotal
: OTEL_SAMPLER_PARENT_RATIO; 3 15 18
38 43 otel_sampler_init(&t->sampler, kind, t->sampler.ratio);
39 }
40 21 ▶ else if (strEQ(k, "ratio"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "ratio")) 21 0 21
41 21 otel_sampler_init(&t->sampler, t->sampler.kind, SvNV(v));
42 }
43 50 RETVAL = sv_bless(newRV_noinc(newSViv(PTR2IV(t))),
44 gv_stashpv("Punk::OpenTelemetry::Tracer", GV_ADD));
45 }
46 OUTPUT:
47 RETVAL
48  
49 void
50 DESTROY(self)
51 SV *self
52 CODE:
53 50 ▶ if (SvROK(self) && SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self) && SvIOK(SvRV(self))) 50 0 50
if (SvROK(self) && SvIOK(SvRV(self))) 50 0 50
54 50 otel_tracer_free(aTHX_ INT2PTR(otel_tracer *, SvIV(SvRV(self))));
55  
56 # start($name, %opt) -> a span object, or undef when the trace is not sampled.
57 #
58 # An undef return is the SUCCESS path for an unsampled trace, not an error:
59 # at a 1% ratio the other 99% of requests allocate nothing, which is the whole reason
60 # sampling is worth having. Callers must cope with undef.
61 #
62 # Options: kind, parent (a { trace_id, span_id, sampled } hashref from an
63 # extracted inbound context).
64 SV *
65 start(self, name, ...)
66 SV *self
67 SV *name
68 CODE:
69 {
70 7204 otel_tracer *t = INT2PTR(otel_tracer *, SvIV(SvRV(self)));
71 unsigned char ptid[16], psid[8];
72 7204 const unsigned char *ptp = NULL, *psp = NULL;
73 7204 int kind = OTEL_KIND_INTERNAL, psampled = 0, i;
74 otel_span *s;
75  
76 7276 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 72 7204 7276
77 72 const char *k = SvPV_nolen(ST(i));
78 72 SV *v = ST(i + 1);
79 72 ▶ if (strEQ(k, "kind")) kind = (int)SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "kind")) kind = (int)SvIV(v); 1 71 72
80 71 ▶ else if (strEQ(k, "parent") && SvROK(v)
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "parent") && SvROK(v) 71 0 71
else if (strEQ(k, "parent") && SvROK(v) 71 0 71
81 71 ▶ && SvTYPE(SvRV(v)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(v)) == SVt_PVHV) { 71 0 71
82 71 HV *p = (HV *)SvRV(v);
83 71 SV **tp = hv_fetchs(p, "trace_id", 0);
84 71 SV **sp = hv_fetchs(p, "span_id", 0);
85 71 SV **fl = hv_fetchs(p, "sampled", 0);
86 STRLEN tl, sl;
87 const char *ts, *ss;
88 71 ▶ if (tp && *tp && sp && *sp) {
Branch: 4 branches
BranchTrueFalseTotal
if (tp && *tp && sp && *sp) { 71 0 71
if (tp && *tp && sp && *sp) { 71 0 71
if (tp && *tp && sp && *sp) { 71 0 71
if (tp && *tp && sp && *sp) { 71 0 71
89 71 ts = SvPV_const(*tp, tl);
90 71 ▶ ss = SvPV_const(*sp, sl);
Branch: 1 branch
BranchTrueFalseTotal
ss = SvPV_const(*sp, sl); 71 0 71
91 /* an unparseable or all-zero id is treated as ABSENT
92 * rather than as a parent: a span claiming a parent that
93 * cannot exist is worse than a root span */
94 71 ▶ if (otel_hex_to_bytes(ts, tl, ptid, 16)
Branch: 1 branch
BranchTrueFalseTotal
if (otel_hex_to_bytes(ts, tl, ptid, 16) 67 4 71
95 67 ▶ && otel_hex_to_bytes(ss, sl, psid, 8)) {
Branch: 1 branch
BranchTrueFalseTotal
&& otel_hex_to_bytes(ss, sl, psid, 8)) { 66 1 67
96 66 ptp = ptid; psp = psid;
97 66 ▶ psampled = (fl && *fl && SvTRUE(*fl)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
psampled = (fl && *fl && SvTRUE(*fl)) ? 1 : 0; 66 0 66
psampled = (fl && *fl && SvTRUE(*fl)) ? 1 : 0; 66 0 66
psampled = (fl && *fl && SvTRUE(*fl)) ? 1 : 0; 64 2 66
98 }
99 }
100 }
101 }
102 7204 s = otel_tracer_start(aTHX_ t, ptp, psp, psampled, name, kind);
103 7204 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 3810 3394 7204
104 3394 RETVAL = sv_bless(newRV_noinc(newSViv(PTR2IV(s))),
105 gv_stashpv("Punk::OpenTelemetry::Span", GV_ADD));
106 }
107 OUTPUT:
108 RETVAL
109  
110 # Move an ended span onto the export queue.
111 void
112 enqueue(self, span)
113 SV *self
114 SV *span
115 CODE:
116 {
117 2519 otel_tracer *t = INT2PTR(otel_tracer *, SvIV(SvRV(self)));
118 otel_span *s;
119 2519 ▶ if (!(SvROK(span) && SvIOK(SvRV(span)))) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(span) && SvIOK(SvRV(span)))) XSRETURN_EMPTY; 2519 0 2519
if (!(SvROK(span) && SvIOK(SvRV(span)))) XSRETURN_EMPTY; 0 2519 2519
120 2519 s = INT2PTR(otel_span *, SvIV(SvRV(span)));
121 2519 otel_span_end(aTHX_ s);
122 2519 otel_tracer_enqueue(aTHX_ t, s);
123 2519 t->ended++;
124 /* the queue owns it now; blank the handle so DESTROY cannot free it
125 * a second time */
126 2519 sv_setiv(SvRV(span), 0);
127 }
128  
129 # Up to $max queued spans as an OTLP payload, or undef when there is nothing
130 # to send - which is the common case and must not allocate.
131 SV *
132 drain(self, max = OTEL_BATCH_MAX)
133 SV *self
134 int max
135 CODE:
136 {
137 17 otel_tracer *t = INT2PTR(otel_tracer *, SvIV(SvRV(self)));
138 17 ▶ SV *p = otel_tracer_drain(aTHX_ t, max > 0 ? max : OTEL_BATCH_MAX);
Branch: 1 branch
BranchTrueFalseTotal
SV *p = otel_tracer_drain(aTHX_ t, max > 0 ? max : OTEL_BATCH_MAX); 17 0 17
139 17 ▶ if (!p) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) XSRETURN_UNDEF; 3 14 17
140 14 RETVAL = p;
141 }
142 OUTPUT:
143 RETVAL
144  
145 IV
146 queued(self)
147 SV *self
148 CODE:
149 {
150 13 otel_tracer *t = INT2PTR(otel_tracer *, SvIV(SvRV(self)));
151 /* honest across a fork: a child inherited the struct but owns none
152 * of those spans, and reporting the parent's count would be a lie
153 * a reader would act on */
154 13 otel_tracer_check_fork(aTHX_ t);
155 13 ▶ RETVAL = t->qcount;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = t->qcount; 4 9 13
156 }
157 OUTPUT:
158 RETVAL
159  
160 # Set one resource attribute after construction. The reason this exists is
161 # service.instance.id: the resource is normally built at boot, in the parent,
162 # and a prefork worker MUST NOT export under the same instance id as its
163 # siblings - a collector seeing several contradictory cumulative series under
164 # one identity resolves it wrongly and invisibly. A worker refreshes it from
165 # the post-fork hook.
166 SV *
167 resource_attr(self, key, value)
168 SV *self
169 SV *key
170 SV *value
171 CODE:
172 {
173 1 otel_tracer *t = INT2PTR(otel_tracer *, SvIV(SvRV(self)));
174 1 (void)hv_store_ent(t->resource, key, newSVsv(value), 0);
175 1 RETVAL = newSVsv(self);
176 }
177 OUTPUT:
178 RETVAL
179  
180 # started, ended, dropped (queue overflow), sampled_out, queued
181 void
182 stats(self)
183 SV *self
184 PPCODE:
185 {
186 3 otel_tracer *t = INT2PTR(otel_tracer *, SvIV(SvRV(self)));
187 3 otel_tracer_check_fork(aTHX_ t);
188 3 ▶ EXTEND(SP, 10);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 10); 0 3 3
189 3 mPUSHp("started", 7); mPUSHi(t->started);
190 3 mPUSHp("ended", 5); mPUSHi(t->ended);
191 3 mPUSHp("dropped", 7); mPUSHi(t->dropped_queue);
192 3 mPUSHp("sampled_out", 11); mPUSHi(t->sampled_out);
193 3 mPUSHp("queued", 6); mPUSHi(t->qcount);
194 }
195  
196 MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::Span
197  
198 PROTOTYPES: DISABLE
199  
200 # A span handle. Every method tolerates a span whose tracer has already taken
201 # it, so a caller that ends twice or annotates after ending gets a no-op
202 # rather than a crash.
203  
204 #define OTEL_SPAN_OF(sv) \
205 ((SvROK(sv) && SvIOK(SvRV(sv)) && SvIV(SvRV(sv))) \
206 ? INT2PTR(otel_span *, SvIV(SvRV(sv))) : NULL)
207  
208 void
209 DESTROY(self)
210 SV *self
211 CODE:
212 {
213 3394 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 3394 0 3394
otel_span *s = OTEL_SPAN_OF(self); 3394 0 3394
otel_span *s = OTEL_SPAN_OF(self); 875 2519 3394
214 3394 ▶ if (s) otel_span_free(aTHX_ s);
Branch: 1 branch
BranchTrueFalseTotal
if (s) otel_span_free(aTHX_ s); 875 2519 3394
215 }
216  
217 SV *
218 attr(self, key, value)
219 SV *self
220 SV *key
221 SV *value
222 CODE:
223 {
224 500 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 500 0 500
otel_span *s = OTEL_SPAN_OF(self); 500 0 500
otel_span *s = OTEL_SPAN_OF(self); 500 0 500
225 500 ▶ if (s) otel_span_attr(aTHX_ s, key, value);
Branch: 1 branch
BranchTrueFalseTotal
if (s) otel_span_attr(aTHX_ s, key, value); 500 0 500
226 500 RETVAL = newSVsv(self); /* chainable */
227 }
228 OUTPUT:
229 RETVAL
230  
231 SV *
232 event(self, name, attrs = &PL_sv_undef)
233 SV *self
234 SV *name
235 SV *attrs
236 CODE:
237 {
238 200 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 200 0 200
otel_span *s = OTEL_SPAN_OF(self); 200 0 200
otel_span *s = OTEL_SPAN_OF(self); 200 0 200
239 0 ▶ HV *a = (SvROK(attrs) && SvTYPE(SvRV(attrs)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *a = (SvROK(attrs) && SvTYPE(SvRV(attrs)) == SVt_PVHV) 0 0 0
240 200 ▶ ? (HV *)SvRV(attrs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(attrs) : NULL; 0 200 200
241 200 ▶ if (s) otel_span_event(aTHX_ s, name, a);
Branch: 1 branch
BranchTrueFalseTotal
if (s) otel_span_event(aTHX_ s, name, a); 200 0 200
242 200 RETVAL = newSVsv(self);
243 }
244 OUTPUT:
245 RETVAL
246  
247 SV *
248 link(self, trace_id, span_id, attrs = &PL_sv_undef)
249 SV *self
250 SV *trace_id
251 SV *span_id
252 SV *attrs
253 CODE:
254 {
255 200 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 200 0 200
otel_span *s = OTEL_SPAN_OF(self); 200 0 200
otel_span *s = OTEL_SPAN_OF(self); 200 0 200
256 unsigned char tid[16], sid[8];
257 STRLEN tl, sl;
258 200 ▶ const char *ts = SvPV_const(trace_id, tl);
Branch: 1 branch
BranchTrueFalseTotal
const char *ts = SvPV_const(trace_id, tl); 200 0 200
259 200 const char *ss = SvPV_const(span_id, sl);
260 0 ▶ HV *a = (SvROK(attrs) && SvTYPE(SvRV(attrs)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *a = (SvROK(attrs) && SvTYPE(SvRV(attrs)) == SVt_PVHV) 0 0 0
261 200 ▶ ? (HV *)SvRV(attrs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(attrs) : NULL; 0 200 200
262 200 ▶ if (s && otel_hex_to_bytes(ts, tl, tid, 16)
Branch: 2 branches
BranchTrueFalseTotal
if (s && otel_hex_to_bytes(ts, tl, tid, 16) 200 0 200
if (s && otel_hex_to_bytes(ts, tl, tid, 16) 200 0 200
263 200 ▶ && otel_hex_to_bytes(ss, sl, sid, 8))
Branch: 1 branch
BranchTrueFalseTotal
&& otel_hex_to_bytes(ss, sl, sid, 8)) 200 0 200
264 200 otel_span_link(aTHX_ s, tid, sid, a);
265 200 RETVAL = newSVsv(self);
266 }
267 OUTPUT:
268 RETVAL
269  
270 # status($code, $message). Only the application may set OK; instrumentation
271 # sets ERROR or leaves it UNSET, because a layer with no opinion about whether
272 # an operation succeeded must not claim one.
273 SV *
274 status(self, code, message = &PL_sv_undef)
275 SV *self
276 IV code
277 SV *message
278 CODE:
279 {
280 1 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
281 1 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 1 0 1
282 1 s->status_code = (int)code;
283 1 ▶ if (s->status_message) SvREFCNT_dec(s->status_message);
Branch: 1 branch
BranchTrueFalseTotal
if (s->status_message) SvREFCNT_dec(s->status_message); 0 1 1
284 1 ▶ s->status_message = SvOK(message) ? newSVsv(message) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
s->status_message = SvOK(message) ? newSVsv(message) : NULL; 1 0 1
285 }
286 1 RETVAL = newSVsv(self);
287 }
288 OUTPUT:
289 RETVAL
290  
291 void
292 end(self)
293 SV *self
294 CODE:
295 {
296 1 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
297 1 ▶ if (s) otel_span_end(aTHX_ s);
Branch: 1 branch
BranchTrueFalseTotal
if (s) otel_span_end(aTHX_ s); 1 0 1
298 }
299  
300 SV *
301 trace_id(self)
302 SV *self
303 CODE:
304 {
305 209 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 209 0 209
otel_span *s = OTEL_SPAN_OF(self); 209 0 209
otel_span *s = OTEL_SPAN_OF(self); 209 0 209
306 char hex[32];
307 209 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 209 209
308 209 otel_bytes_to_hex(s->trace_id, 16, hex);
309 209 RETVAL = newSVpvn(hex, 32);
310 }
311 OUTPUT:
312 RETVAL
313  
314 SV *
315 span_id(self)
316 SV *self
317 CODE:
318 {
319 206 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 206 0 206
otel_span *s = OTEL_SPAN_OF(self); 206 0 206
otel_span *s = OTEL_SPAN_OF(self); 206 0 206
320 char hex[16];
321 206 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 206 206
322 206 otel_bytes_to_hex(s->span_id, 8, hex);
323 206 RETVAL = newSVpvn(hex, 16);
324 }
325 OUTPUT:
326 RETVAL
327  
328 # The sampled flag, which is the third thing a `traceparent` needs and the
329 # only one of the three that had no accessor. A caller propagating a context
330 # by hand had the trace id and the span id and no way to ask this, so it
331 # either guessed - and a guess of "sampled" makes the callee keep spans whose
332 # parent was dropped - or died reaching for a method that was not there.
333 int
334 sampled(self)
335 SV *self
336 CODE:
337 {
338 0 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 0 0 0
otel_span *s = OTEL_SPAN_OF(self); 0 0 0
otel_span *s = OTEL_SPAN_OF(self); 0 0 0
339 0 ▶ RETVAL = s ? s->sampled : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = s ? s->sampled : 0; 0 0 0
RETVAL = s ? s->sampled : 0; 0 0 0
340 }
341 OUTPUT:
342 RETVAL
343  
344 # The parent shape `start` takes, so a child span is one call rather than
345 # three and cannot be assembled wrongly.
346 SV *
347 child_of(self)
348 SV *self
349 CODE:
350 {
351 0 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 0 0 0
otel_span *s = OTEL_SPAN_OF(self); 0 0 0
otel_span *s = OTEL_SPAN_OF(self); 0 0 0
352 char thex[32], shex[16];
353 HV *h;
354 0 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 0 0
355 0 otel_bytes_to_hex(s->trace_id, 16, thex);
356 0 otel_bytes_to_hex(s->span_id, 8, shex);
357 0 h = newHV();
358 0 hv_stores(h, "trace_id", newSVpvn(thex, 32));
359 0 hv_stores(h, "span_id", newSVpvn(shex, 16));
360 0 hv_stores(h, "sampled", newSViv(s->sampled));
361 0 RETVAL = newRV_noinc((SV *)h);
362 }
363 OUTPUT:
364 RETVAL
365  
366 # The W3C trace context header for this span, which is what an outbound call
367 # has to carry for the callee's server span to join this trace.
368 SV *
369 traceparent(self)
370 SV *self
371 CODE:
372 {
373 1 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
otel_span *s = OTEL_SPAN_OF(self); 1 0 1
374 char thex[32], shex[16], out[64];
375 1 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 1 1
376 1 otel_bytes_to_hex(s->trace_id, 16, thex);
377 1 otel_bytes_to_hex(s->span_id, 8, shex);
378 /* version-traceid-parentid-flags, and the parent id is THIS span:
379 * the callee is a child of the span that made the call. */
380 1 (void)snprintf(out, sizeof(out), "00-%.32s-%.16s-%02x",
381 1 thex, shex, s->sampled ? 1 : 0);
382 1 RETVAL = newSVpv(out, 0);
383 }
384 OUTPUT:
385 RETVAL
386  
387 # The span as the payload shape the encoders take, without queueing it.
388 SV *
389 to_hash(self)
390 SV *self
391 CODE:
392 {
393 13 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 13 0 13
otel_span *s = OTEL_SPAN_OF(self); 13 0 13
otel_span *s = OTEL_SPAN_OF(self); 13 0 13
394 13 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 13 13
395 13 RETVAL = otel_span_to_hv(aTHX_ s);
396 }
397 OUTPUT:
398 RETVAL
399  
400 void
401 counts(self)
402 SV *self
403 PPCODE:
404 {
405 4 ▶ otel_span *s = OTEL_SPAN_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_SPAN_OF(self); 4 0 4
otel_span *s = OTEL_SPAN_OF(self); 4 0 4
otel_span *s = OTEL_SPAN_OF(self); 4 0 4
406 4 ▶ if (!s) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_EMPTY; 0 4 4
407 4 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 4 4
408 4 ▶ mPUSHi((IV)HvUSEDKEYS(s->attrs)); mPUSHi(s->dropped_attrs);
Branch: 1 branch
BranchTrueFalseTotal
mPUSHi((IV)HvUSEDKEYS(s->attrs)); mPUSHi(s->dropped_attrs); 0 4 4
409 4 mPUSHi(s->n_events); mPUSHi(s->dropped_events);
410 4 mPUSHi(s->n_links); mPUSHi(s->dropped_links);
411 }
412  
413 MODULE = Punk::OpenTelemetry PACKAGE = Punk::OpenTelemetry::SpanRef
414  
415 PROTOTYPES: DISABLE
416  
417 # A BORROWED span handle. Same pointer as Punk::OpenTelemetry::Span, and
418 # deliberately NOT that class, because that one frees what it points at.
419 #
420 # The instrumentation owns the request's server span: it opened it in one
421 # callback and will end and enqueue it in another. A handle to it blessed into
422 # Span would free it the moment the caller's variable went out of scope, and
423 # the response side would then end a span that had already been returned to
424 # the allocator. So this class exists to have no DESTROY at all.
425 #
426 # It answers the two questions an application actually has - what trace am I
427 # in, and what should I parent a child span to - and lets an annotation
428 # through, because annotating the request's own span is the ordinary thing to
429 # want and cannot outlive it.
430  
431 #define OTEL_REF_OF(sv) \
432 ((SvROK(sv) && SvIOK(SvRV(sv)) && SvIV(SvRV(sv))) \
433 ? INT2PTR(otel_span *, SvIV(SvRV(sv))) : NULL)
434  
435 SV *
436 trace_id(self)
437 SV *self
438 CODE:
439 {
440 0 ▶ otel_span *s = OTEL_REF_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
441 char hex[32];
442 0 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 0 0
443 0 otel_bytes_to_hex(s->trace_id, 16, hex);
444 0 RETVAL = newSVpvn(hex, 32);
445 }
446 OUTPUT:
447 RETVAL
448  
449 SV *
450 span_id(self)
451 SV *self
452 CODE:
453 {
454 0 ▶ otel_span *s = OTEL_REF_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
455 char hex[16];
456 0 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 0 0
457 0 otel_bytes_to_hex(s->span_id, 8, hex);
458 0 RETVAL = newSVpvn(hex, 16);
459 }
460 OUTPUT:
461 RETVAL
462  
463 int
464 sampled(self)
465 SV *self
466 CODE:
467 {
468 0 ▶ otel_span *s = OTEL_REF_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
469 0 ▶ RETVAL = s ? s->sampled : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = s ? s->sampled : 0; 0 0 0
RETVAL = s ? s->sampled : 0; 0 0 0
470 }
471 OUTPUT:
472 RETVAL
473  
474 # The parent shape `start` takes, so a child span is one call rather than
475 # three and cannot be assembled wrongly.
476 SV *
477 child_of(self)
478 SV *self
479 CODE:
480 {
481 0 ▶ otel_span *s = OTEL_REF_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
482 char thex[32], shex[16];
483 HV *h;
484 0 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 0 0
485 0 otel_bytes_to_hex(s->trace_id, 16, thex);
486 0 otel_bytes_to_hex(s->span_id, 8, shex);
487 0 h = newHV();
488 0 hv_stores(h, "trace_id", newSVpvn(thex, 32));
489 0 hv_stores(h, "span_id", newSVpvn(shex, 16));
490 0 hv_stores(h, "sampled", newSViv(s->sampled));
491 0 RETVAL = newRV_noinc((SV *)h);
492 }
493 OUTPUT:
494 RETVAL
495  
496 SV *
497 attr(self, key, value)
498 SV *self
499 SV *key
500 SV *value
501 CODE:
502 {
503 0 ▶ otel_span *s = OTEL_REF_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
504 0 ▶ if (s) otel_span_attr(aTHX_ s, key, value);
Branch: 1 branch
BranchTrueFalseTotal
if (s) otel_span_attr(aTHX_ s, key, value); 0 0 0
505 0 RETVAL = newSVsv(self); /* chainable */
506 }
507 OUTPUT:
508 RETVAL
509  
510 SV *
511 traceparent(self)
512 SV *self
513 CODE:
514 {
515 0 ▶ otel_span *s = OTEL_REF_OF(self);
Branch: 3 branches
BranchTrueFalseTotal
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
otel_span *s = OTEL_REF_OF(self); 0 0 0
516 char thex[32], shex[16], out[64];
517 0 ▶ if (!s) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) XSRETURN_UNDEF; 0 0 0
518 0 otel_bytes_to_hex(s->trace_id, 16, thex);
519 0 otel_bytes_to_hex(s->span_id, 8, shex);
520 0 (void)snprintf(out, sizeof(out), "00-%.32s-%.16s-%02x",
521 0 thex, shex, s->sampled ? 1 : 0);
522 0 RETVAL = newSVpv(out, 0);
523 }
524 OUTPUT:
525 RETVAL
« xs/grpc.xs ↑ Summary xs/schema.xs »