@blamejs/core 0.5.15 → 0.5.16
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/CHANGELOG.md +1 -0
- package/index.js +2 -0
- package/lib/otel-export.js +272 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,7 @@ upgrading across more than a few patches at a time.
|
|
|
8
8
|
|
|
9
9
|
## v0.5.x
|
|
10
10
|
|
|
11
|
+
- **0.5.15** (2026-04-30) — b.archive: ZIP creation
|
|
11
12
|
- **0.5.14** (2026-04-30) — b.time: timezone-aware datetime arithmetic + formatting
|
|
12
13
|
- **0.5.13** (2026-04-30) — b.testing.request: supertest-style chainable HTTP test helper
|
|
13
14
|
- **0.5.12** (2026-04-30) — b.middleware.requestLog: HTTP access-log middleware
|
package/index.js
CHANGED
|
@@ -145,6 +145,7 @@ var pagination = require("./lib/pagination");
|
|
|
145
145
|
var metrics = require("./lib/metrics");
|
|
146
146
|
var tracing = require("./lib/tracing");
|
|
147
147
|
var observability = require("./lib/observability");
|
|
148
|
+
var otelExport = require("./lib/otel-export");
|
|
148
149
|
var protocolDispatcher = require("./lib/protocol-dispatcher");
|
|
149
150
|
var requestHelpers = require("./lib/request-helpers");
|
|
150
151
|
var appShutdown = require("./lib/app-shutdown");
|
|
@@ -251,6 +252,7 @@ module.exports = {
|
|
|
251
252
|
metrics: metrics,
|
|
252
253
|
tracing: tracing,
|
|
253
254
|
observability: observability,
|
|
255
|
+
otelExport: otelExport,
|
|
254
256
|
protocolDispatcher: protocolDispatcher,
|
|
255
257
|
requestHelpers: requestHelpers,
|
|
256
258
|
appShutdown: appShutdown,
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* otel-export — OTLP/HTTP-JSON exporter for `b.observability` events.
|
|
4
|
+
*
|
|
5
|
+
* Bridges the framework's `observability.event(name, value, attrs)`
|
|
6
|
+
* surface to any OTel-compatible backend (Honeycomb, Datadog, Jaeger
|
|
7
|
+
* Collector, AWS Distro, Grafana, NewRelic — anything that speaks
|
|
8
|
+
* OTLP/HTTP). Spec: opentelemetry.io/docs/specs/otlp.
|
|
9
|
+
*
|
|
10
|
+
* var otel = b.otelExport.create({
|
|
11
|
+
* endpoint: "https://otel.honeycomb.io/v1/metrics",
|
|
12
|
+
* headers: { "X-Honeycomb-Team": env("HONEYCOMB_API_KEY") },
|
|
13
|
+
* serviceName: "wiki",
|
|
14
|
+
* intervalMs: b.constants.TIME.seconds(15), // auto-flush cadence
|
|
15
|
+
* httpClient: b.httpClient, // for testing
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Counter — accumulates per (name, attrs) tuple, flushed in batches.
|
|
19
|
+
* otel.recordCounter("http.requests", 1, { method: "GET", status: 200 });
|
|
20
|
+
*
|
|
21
|
+
* // Histogram — operator-bucketed observation. Less common; operators
|
|
22
|
+
* // wanting full histogram support build on top.
|
|
23
|
+
* otel.recordObservation("http.duration_ms", 142, { route: "/api/x" });
|
|
24
|
+
*
|
|
25
|
+
* await otel.flush(); // manual flush
|
|
26
|
+
* otel.close(); // cancels interval, final flush
|
|
27
|
+
*
|
|
28
|
+
* Wiring with `b.observability`:
|
|
29
|
+
*
|
|
30
|
+
* // Option A: replace the metrics tap entirely
|
|
31
|
+
* b.observability._setTap(otel.tapHandler);
|
|
32
|
+
*
|
|
33
|
+
* // Option B: alongside b.metrics — operators write their own
|
|
34
|
+
* // multi-tap fan-out (or pick one or the other for v1).
|
|
35
|
+
*
|
|
36
|
+
* Endpoint must accept OTLP/HTTP with Content-Type: application/json
|
|
37
|
+
* (the JSON variant of the OTLP protobuf — every modern collector
|
|
38
|
+
* supports it). The framework refuses to ship the binary protobuf
|
|
39
|
+
* encoding because it requires either a vendored proto runtime or
|
|
40
|
+
* hand-rolled wire format with no compelling benefit at this scope.
|
|
41
|
+
*/
|
|
42
|
+
var C = require("./constants");
|
|
43
|
+
var validateOpts = require("./validate-opts");
|
|
44
|
+
var { defineClass } = require("./framework-error");
|
|
45
|
+
|
|
46
|
+
var OtelExportError = defineClass("OtelExportError", { alwaysPermanent: false });
|
|
47
|
+
|
|
48
|
+
var DEFAULT_INTERVAL_MS = C.TIME.seconds(15);
|
|
49
|
+
|
|
50
|
+
// OTLP aggregation temporality:
|
|
51
|
+
// 1 = DELTA — counters report deltas since last export
|
|
52
|
+
// 2 = CUMULATIVE — counters report running totals
|
|
53
|
+
// DELTA is what most exporters do for short-lived processes; the
|
|
54
|
+
// receiving backend handles the running sum.
|
|
55
|
+
var TEMPORALITY_DELTA = 1;
|
|
56
|
+
|
|
57
|
+
// ---- attribute encoding ----
|
|
58
|
+
// OTLP attributes are KeyValue with typed `value` fields:
|
|
59
|
+
// { key, value: { stringValue | intValue | doubleValue | boolValue } }
|
|
60
|
+
function _attrsToOtlp(attrs) {
|
|
61
|
+
if (!attrs || typeof attrs !== "object") return [];
|
|
62
|
+
var out = [];
|
|
63
|
+
for (var k in attrs) {
|
|
64
|
+
if (!Object.prototype.hasOwnProperty.call(attrs, k)) continue;
|
|
65
|
+
var v = attrs[k];
|
|
66
|
+
var kv;
|
|
67
|
+
if (typeof v === "string") kv = { stringValue: v };
|
|
68
|
+
else if (typeof v === "number") {
|
|
69
|
+
kv = Number.isInteger(v) ? { intValue: String(v) } : { doubleValue: v };
|
|
70
|
+
}
|
|
71
|
+
else if (typeof v === "boolean") kv = { boolValue: v };
|
|
72
|
+
else if (v == null) continue;
|
|
73
|
+
else kv = { stringValue: String(v) };
|
|
74
|
+
out.push({ key: k, value: kv });
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Stable key per (name, attrs) so tap calls aggregate.
|
|
80
|
+
function _bucketKey(name, attrs) {
|
|
81
|
+
if (!attrs) return name + "|";
|
|
82
|
+
var ks = Object.keys(attrs).sort();
|
|
83
|
+
var parts = [name];
|
|
84
|
+
for (var i = 0; i < ks.length; i++) parts.push(ks[i] + "=" + String(attrs[ks[i]]));
|
|
85
|
+
return parts.join("|");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function create(opts) {
|
|
89
|
+
opts = opts || {};
|
|
90
|
+
validateOpts(opts, [
|
|
91
|
+
"endpoint", "headers", "serviceName", "intervalMs",
|
|
92
|
+
"httpClient", "resourceAttributes", "scope",
|
|
93
|
+
], "otelExport.create");
|
|
94
|
+
if (typeof opts.endpoint !== "string" || opts.endpoint.length === 0) {
|
|
95
|
+
throw new OtelExportError("otel-export/bad-endpoint",
|
|
96
|
+
"create: endpoint must be a non-empty URL");
|
|
97
|
+
}
|
|
98
|
+
if (typeof opts.serviceName !== "string" || opts.serviceName.length === 0) {
|
|
99
|
+
throw new OtelExportError("otel-export/bad-service-name",
|
|
100
|
+
"create: serviceName must be a non-empty string");
|
|
101
|
+
}
|
|
102
|
+
var endpoint = opts.endpoint;
|
|
103
|
+
var serviceName = opts.serviceName;
|
|
104
|
+
var headers = opts.headers || {};
|
|
105
|
+
var intervalMs = opts.intervalMs != null ? opts.intervalMs : DEFAULT_INTERVAL_MS;
|
|
106
|
+
if (typeof intervalMs !== "number" || !isFinite(intervalMs) || intervalMs < 0) {
|
|
107
|
+
throw new OtelExportError("otel-export/bad-interval",
|
|
108
|
+
"create: intervalMs must be a non-negative finite number");
|
|
109
|
+
}
|
|
110
|
+
var httpClient = opts.httpClient || require("./http-client");
|
|
111
|
+
var scopeName = (opts.scope && opts.scope.name) || "blamejs";
|
|
112
|
+
var scopeVersion = (opts.scope && opts.scope.version) || "0.5.x";
|
|
113
|
+
var resourceAttrs = Object.assign({ "service.name": serviceName },
|
|
114
|
+
opts.resourceAttributes || {});
|
|
115
|
+
|
|
116
|
+
// Buckets: counters and observations keyed by (name, sorted-attrs).
|
|
117
|
+
var counters = new Map(); // bucketKey → { name, attrs, value, startUnixNano }
|
|
118
|
+
var observations = new Map(); // bucketKey → { name, attrs, sum, count, min, max, startUnixNano }
|
|
119
|
+
var startUnixNano = String(Date.now() * 1e6);
|
|
120
|
+
var timer = null;
|
|
121
|
+
var closed = false;
|
|
122
|
+
|
|
123
|
+
function recordCounter(name, value, attrs) {
|
|
124
|
+
if (closed) return;
|
|
125
|
+
if (typeof name !== "string" || name.length === 0) return;
|
|
126
|
+
var v = typeof value === "number" && isFinite(value) ? value : 1;
|
|
127
|
+
var key = _bucketKey(name, attrs);
|
|
128
|
+
var b = counters.get(key);
|
|
129
|
+
if (!b) {
|
|
130
|
+
b = { name: name, attrs: attrs || {}, value: 0, startUnixNano: startUnixNano };
|
|
131
|
+
counters.set(key, b);
|
|
132
|
+
}
|
|
133
|
+
b.value += v;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function recordObservation(name, value, attrs) {
|
|
137
|
+
if (closed) return;
|
|
138
|
+
if (typeof name !== "string" || name.length === 0) return;
|
|
139
|
+
if (typeof value !== "number" || !isFinite(value)) return;
|
|
140
|
+
var key = _bucketKey(name, attrs);
|
|
141
|
+
var b = observations.get(key);
|
|
142
|
+
if (!b) {
|
|
143
|
+
b = { name: name, attrs: attrs || {}, sum: 0, count: 0, min: value, max: value, startUnixNano: startUnixNano };
|
|
144
|
+
observations.set(key, b);
|
|
145
|
+
}
|
|
146
|
+
b.sum += value;
|
|
147
|
+
b.count += 1;
|
|
148
|
+
if (value < b.min) b.min = value;
|
|
149
|
+
if (value > b.max) b.max = value;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Operators wire this as the observability tap. event(name, value, labels)
|
|
153
|
+
// → recordCounter for value=1 fire-and-forget shapes.
|
|
154
|
+
function tapHandler(name, value, labels) {
|
|
155
|
+
recordCounter(name, value, labels);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function _drainAndEncode() {
|
|
159
|
+
var nowUnixNano = String(Date.now() * 1e6);
|
|
160
|
+
var metrics = [];
|
|
161
|
+
var c, o;
|
|
162
|
+
|
|
163
|
+
counters.forEach(function (entry) {
|
|
164
|
+
metrics.push({
|
|
165
|
+
name: entry.name,
|
|
166
|
+
sum: {
|
|
167
|
+
dataPoints: [{
|
|
168
|
+
attributes: _attrsToOtlp(entry.attrs),
|
|
169
|
+
startTimeUnixNano: entry.startUnixNano,
|
|
170
|
+
timeUnixNano: nowUnixNano,
|
|
171
|
+
asDouble: entry.value,
|
|
172
|
+
}],
|
|
173
|
+
aggregationTemporality: TEMPORALITY_DELTA,
|
|
174
|
+
isMonotonic: true,
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
void c;
|
|
179
|
+
observations.forEach(function (entry) {
|
|
180
|
+
metrics.push({
|
|
181
|
+
name: entry.name,
|
|
182
|
+
summary: {
|
|
183
|
+
dataPoints: [{
|
|
184
|
+
attributes: _attrsToOtlp(entry.attrs),
|
|
185
|
+
startTimeUnixNano: entry.startUnixNano,
|
|
186
|
+
timeUnixNano: nowUnixNano,
|
|
187
|
+
count: String(entry.count),
|
|
188
|
+
sum: entry.sum,
|
|
189
|
+
quantileValues: [
|
|
190
|
+
{ quantile: 0, value: entry.min },
|
|
191
|
+
{ quantile: 1, value: entry.max },
|
|
192
|
+
],
|
|
193
|
+
}],
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
void o;
|
|
198
|
+
|
|
199
|
+
// Reset buckets (DELTA temporality — each export is the delta).
|
|
200
|
+
counters.clear();
|
|
201
|
+
observations.clear();
|
|
202
|
+
startUnixNano = nowUnixNano;
|
|
203
|
+
if (metrics.length === 0) return null;
|
|
204
|
+
return {
|
|
205
|
+
resourceMetrics: [{
|
|
206
|
+
resource: { attributes: _attrsToOtlp(resourceAttrs) },
|
|
207
|
+
scopeMetrics: [{
|
|
208
|
+
scope: { name: scopeName, version: scopeVersion },
|
|
209
|
+
metrics: metrics,
|
|
210
|
+
}],
|
|
211
|
+
}],
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function flush() {
|
|
216
|
+
var payload = _drainAndEncode();
|
|
217
|
+
if (!payload) return { sent: false, reason: "no-data" };
|
|
218
|
+
var body = JSON.stringify(payload);
|
|
219
|
+
try {
|
|
220
|
+
var res = await httpClient.request({
|
|
221
|
+
method: "POST",
|
|
222
|
+
url: endpoint,
|
|
223
|
+
headers: Object.assign({ "Content-Type": "application/json" }, headers),
|
|
224
|
+
body: body,
|
|
225
|
+
});
|
|
226
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
227
|
+
throw new OtelExportError("otel-export/upstream-rejected",
|
|
228
|
+
"OTLP endpoint returned " + res.statusCode);
|
|
229
|
+
}
|
|
230
|
+
return { sent: true, statusCode: res.statusCode, bodyLength: body.length };
|
|
231
|
+
} catch (e) {
|
|
232
|
+
if (e && e.isOtelExportError) throw e;
|
|
233
|
+
throw new OtelExportError("otel-export/send-failed",
|
|
234
|
+
"OTLP send failed: " + ((e && e.message) || String(e)));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function _scheduleFlush() {
|
|
239
|
+
if (intervalMs === 0 || closed) return;
|
|
240
|
+
timer = setTimeout(function () {
|
|
241
|
+
flush().catch(function (_e) { /* never let a flush error crash the timer */ });
|
|
242
|
+
_scheduleFlush();
|
|
243
|
+
}, intervalMs);
|
|
244
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
245
|
+
}
|
|
246
|
+
_scheduleFlush();
|
|
247
|
+
|
|
248
|
+
function close() {
|
|
249
|
+
if (closed) return;
|
|
250
|
+
closed = true;
|
|
251
|
+
if (timer) { clearTimeout(timer); timer = null; }
|
|
252
|
+
return flush().catch(function (_e) { /* close path swallows final-flush errors */ });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
recordCounter: recordCounter,
|
|
257
|
+
recordObservation: recordObservation,
|
|
258
|
+
tapHandler: tapHandler,
|
|
259
|
+
flush: flush,
|
|
260
|
+
close: close,
|
|
261
|
+
get bufferedCounters() { return counters.size; },
|
|
262
|
+
get bufferedObservations() { return observations.size; },
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
module.exports = {
|
|
267
|
+
create: create,
|
|
268
|
+
OtelExportError: OtelExportError,
|
|
269
|
+
// Test-only encoders for unit-testing the OTLP shape without an HTTP client.
|
|
270
|
+
_attrsToOtlpForTest: _attrsToOtlp,
|
|
271
|
+
_bucketKeyForTest: _bucketKey,
|
|
272
|
+
};
|