@tinyactors/node 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +388 -156
- package/node/index.d.ts +915 -0
- package/node/index.js +44 -0
- package/node/transports.js +60 -0
- package/node/ws.js +152 -0
- package/package.json +22 -17
- package/shared/core.mjs +2966 -0
- package/shared/ecmascript.mjs +670 -0
- package/index.d.ts +0 -1875
- package/index.js +0 -34
- package/lib/actor.js +0 -85
- package/lib/debug/breakpoints.js +0 -349
- package/lib/debug/console.js +0 -149
- package/lib/debug/expressions.js +0 -133
- package/lib/debug/frames.js +0 -119
- package/lib/debug/labels.js +0 -142
- package/lib/debug/macrosteps.js +0 -207
- package/lib/debug/protocol.js +0 -186
- package/lib/debug/sources.js +0 -218
- package/lib/debug/variables.js +0 -265
- package/lib/debug.js +0 -1434
- package/lib/definition.js +0 -139
- package/lib/errors.js +0 -24
- package/lib/events.js +0 -56
- package/lib/image.js +0 -90
- package/lib/loader.js +0 -68
- package/lib/metrics.js +0 -333
- package/lib/native.js +0 -8
- package/lib/otlp.js +0 -332
- package/lib/statechart.js +0 -768
- package/lib/system.js +0 -2183
- package/lib/tracing.js +0 -164
package/lib/metrics.js
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Metrics as plain data (napi ADR 0017 section 7, core ADR 0072).
|
|
4
|
-
//
|
|
5
|
-
// system.metrics() returns a snapshot: a list of metrics, each with a
|
|
6
|
-
// name, a unit, a kind ("sum" for cumulative monotonic counters, "gauge"
|
|
7
|
-
// for values that go up and down, "histogram" for distributions) and data
|
|
8
|
-
// points of attributes and a value. A histogram point instead has count,
|
|
9
|
-
// sum, min, max, explicit bucket bounds and counts, and exemplars that link
|
|
10
|
-
// a bucket to the trace of a sampled step. That is the shape OpenTelemetry's observable instruments read, so
|
|
11
|
-
// an SDK user registers one observable per metric and reports its points;
|
|
12
|
-
// a user without an SDK hands the snapshot to metricsToOTLP() and posts the
|
|
13
|
-
// bytes to /v1/metrics. No telemetry dependency either way.
|
|
14
|
-
//
|
|
15
|
-
// Names follow core ADR 0072 section 6. Attributes never name one actor:
|
|
16
|
-
// a metric says how many and where; which ones is inspection's job.
|
|
17
|
-
|
|
18
|
-
const { TinyactorsError } = require('./errors');
|
|
19
|
-
|
|
20
|
-
// core ADR 0072's counters: [field, metric name, unit, extra attributes].
|
|
21
|
-
const COUNTERS = [
|
|
22
|
-
['spawned', 'tinyactors.actors.spawned', '{actor}'],
|
|
23
|
-
['loaded', 'tinyactors.actors.loaded', '{actor}'],
|
|
24
|
-
['unloaded', 'tinyactors.actors.unloaded', '{actor}'],
|
|
25
|
-
['finished', 'tinyactors.actors.finished', '{actor}'],
|
|
26
|
-
['destroyed', 'tinyactors.actors.destroyed', '{actor}'],
|
|
27
|
-
['events_taken', 'tinyactors.events.taken', '{event}'],
|
|
28
|
-
['events_ignored', 'tinyactors.events.ignored', '{event}', { 'scxml.event.type': 'external' }],
|
|
29
|
-
['internal_ignored', 'tinyactors.events.ignored', '{event}', { 'scxml.event.type': 'internal' }],
|
|
30
|
-
['macrosteps', 'tinyactors.macrosteps', '{step}'],
|
|
31
|
-
['microsteps', 'tinyactors.microsteps', '{step}'],
|
|
32
|
-
['errors_execution', 'tinyactors.errors', '{error}', { 'scxml.error.type': 'execution' }],
|
|
33
|
-
['errors_communication', 'tinyactors.errors', '{error}', { 'scxml.error.type': 'communication' }],
|
|
34
|
-
['errors_platform', 'tinyactors.errors', '{error}', { 'scxml.error.type': 'platform' }],
|
|
35
|
-
['sends', 'tinyactors.sends', '{send}'],
|
|
36
|
-
['sends_delayed', 'tinyactors.sends.delayed', '{send}'],
|
|
37
|
-
['sends_cancelled', 'tinyactors.sends.cancelled', '{send}'],
|
|
38
|
-
['delayed_fired', 'tinyactors.timers.fired', '{send}'],
|
|
39
|
-
['invocations_started', 'tinyactors.invocations.started', '{invocation}'],
|
|
40
|
-
['invocations_ended', 'tinyactors.invocations.ended', '{invocation}'],
|
|
41
|
-
['runs_paused', 'tinyactors.runs.paused', '{run}'],
|
|
42
|
-
['runs_stopped', 'tinyactors.runs.stopped', '{run}'],
|
|
43
|
-
['macrosteps_long', 'tinyactors.macrosteps.long', '{macrostep}'],
|
|
44
|
-
['runaways', 'tinyactors.macrosteps.runaway', '{macrostep}'],
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The snapshot system.metrics() returns. `definitions` is a list of
|
|
49
|
-
* { attributes, counters, occupancy: [{ state, actors }] } for the live
|
|
50
|
-
* definitions; `system` is core's totals (freed definitions included);
|
|
51
|
-
* `host` is napi's own view: messages, memory, and what native
|
|
52
|
-
* hostMetrics() reports (turn duration, timer lateness and overdue time,
|
|
53
|
-
* the run queues, quarantine, faults by code, dead letters by reason,
|
|
54
|
-
* loads of unloaded actors: outcomes, duration and waiting mail, and the
|
|
55
|
-
* definition lifecycle: events, and what is alive by origin and state).
|
|
56
|
-
*/
|
|
57
|
-
function buildSnapshot({ startTime, time, systemName, system, definitions, host }) {
|
|
58
|
-
const metrics = new Map();
|
|
59
|
-
const metric = (name, unit, kind) => {
|
|
60
|
-
let entry = metrics.get(name);
|
|
61
|
-
if (entry === undefined) {
|
|
62
|
-
entry = { name, unit, kind, points: [] };
|
|
63
|
-
metrics.set(name, entry);
|
|
64
|
-
}
|
|
65
|
-
return entry;
|
|
66
|
-
};
|
|
67
|
-
// Every metric is listed from the first snapshot on, with no points until
|
|
68
|
-
// there is something to count, so a reader can register its instruments
|
|
69
|
-
// once.
|
|
70
|
-
for (const [, name, unit] of COUNTERS) metric(name, unit, 'sum');
|
|
71
|
-
metric('tinyactors.actors.occupancy', '{actor}', 'gauge');
|
|
72
|
-
const base = { 'tinyactors.system': systemName };
|
|
73
|
-
for (const definition of definitions) {
|
|
74
|
-
const attributes = { ...base, ...definition.attributes };
|
|
75
|
-
for (const [field, name, unit, extra] of COUNTERS) {
|
|
76
|
-
metric(name, unit, 'sum').points.push({ attributes: { ...attributes, ...extra }, value: definition.counters[field] });
|
|
77
|
-
}
|
|
78
|
-
const occupancy = metric('tinyactors.actors.occupancy', '{actor}', 'gauge');
|
|
79
|
-
for (const { state, actors } of definition.occupancy) {
|
|
80
|
-
occupancy.points.push({ attributes: { ...attributes, 'scxml.state': state }, value: actors });
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
metric('tinyactors.messages.queued', '{message}', 'gauge').points.push({ attributes: base, value: host.queued });
|
|
84
|
-
metric('tinyactors.messages.delivered', '{message}', 'sum').points.push({ attributes: base, value: host.delivered });
|
|
85
|
-
const dead = metric('tinyactors.messages.dead_lettered', '{message}', 'sum');
|
|
86
|
-
for (const [reason, count] of Object.entries(host.native.deadLetters)) {
|
|
87
|
-
dead.points.push({ attributes: { ...base, 'tinyactors.dead_letter.reason': reason }, value: count });
|
|
88
|
-
}
|
|
89
|
-
// Is the host keeping up? Turn duration, the run queues, quarantine.
|
|
90
|
-
metric('tinyactors.pump.turn.duration', 's', 'histogram')
|
|
91
|
-
.points.push({ attributes: base, ...histogramPoint(host.native.turn) });
|
|
92
|
-
metric('tinyactors.scheduler.runnable', '{actor}', 'gauge').points.push({ attributes: base, value: host.native.runnable });
|
|
93
|
-
metric('tinyactors.scheduler.ready', '{actor}', 'gauge').points.push({ attributes: base, value: host.native.ready });
|
|
94
|
-
metric('tinyactors.actors.quarantined', '{actor}', 'gauge')
|
|
95
|
-
.points.push({ attributes: base, value: host.native.quarantined });
|
|
96
|
-
const faults = metric('tinyactors.faults', '{fault}', 'sum');
|
|
97
|
-
for (const [code, count] of Object.entries(host.native.faults)) {
|
|
98
|
-
faults.points.push({ attributes: { ...base, 'tinyactors.fault.code': code }, value: count });
|
|
99
|
-
}
|
|
100
|
-
// Are timers late? How late each fired, and how far behind the next is.
|
|
101
|
-
metric('tinyactors.timers.lateness', 's', 'histogram')
|
|
102
|
-
.points.push({ attributes: base, ...histogramPoint(host.native.lateness) });
|
|
103
|
-
metric('tinyactors.timers.overdue', 's', 'gauge').points.push({ attributes: base, value: host.native.overdue });
|
|
104
|
-
// Loads of unloaded actors (napi ADR 0020): outcomes, how long the loader
|
|
105
|
-
// took, and the mail waiting for it.
|
|
106
|
-
const loads = metric('tinyactors.loads', '{load}', 'sum');
|
|
107
|
-
for (const [outcome, count] of Object.entries(host.native.loads)) {
|
|
108
|
-
loads.points.push({ attributes: { ...base, 'tinyactors.load.outcome': outcome }, value: count });
|
|
109
|
-
}
|
|
110
|
-
metric('tinyactors.load.duration', 's', 'histogram')
|
|
111
|
-
.points.push({ attributes: base, ...histogramPoint(host.native.load) });
|
|
112
|
-
metric('tinyactors.messages.waiting', '{message}', 'gauge').points.push({ attributes: base, value: host.native.waiting });
|
|
113
|
-
// The definition lifecycle (core ADR 0079). A registered count that keeps
|
|
114
|
-
// climbing with a freed count that keeps pace is churn: an invoke that
|
|
115
|
-
// compiles new chart text for every child.
|
|
116
|
-
const lifecycle = host.native.definitions;
|
|
117
|
-
metric('tinyactors.definitions.registered', '{definition}', 'sum')
|
|
118
|
-
.points.push({ attributes: base, value: lifecycle.registered });
|
|
119
|
-
metric('tinyactors.definitions.retired', '{definition}', 'sum').points.push({ attributes: base, value: lifecycle.retired });
|
|
120
|
-
metric('tinyactors.definitions.freed', '{definition}', 'sum').points.push({ attributes: base, value: lifecycle.freed });
|
|
121
|
-
const alive = metric('tinyactors.definitions', '{definition}', 'gauge');
|
|
122
|
-
for (const { origin, retired, count } of lifecycle.alive) {
|
|
123
|
-
alive.points.push({
|
|
124
|
-
attributes: { ...base, 'tinyactors.definition.origin': origin,
|
|
125
|
-
'tinyactors.definition.state': retired ? 'retired' : 'live' },
|
|
126
|
-
value: count,
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
const memory = metric('tinyactors.memory.usage', 'By', 'gauge');
|
|
130
|
-
for (const [category, bytes] of Object.entries(host.memory)) {
|
|
131
|
-
memory.points.push({ attributes: { ...base, 'tinyactors.memory.category': category }, value: bytes });
|
|
132
|
-
}
|
|
133
|
-
return { startTime, time, system, metrics: [...metrics.values()] };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// A native histogram as a data point: min and max only once something was recorded.
|
|
137
|
-
function histogramPoint(native) {
|
|
138
|
-
const point = { count: native.count, sum: native.sum, bounds: native.bounds, buckets: native.buckets,
|
|
139
|
-
exemplars: native.exemplars };
|
|
140
|
-
if (native.count !== 0) {
|
|
141
|
-
point.min = native.min;
|
|
142
|
-
point.max = native.max;
|
|
143
|
-
}
|
|
144
|
-
return point;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// ------------------------------------------------------------------------
|
|
148
|
-
// metricsToOTLP: a small protobuf writer for ExportMetricsServiceRequest
|
|
149
|
-
// (opentelemetry-proto, metrics/v1), no dependency.
|
|
150
|
-
|
|
151
|
-
const encoder = new TextEncoder();
|
|
152
|
-
|
|
153
|
-
class Writer {
|
|
154
|
-
constructor() {
|
|
155
|
-
this.parts = [];
|
|
156
|
-
this.length = 0;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
push(bytes) {
|
|
160
|
-
this.parts.push(bytes);
|
|
161
|
-
this.length += bytes.length;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
varint(value) {
|
|
165
|
-
const out = [];
|
|
166
|
-
let v = BigInt(value);
|
|
167
|
-
do {
|
|
168
|
-
let byte = Number(v & 0x7fn);
|
|
169
|
-
v >>= 7n;
|
|
170
|
-
if (v !== 0n) byte |= 0x80;
|
|
171
|
-
out.push(byte);
|
|
172
|
-
} while (v !== 0n);
|
|
173
|
-
this.push(Uint8Array.from(out));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
tag(field, wire) {
|
|
177
|
-
this.varint((field << 3) | wire);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
bytes(field, bytes) {
|
|
181
|
-
this.tag(field, 2);
|
|
182
|
-
this.varint(bytes.length);
|
|
183
|
-
this.push(bytes);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
string(field, text) {
|
|
187
|
-
this.bytes(field, encoder.encode(String(text)));
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
message(field, build) {
|
|
191
|
-
const inner = new Writer();
|
|
192
|
-
build(inner);
|
|
193
|
-
this.bytes(field, inner.finish());
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
double(field, value) {
|
|
197
|
-
this.tag(field, 1);
|
|
198
|
-
const out = new Uint8Array(8);
|
|
199
|
-
new DataView(out.buffer).setFloat64(0, value, true);
|
|
200
|
-
this.push(out);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// A packed repeated fixed64 or double field.
|
|
204
|
-
packed(field, values, write) {
|
|
205
|
-
const out = new Uint8Array(values.length * 8);
|
|
206
|
-
const view = new DataView(out.buffer);
|
|
207
|
-
values.forEach((value, at) => write(view, at * 8, value));
|
|
208
|
-
this.bytes(field, out);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
hex(field, text) {
|
|
212
|
-
const out = new Uint8Array(text.length / 2);
|
|
213
|
-
for (let at = 0; at < out.length; at += 1) out[at] = Number.parseInt(text.slice(at * 2, at * 2 + 2), 16);
|
|
214
|
-
this.bytes(field, out);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
fixed64(field, value) {
|
|
218
|
-
this.tag(field, 1);
|
|
219
|
-
const out = new Uint8Array(8);
|
|
220
|
-
new DataView(out.buffer).setBigUint64(0, BigInt.asUintN(64, BigInt(value)), true);
|
|
221
|
-
this.push(out);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
finish() {
|
|
225
|
-
const out = new Uint8Array(this.length);
|
|
226
|
-
let at = 0;
|
|
227
|
-
for (const part of this.parts) {
|
|
228
|
-
out.set(part, at);
|
|
229
|
-
at += part.length;
|
|
230
|
-
}
|
|
231
|
-
return out;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const nanos = (ms) => BigInt(Math.round(ms)) * 1000000n;
|
|
236
|
-
|
|
237
|
-
function keyValues(writer, field, attributes) {
|
|
238
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
239
|
-
writer.message(field, (kv) => {
|
|
240
|
-
kv.string(1, key);
|
|
241
|
-
kv.message(2, (any) => any.string(1, value));
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function dataPoints(writer, field, metric, startTime, time) {
|
|
247
|
-
for (const point of metric.points) {
|
|
248
|
-
writer.message(field, (p) => {
|
|
249
|
-
if (metric.kind === 'sum') p.fixed64(2, nanos(startTime));
|
|
250
|
-
p.fixed64(3, nanos(time));
|
|
251
|
-
if (Number.isInteger(point.value)) {
|
|
252
|
-
p.fixed64(6, point.value); // as_int (sfixed64)
|
|
253
|
-
} else {
|
|
254
|
-
p.double(4, point.value); // as_double
|
|
255
|
-
}
|
|
256
|
-
keyValues(p, 7, point.attributes);
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// HistogramDataPoint (opentelemetry-proto metrics/v1), with its exemplars.
|
|
262
|
-
function histogramPoints(writer, metric, startTime, time) {
|
|
263
|
-
for (const point of metric.points) {
|
|
264
|
-
writer.message(1, (p) => {
|
|
265
|
-
p.fixed64(2, nanos(startTime));
|
|
266
|
-
p.fixed64(3, nanos(time));
|
|
267
|
-
p.fixed64(4, point.count);
|
|
268
|
-
p.double(5, point.sum);
|
|
269
|
-
p.packed(6, point.buckets, (view, at, value) => view.setBigUint64(at, BigInt(value), true));
|
|
270
|
-
p.packed(7, point.bounds, (view, at, value) => view.setFloat64(at, value, true));
|
|
271
|
-
for (const exemplar of point.exemplars) {
|
|
272
|
-
p.message(8, (e) => {
|
|
273
|
-
e.fixed64(2, BigInt(Math.round(exemplar.time * 1000)) * 1000n);
|
|
274
|
-
e.double(3, exemplar.value);
|
|
275
|
-
e.hex(4, exemplar.spanID);
|
|
276
|
-
e.hex(5, exemplar.traceID);
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
keyValues(p, 9, point.attributes);
|
|
280
|
-
if (point.min !== undefined) p.double(11, point.min);
|
|
281
|
-
if (point.max !== undefined) p.double(12, point.max);
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* The OTLP protobuf bytes of a metrics snapshot: one
|
|
288
|
-
* ExportMetricsServiceRequest, to post to an OTLP/HTTP `/v1/metrics`
|
|
289
|
-
* endpoint with Content-Type application/x-protobuf. `options.resource`
|
|
290
|
-
* holds resource attributes (strings), as setTraceSink's does.
|
|
291
|
-
*/
|
|
292
|
-
function metricsToOTLP(snapshot, options = {}) {
|
|
293
|
-
if (snapshot === null || typeof snapshot !== 'object' || !Array.isArray(snapshot.metrics)) {
|
|
294
|
-
throw new TinyactorsError('metricsToOTLP(): expected a snapshot from system.metrics()', 'invalid');
|
|
295
|
-
}
|
|
296
|
-
const { resource = {}, version = '' } = options;
|
|
297
|
-
const request = new Writer();
|
|
298
|
-
request.message(1, (resourceMetrics) => {
|
|
299
|
-
resourceMetrics.message(1, (r) => keyValues(r, 1, resource));
|
|
300
|
-
resourceMetrics.message(2, (scopeMetrics) => {
|
|
301
|
-
scopeMetrics.message(1, (scope) => {
|
|
302
|
-
scope.string(1, 'tinyactors');
|
|
303
|
-
if (version !== '') scope.string(2, version);
|
|
304
|
-
});
|
|
305
|
-
for (const metric of snapshot.metrics) {
|
|
306
|
-
scopeMetrics.message(2, (m) => {
|
|
307
|
-
m.string(1, metric.name);
|
|
308
|
-
m.string(3, metric.unit);
|
|
309
|
-
if (metric.kind === 'histogram') {
|
|
310
|
-
m.message(9, (histogram) => {
|
|
311
|
-
histogramPoints(histogram, metric, snapshot.startTime, snapshot.time);
|
|
312
|
-
histogram.tag(2, 0);
|
|
313
|
-
histogram.varint(2); // AGGREGATION_TEMPORALITY_CUMULATIVE
|
|
314
|
-
});
|
|
315
|
-
} else if (metric.kind === 'sum') {
|
|
316
|
-
m.message(7, (sum) => {
|
|
317
|
-
dataPoints(sum, 1, metric, snapshot.startTime, snapshot.time);
|
|
318
|
-
sum.tag(2, 0);
|
|
319
|
-
sum.varint(2); // AGGREGATION_TEMPORALITY_CUMULATIVE
|
|
320
|
-
sum.tag(3, 0);
|
|
321
|
-
sum.varint(1); // is_monotonic
|
|
322
|
-
});
|
|
323
|
-
} else {
|
|
324
|
-
m.message(5, (gauge) => dataPoints(gauge, 1, metric, snapshot.startTime, snapshot.time));
|
|
325
|
-
}
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
return request.finish();
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
module.exports = { buildSnapshot, metricsToOTLP, COUNTERS };
|
package/lib/native.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// The compiled addon. Every other lib/ module reaches native code through
|
|
4
|
-
// this one, so the addon is loaded, and its error class registered, once.
|
|
5
|
-
// lib/loader.js finds it: the installed platform package, else the
|
|
6
|
-
// repository build.
|
|
7
|
-
|
|
8
|
-
module.exports = require('./loader').loadAddon();
|
package/lib/otlp.js
DELETED
|
@@ -1,332 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// decodeTraces(bytes): OTLP protobuf (an ExportTraceServiceRequest, or the
|
|
4
|
-
// TracesData of the same shape) to span-shaped objects (napi ADR 0016). A
|
|
5
|
-
// small reader with no dependency, for those who want objects: in-process
|
|
6
|
-
// span processors, exporters that don't speak OTLP, verifiers. It is strict:
|
|
7
|
-
// a malformed message throws. It reads any OTLP trace request, not only the
|
|
8
|
-
// ones tinyactors writes.
|
|
9
|
-
|
|
10
|
-
const { TinyactorsError } = require('./errors');
|
|
11
|
-
|
|
12
|
-
const HEX = Array.from({ length: 256 }, (_, byte) => byte.toString(16).padStart(2, '0'));
|
|
13
|
-
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
14
|
-
|
|
15
|
-
function malformed(what) {
|
|
16
|
-
return new TinyactorsError(`decodeTraces(): malformed OTLP: ${what}`, 'invalid');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// A cursor over one message's bytes.
|
|
20
|
-
class Reader {
|
|
21
|
-
constructor(bytes, start, end) {
|
|
22
|
-
this.bytes = bytes;
|
|
23
|
-
this.at = start;
|
|
24
|
-
this.end = end;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get done() {
|
|
28
|
-
return this.at >= this.end;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// A varint as a number when it is safe, else a bigint.
|
|
32
|
-
varint() {
|
|
33
|
-
let value = 0;
|
|
34
|
-
let scale = 1;
|
|
35
|
-
for (let shift = 0; shift < 70; shift += 7) {
|
|
36
|
-
if (this.at >= this.end) throw malformed('a varint runs past its message');
|
|
37
|
-
const byte = this.bytes[this.at++];
|
|
38
|
-
value += (byte & 0x7f) * scale;
|
|
39
|
-
scale *= 128;
|
|
40
|
-
if ((byte & 0x80) === 0) {
|
|
41
|
-
return Number.isSafeInteger(value) ? value : this.#bigVarint(shift);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
throw malformed('a varint is longer than ten bytes');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Re-reads the varint that just ended as a bigint.
|
|
48
|
-
#bigVarint(lastShift) {
|
|
49
|
-
let at = this.at - lastShift / 7 - 1;
|
|
50
|
-
let value = 0n;
|
|
51
|
-
for (let shift = 0n; at < this.at; shift += 7n, at += 1) {
|
|
52
|
-
value |= BigInt(this.bytes[at] & 0x7f) << shift;
|
|
53
|
-
}
|
|
54
|
-
return BigInt.asUintN(64, value);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
fixed64() {
|
|
58
|
-
if (this.end - this.at < 8) throw malformed('a fixed64 runs past its message');
|
|
59
|
-
let value = 0n;
|
|
60
|
-
for (let at = 7; at >= 0; at -= 1) value = (value << 8n) | BigInt(this.bytes[this.at + at]);
|
|
61
|
-
this.at += 8;
|
|
62
|
-
return value;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// The next field: [number, wire type].
|
|
66
|
-
tag() {
|
|
67
|
-
const tag = this.varint();
|
|
68
|
-
const field = Number(tag) >>> 3;
|
|
69
|
-
if (field === 0) throw malformed('a field number is 0');
|
|
70
|
-
return [field, Number(tag) & 7];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// A length-delimited body, as a reader.
|
|
74
|
-
body() {
|
|
75
|
-
const length = this.varint();
|
|
76
|
-
if (typeof length !== 'number' || length > this.end - this.at) throw malformed('a length runs past its message');
|
|
77
|
-
const reader = new Reader(this.bytes, this.at, this.at + length);
|
|
78
|
-
this.at += length;
|
|
79
|
-
return reader;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
skip(wire) {
|
|
83
|
-
switch (wire) {
|
|
84
|
-
case 0: this.varint(); return;
|
|
85
|
-
case 1: this.fixed64(); return;
|
|
86
|
-
case 2: this.body(); return;
|
|
87
|
-
case 5:
|
|
88
|
-
if (this.end - this.at < 4) throw malformed('a fixed32 runs past its message');
|
|
89
|
-
this.at += 4;
|
|
90
|
-
return;
|
|
91
|
-
default: throw malformed(`wire type ${wire}`);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
string() {
|
|
96
|
-
const body = this.body();
|
|
97
|
-
return decoder.decode(this.bytes.subarray(body.at, body.end));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
hex() {
|
|
101
|
-
const body = this.body();
|
|
102
|
-
let text = '';
|
|
103
|
-
for (let at = body.at; at < body.end; at += 1) text += HEX[this.bytes[at]];
|
|
104
|
-
return text;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function expect(wire, wanted, what) {
|
|
109
|
-
if (wire !== wanted) throw malformed(`${what} has wire type ${wire}`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// AnyValue → a JavaScript value: string, boolean, number (or bigint when
|
|
113
|
-
// it is not safe), array, or an object for a kvlist.
|
|
114
|
-
function anyValue(reader) {
|
|
115
|
-
let value;
|
|
116
|
-
while (!reader.done) {
|
|
117
|
-
const [field, wire] = reader.tag();
|
|
118
|
-
switch (field) {
|
|
119
|
-
case 1: expect(wire, 2, 'string_value'); value = reader.string(); break;
|
|
120
|
-
case 2: expect(wire, 0, 'bool_value'); value = reader.varint() !== 0; break;
|
|
121
|
-
case 3: {
|
|
122
|
-
expect(wire, 0, 'int_value');
|
|
123
|
-
const raw = reader.varint();
|
|
124
|
-
value = typeof raw === 'bigint' ? BigInt.asIntN(64, raw) : raw;
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
case 4: {
|
|
128
|
-
expect(wire, 1, 'double_value');
|
|
129
|
-
const bits = reader.fixed64();
|
|
130
|
-
const view = new DataView(new ArrayBuffer(8));
|
|
131
|
-
view.setBigUint64(0, bits);
|
|
132
|
-
value = view.getFloat64(0);
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
case 5: {
|
|
136
|
-
expect(wire, 2, 'array_value');
|
|
137
|
-
const array = reader.body();
|
|
138
|
-
value = [];
|
|
139
|
-
while (!array.done) {
|
|
140
|
-
const [item, itemWire] = array.tag();
|
|
141
|
-
if (item === 1) {
|
|
142
|
-
expect(itemWire, 2, 'ArrayValue.values');
|
|
143
|
-
value.push(anyValue(array.body()));
|
|
144
|
-
} else {
|
|
145
|
-
array.skip(itemWire);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
case 6: expect(wire, 2, 'kvlist_value'); value = attributes(reader.body(), 1); break;
|
|
151
|
-
case 7: expect(wire, 2, 'bytes_value'); value = reader.hex(); break;
|
|
152
|
-
default: reader.skip(wire);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return value;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Repeated KeyValue at `field` of a message → { key: value }.
|
|
159
|
-
function keyValue(reader, into) {
|
|
160
|
-
let key;
|
|
161
|
-
let value;
|
|
162
|
-
while (!reader.done) {
|
|
163
|
-
const [field, wire] = reader.tag();
|
|
164
|
-
if (field === 1) {
|
|
165
|
-
expect(wire, 2, 'KeyValue.key');
|
|
166
|
-
key = reader.string();
|
|
167
|
-
} else if (field === 2) {
|
|
168
|
-
expect(wire, 2, 'KeyValue.value');
|
|
169
|
-
value = anyValue(reader.body());
|
|
170
|
-
} else {
|
|
171
|
-
reader.skip(wire);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
if (key !== undefined) into[key] = value;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function attributes(reader, field) {
|
|
178
|
-
const out = {};
|
|
179
|
-
while (!reader.done) {
|
|
180
|
-
const [number, wire] = reader.tag();
|
|
181
|
-
if (number === field) {
|
|
182
|
-
expect(wire, 2, 'attributes');
|
|
183
|
-
keyValue(reader.body(), out);
|
|
184
|
-
} else {
|
|
185
|
-
reader.skip(wire);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return out;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const nanosToMillis = (nanos) => Number(nanos / 1000n) / 1000;
|
|
192
|
-
|
|
193
|
-
const STATUS = ['unset', 'ok', 'error'];
|
|
194
|
-
const KINDS = ['unspecified', 'internal', 'server', 'client', 'producer', 'consumer'];
|
|
195
|
-
|
|
196
|
-
function span(reader, resource, scope) {
|
|
197
|
-
const out = {
|
|
198
|
-
traceID: '', spanID: '', parentSpanID: undefined, name: '', kind: 'unspecified',
|
|
199
|
-
startTimeUnixNano: 0n, endTimeUnixNano: 0n, startTime: 0, endTime: 0,
|
|
200
|
-
attributes: {}, events: [], links: [], status: { code: 'unset' }, resource, scope,
|
|
201
|
-
};
|
|
202
|
-
while (!reader.done) {
|
|
203
|
-
const [field, wire] = reader.tag();
|
|
204
|
-
switch (field) {
|
|
205
|
-
case 1: expect(wire, 2, 'trace_id'); out.traceID = reader.hex(); break;
|
|
206
|
-
case 2: expect(wire, 2, 'span_id'); out.spanID = reader.hex(); break;
|
|
207
|
-
case 4: {
|
|
208
|
-
expect(wire, 2, 'parent_span_id');
|
|
209
|
-
const parent = reader.hex();
|
|
210
|
-
if (parent !== '') out.parentSpanID = parent;
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
case 5: expect(wire, 2, 'name'); out.name = reader.string(); break;
|
|
214
|
-
case 6: expect(wire, 0, 'kind'); out.kind = KINDS[reader.varint()] ?? 'unspecified'; break;
|
|
215
|
-
case 7: expect(wire, 1, 'start_time_unix_nano'); out.startTimeUnixNano = reader.fixed64(); break;
|
|
216
|
-
case 8: expect(wire, 1, 'end_time_unix_nano'); out.endTimeUnixNano = reader.fixed64(); break;
|
|
217
|
-
case 9: expect(wire, 2, 'attributes'); keyValue(reader.body(), out.attributes); break;
|
|
218
|
-
case 11: {
|
|
219
|
-
expect(wire, 2, 'events');
|
|
220
|
-
const event = reader.body();
|
|
221
|
-
const item = { name: '', timeUnixNano: 0n, time: 0, attributes: {} };
|
|
222
|
-
while (!event.done) {
|
|
223
|
-
const [number, eventWire] = event.tag();
|
|
224
|
-
if (number === 1) { expect(eventWire, 1, 'Event.time_unix_nano'); item.timeUnixNano = event.fixed64(); }
|
|
225
|
-
else if (number === 2) { expect(eventWire, 2, 'Event.name'); item.name = event.string(); }
|
|
226
|
-
else if (number === 3) { expect(eventWire, 2, 'Event.attributes'); keyValue(event.body(), item.attributes); }
|
|
227
|
-
else event.skip(eventWire);
|
|
228
|
-
}
|
|
229
|
-
item.time = nanosToMillis(item.timeUnixNano);
|
|
230
|
-
out.events.push(item);
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
case 13: {
|
|
234
|
-
expect(wire, 2, 'links');
|
|
235
|
-
const link = reader.body();
|
|
236
|
-
const item = { traceID: '', spanID: '', attributes: {} };
|
|
237
|
-
while (!link.done) {
|
|
238
|
-
const [number, linkWire] = link.tag();
|
|
239
|
-
if (number === 1) { expect(linkWire, 2, 'Link.trace_id'); item.traceID = link.hex(); }
|
|
240
|
-
else if (number === 2) { expect(linkWire, 2, 'Link.span_id'); item.spanID = link.hex(); }
|
|
241
|
-
else if (number === 4) { expect(linkWire, 2, 'Link.attributes'); keyValue(link.body(), item.attributes); }
|
|
242
|
-
else link.skip(linkWire);
|
|
243
|
-
}
|
|
244
|
-
out.links.push(item);
|
|
245
|
-
break;
|
|
246
|
-
}
|
|
247
|
-
case 15: {
|
|
248
|
-
expect(wire, 2, 'status');
|
|
249
|
-
const status = reader.body();
|
|
250
|
-
while (!status.done) {
|
|
251
|
-
const [number, statusWire] = status.tag();
|
|
252
|
-
if (number === 3) { expect(statusWire, 0, 'Status.code'); out.status.code = STATUS[status.varint()] ?? 'unset'; }
|
|
253
|
-
else if (number === 2) { expect(statusWire, 2, 'Status.message'); out.status.message = status.string(); }
|
|
254
|
-
else status.skip(statusWire);
|
|
255
|
-
}
|
|
256
|
-
break;
|
|
257
|
-
}
|
|
258
|
-
default: reader.skip(wire);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
if (out.traceID.length !== 32) throw malformed('a span has no 16-byte trace_id');
|
|
262
|
-
if (out.spanID.length !== 16) throw malformed('a span has no 8-byte span_id');
|
|
263
|
-
out.startTime = nanosToMillis(out.startTimeUnixNano);
|
|
264
|
-
out.endTime = nanosToMillis(out.endTimeUnixNano);
|
|
265
|
-
return out;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Decodes OTLP trace bytes (the Uint8Array a trace sink receives) into
|
|
270
|
-
* spans: { traceID, spanID, parentSpanID?, name, kind, startTime, endTime
|
|
271
|
-
* (milliseconds since the epoch), startTimeUnixNano, endTimeUnixNano
|
|
272
|
-
* (bigints), attributes, events: [{ name, time, timeUnixNano, attributes }],
|
|
273
|
-
* links: [{ traceID, spanID, attributes }], status: { code, message? },
|
|
274
|
-
* resource: { attributes }, scope: { name, version } }. IDs are lowercase
|
|
275
|
-
* hex. Spans sharing a resource or scope share that object.
|
|
276
|
-
*/
|
|
277
|
-
function decodeTraces(bytes) {
|
|
278
|
-
if (!(bytes instanceof Uint8Array)) {
|
|
279
|
-
throw new TinyactorsError('decodeTraces(): expected a Uint8Array', 'invalid');
|
|
280
|
-
}
|
|
281
|
-
const request = new Reader(bytes, 0, bytes.length);
|
|
282
|
-
const spans = [];
|
|
283
|
-
while (!request.done) {
|
|
284
|
-
const [field, wire] = request.tag();
|
|
285
|
-
if (field !== 1) {
|
|
286
|
-
request.skip(wire);
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
expect(wire, 2, 'resource_spans');
|
|
290
|
-
const resourceSpans = request.body();
|
|
291
|
-
const resource = { attributes: {} };
|
|
292
|
-
const scopes = [];
|
|
293
|
-
while (!resourceSpans.done) {
|
|
294
|
-
const [number, rsWire] = resourceSpans.tag();
|
|
295
|
-
if (number === 1) {
|
|
296
|
-
expect(rsWire, 2, 'resource');
|
|
297
|
-
resource.attributes = attributes(resourceSpans.body(), 1);
|
|
298
|
-
} else if (number === 2) {
|
|
299
|
-
expect(rsWire, 2, 'scope_spans');
|
|
300
|
-
scopes.push(resourceSpans.body());
|
|
301
|
-
} else {
|
|
302
|
-
resourceSpans.skip(rsWire);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
for (const scopeSpans of scopes) {
|
|
306
|
-
const scope = { name: '', version: '' };
|
|
307
|
-
const bodies = [];
|
|
308
|
-
while (!scopeSpans.done) {
|
|
309
|
-
const [number, ssWire] = scopeSpans.tag();
|
|
310
|
-
if (number === 1) {
|
|
311
|
-
expect(ssWire, 2, 'scope');
|
|
312
|
-
const reader = scopeSpans.body();
|
|
313
|
-
while (!reader.done) {
|
|
314
|
-
const [item, itemWire] = reader.tag();
|
|
315
|
-
if (item === 1) { expect(itemWire, 2, 'scope.name'); scope.name = reader.string(); }
|
|
316
|
-
else if (item === 2) { expect(itemWire, 2, 'scope.version'); scope.version = reader.string(); }
|
|
317
|
-
else reader.skip(itemWire);
|
|
318
|
-
}
|
|
319
|
-
} else if (number === 2) {
|
|
320
|
-
expect(ssWire, 2, 'spans');
|
|
321
|
-
bodies.push(scopeSpans.body());
|
|
322
|
-
} else {
|
|
323
|
-
scopeSpans.skip(ssWire);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
for (const body of bodies) spans.push(span(body, resource, scope));
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
return spans;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
module.exports = { decodeTraces };
|