include/otel_log.h

filter: Statementstmt 100% 102 / 102 Branchbran 81.5% 62 / 76 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.1% 164 / 178 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/otel_meter.h ↑ Summary include/otel_sample.h »
1 /* otel_log.h - the logs signal.
2 *
3 * The cheapest of the three, because Punk 0.19 already did the hard part: a
4 * log record is a message plus fields, which is exactly what an OTLP
5 * LogRecord is. What remains is severity, trace correlation, and a queue.
6 *
7 * SEVERITY. Punk has five levels; OTLP has a 24-point scale in bands of four
8 * (TRACE 1-4, DEBUG 5-8, INFO 9-12, WARN 13-16, ERROR 17-20, FATAL 21-24).
9 * The mapping picks the FIRST value of each band rather than the middle,
10 * because that is what every other SDK emits and because a backend filtering
11 * "warn and above" compares against the band start. A number in the right
12 * band is not enough if a dashboard's threshold is written as `>= 13`.
13 *
14 * THE TAP. These records are a COPY. The application's logs still go wherever
15 * they were going - stderr, psgix.logger, a file - and the collector gets a
16 * duplicate. A telemetry layer that silently redirects an operator's logs
17 * somewhere else is a bad neighbour, and the failure mode when the collector
18 * is down would be that the logs vanish.
19 */
20  
21 #ifndef OTEL_LOG_H
22 #define OTEL_LOG_H
23  
24 #include "otel_clock.h"
25 #include "otel_span.h"
26  
27 #define OTEL_SEV_TRACE 1
28 #define OTEL_SEV_DEBUG 5
29 #define OTEL_SEV_INFO 9
30 #define OTEL_SEV_WARN 13
31 #define OTEL_SEV_ERROR 17
32 #define OTEL_SEV_FATAL 21
33  
34 /* Logs are far higher volume than spans, so the queue is bigger and has its
35 * own drop counter - but it is the same bounded, drop-oldest, count-what-you
36 * -dropped arrangement, for the same reasons. */
37 #define OTEL_LOG_QUEUE_MAX 4096
38  
39 typedef struct {
40 SV *body;
41 HV *attrs;
42 int severity;
43 SV *severity_text;
44 U64TYPE time;
45 unsigned char trace_id[16];
46 unsigned char span_id[8];
47 int has_span;
48 } otel_logrec;
49  
50 typedef struct {
51 otel_logrec *queue;
52 int qcap, qhead, qcount;
53 IV emitted, dropped;
54 HV *resource;
55 SV *scope_name, *scope_version;
56 pid_t owner_pid;
57 } otel_logger;
58  
59 10038 static int otel_sev_of(pTHX_ SV *level) {
60 STRLEN l;
61 const char *s;
62 10038 ▶ if (!level || !SvOK(level)) return OTEL_SEV_INFO;
Branch: 2 branches
BranchTrueFalseTotal
if (!level || !SvOK(level)) return OTEL_SEV_INFO; 10038 0 10038
if (!level || !SvOK(level)) return OTEL_SEV_INFO; 2 10036 10038
63 10036 s = SvPV_const(level, l);
64 10036 ▶ if (l == 5 && memEQ(s, "debug", 5)) return OTEL_SEV_DEBUG;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(s, "debug", 5)) return OTEL_SEV_DEBUG; 18 10018 10036
if (l == 5 && memEQ(s, "debug", 5)) return OTEL_SEV_DEBUG; 2 16 18
65 10034 ▶ if (l == 4 && memEQ(s, "info", 4)) return OTEL_SEV_INFO;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(s, "info", 4)) return OTEL_SEV_INFO; 10016 18 10034
if (l == 4 && memEQ(s, "info", 4)) return OTEL_SEV_INFO; 10014 2 10016
66 20 ▶ if (l == 4 && memEQ(s, "warn", 4)) return OTEL_SEV_WARN;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(s, "warn", 4)) return OTEL_SEV_WARN; 2 18 20
if (l == 4 && memEQ(s, "warn", 4)) return OTEL_SEV_WARN; 2 0 2
67 18 ▶ if (l == 5 && memEQ(s, "error", 5)) return OTEL_SEV_ERROR;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(s, "error", 5)) return OTEL_SEV_ERROR; 16 2 18
if (l == 5 && memEQ(s, "error", 5)) return OTEL_SEV_ERROR; 12 4 16
68 6 ▶ if (l == 5 && memEQ(s, "fatal", 5)) return OTEL_SEV_FATAL;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(s, "fatal", 5)) return OTEL_SEV_FATAL; 4 2 6
if (l == 5 && memEQ(s, "fatal", 5)) return OTEL_SEV_FATAL; 2 2 4
69 4 ▶ if (l == 5 && memEQ(s, "trace", 5)) return OTEL_SEV_TRACE;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(s, "trace", 5)) return OTEL_SEV_TRACE; 2 2 4
if (l == 5 && memEQ(s, "trace", 5)) return OTEL_SEV_TRACE; 2 0 2
70 2 return OTEL_SEV_INFO;
71 }
72  
73 24 static otel_logger *otel_logger_new(pTHX) {
74 otel_logger *lg;
75 24 Newxz(lg, 1, otel_logger);
76 24 lg->qcap = OTEL_LOG_QUEUE_MAX;
77 24 Newxz(lg->queue, lg->qcap, otel_logrec);
78 24 lg->resource = newHV();
79 24 lg->owner_pid = getpid();
80 24 return lg;
81 }
82  
83 10022 static void otel_logrec_clear(pTHX_ otel_logrec *r) {
84 10022 ▶ if (r->body) SvREFCNT_dec(r->body);
Branch: 1 branch
BranchTrueFalseTotal
if (r->body) SvREFCNT_dec(r->body); 10022 0 10022
85 10022 ▶ if (r->attrs) SvREFCNT_dec((SV *)r->attrs);
Branch: 1 branch
BranchTrueFalseTotal
if (r->attrs) SvREFCNT_dec((SV *)r->attrs); 3 10019 10022
86 10022 ▶ if (r->severity_text) SvREFCNT_dec(r->severity_text);
Branch: 1 branch
BranchTrueFalseTotal
if (r->severity_text) SvREFCNT_dec(r->severity_text); 10022 0 10022
87 10022 Zero(r, 1, otel_logrec);
88 10022 }
89  
90 24 static void otel_logger_free(pTHX_ otel_logger *lg) {
91 int i;
92 24 ▶ if (!lg) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!lg) return; 0 24 24
93 8219 ▶ for (i = 0; i < lg->qcount; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < lg->qcount; i++) 8195 24 8219
94 8195 otel_logrec_clear(aTHX_ &lg->queue[(lg->qhead + i) % lg->qcap]);
95 24 Safefree(lg->queue);
96 24 ▶ if (lg->resource) SvREFCNT_dec((SV *)lg->resource);
Branch: 1 branch
BranchTrueFalseTotal
if (lg->resource) SvREFCNT_dec((SV *)lg->resource); 24 0 24
97 24 ▶ if (lg->scope_name) SvREFCNT_dec(lg->scope_name);
Branch: 1 branch
BranchTrueFalseTotal
if (lg->scope_name) SvREFCNT_dec(lg->scope_name); 16 8 24
98 24 ▶ if (lg->scope_version) SvREFCNT_dec(lg->scope_version);
Branch: 1 branch
BranchTrueFalseTotal
if (lg->scope_version) SvREFCNT_dec(lg->scope_version); 0 24 24
99 24 Safefree(lg);
100 }
101  
102 /* Same rule as the span queue: a child owns none of its parent's records. */
103 10040 static void otel_logger_check_fork(pTHX_ otel_logger *lg) {
104 10040 pid_t me = getpid();
105 int i;
106 10040 ▶ if (lg->owner_pid == me) return;
Branch: 1 branch
BranchTrueFalseTotal
if (lg->owner_pid == me) return; 10039 1 10040
107 6 ▶ for (i = 0; i < lg->qcount; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < lg->qcount; i++) 5 1 6
108 5 otel_logrec_clear(aTHX_ &lg->queue[(lg->qhead + i) % lg->qcap]);
109 1 lg->qhead = lg->qcount = 0;
110 1 lg->owner_pid = me;
111 }
112  
113 10022 static void otel_logger_emit(pTHX_ otel_logger *lg, SV *level, SV *body,
114 HV *attrs, otel_span *span) {
115 otel_logrec *r;
116 10022 otel_logger_check_fork(aTHX_ lg);
117 10022 ▶ if (lg->qcount == lg->qcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (lg->qcount == lg->qcap) { 1808 8214 10022
118 1808 otel_logrec_clear(aTHX_ &lg->queue[lg->qhead]);
119 1808 lg->qhead = (lg->qhead + 1) % lg->qcap;
120 1808 lg->qcount--;
121 1808 lg->dropped++;
122 }
123 10022 r = &lg->queue[(lg->qhead + lg->qcount) % lg->qcap];
124 10022 Zero(r, 1, otel_logrec);
125 10022 ▶ r->body = body ? newSVsv(body) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r->body = body ? newSVsv(body) : NULL; 10022 0 10022
126 10022 ▶ r->attrs = attrs ? (HV *)SvREFCNT_inc((SV *)attrs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r->attrs = attrs ? (HV *)SvREFCNT_inc((SV *)attrs) : NULL; 3 10019 10022
127 10022 r->severity = otel_sev_of(aTHX_ level);
128 10022 ▶ r->severity_text = level && SvOK(level) ? newSVsv(level) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
r->severity_text = level && SvOK(level) ? newSVsv(level) : NULL; 10022 0 10022
r->severity_text = level && SvOK(level) ? newSVsv(level) : NULL; 10022 0 10022
129 10022 r->time = otel_wall_nanos();
130 10022 ▶ if (span) {
Branch: 1 branch
BranchTrueFalseTotal
if (span) { 8 10014 10022
131 8 Copy(span->trace_id, r->trace_id, 16, unsigned char);
132 8 Copy(span->span_id, r->span_id, 8, unsigned char);
133 8 r->has_span = 1;
134 }
135 10022 lg->qcount++;
136 10022 lg->emitted++;
137 10022 }
138  
139 10 static SV *otel_logger_drain(pTHX_ otel_logger *lg, int max) {
140 HV *payload, *rl, *sl, *res, *scope;
141 AV *rls, *sls, *recs;
142 int n, i;
143  
144 10 otel_logger_check_fork(aTHX_ lg);
145 10 ▶ if (lg->qcount <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (lg->qcount <= 0) return NULL; 3 7 10
146 7 n = lg->qcount < max ? lg->qcount : max;
147  
148 7 recs = newAV();
149 21 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 14 7 21
150 14 otel_logrec *r = &lg->queue[lg->qhead];
151 14 HV *h = newHV();
152 char hex[32];
153 14 (void)hv_stores(h, "time_unix_nano", newSVuv((UV)r->time));
154 14 (void)hv_stores(h, "observed_time_unix_nano",
155 newSVuv((UV)otel_wall_nanos()));
156 14 (void)hv_stores(h, "severity_number", newSViv(r->severity));
157 14 ▶ if (r->severity_text)
Branch: 1 branch
BranchTrueFalseTotal
if (r->severity_text) 14 0 14
158 14 (void)hv_stores(h, "severity_text", newSVsv(r->severity_text));
159 14 ▶ if (r->body) (void)hv_stores(h, "body", newSVsv(r->body));
Branch: 1 branch
BranchTrueFalseTotal
if (r->body) (void)hv_stores(h, "body", newSVsv(r->body)); 14 0 14
160 14 ▶ if (r->attrs) (void)hv_stores(h, "attributes",
Branch: 1 branch
BranchTrueFalseTotal
if (r->attrs) (void)hv_stores(h, "attributes", 3 11 14
161 newRV_inc((SV *)r->attrs));
162 14 ▶ if (r->has_span) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->has_span) { 6 8 14
163 6 otel_bytes_to_hex(r->trace_id, 16, hex);
164 6 (void)hv_stores(h, "trace_id", newSVpvn(hex, 32));
165 6 otel_bytes_to_hex(r->span_id, 8, hex);
166 6 (void)hv_stores(h, "span_id", newSVpvn(hex, 16));
167 }
168 14 av_push(recs, newRV_noinc((SV *)h));
169 14 otel_logrec_clear(aTHX_ r);
170 14 lg->qhead = (lg->qhead + 1) % lg->qcap;
171 14 lg->qcount--;
172 }
173  
174 7 scope = newHV();
175 7 ▶ if (lg->scope_name) (void)hv_stores(scope, "name",
Branch: 1 branch
BranchTrueFalseTotal
if (lg->scope_name) (void)hv_stores(scope, "name", 5 2 7
176 newSVsv(lg->scope_name));
177 7 ▶ if (lg->scope_version) (void)hv_stores(scope, "version",
Branch: 1 branch
BranchTrueFalseTotal
if (lg->scope_version) (void)hv_stores(scope, "version", 0 7 7
178 newSVsv(lg->scope_version));
179 7 sl = newHV();
180 7 (void)hv_stores(sl, "scope", newRV_noinc((SV *)scope));
181 7 (void)hv_stores(sl, "log_records", newRV_noinc((SV *)recs));
182 7 sls = newAV();
183 7 av_push(sls, newRV_noinc((SV *)sl));
184  
185 7 res = newHV();
186 7 (void)hv_stores(res, "attributes", newRV_inc((SV *)lg->resource));
187 7 rl = newHV();
188 7 (void)hv_stores(rl, "resource", newRV_noinc((SV *)res));
189 7 (void)hv_stores(rl, "scope_logs", newRV_noinc((SV *)sls));
190 7 rls = newAV();
191 7 av_push(rls, newRV_noinc((SV *)rl));
192  
193 7 payload = newHV();
194 7 (void)hv_stores(payload, "resource_logs", newRV_noinc((SV *)rls));
195 7 return newRV_noinc((SV *)payload);
196 }
197  
198 #endif /* OTEL_LOG_H */
« include/otel_meter.h ↑ Summary include/otel_sample.h »