include/otel_tracer.h

filter: Statementstmt 95.5% 86 / 90 Branchbran 80.0% 40 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.0% 126 / 140 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_value.h ↑ Summary include/otel_json.h »
1 /* otel_tracer.h - the tracer: what starts spans, and what holds them until
2 * they are exported.
3 *
4 * THE BATCH QUEUE, and the doctrine it is built on.
5 *
6 * The queue is BOUNDED and drops the OLDEST span when it is full, and it
7 * counts what it dropped. Every part of that is deliberate:
8 *
9 * - Bounded, because an unbounded queue in front of an unreachable
10 * collector is not a queue, it is a memory leak with a schedule. The
11 * failure it produces is a web server dying of OOM some hours after a
12 * collector went down, which is a far worse outage than the missing
13 * telemetry.
14 *
15 * - Drop-oldest rather than drop-newest, because when a system is in
16 * trouble the interesting spans are the recent ones. Keeping the oldest
17 * preserves a snapshot of the moment things were still fine.
18 *
19 * - Counted, and the count exported, because a telemetry layer that cannot
20 * report its own losses is asking to be trusted for no reason. A gap in a
21 * trace with a number beside it is a diagnosis; a gap without one is a
22 * mystery.
23 *
24 * This is Maat::Shipper's rule restated: the job is not "do not lose data",
25 * it is DO NOT BECOME THE PROBLEM.
26 */
27  
28 #ifndef OTEL_TRACER_H
29 #define OTEL_TRACER_H
30  
31 #include "otel_id.h"
32 #include "otel_sample.h"
33 #include "otel_span.h"
34  
35 /* The spec's BatchSpanProcessor defaults. */
36 #define OTEL_QUEUE_MAX 2048
37 #define OTEL_BATCH_MAX 512
38  
39 typedef struct {
40 otel_sampler sampler;
41  
42 HV *resource; /* +1 owned: attributes, hashref */
43 SV *scope_name;
44 SV *scope_version;
45 SV *schema_url; /* the resource's */
46 SV *scope_schema_url; /* the scope's, which may legitimately differ */
47  
48 /* the bounded ring of ended spans awaiting export */
49 otel_span **queue;
50 int qcap, qhead, qcount;
51  
52 /* what happened, so the SDK can report its own losses */
53 IV started, ended, dropped_queue, sampled_out;
54  
55 pid_t owner_pid; /* the process this tracer's queue belongs to */
56 } otel_tracer;
57  
58 50 static otel_tracer *otel_tracer_new(pTHX) {
59 otel_tracer *t;
60 50 Newxz(t, 1, otel_tracer);
61 50 otel_sampler_init(&t->sampler, OTEL_SAMPLER_PARENT_RATIO, 1.0);
62 50 t->resource = newHV();
63 50 t->qcap = OTEL_QUEUE_MAX;
64 50 Newxz(t->queue, t->qcap, otel_span *);
65 50 t->owner_pid = getpid();
66 50 return t;
67 }
68  
69 50 static void otel_tracer_free(pTHX_ otel_tracer *t) {
70 int i;
71 50 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 50 50
72 2110 ▶ for (i = 0; i < t->qcount; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->qcount; i++) 2060 50 2110
73 2060 otel_span_free(aTHX_ t->queue[(t->qhead + i) % t->qcap]);
74 50 Safefree(t->queue);
75 50 ▶ if (t->resource) SvREFCNT_dec((SV *)t->resource);
Branch: 1 branch
BranchTrueFalseTotal
if (t->resource) SvREFCNT_dec((SV *)t->resource); 50 0 50
76 50 ▶ if (t->scope_name) SvREFCNT_dec(t->scope_name);
Branch: 1 branch
BranchTrueFalseTotal
if (t->scope_name) SvREFCNT_dec(t->scope_name); 23 27 50
77 50 ▶ if (t->scope_version) SvREFCNT_dec(t->scope_version);
Branch: 1 branch
BranchTrueFalseTotal
if (t->scope_version) SvREFCNT_dec(t->scope_version); 16 34 50
78 50 ▶ if (t->schema_url) SvREFCNT_dec(t->schema_url);
Branch: 1 branch
BranchTrueFalseTotal
if (t->schema_url) SvREFCNT_dec(t->schema_url); 16 34 50
79 50 ▶ if (t->scope_schema_url) SvREFCNT_dec(t->scope_schema_url);
Branch: 1 branch
BranchTrueFalseTotal
if (t->scope_schema_url) SvREFCNT_dec(t->scope_schema_url); 16 34 50
80 50 Safefree(t);
81 }
82  
83 /* A worker that inherited a queue across fork would export the parent's spans
84 * as its own, duplicating every one of them - once per worker. The queue
85 * belongs to the process that filled it, so a child starts empty. */
86 2564 static void otel_tracer_check_fork(pTHX_ otel_tracer *t) {
87 2564 pid_t me = getpid();
88 int i;
89 2564 ▶ if (t->owner_pid == me) return;
Branch: 1 branch
BranchTrueFalseTotal
if (t->owner_pid == me) return; 2564 0 2564
90 0 ▶ for (i = 0; i < t->qcount; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->qcount; i++) 0 0 0
91 0 otel_span_free(aTHX_ t->queue[(t->qhead + i) % t->qcap]);
92 0 t->qhead = t->qcount = 0;
93 0 t->owner_pid = me;
94 }
95  
96 /* Push an ended span, dropping the oldest when full. */
97 2531 static void otel_tracer_enqueue(pTHX_ otel_tracer *t, otel_span *s) {
98 2531 otel_tracer_check_fork(aTHX_ t);
99 2531 ▶ if (t->qcount == t->qcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->qcount == t->qcap) { 452 2079 2531
100 452 otel_span *old = t->queue[t->qhead];
101 452 otel_span_free(aTHX_ old);
102 452 t->qhead = (t->qhead + 1) % t->qcap;
103 452 t->qcount--;
104 452 t->dropped_queue++;
105 }
106 2531 t->queue[(t->qhead + t->qcount) % t->qcap] = s;
107 2531 t->qcount++;
108 2531 }
109  
110 /* Start a span.
111 *
112 * An UNSAMPLED span returns NULL and allocates NOTHING. That is the property
113 * that makes sampling worth having: at 1% the other 99% of requests must not
114 * pay for a struct, a hash, an id or a timestamp. Everything above has to
115 * cope with a NULL span, which is a small price for the 99%.
116 *
117 * parent_tid / parent_sid are the extracted inbound context, or NULL. */
118 7218 static otel_span *otel_tracer_start(pTHX_ otel_tracer *t,
119 const unsigned char *parent_tid,
120 const unsigned char *parent_sid,
121 int parent_sampled, SV *name, int kind) {
122 unsigned char tid[16], sid[8];
123 7218 ▶ int has_parent = (parent_tid && parent_sid) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
int has_parent = (parent_tid && parent_sid) ? 1 : 0; 68 7150 7218
int has_parent = (parent_tid && parent_sid) ? 1 : 0; 68 0 68
124 otel_span *s;
125  
126 7218 ▶ if (has_parent) Copy(parent_tid, tid, 16, unsigned char);
Branch: 1 branch
BranchTrueFalseTotal
if (has_parent) Copy(parent_tid, tid, 16, unsigned char); 68 7150 7218
127 7150 ▶ else if (!otel_gen_id(tid, 16)) return NULL; /* no entropy: no trace */
Branch: 1 branch
BranchTrueFalseTotal
else if (!otel_gen_id(tid, 16)) return NULL; /* no entropy: no trace */ 0 7150 7150
128  
129 7218 ▶ if (!otel_should_sample(&t->sampler, tid, has_parent, parent_sampled)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_should_sample(&t->sampler, tid, has_parent, parent_sampled)) { 3812 3406 7218
130 3812 t->sampled_out++;
131 3812 return NULL;
132 }
133 3406 ▶ if (!otel_gen_id(sid, 8)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_gen_id(sid, 8)) return NULL; 0 3406 3406
134  
135 3406 s = otel_span_new(aTHX);
136 3406 Copy(tid, s->trace_id, 16, unsigned char);
137 3406 Copy(sid, s->span_id, 8, unsigned char);
138 3406 ▶ if (has_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_parent) { 66 3340 3406
139 66 Copy(parent_sid, s->parent_id, 8, unsigned char);
140 66 s->has_parent = 1;
141 }
142 3406 s->sampled = 1;
143 3406 s->kind = kind;
144 3406 ▶ if (name && SvOK(name)) s->name = newSVsv(name);
Branch: 2 branches
BranchTrueFalseTotal
if (name && SvOK(name)) s->name = newSVsv(name); 3406 0 3406
if (name && SvOK(name)) s->name = newSVsv(name); 3406 0 3406
145 3406 t->started++;
146 3406 return s;
147 }
148  
149 /* Take up to `max` spans off the queue as the payload the phase-2 encoder
150 * wants. Returns NULL when there is nothing to send - the common case, and
151 * the one that must not allocate. */
152 17 static SV *otel_tracer_drain(pTHX_ otel_tracer *t, int max) {
153 HV *payload, *rs, *ss, *res, *scope;
154 AV *rss, *sss, *spans;
155 int n, i;
156  
157 17 otel_tracer_check_fork(aTHX_ t);
158 17 ▶ if (t->qcount <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (t->qcount <= 0) return NULL; 3 14 17
159 14 n = t->qcount < max ? t->qcount : max;
160  
161 14 spans = newAV();
162 33 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 19 14 33
163 19 otel_span *s = t->queue[t->qhead];
164 19 t->queue[t->qhead] = NULL;
165 19 t->qhead = (t->qhead + 1) % t->qcap;
166 19 t->qcount--;
167 19 av_push(spans, otel_span_to_hv(aTHX_ s));
168 19 otel_span_free(aTHX_ s);
169 }
170  
171 14 scope = newHV();
172 14 ▶ if (t->scope_name) (void)hv_stores(scope, "name", newSVsv(t->scope_name));
Branch: 1 branch
BranchTrueFalseTotal
if (t->scope_name) (void)hv_stores(scope, "name", newSVsv(t->scope_name)); 12 2 14
173 14 ▶ if (t->scope_version) (void)hv_stores(scope, "version",
Branch: 1 branch
BranchTrueFalseTotal
if (t->scope_version) (void)hv_stores(scope, "version", 6 8 14
174 newSVsv(t->scope_version));
175 14 ss = newHV();
176 14 (void)hv_stores(ss, "scope", newRV_noinc((SV *)scope));
177 14 (void)hv_stores(ss, "spans", newRV_noinc((SV *)spans));
178 14 ▶ if (t->scope_schema_url)
Branch: 1 branch
BranchTrueFalseTotal
if (t->scope_schema_url) 6 8 14
179 6 (void)hv_stores(ss, "schema_url", newSVsv(t->scope_schema_url));
180 14 sss = newAV();
181 14 av_push(sss, newRV_noinc((SV *)ss));
182  
183 14 res = newHV();
184 14 (void)hv_stores(res, "attributes", newRV_inc((SV *)t->resource));
185 14 rs = newHV();
186 14 (void)hv_stores(rs, "resource", newRV_noinc((SV *)res));
187 14 (void)hv_stores(rs, "scope_spans", newRV_noinc((SV *)sss));
188 14 ▶ if (t->schema_url)
Branch: 1 branch
BranchTrueFalseTotal
if (t->schema_url) 6 8 14
189 6 (void)hv_stores(rs, "schema_url", newSVsv(t->schema_url));
190 14 rss = newAV();
191 14 av_push(rss, newRV_noinc((SV *)rs));
192  
193 14 payload = newHV();
194 14 (void)hv_stores(payload, "resource_spans", newRV_noinc((SV *)rss));
195 14 return newRV_noinc((SV *)payload);
196 }
197  
198 #endif /* OTEL_TRACER_H */
« include/otel_value.h ↑ Summary include/otel_json.h »