include/otel_span.h

filter: Statementstmt 87.6% 99 / 113 Branchbran 66.3% 61 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.0% 160 / 205 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_export.h ↑ Summary xs/propagate.xs »
1 /* otel_span.h - a span, as a C struct.
2 *
3 * The span is the thing there is one of per operation, so its cost is the
4 * SDK's cost. It holds ids, times, kind, status and three bounded lists, and
5 * it materialises no SV until something asks for one.
6 *
7 * SPAN LIMITS. The spec caps attributes, events and links, and requires the
8 * count of what was dropped to be reported. Both halves matter, and the
9 * second more than the first: a span that quietly loses its 129th attribute
10 * looks complete, and someone will spend an afternoon working out why the
11 * attribute they added is missing. A span that says "129 attributes, 1
12 * dropped" answers that question before it is asked.
13 *
14 * ATTRIBUTE STORAGE. Keys and values are Perl SVs, because they came from
15 * Perl and go back to Perl at export - and because the encoder in phase 2
16 * already takes SVs. What is avoided is an SV per SPAN, and the per-span
17 * hashes and blessed wrappers a Perl-side implementation would build. An
18 * unsampled span allocates none of this at all: it is never created.
19 */
20  
21 #ifndef OTEL_SPAN_H
22 #define OTEL_SPAN_H
23  
24 #include "otel_clock.h"
25 #include "otel_id.h"
26 #include "otel_proto.h"
27  
28 /* The spec defaults. */
29 #define OTEL_LIMIT_ATTRS 128
30 #define OTEL_LIMIT_EVENTS 128
31 #define OTEL_LIMIT_LINKS 128
32  
33 typedef struct otel_event {
34 SV *name;
35 U64TYPE time;
36 HV *attrs; /* +1 owned, or NULL */
37 struct otel_event *next;
38 } otel_event;
39  
40 typedef struct otel_link {
41 unsigned char trace_id[16];
42 unsigned char span_id[8];
43 HV *attrs;
44 struct otel_link *next;
45 } otel_link;
46  
47 typedef struct otel_span {
48 unsigned char trace_id[16];
49 unsigned char span_id[8];
50 unsigned char parent_id[8];
51 int has_parent;
52 int sampled;
53  
54 SV *name; /* +1 owned */
55 int kind;
56 U64TYPE start_wall; /* unix nanos: where the span sits in time */
57 U64TYPE start_mono; /* monotonic: only for the duration */
58 U64TYPE end_wall; /* derived; 0 until ended */
59 int ended;
60  
61 int status_code;
62 SV *status_message; /* +1 owned, or NULL */
63  
64 HV *attrs; /* +1 owned */
65 IV dropped_attrs;
66  
67 otel_event *events, *events_tail;
68 IV n_events, dropped_events;
69  
70 otel_link *links, *links_tail;
71 IV n_links, dropped_links;
72  
73 SV *trace_state; /* +1 owned, or NULL */
74 } otel_span;
75  
76 3406 static otel_span *otel_span_new(pTHX) {
77 otel_span *s;
78 3406 Newxz(s, 1, otel_span);
79 3406 s->attrs = newHV();
80 3406 s->kind = OTEL_KIND_INTERNAL;
81 3406 s->status_code = OTEL_STATUS_UNSET;
82 3406 s->start_wall = otel_wall_nanos();
83 3406 s->start_mono = otel_mono_nanos();
84 3406 return s;
85 }
86  
87 3406 static void otel_span_free(pTHX_ otel_span *s) {
88 otel_event *e;
89 otel_link *l;
90 3406 ▶ if (!s) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; 0 3406 3406
91 3406 ▶ if (s->name) SvREFCNT_dec(s->name);
Branch: 1 branch
BranchTrueFalseTotal
if (s->name) SvREFCNT_dec(s->name); 3406 0 3406
92 3406 ▶ if (s->status_message) SvREFCNT_dec(s->status_message);
Branch: 1 branch
BranchTrueFalseTotal
if (s->status_message) SvREFCNT_dec(s->status_message); 1 3405 3406
93 3406 ▶ if (s->trace_state) SvREFCNT_dec(s->trace_state);
Branch: 1 branch
BranchTrueFalseTotal
if (s->trace_state) SvREFCNT_dec(s->trace_state); 0 3406 3406
94 3406 ▶ if (s->attrs) SvREFCNT_dec((SV *)s->attrs);
Branch: 1 branch
BranchTrueFalseTotal
if (s->attrs) SvREFCNT_dec((SV *)s->attrs); 3406 0 3406
95 3534 ▶ for (e = s->events; e; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (e = s->events; e; ) { 128 3406 3534
96 128 otel_event *n = e->next;
97 128 ▶ if (e->name) SvREFCNT_dec(e->name);
Branch: 1 branch
BranchTrueFalseTotal
if (e->name) SvREFCNT_dec(e->name); 128 0 128
98 128 ▶ if (e->attrs) SvREFCNT_dec((SV *)e->attrs);
Branch: 1 branch
BranchTrueFalseTotal
if (e->attrs) SvREFCNT_dec((SV *)e->attrs); 0 128 128
99 128 Safefree(e);
100 128 e = n;
101 }
102 3534 ▶ for (l = s->links; l; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (l = s->links; l; ) { 128 3406 3534
103 128 otel_link *n = l->next;
104 128 ▶ if (l->attrs) SvREFCNT_dec((SV *)l->attrs);
Branch: 1 branch
BranchTrueFalseTotal
if (l->attrs) SvREFCNT_dec((SV *)l->attrs); 0 128 128
105 128 Safefree(l);
106 128 l = n;
107 }
108 3406 Safefree(s);
109 }
110  
111 /* Set an attribute, honouring the cap.
112 *
113 * An existing key is replaced and does NOT count against the limit or the
114 * dropped count: overwriting is not adding. Getting that wrong makes a loop
115 * that updates one attribute look like a span with a hundred dropped ones. */
116 561 static void otel_span_attr(pTHX_ otel_span *s, SV *key, SV *val) {
117 561 ▶ if (!s || !key) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !key) return; 561 0 561
if (!s || !key) return; 0 561 561
118 561 ▶ if (hv_exists_ent(s->attrs, key, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(s->attrs, key, 0)) { 299 262 561
119 299 (void)hv_store_ent(s->attrs, key, newSVsv(val), 0);
120 299 return;
121 }
122 262 ▶ if ((IV)HvUSEDKEYS(s->attrs) >= OTEL_LIMIT_ATTRS) {
Branch: 2 branches
BranchTrueFalseTotal
if ((IV)HvUSEDKEYS(s->attrs) >= OTEL_LIMIT_ATTRS) { 0 262 262
if ((IV)HvUSEDKEYS(s->attrs) >= OTEL_LIMIT_ATTRS) { 72 190 262
123 72 s->dropped_attrs++;
124 72 return;
125 }
126 190 (void)hv_store_ent(s->attrs, key, newSVsv(val), 0);
127 }
128  
129 200 static void otel_span_event(pTHX_ otel_span *s, SV *name, HV *attrs) {
130 otel_event *e;
131 200 ▶ if (!s) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; 0 200 200
132 200 ▶ if (s->n_events >= OTEL_LIMIT_EVENTS) { s->dropped_events++; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->n_events >= OTEL_LIMIT_EVENTS) { s->dropped_events++; return; } 72 128 200
133 128 Newxz(e, 1, otel_event);
134 128 ▶ e->name = name ? newSVsv(name) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
e->name = name ? newSVsv(name) : NULL; 128 0 128
135 128 e->time = otel_wall_nanos();
136 128 ▶ e->attrs = attrs ? (HV *)SvREFCNT_inc((SV *)attrs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
e->attrs = attrs ? (HV *)SvREFCNT_inc((SV *)attrs) : NULL; 0 128 128
137 128 ▶ if (s->events_tail) s->events_tail->next = e;
Branch: 1 branch
BranchTrueFalseTotal
if (s->events_tail) s->events_tail->next = e; 127 1 128
138 1 else s->events = e;
139 128 s->events_tail = e;
140 128 s->n_events++;
141 }
142  
143 200 static void otel_span_link(pTHX_ otel_span *s, const unsigned char *tid,
144 const unsigned char *sid, HV *attrs) {
145 otel_link *l;
146 200 ▶ if (!s || !tid || !sid) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!s || !tid || !sid) return; 200 0 200
if (!s || !tid || !sid) return; 200 0 200
if (!s || !tid || !sid) return; 0 200 200
147 200 ▶ if (s->n_links >= OTEL_LIMIT_LINKS) { s->dropped_links++; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->n_links >= OTEL_LIMIT_LINKS) { s->dropped_links++; return; } 72 128 200
148 128 Newxz(l, 1, otel_link);
149 128 Copy(tid, l->trace_id, 16, unsigned char);
150 128 Copy(sid, l->span_id, 8, unsigned char);
151 128 ▶ l->attrs = attrs ? (HV *)SvREFCNT_inc((SV *)attrs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
l->attrs = attrs ? (HV *)SvREFCNT_inc((SV *)attrs) : NULL; 0 128 128
152 128 ▶ if (s->links_tail) s->links_tail->next = l;
Branch: 1 branch
BranchTrueFalseTotal
if (s->links_tail) s->links_tail->next = l; 127 1 128
153 1 else s->links = l;
154 128 s->links_tail = l;
155 128 s->n_links++;
156 }
157  
158 /* End the span. Idempotent: ending twice is a bug in the caller, not a reason
159 * to move the end timestamp, and a scoped guard firing after an explicit end
160 * is the ordinary way it happens. */
161 2532 static void otel_span_end(pTHX_ otel_span *s) {
162 2532 ▶ if (!s || s->ended) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || s->ended) return; 2532 0 2532
if (!s || s->ended) return; 0 2532 2532
163 2532 s->end_wall = otel_end_nanos(s->start_wall, s->start_mono);
164 2532 s->ended = 1;
165 }
166  
167 /* ---- to the payload shape the phase-2 encoder takes --------------------- */
168  
169 32 static SV *otel_span_to_hv(pTHX_ otel_span *s) {
170 32 HV *h = newHV();
171 char hex[32];
172 otel_event *e;
173 otel_link *l;
174 AV *av;
175  
176 32 otel_bytes_to_hex(s->trace_id, 16, hex);
177 32 (void)hv_stores(h, "trace_id", newSVpvn(hex, 32));
178 32 otel_bytes_to_hex(s->span_id, 8, hex);
179 32 (void)hv_stores(h, "span_id", newSVpvn(hex, 16));
180 32 ▶ if (s->has_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->has_parent) { 3 29 32
181 3 otel_bytes_to_hex(s->parent_id, 8, hex);
182 3 (void)hv_stores(h, "parent_span_id", newSVpvn(hex, 16));
183 }
184 32 ▶ if (s->trace_state)
Branch: 1 branch
BranchTrueFalseTotal
if (s->trace_state) 0 32 32
185 0 (void)hv_stores(h, "trace_state", newSVsv(s->trace_state));
186 32 ▶ if (s->name) (void)hv_stores(h, "name", newSVsv(s->name));
Branch: 1 branch
BranchTrueFalseTotal
if (s->name) (void)hv_stores(h, "name", newSVsv(s->name)); 32 0 32
187 32 (void)hv_stores(h, "kind", newSViv(s->kind));
188 32 (void)hv_stores(h, "start_time_unix_nano", newSVuv((UV)s->start_wall));
189 32 ▶ (void)hv_stores(h, "end_time_unix_nano",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "end_time_unix_nano", 20 12 32
190 newSVuv((UV)(s->ended ? s->end_wall
191 : otel_end_nanos(s->start_wall,
192 s->start_mono))));
193 32 (void)hv_stores(h, "attributes", newRV_inc((SV *)s->attrs));
194 32 ▶ if (s->dropped_attrs)
Branch: 1 branch
BranchTrueFalseTotal
if (s->dropped_attrs) 1 31 32
195 1 (void)hv_stores(h, "dropped_attributes_count",
196 newSViv(s->dropped_attrs));
197  
198 32 ▶ if (s->events) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->events) { 1 31 32
199 1 av = newAV();
200 129 ▶ for (e = s->events; e; e = e->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (e = s->events; e; e = e->next) { 128 1 129
201 128 HV *eh = newHV();
202 128 ▶ if (e->name) (void)hv_stores(eh, "name", newSVsv(e->name));
Branch: 1 branch
BranchTrueFalseTotal
if (e->name) (void)hv_stores(eh, "name", newSVsv(e->name)); 128 0 128
203 128 (void)hv_stores(eh, "time_unix_nano", newSVuv((UV)e->time));
204 128 ▶ if (e->attrs)
Branch: 1 branch
BranchTrueFalseTotal
if (e->attrs) 0 128 128
205 0 (void)hv_stores(eh, "attributes", newRV_inc((SV *)e->attrs));
206 128 av_push(av, newRV_noinc((SV *)eh));
207 }
208 1 (void)hv_stores(h, "events", newRV_noinc((SV *)av));
209 }
210 32 ▶ if (s->dropped_events)
Branch: 1 branch
BranchTrueFalseTotal
if (s->dropped_events) 1 31 32
211 1 (void)hv_stores(h, "dropped_events_count", newSViv(s->dropped_events));
212  
213 32 ▶ if (s->links) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->links) { 0 32 32
214 0 av = newAV();
215 0 ▶ for (l = s->links; l; l = l->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (l = s->links; l; l = l->next) { 0 0 0
216 0 HV *lh = newHV();
217 0 otel_bytes_to_hex(l->trace_id, 16, hex);
218 0 (void)hv_stores(lh, "trace_id", newSVpvn(hex, 32));
219 0 otel_bytes_to_hex(l->span_id, 8, hex);
220 0 (void)hv_stores(lh, "span_id", newSVpvn(hex, 16));
221 0 ▶ if (l->attrs)
Branch: 1 branch
BranchTrueFalseTotal
if (l->attrs) 0 0 0
222 0 (void)hv_stores(lh, "attributes", newRV_inc((SV *)l->attrs));
223 0 av_push(av, newRV_noinc((SV *)lh));
224 }
225 0 (void)hv_stores(h, "links", newRV_noinc((SV *)av));
226 }
227 32 ▶ if (s->dropped_links)
Branch: 1 branch
BranchTrueFalseTotal
if (s->dropped_links) 0 32 32
228 0 (void)hv_stores(h, "dropped_links_count", newSViv(s->dropped_links));
229  
230 32 ▶ if (s->status_code != OTEL_STATUS_UNSET || s->status_message) {
Branch: 2 branches
BranchTrueFalseTotal
if (s->status_code != OTEL_STATUS_UNSET || s->status_message) { 25 7 32
if (s->status_code != OTEL_STATUS_UNSET || s->status_message) { 0 25 25
231 7 HV *st = newHV();
232 7 (void)hv_stores(st, "code", newSViv(s->status_code));
233 7 ▶ if (s->status_message)
Branch: 1 branch
BranchTrueFalseTotal
if (s->status_message) 2 5 7
234 2 (void)hv_stores(st, "message", newSVsv(s->status_message));
235 7 (void)hv_stores(h, "status", newRV_noinc((SV *)st));
236 }
237 /* the W3C sampled flag, which OTLP carries on the span too */
238 32 ▶ if (s->sampled) (void)hv_stores(h, "flags", newSVuv(OTEL_FLAG_SAMPLED));
Branch: 1 branch
BranchTrueFalseTotal
if (s->sampled) (void)hv_stores(h, "flags", newSVuv(OTEL_FLAG_SAMPLED)); 32 0 32
239 32 return newRV_noinc((SV *)h);
240 }
241  
242 #endif /* OTEL_SPAN_H */
« include/otel_export.h ↑ Summary xs/propagate.xs »