| 1 |
|
| /* po_rec.h - the one record shape. |
| 2 |
|
| * |
| 3 |
|
| * A span is not a metric point and a log line is neither, so three structs is |
| 4 |
|
| * the obvious design. Taken, the query language then has three row shapes, |
| 5 |
|
| * `where` has three implementations, and the cross-signal stages have to |
| 6 |
|
| * convert between them at runtime. That is the shape most observability |
| 7 |
|
| * stacks have, and it is exactly why they cannot express |
| 8 |
|
| * |
| 9 |
|
| * metric ... | exemplars | traces | logs |
| 10 |
|
| * |
| 11 |
|
| * So: one record, and the signals are three views of it. A span is a record |
| 12 |
|
| * with a duration and a trace id. A log line is a record with a body and a |
| 13 |
|
| * severity. A metric point is a record with a value. The three storage |
| 14 |
|
| * layouts differ in how they lay records OUT, not in what a record IS, and |
| 15 |
|
| * the executor sees po_rec from all three. |
| 16 |
|
| * |
| 17 |
|
| * Fixed width, no pointers, no allocation. Variable-length data lives in a |
| 18 |
|
| * per-batch arena and the record carries offsets into it, which is what makes |
| 19 |
|
| * a memcpy of the record array into the WAL correct without a serialiser. |
| 20 |
|
| */ |
| 21 |
|
| #ifndef PO_REC_H |
| 22 |
|
| #define PO_REC_H |
| 23 |
|
| |
| 24 |
|
| #include "punk_observe/po_compat.h" |
| 25 |
|
| |
| 26 |
|
| #define PO_REC_VERSION 1 |
| 27 |
|
| |
| 28 |
|
| /* kind. The byte has room for PO_PROFILE and nothing in the struct forbids |
| 29 |
|
| * it; that is the entire accommodation OTLP profiles get until the spec |
| 30 |
|
| * settles, and it is deliberately not more. */ |
| 31 |
|
| #define PO_METRIC 1 |
| 32 |
|
| #define PO_LOG 2 |
| 33 |
|
| #define PO_SPAN 3 |
| 34 |
|
| /* #define PO_PROFILE 4 - reserved, not implemented */ |
| 35 |
|
| |
| 36 |
|
| /* flags */ |
| 37 |
|
| #define PO_F_HAS_TRACE 0x0001 /* trace_id is present and non-zero */ |
| 38 |
|
| #define PO_F_HAS_PARENT 0x0002 /* parent_span_id is present */ |
| 39 |
|
| #define PO_F_MONOTONIC 0x0004 /* metric: the sum is monotonic */ |
| 40 |
|
| #define PO_F_CUMULATIVE 0x0008 /* metric: cumulative, else delta */ |
| 41 |
|
| #define PO_F_VALUE_IS_INT 0x0010 /* metric: as_int, not as_double */ |
| 42 |
|
| #define PO_F_CLAMPED_DUR 0x0020 /* span: end < start, clamped, counted */ |
| 43 |
|
| #define PO_F_TRUNCATED 0x0040 /* body or attributes hit a limit */ |
| 44 |
|
| |
| 45 |
|
| /* span kind, in the high nibble of `aux`; OTLP SpanKind values. */ |
| 46 |
|
| #define PO_SK_UNSPECIFIED 0 |
| 47 |
|
| #define PO_SK_INTERNAL 1 |
| 48 |
|
| #define PO_SK_SERVER 2 |
| 49 |
|
| #define PO_SK_CLIENT 3 |
| 50 |
|
| #define PO_SK_PRODUCER 4 |
| 51 |
|
| #define PO_SK_CONSUMER 5 |
| 52 |
|
| |
| 53 |
|
| /* status, OTLP StatusCode. */ |
| 54 |
|
| #define PO_ST_UNSET 0 |
| 55 |
|
| #define PO_ST_OK 1 |
| 56 |
|
| #define PO_ST_ERROR 2 |
| 57 |
|
| |
| 58 |
|
| /* Field order is chosen so that the struct packs the same on every ABI in the |
| 59 |
|
| * smoker matrix: all 8-byte members first, then 4, then 2. t/0001-contracts.t |
| 60 |
|
| * asserts sizeof and the absence of interior padding rather than trusting it. */ |
| 61 |
|
| typedef struct { |
| 62 |
|
| po_u64 t_unix_nano; /* event time; a span's START time */ |
| 63 |
|
| po_u64 series; /* interned (name + label set); 0 until seal */ |
| 64 |
|
| po_u64 trace_id_hi; /* 0 when absent */ |
| 65 |
|
| po_u64 trace_id_lo; |
| 66 |
|
| po_u64 span_id; |
| 67 |
|
| po_u64 parent_span_id; |
| 68 |
|
| po_u64 dur_nano; /* spans; 0 otherwise */ |
| 69 |
|
| double value; /* metric points; also the int, bit-cast */ |
| 70 |
|
| |
| 71 |
|
| uint32_t body_off; /* arena: log body, span name, metric name */ |
| 72 |
|
| uint32_t body_len; |
| 73 |
|
| uint32_t attr_off; /* arena: the sorted attribute block */ |
| 74 |
|
| uint32_t attr_len; |
| 75 |
|
| |
| 76 |
|
| uint16_t severity; /* OTLP SeverityNumber, the 24-point scale */ |
| 77 |
|
| uint16_t flags; |
| 78 |
|
| uint8_t kind; /* PO_METRIC | PO_LOG | PO_SPAN */ |
| 79 |
|
| uint8_t aux; /* span kind low nibble, status high nibble */ |
| 80 |
|
| uint16_t pad; /* explicit, so sizeof is stable and stated */ |
| 81 |
|
| } po_rec; |
| 82 |
|
| |
| 83 |
|
| #define PO_REC_SIZE 88 |
| 84 |
|
| |
| 85 |
|
| /* aux packing. Two 4-bit fields rather than two bytes, because the struct is |
| 86 |
|
| * copied by the million and 88 bytes divides cleanly by 8. */ |
| 87 |
|
| #define po_rec_span_kind(r) ((r)->aux & 0x0F) |
| 88 |
|
| #define po_rec_status(r) (((r)->aux >> 4) & 0x0F) |
| 89 |
|
| #define po_rec_set_aux(r, k, s) ((r)->aux = (uint8_t)(((k) & 0x0F) | (((s) & 0x0F) << 4))) |
| 90 |
|
| |
| 91 |
|
| /* An all-zero trace id is invalid and OTLP says so. It arrives anyway, from a |
| 92 |
|
| * misconfigured propagator, and a "trace" that collects every unparented span |
| 93 |
|
| * from a broken deployment is a landmine in the UI. Rejected at ingest and |
| 94 |
|
| * counted, never stored. */ |
| 95 |
|
| #define po_trace_id_valid(hi, lo) ((hi) != 0 || (lo) != 0) |
| 96 |
|
| |
| 97 |
28317 |
| static void po_rec_zero(po_rec *r) { memset(r, 0, sizeof(*r)); } |
| 98 |
|
| |
| 99 |
|
| /* The arena a batch's variable-length bytes live in. Records hold offsets |
| 100 |
|
| * into it and never pointers, so the whole array survives being memcpy'd to |
| 101 |
|
| * disk, and nothing dangles when the request body goes away. */ |
| 102 |
|
| typedef struct { |
| 103 |
|
| char *base; |
| 104 |
|
| size_t len; |
| 105 |
|
| size_t cap; |
| 106 |
|
| } po_arena; |
| 107 |
|
| |
| 108 |
586 |
| static int po_arena_init(po_arena *a, size_t hint) { |
| 109 |
586 |
▶ | a->base = (char *)malloc(hint ? hint : 4096); |
Branch: 1 branch
| Branch | True | False | Total |
| a->base = (char *)malloc(hint ? hint : 4096); |
586 |
0 |
586 |
|
| 110 |
586 |
▶ | if (!a->base) { a->len = a->cap = 0; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a->base) { a->len = a->cap = 0; return 0; } |
0 |
586 |
586 |
|
| 111 |
586 |
▶ | a->len = 0; a->cap = hint ? hint : 4096; |
Branch: 1 branch
| Branch | True | False | Total |
| a->len = 0; a->cap = hint ? hint : 4096; |
586 |
0 |
586 |
|
| 112 |
586 |
| return 1; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
586 |
| static void po_arena_free(po_arena *a) { |
| 116 |
586 |
| free(a->base); a->base = NULL; a->len = a->cap = 0; |
| 117 |
586 |
| } |
| 118 |
|
| |
| 119 |
|
| /* Returns the offset, or PO_ARENA_ERR. Offsets are uint32_t in po_rec, so an |
| 120 |
|
| * arena is capped at 4GB and the cap is enforced HERE rather than discovered |
| 121 |
|
| * as a wrapped offset pointing at the wrong bytes. */ |
| 122 |
|
| #define PO_ARENA_ERR 0xFFFFFFFFu |
| 123 |
|
| |
| 124 |
101921 |
| static uint32_t po_arena_put(po_arena *a, const char *p, size_t n) { |
| 125 |
101921 |
| size_t off = a->len; |
| 126 |
101921 |
▶ | if (n > PO_ARENA_ERR || off + n >= PO_ARENA_ERR) return PO_ARENA_ERR; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > PO_ARENA_ERR || off + n >= PO_ARENA_ERR) return PO_ARENA_ERR; |
101921 |
0 |
101921 |
| if (n > PO_ARENA_ERR || off + n >= PO_ARENA_ERR) return PO_ARENA_ERR; |
0 |
101921 |
101921 |
|
| 127 |
101921 |
▶ | if (off + n > a->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (off + n > a->cap) { |
160 |
101761 |
101921 |
|
| 128 |
160 |
| size_t want = a->cap * 2; |
| 129 |
|
| char *nb; |
| 130 |
173 |
▶ | while (want < off + n) want *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (want < off + n) want *= 2; |
13 |
160 |
173 |
|
| 131 |
160 |
▶ | if (want >= PO_ARENA_ERR) return PO_ARENA_ERR; |
Branch: 1 branch
| Branch | True | False | Total |
| if (want >= PO_ARENA_ERR) return PO_ARENA_ERR; |
0 |
160 |
160 |
|
| 132 |
160 |
| nb = (char *)realloc(a->base, want); |
| 133 |
160 |
▶ | if (!nb) return PO_ARENA_ERR; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nb) return PO_ARENA_ERR; |
0 |
160 |
160 |
|
| 134 |
160 |
| a->base = nb; a->cap = want; |
| 135 |
|
| } |
| 136 |
101921 |
▶ | if (n) memcpy(a->base + off, p, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) memcpy(a->base + off, p, n); |
101919 |
2 |
101921 |
|
| 137 |
101921 |
| a->len = off + n; |
| 138 |
101921 |
| return (uint32_t)off; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| #endif /* PO_REC_H */ |