include/otel_resource.h

filter: Statementstmt 90.2% 46 / 51 Branchbran 56.2% 36 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.3% 82 / 115 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_b3.h ↑ Summary include/otel_trace.h »
1 /* otel_resource.h - what produced this telemetry, in C.
2 *
3 * The resource attributes attached to everything a process exports: which
4 * service this is, which instance of it, and what it is running on.
5 * lib/Punk/OpenTelemetry/Resource.pm is documentation.
6 *
7 * The instance id comes from otel_id.h's generator - the same getentropy /
8 * cached-urandom source the trace and span ids use - and not from rand().
9 * That is not tidiness. Perl seeds its RNG lazily and does NOT reseed it
10 * across fork, so a parent that has called rand() once (anything might) hands
11 * every prefork worker the SAME sequence, and therefore the same
12 * service.instance.id. Which is the exact failure this attribute exists to
13 * prevent: a collector seeing eight contradictory cumulative series that all
14 * claim to be one instance, resolving it by resetting, summing or taking the
15 * last write - all three wrong, none of them wrong-looking on a dashboard.
16 *
17 * Needs otel_id.h (otel_gen_id, otel_bytes_to_hex).
18 */
19  
20 #ifndef OTEL_RESOURCE_H
21 #define OTEL_RESOURCE_H
22  
23 /* A process-unique instance id, shaped like a UUID because that is what every
24 * backend expects to see in this attribute. Generated on demand and never
25 * cached: the one thing it must not do is hand the same value to two
26 * processes. Owned (+1). */
27 70 static SV *otel_res_instance_id(pTHX) {
28 unsigned char b[16];
29 char hex[32];
30 SV *out;
31 70 ▶ if (!otel_gen_id(b, sizeof b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!otel_gen_id(b, sizeof b)) { 0 70 70
32 /* No entropy source at all. Rather than return something a caller
33 * cannot tell from a real id, fall back to something that is at
34 * least per-process distinct and say so in the shape. */
35 0 UV pid = (UV)PerlProc_getpid();
36 0 UV now = (UV)time(NULL);
37 int i;
38 0 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 0 0 0
39 0 b[i] = (unsigned char)((pid >> ((i % 4) * 8)) ^ (now >> ((i % 4) * 8))
40 ^ (unsigned)(i * 31));
41 }
42 70 otel_bytes_to_hex(b, sizeof b, hex);
43 70 out = newSVpvn(hex, 8);
44 70 sv_catpvs(out, "-"); sv_catpvn(out, hex + 8, 4);
45 70 sv_catpvs(out, "-"); sv_catpvn(out, hex + 12, 4);
46 70 sv_catpvs(out, "-"); sv_catpvn(out, hex + 16, 4);
47 70 sv_catpvs(out, "-"); sv_catpvn(out, hex + 20, 12);
48 70 return out;
49 }
50  
51 /* $^X, $^V and friends, read from the interpreter rather than guessed. */
52 70 static SV *otel_res_perl_sv(pTHX_ const char *name, STRLEN len) {
53 70 SV *sv = get_sv(name, 0);
54 70 ▶ return (sv && SvOK(sv)) ? sv : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (sv && SvOK(sv)) ? sv : NULL; 70 0 70
return (sv && SvOK(sv)) ? sv : NULL; 70 0 70
55 PERL_UNUSED_ARG(len);
56 }
57  
58 /* Sys::Hostname, by name and under eval, exactly as the Perl did: it tries
59 * several sources and knows more about the odd platforms than a bare
60 * gethostname call does. A failure means the attribute is simply omitted -
61 * a resource missing host.name is fine, and a boot that died looking for it
62 * is not. Mortal SV, or NULL. */
63 70 static SV *otel_res_hostname(pTHX) {
64 70 dSP;
65 int count;
66 70 SV *h = NULL;
67 70 eval_pv("require Sys::Hostname;", FALSE);
68 70 SPAGAIN;
69 70 ▶ if (SvTRUE(ERRSV)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) return NULL; 70 0 70
if (SvTRUE(ERRSV)) return NULL; 0 70 70
70 70 ENTER; SAVETMPS;
71 70 ▶ PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); PUTBACK; 0 70 70
72 70 count = call_pv("Sys::Hostname::hostname", G_SCALAR | G_EVAL);
73 70 SPAGAIN;
74 70 ▶ if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) h = newSVsv(r); }
Branch: 4 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) h = newSVsv(r); } 70 0 70
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) h = newSVsv(r); } 70 0 70
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) h = newSVsv(r); } 70 0 70
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) h = newSVsv(r); } 70 0 70
75 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
76 70 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 70 0 70
77 70 ▶ if (!h) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return NULL; 0 70 70
78 70 sv_2mortal(h);
79 70 ▶ return SvCUR(h) ? h : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvCUR(h) ? h : NULL; 70 0 70
80 }
81  
82 /* OTEL_RESOURCE_ATTRIBUTES: comma-separated key=value, spec-defined.
83 *
84 * The split is the Perl's, byte for byte: pairs on comma with the whitespace
85 * around it eaten, then key and value on the FIRST '=' only, so a value may
86 * contain one. A pair with no '=' or an empty key is skipped rather than
87 * guessed at. The key is trimmed at both ends; the value is not, because
88 * whitespace inside a value is the caller's business and only the run beside
89 * a comma was ever eaten. */
90 5 static void otel_res_parse_env_attrs(pTHX_ HV *out, const char *s, STRLEN len) {
91 5 STRLEN i = 0;
92 14 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 9 5 14
93 STRLEN start, end, eq;
94 const char *kp, *vp;
95 STRLEN kl, vl;
96 /* leading whitespace of this pair, then up to the next comma */
97 10 ▶ while (i < len && isSPACE(s[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && isSPACE(s[i])) i++; 10 0 10
while (i < len && isSPACE(s[i])) i++; 1 9 10
98 9 start = i;
99 134 ▶ while (i < len && s[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && s[i] != ',') i++; 129 5 134
while (i < len && s[i] != ',') i++; 125 4 129
100 9 end = i;
101 9 ▶ if (i < len) i++; /* step over the comma */
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) i++; /* step over the comma */ 4 5 9
102 9 ▶ while (end > start && isSPACE(s[end - 1])) end--; /* trailing */
Branch: 2 branches
BranchTrueFalseTotal
while (end > start && isSPACE(s[end - 1])) end--; /* trailing */ 9 0 9
while (end > start && isSPACE(s[end - 1])) end--; /* trailing */ 0 9 9
103 9 ▶ if (end <= start) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (end <= start) continue; 0 9 9
104  
105 87 ▶ for (eq = start; eq < end && s[eq] != '='; eq++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (eq = start; eq < end && s[eq] != '='; eq++) ; 87 0 87
for (eq = start; eq < end && s[eq] != '='; eq++) ; 78 9 87
106 9 ▶ if (eq >= end) continue; /* no '=' : skipped */
Branch: 1 branch
BranchTrueFalseTotal
if (eq >= end) continue; /* no '=' : skipped */ 0 9 9
107 9 kp = s + start; kl = eq - start;
108 9 vp = s + eq + 1; vl = end - eq - 1;
109 9 ▶ while (kl && isSPACE(kp[0])) { kp++; kl--; } /* trim the key */
Branch: 2 branches
BranchTrueFalseTotal
while (kl && isSPACE(kp[0])) { kp++; kl--; } /* trim the key */ 9 0 9
while (kl && isSPACE(kp[0])) { kp++; kl--; } /* trim the key */ 0 9 9
110 9 ▶ while (kl && isSPACE(kp[kl - 1])) { kl--; }
Branch: 2 branches
BranchTrueFalseTotal
while (kl && isSPACE(kp[kl - 1])) { kl--; } 9 0 9
while (kl && isSPACE(kp[kl - 1])) { kl--; } 0 9 9
111 9 ▶ if (!kl) continue; /* empty key : skipped */
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) continue; /* empty key : skipped */ 0 9 9
112 9 (void)hv_store(out, kp, (I32)kl, newSVpvn(vp, vl), 0);
113 }
114 5 }
115  
116 #endif /* OTEL_RESOURCE_H */
« include/otel_b3.h ↑ Summary include/otel_trace.h »