@iii-dev/observability 0.16.0 → 0.16.2-next.1
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/dist/index-CQOJ6aXo.d.cts +170 -0
- package/dist/index-Cjy1as4B.d.mts +170 -0
- package/dist/index.cjs +32 -1089
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -168
- package/dist/index.d.mts +3 -168
- package/dist/index.mjs +5 -1059
- package/dist/index.mjs.map +1 -1
- package/dist/internal.cjs +5 -0
- package/dist/internal.d.cts +2 -0
- package/dist/internal.d.mts +2 -0
- package/dist/internal.mjs +3 -0
- package/dist/telemetry-system-B84W8JWm.mjs +1060 -0
- package/dist/telemetry-system-B84W8JWm.mjs.map +1 -0
- package/dist/telemetry-system-BNjGWyzY.cjs +1239 -0
- package/dist/telemetry-system-BNjGWyzY.cjs.map +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,1239 @@
|
|
|
1
|
+
let _opentelemetry_api_logs = require("@opentelemetry/api-logs");
|
|
2
|
+
let _opentelemetry_resources = require("@opentelemetry/resources");
|
|
3
|
+
let _opentelemetry_semantic_conventions = require("@opentelemetry/semantic-conventions");
|
|
4
|
+
let node_crypto = require("node:crypto");
|
|
5
|
+
let _opentelemetry_api = require("@opentelemetry/api");
|
|
6
|
+
let _opentelemetry_sdk_trace_base = require("@opentelemetry/sdk-trace-base");
|
|
7
|
+
let _opentelemetry_sdk_metrics = require("@opentelemetry/sdk-metrics");
|
|
8
|
+
let _opentelemetry_core = require("@opentelemetry/core");
|
|
9
|
+
let _opentelemetry_sdk_trace_node = require("@opentelemetry/sdk-trace-node");
|
|
10
|
+
let _opentelemetry_instrumentation = require("@opentelemetry/instrumentation");
|
|
11
|
+
let _opentelemetry_sdk_logs = require("@opentelemetry/sdk-logs");
|
|
12
|
+
let ws = require("ws");
|
|
13
|
+
let _opentelemetry_otlp_transformer = require("@opentelemetry/otlp-transformer");
|
|
14
|
+
|
|
15
|
+
//#region src/telemetry-system/baggage-span-processor.ts
|
|
16
|
+
/** DEFAULT_ALLOWLIST drift across languages would break worker chains;
|
|
17
|
+
* lockstep tests in each SDK pin this constant at CI time. */
|
|
18
|
+
const DEFAULT_ALLOWLIST = [
|
|
19
|
+
"iii.session.id",
|
|
20
|
+
"iii.message.id",
|
|
21
|
+
"iii.function.id"
|
|
22
|
+
];
|
|
23
|
+
var BaggageSpanProcessor = class {
|
|
24
|
+
allowlist;
|
|
25
|
+
constructor(allowlist = DEFAULT_ALLOWLIST) {
|
|
26
|
+
this.allowlist = allowlist;
|
|
27
|
+
}
|
|
28
|
+
onStart(span, parentContext) {
|
|
29
|
+
if (!span.isRecording()) return;
|
|
30
|
+
const baggage = _opentelemetry_api.propagation.getBaggage(parentContext);
|
|
31
|
+
if (!baggage) return;
|
|
32
|
+
for (const key of this.allowlist) {
|
|
33
|
+
const entry = baggage.getEntry(key);
|
|
34
|
+
if (entry) span.setAttribute(key, entry.value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
onEnd(_span) {}
|
|
38
|
+
async shutdown() {}
|
|
39
|
+
async forceFlush() {}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/telemetry-system/types.ts
|
|
44
|
+
const ATTR_SERVICE_VERSION = "service.version";
|
|
45
|
+
const ATTR_SERVICE_NAMESPACE = "service.namespace";
|
|
46
|
+
const ATTR_SERVICE_INSTANCE_ID = "service.instance.id";
|
|
47
|
+
/** Magic prefixes for binary frames over WebSocket */
|
|
48
|
+
const PREFIX_TRACES = "OTLP";
|
|
49
|
+
const PREFIX_METRICS = "MTRC";
|
|
50
|
+
const PREFIX_LOGS = "LOGS";
|
|
51
|
+
/** Default reconnection configuration */
|
|
52
|
+
const DEFAULT_RECONNECTION_CONFIG = {
|
|
53
|
+
initialDelayMs: 1e3,
|
|
54
|
+
maxDelayMs: 3e4,
|
|
55
|
+
backoffMultiplier: 2,
|
|
56
|
+
jitterFactor: .3,
|
|
57
|
+
maxRetries: -1
|
|
58
|
+
};
|
|
59
|
+
/** Default configuration values for OpenTelemetry initialization. */
|
|
60
|
+
const DEFAULT_OTEL_CONFIG = {
|
|
61
|
+
enabled: true,
|
|
62
|
+
serviceName: "iii-node",
|
|
63
|
+
serviceVersion: "unknown",
|
|
64
|
+
engineWsUrl: "ws://localhost:49134",
|
|
65
|
+
metricsEnabled: true,
|
|
66
|
+
metricsExportIntervalMs: 6e4,
|
|
67
|
+
logsFlushIntervalMs: 100,
|
|
68
|
+
logsBatchSize: 1,
|
|
69
|
+
fetchInstrumentationEnabled: true
|
|
70
|
+
};
|
|
71
|
+
/** Parse a boolean environment variable, recognizing 'false', '0', 'no', 'off' as false. */
|
|
72
|
+
function parseBoolEnv(value, defaultValue) {
|
|
73
|
+
if (value === void 0) return defaultValue;
|
|
74
|
+
const lower = value.toLowerCase();
|
|
75
|
+
return lower !== "false" && lower !== "0" && lower !== "no" && lower !== "off";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/telemetry-system/connection.ts
|
|
80
|
+
/**
|
|
81
|
+
* Shared WebSocket connection for OpenTelemetry exporters.
|
|
82
|
+
*/
|
|
83
|
+
/**
|
|
84
|
+
* Shared WebSocket connection for all OTEL exporters (traces, metrics, logs).
|
|
85
|
+
* Uses a single connection with message prefixes to identify signal type.
|
|
86
|
+
*/
|
|
87
|
+
var SharedEngineConnection = class SharedEngineConnection {
|
|
88
|
+
static MAX_PENDING_MESSAGES = 1e3;
|
|
89
|
+
ws = null;
|
|
90
|
+
wsUrl;
|
|
91
|
+
connecting = false;
|
|
92
|
+
shuttingDown = false;
|
|
93
|
+
pendingMessages = [];
|
|
94
|
+
reconnectAttempt = 0;
|
|
95
|
+
reconnectTimeout = null;
|
|
96
|
+
config;
|
|
97
|
+
state = "disconnected";
|
|
98
|
+
onConnectedCallbacks = [];
|
|
99
|
+
onFailedCallbacks = [];
|
|
100
|
+
constructor(wsUrl, config = {}) {
|
|
101
|
+
this.wsUrl = wsUrl;
|
|
102
|
+
this.config = {
|
|
103
|
+
...DEFAULT_RECONNECTION_CONFIG,
|
|
104
|
+
...config
|
|
105
|
+
};
|
|
106
|
+
this.connect();
|
|
107
|
+
}
|
|
108
|
+
connect() {
|
|
109
|
+
if (this.connecting || this.ws && this.ws.readyState === ws.WebSocket.OPEN) return;
|
|
110
|
+
this.connecting = true;
|
|
111
|
+
this.state = "connecting";
|
|
112
|
+
try {
|
|
113
|
+
this.ws = new ws.WebSocket(this.wsUrl);
|
|
114
|
+
this.ws.on("open", () => {
|
|
115
|
+
this.connecting = false;
|
|
116
|
+
this.state = "connected";
|
|
117
|
+
console.log(`[OTel] Connected to engine at ${this.wsUrl}`);
|
|
118
|
+
if (this.reconnectAttempt > 0) console.log("[OTel] Successfully reconnected");
|
|
119
|
+
this.reconnectAttempt = 0;
|
|
120
|
+
if (this.reconnectTimeout) {
|
|
121
|
+
clearTimeout(this.reconnectTimeout);
|
|
122
|
+
this.reconnectTimeout = null;
|
|
123
|
+
}
|
|
124
|
+
const pending = this.pendingMessages.splice(0, this.pendingMessages.length);
|
|
125
|
+
for (const { frame, callback } of pending) this.ws?.send(frame, (err) => callback?.(err));
|
|
126
|
+
for (const cb of this.onConnectedCallbacks) cb();
|
|
127
|
+
});
|
|
128
|
+
this.ws.on("close", () => {
|
|
129
|
+
this.connecting = false;
|
|
130
|
+
this.ws = null;
|
|
131
|
+
if (this.shuttingDown) {
|
|
132
|
+
this.state = "disconnected";
|
|
133
|
+
console.log("[OTel] Connection closed during shutdown");
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
this.state = "disconnected";
|
|
137
|
+
console.log("[OTel] Disconnected from engine, will reconnect...");
|
|
138
|
+
this.scheduleReconnect();
|
|
139
|
+
});
|
|
140
|
+
this.ws.on("error", (err) => {
|
|
141
|
+
this.connecting = false;
|
|
142
|
+
if (this.shuttingDown) return;
|
|
143
|
+
console.error("[OTel] WebSocket error:", err.message);
|
|
144
|
+
});
|
|
145
|
+
} catch (err) {
|
|
146
|
+
this.connecting = false;
|
|
147
|
+
console.error("[OTel] Connection failed:", err);
|
|
148
|
+
this.scheduleReconnect();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
scheduleReconnect() {
|
|
152
|
+
if (this.config.maxRetries !== -1 && this.reconnectAttempt >= this.config.maxRetries) {
|
|
153
|
+
this.state = "failed";
|
|
154
|
+
console.error(`[OTel] Max retries (${this.config.maxRetries}) reached, giving up`);
|
|
155
|
+
const pending = this.pendingMessages.splice(0, this.pendingMessages.length);
|
|
156
|
+
const failedError = /* @__PURE__ */ new Error("Connection failed after max retries");
|
|
157
|
+
for (const { callback } of pending) callback?.(failedError);
|
|
158
|
+
for (const cb of this.onFailedCallbacks) try {
|
|
159
|
+
cb();
|
|
160
|
+
} catch (err) {
|
|
161
|
+
console.error("[OTel] onFailed callback threw:", err);
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (this.reconnectTimeout) return;
|
|
166
|
+
const exponentialDelay = this.config.initialDelayMs * this.config.backoffMultiplier ** this.reconnectAttempt;
|
|
167
|
+
const cappedDelay = Math.min(exponentialDelay, this.config.maxDelayMs);
|
|
168
|
+
const jitter = cappedDelay * this.config.jitterFactor * (2 * Math.random() - 1);
|
|
169
|
+
const delay = Math.max(0, Math.floor(cappedDelay + jitter));
|
|
170
|
+
this.state = "reconnecting";
|
|
171
|
+
console.log(`[OTel] Reconnecting in ${delay}ms (attempt ${this.reconnectAttempt + 1})...`);
|
|
172
|
+
this.reconnectTimeout = setTimeout(() => {
|
|
173
|
+
this.reconnectTimeout = null;
|
|
174
|
+
this.reconnectAttempt++;
|
|
175
|
+
this.connect();
|
|
176
|
+
}, delay);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Send a message with a signal prefix.
|
|
180
|
+
*/
|
|
181
|
+
send(prefix, data, callback) {
|
|
182
|
+
const prefixBytes = Buffer.from(prefix, "utf-8");
|
|
183
|
+
const frame = Buffer.concat([prefixBytes, Buffer.from(data)]);
|
|
184
|
+
if (this.ws && this.ws.readyState === ws.WebSocket.OPEN) this.ws.send(frame, callback);
|
|
185
|
+
else {
|
|
186
|
+
if (this.pendingMessages.length >= SharedEngineConnection.MAX_PENDING_MESSAGES) {
|
|
187
|
+
console.warn("[OTel] Pending message queue full, dropping oldest message");
|
|
188
|
+
this.pendingMessages.shift()?.callback?.(/* @__PURE__ */ new Error("Message dropped due to queue overflow"));
|
|
189
|
+
}
|
|
190
|
+
this.pendingMessages.push({
|
|
191
|
+
frame,
|
|
192
|
+
callback
|
|
193
|
+
});
|
|
194
|
+
this.connect();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Register a callback to be called when connected.
|
|
199
|
+
*/
|
|
200
|
+
onConnected(callback) {
|
|
201
|
+
this.onConnectedCallbacks.push(callback);
|
|
202
|
+
if (this.state === "connected") callback();
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Register a callback to be called when the connection enters the failed
|
|
206
|
+
* terminal state (max retries reached). Exporters use this to drain their
|
|
207
|
+
* own pending queues so in-flight forceFlush() calls do not hang.
|
|
208
|
+
*/
|
|
209
|
+
onFailed(callback) {
|
|
210
|
+
this.onFailedCallbacks.push(callback);
|
|
211
|
+
if (this.state === "failed") try {
|
|
212
|
+
callback();
|
|
213
|
+
} catch (err) {
|
|
214
|
+
console.error("[OTel] onFailed callback threw:", err);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Get the current connection state.
|
|
219
|
+
*/
|
|
220
|
+
getState() {
|
|
221
|
+
return this.state;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Shutdown the connection.
|
|
225
|
+
*/
|
|
226
|
+
async shutdown() {
|
|
227
|
+
this.shuttingDown = true;
|
|
228
|
+
if (this.reconnectTimeout) {
|
|
229
|
+
clearTimeout(this.reconnectTimeout);
|
|
230
|
+
this.reconnectTimeout = null;
|
|
231
|
+
}
|
|
232
|
+
if (this.ws) {
|
|
233
|
+
this.ws.close();
|
|
234
|
+
this.ws = null;
|
|
235
|
+
}
|
|
236
|
+
const pending = this.pendingMessages.splice(0, this.pendingMessages.length);
|
|
237
|
+
const shutdownError = /* @__PURE__ */ new Error("Connection shutdown before message could be sent");
|
|
238
|
+
for (const { callback } of pending) callback?.(shutdownError);
|
|
239
|
+
this.onConnectedCallbacks = [];
|
|
240
|
+
this.onFailedCallbacks = [];
|
|
241
|
+
this.state = "disconnected";
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/telemetry-system/span-exporter.ts
|
|
247
|
+
/**
|
|
248
|
+
* Span exporter for the III Engine.
|
|
249
|
+
*/
|
|
250
|
+
/**
|
|
251
|
+
* Span exporter using the shared WebSocket connection.
|
|
252
|
+
*/
|
|
253
|
+
var EngineSpanExporter = class EngineSpanExporter {
|
|
254
|
+
static MAX_PENDING_EXPORTS = 100;
|
|
255
|
+
connection;
|
|
256
|
+
pendingExports = [];
|
|
257
|
+
constructor(connection) {
|
|
258
|
+
this.connection = connection;
|
|
259
|
+
this.connection.onConnected(() => this.flushPending());
|
|
260
|
+
this.connection.onFailed(() => this.failPending());
|
|
261
|
+
}
|
|
262
|
+
flushPending() {
|
|
263
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
264
|
+
for (const { spans, resultCallback } of pending) this.sendExport(spans, resultCallback);
|
|
265
|
+
}
|
|
266
|
+
failPending() {
|
|
267
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
268
|
+
const error = /* @__PURE__ */ new Error("Connection failed: dropping queued spans");
|
|
269
|
+
for (const { resultCallback } of pending) resultCallback?.({
|
|
270
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
271
|
+
error
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
sendExport(spans, resultCallback) {
|
|
275
|
+
try {
|
|
276
|
+
const serialized = _opentelemetry_otlp_transformer.JsonTraceSerializer.serializeRequest(spans);
|
|
277
|
+
if (!serialized) {
|
|
278
|
+
resultCallback?.({ code: _opentelemetry_core.ExportResultCode.SUCCESS });
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
this.connection.send(PREFIX_TRACES, serialized, (err) => {
|
|
282
|
+
if (err) {
|
|
283
|
+
console.error("[OTel] Failed to send spans:", err.message);
|
|
284
|
+
resultCallback?.({
|
|
285
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
286
|
+
error: err
|
|
287
|
+
});
|
|
288
|
+
} else resultCallback?.({ code: _opentelemetry_core.ExportResultCode.SUCCESS });
|
|
289
|
+
});
|
|
290
|
+
} catch (err) {
|
|
291
|
+
console.error("[OTel] Error exporting spans:", err);
|
|
292
|
+
resultCallback?.({
|
|
293
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
294
|
+
error: err
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
doExport(spans, resultCallback) {
|
|
299
|
+
const state = this.connection.getState();
|
|
300
|
+
if (state === "failed") {
|
|
301
|
+
resultCallback({
|
|
302
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
303
|
+
error: /* @__PURE__ */ new Error("Connection failed: dropping spans")
|
|
304
|
+
});
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (state !== "connected") {
|
|
308
|
+
if (this.pendingExports.length >= EngineSpanExporter.MAX_PENDING_EXPORTS) {
|
|
309
|
+
this.pendingExports.shift()?.resultCallback?.({
|
|
310
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
311
|
+
error: /* @__PURE__ */ new Error("Queue overflow")
|
|
312
|
+
});
|
|
313
|
+
console.warn("[OTel] Spans export queue full, dropped oldest entry");
|
|
314
|
+
}
|
|
315
|
+
this.pendingExports.push({
|
|
316
|
+
spans,
|
|
317
|
+
resultCallback
|
|
318
|
+
});
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
this.sendExport(spans, resultCallback);
|
|
322
|
+
}
|
|
323
|
+
export(spans, resultCallback) {
|
|
324
|
+
this.doExport(spans, resultCallback);
|
|
325
|
+
}
|
|
326
|
+
async shutdown() {
|
|
327
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
328
|
+
const shutdownError = /* @__PURE__ */ new Error("Exporter shutdown before export completed");
|
|
329
|
+
for (const { resultCallback } of pending) resultCallback?.({
|
|
330
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
331
|
+
error: shutdownError
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
async forceFlush() {}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/telemetry-system/metrics-exporter.ts
|
|
339
|
+
/**
|
|
340
|
+
* Metrics exporter for the III Engine.
|
|
341
|
+
*/
|
|
342
|
+
/**
|
|
343
|
+
* Metrics exporter using the shared WebSocket connection.
|
|
344
|
+
*/
|
|
345
|
+
var EngineMetricsExporter = class EngineMetricsExporter {
|
|
346
|
+
static MAX_PENDING_EXPORTS = 100;
|
|
347
|
+
connection;
|
|
348
|
+
pendingExports = [];
|
|
349
|
+
constructor(connection) {
|
|
350
|
+
this.connection = connection;
|
|
351
|
+
this.connection.onConnected(() => this.flushPending());
|
|
352
|
+
this.connection.onFailed(() => this.failPending());
|
|
353
|
+
}
|
|
354
|
+
flushPending() {
|
|
355
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
356
|
+
for (const { metrics, resultCallback } of pending) this.sendExport(metrics, resultCallback);
|
|
357
|
+
}
|
|
358
|
+
failPending() {
|
|
359
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
360
|
+
const error = /* @__PURE__ */ new Error("Connection failed: dropping queued metrics");
|
|
361
|
+
for (const { resultCallback } of pending) resultCallback?.({
|
|
362
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
363
|
+
error
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
sendExport(metricsData, resultCallback) {
|
|
367
|
+
try {
|
|
368
|
+
const serialized = _opentelemetry_otlp_transformer.JsonMetricsSerializer.serializeRequest(metricsData);
|
|
369
|
+
if (!serialized) {
|
|
370
|
+
resultCallback?.({ code: _opentelemetry_core.ExportResultCode.SUCCESS });
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
this.connection.send(PREFIX_METRICS, serialized, (err) => {
|
|
374
|
+
if (err) {
|
|
375
|
+
console.error("[OTel] Failed to send metrics:", err.message);
|
|
376
|
+
resultCallback?.({
|
|
377
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
378
|
+
error: err
|
|
379
|
+
});
|
|
380
|
+
} else resultCallback?.({ code: _opentelemetry_core.ExportResultCode.SUCCESS });
|
|
381
|
+
});
|
|
382
|
+
} catch (err) {
|
|
383
|
+
console.error("[OTel] Error exporting metrics:", err);
|
|
384
|
+
resultCallback?.({
|
|
385
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
386
|
+
error: err
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
doExport(metricsData, resultCallback) {
|
|
391
|
+
const state = this.connection.getState();
|
|
392
|
+
if (state === "failed") {
|
|
393
|
+
resultCallback({
|
|
394
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
395
|
+
error: /* @__PURE__ */ new Error("Connection failed: dropping metrics")
|
|
396
|
+
});
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (state !== "connected") {
|
|
400
|
+
if (this.pendingExports.length >= EngineMetricsExporter.MAX_PENDING_EXPORTS) {
|
|
401
|
+
this.pendingExports.shift()?.resultCallback?.({
|
|
402
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
403
|
+
error: /* @__PURE__ */ new Error("Queue overflow")
|
|
404
|
+
});
|
|
405
|
+
console.warn("[OTel] Metrics export queue full, dropped oldest entry");
|
|
406
|
+
}
|
|
407
|
+
this.pendingExports.push({
|
|
408
|
+
metrics: metricsData,
|
|
409
|
+
resultCallback
|
|
410
|
+
});
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
this.sendExport(metricsData, resultCallback);
|
|
414
|
+
}
|
|
415
|
+
export(metrics, resultCallback) {
|
|
416
|
+
this.doExport(metrics, resultCallback);
|
|
417
|
+
}
|
|
418
|
+
async shutdown() {
|
|
419
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
420
|
+
const shutdownError = /* @__PURE__ */ new Error("Exporter shutdown before export completed");
|
|
421
|
+
for (const { resultCallback } of pending) resultCallback?.({
|
|
422
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
423
|
+
error: shutdownError
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
async forceFlush() {}
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
//#endregion
|
|
430
|
+
//#region src/telemetry-system/log-exporter.ts
|
|
431
|
+
/**
|
|
432
|
+
* Log exporter for the III Engine.
|
|
433
|
+
*/
|
|
434
|
+
/**
|
|
435
|
+
* Log exporter using the shared WebSocket connection.
|
|
436
|
+
*/
|
|
437
|
+
var EngineLogExporter = class EngineLogExporter {
|
|
438
|
+
static MAX_PENDING_EXPORTS = 100;
|
|
439
|
+
connection;
|
|
440
|
+
pendingExports = [];
|
|
441
|
+
constructor(connection) {
|
|
442
|
+
this.connection = connection;
|
|
443
|
+
this.connection.onConnected(() => this.flushPending());
|
|
444
|
+
this.connection.onFailed(() => this.failPending());
|
|
445
|
+
}
|
|
446
|
+
flushPending() {
|
|
447
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
448
|
+
for (const { logs, callback } of pending) this.doExport(logs, callback);
|
|
449
|
+
}
|
|
450
|
+
failPending() {
|
|
451
|
+
const pending = this.pendingExports.splice(0, this.pendingExports.length);
|
|
452
|
+
const error = /* @__PURE__ */ new Error("Connection failed: dropping queued logs");
|
|
453
|
+
for (const { callback } of pending) callback({
|
|
454
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
455
|
+
error
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
doExport(logs, resultCallback) {
|
|
459
|
+
const state = this.connection.getState();
|
|
460
|
+
if (state === "failed") {
|
|
461
|
+
resultCallback({
|
|
462
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
463
|
+
error: /* @__PURE__ */ new Error("Connection failed: dropping logs")
|
|
464
|
+
});
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (state !== "connected") {
|
|
468
|
+
if (this.pendingExports.length >= EngineLogExporter.MAX_PENDING_EXPORTS) {
|
|
469
|
+
this.pendingExports.shift()?.callback({
|
|
470
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
471
|
+
error: /* @__PURE__ */ new Error("Logs export queue full")
|
|
472
|
+
});
|
|
473
|
+
console.warn("[OTel] Logs export queue full, dropped oldest entry");
|
|
474
|
+
}
|
|
475
|
+
this.pendingExports.push({
|
|
476
|
+
logs,
|
|
477
|
+
callback: resultCallback
|
|
478
|
+
});
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
try {
|
|
482
|
+
const serialized = _opentelemetry_otlp_transformer.JsonLogsSerializer.serializeRequest(logs);
|
|
483
|
+
if (!serialized) {
|
|
484
|
+
resultCallback({ code: _opentelemetry_core.ExportResultCode.SUCCESS });
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
this.connection.send(PREFIX_LOGS, serialized, (err) => {
|
|
488
|
+
if (err) {
|
|
489
|
+
console.error("[OTel] Failed to send logs:", err.message);
|
|
490
|
+
resultCallback({
|
|
491
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
492
|
+
error: err
|
|
493
|
+
});
|
|
494
|
+
} else resultCallback({ code: _opentelemetry_core.ExportResultCode.SUCCESS });
|
|
495
|
+
});
|
|
496
|
+
} catch (err) {
|
|
497
|
+
console.error("[OTel] Error exporting logs:", err);
|
|
498
|
+
resultCallback({
|
|
499
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
500
|
+
error: err
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
export(logs, resultCallback) {
|
|
505
|
+
this.doExport(logs, resultCallback);
|
|
506
|
+
}
|
|
507
|
+
async forceFlush() {}
|
|
508
|
+
async shutdown() {
|
|
509
|
+
for (const { callback } of this.pendingExports) callback({
|
|
510
|
+
code: _opentelemetry_core.ExportResultCode.FAILED,
|
|
511
|
+
error: /* @__PURE__ */ new Error("Exporter shutdown")
|
|
512
|
+
});
|
|
513
|
+
this.pendingExports = [];
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
//#endregion
|
|
518
|
+
//#region src/telemetry-system/context.ts
|
|
519
|
+
/**
|
|
520
|
+
* Trace context and baggage propagation utilities.
|
|
521
|
+
*/
|
|
522
|
+
/**
|
|
523
|
+
* Extract the current trace ID from the active span context.
|
|
524
|
+
*/
|
|
525
|
+
function currentTraceId() {
|
|
526
|
+
const span = _opentelemetry_api.trace.getActiveSpan();
|
|
527
|
+
if (span) {
|
|
528
|
+
const spanContext = span.spanContext();
|
|
529
|
+
if (spanContext.traceId && spanContext.traceId !== "00000000000000000000000000000000") return spanContext.traceId;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Extract the current span ID from the active span context.
|
|
534
|
+
*/
|
|
535
|
+
function currentSpanId() {
|
|
536
|
+
const span = _opentelemetry_api.trace.getActiveSpan();
|
|
537
|
+
if (span) {
|
|
538
|
+
const spanContext = span.spanContext();
|
|
539
|
+
if (spanContext.spanId && spanContext.spanId !== "0000000000000000") return spanContext.spanId;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Inject the current trace context into a W3C traceparent header string.
|
|
544
|
+
*/
|
|
545
|
+
function injectTraceparent() {
|
|
546
|
+
const carrier = {};
|
|
547
|
+
_opentelemetry_api.propagation.inject(_opentelemetry_api.context.active(), carrier);
|
|
548
|
+
return carrier.traceparent;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Extract a trace context from a W3C traceparent header string.
|
|
552
|
+
*/
|
|
553
|
+
function extractTraceparent(traceparent) {
|
|
554
|
+
const carrier = { traceparent };
|
|
555
|
+
return _opentelemetry_api.propagation.extract(_opentelemetry_api.context.active(), carrier);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Inject the current baggage into a W3C baggage header string.
|
|
559
|
+
*/
|
|
560
|
+
function injectBaggage() {
|
|
561
|
+
const carrier = {};
|
|
562
|
+
_opentelemetry_api.propagation.inject(_opentelemetry_api.context.active(), carrier);
|
|
563
|
+
return carrier.baggage;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Extract baggage from a W3C baggage header string.
|
|
567
|
+
*/
|
|
568
|
+
function extractBaggage(baggage) {
|
|
569
|
+
const carrier = { baggage };
|
|
570
|
+
return _opentelemetry_api.propagation.extract(_opentelemetry_api.context.active(), carrier);
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Extract both trace context and baggage from their respective headers.
|
|
574
|
+
*/
|
|
575
|
+
function extractContext(traceparent, baggage) {
|
|
576
|
+
const carrier = {};
|
|
577
|
+
if (traceparent) carrier.traceparent = traceparent;
|
|
578
|
+
if (baggage) carrier.baggage = baggage;
|
|
579
|
+
return _opentelemetry_api.propagation.extract(_opentelemetry_api.context.active(), carrier);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Get a baggage entry from the current context.
|
|
583
|
+
*/
|
|
584
|
+
function getBaggageEntry(key) {
|
|
585
|
+
return _opentelemetry_api.propagation.getBaggage(_opentelemetry_api.context.active())?.getEntry(key)?.value;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Set a baggage entry in the current context.
|
|
589
|
+
*/
|
|
590
|
+
function setBaggageEntry(key, value) {
|
|
591
|
+
let bag = _opentelemetry_api.propagation.getBaggage(_opentelemetry_api.context.active()) ?? _opentelemetry_api.propagation.createBaggage();
|
|
592
|
+
bag = bag.setEntry(key, { value });
|
|
593
|
+
return _opentelemetry_api.propagation.setBaggage(_opentelemetry_api.context.active(), bag);
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Remove a baggage entry from the current context.
|
|
597
|
+
*/
|
|
598
|
+
function removeBaggageEntry(key) {
|
|
599
|
+
const bag = _opentelemetry_api.propagation.getBaggage(_opentelemetry_api.context.active());
|
|
600
|
+
if (!bag) return _opentelemetry_api.context.active();
|
|
601
|
+
const newBag = bag.removeEntry(key);
|
|
602
|
+
return _opentelemetry_api.propagation.setBaggage(_opentelemetry_api.context.active(), newBag);
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Get all baggage entries from the current context.
|
|
606
|
+
*/
|
|
607
|
+
function getAllBaggage() {
|
|
608
|
+
const bag = _opentelemetry_api.propagation.getBaggage(_opentelemetry_api.context.active());
|
|
609
|
+
if (!bag) return {};
|
|
610
|
+
const entries = {};
|
|
611
|
+
for (const [key, entry] of bag.getAllEntries()) entries[key] = entry.value;
|
|
612
|
+
return entries;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
//#endregion
|
|
616
|
+
//#region src/telemetry-system/fetch-instrumentation.ts
|
|
617
|
+
/**
|
|
618
|
+
* Global fetch auto-instrumentation for the III Node SDK.
|
|
619
|
+
*
|
|
620
|
+
* Patches globalThis.fetch to create OTel CLIENT spans for every HTTP request.
|
|
621
|
+
* Works on all runtimes (Bun, Node.js, Deno) unlike UndiciInstrumentation
|
|
622
|
+
* which only works when fetch is backed by Node.js's undici.
|
|
623
|
+
*/
|
|
624
|
+
const textEncoder = new TextEncoder();
|
|
625
|
+
function getBodyByteSize(body) {
|
|
626
|
+
if (body == null) return void 0;
|
|
627
|
+
if (typeof body === "string") return textEncoder.encode(body).byteLength;
|
|
628
|
+
if (body instanceof ArrayBuffer) return body.byteLength;
|
|
629
|
+
if (ArrayBuffer.isView(body)) return body.byteLength;
|
|
630
|
+
if (body instanceof Blob) return body.size;
|
|
631
|
+
if (body instanceof URLSearchParams) return new TextEncoder().encode(body.toString()).byteLength;
|
|
632
|
+
}
|
|
633
|
+
const SAFE_REQUEST_HEADERS = ["content-type", "accept"];
|
|
634
|
+
const SAFE_RESPONSE_HEADERS = ["content-type"];
|
|
635
|
+
let originalFetch = null;
|
|
636
|
+
/**
|
|
637
|
+
* Patch globalThis.fetch to create OTel CLIENT spans for every HTTP request.
|
|
638
|
+
*/
|
|
639
|
+
function patchGlobalFetch(tracer) {
|
|
640
|
+
if (originalFetch) return;
|
|
641
|
+
originalFetch = globalThis.fetch;
|
|
642
|
+
const capturedFetch = originalFetch;
|
|
643
|
+
globalThis.fetch = async (input, init) => {
|
|
644
|
+
const url = input instanceof Request ? input.url : String(input);
|
|
645
|
+
const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
|
|
646
|
+
let host;
|
|
647
|
+
let scheme;
|
|
648
|
+
let path;
|
|
649
|
+
let port;
|
|
650
|
+
let query;
|
|
651
|
+
try {
|
|
652
|
+
const parsed = new URL(url);
|
|
653
|
+
host = parsed.hostname;
|
|
654
|
+
scheme = parsed.protocol.replace(":", "");
|
|
655
|
+
path = parsed.pathname;
|
|
656
|
+
port = parsed.port ? parseInt(parsed.port, 10) : void 0;
|
|
657
|
+
query = parsed.search ? parsed.search.slice(1) : void 0;
|
|
658
|
+
} catch {}
|
|
659
|
+
const spanAttributes = {
|
|
660
|
+
"http.request.method": method,
|
|
661
|
+
"url.full": url
|
|
662
|
+
};
|
|
663
|
+
if (host) spanAttributes["server.address"] = host;
|
|
664
|
+
if (scheme) {
|
|
665
|
+
spanAttributes["url.scheme"] = scheme;
|
|
666
|
+
spanAttributes["network.protocol.name"] = "http";
|
|
667
|
+
}
|
|
668
|
+
if (path) spanAttributes["url.path"] = path;
|
|
669
|
+
if (port) spanAttributes["server.port"] = port;
|
|
670
|
+
if (query) spanAttributes["url.query"] = query;
|
|
671
|
+
const spanName = path ? `${method} ${path}` : method;
|
|
672
|
+
return tracer.startActiveSpan(spanName, {
|
|
673
|
+
kind: _opentelemetry_api.SpanKind.CLIENT,
|
|
674
|
+
attributes: spanAttributes
|
|
675
|
+
}, _opentelemetry_api.context.active(), async (span) => {
|
|
676
|
+
try {
|
|
677
|
+
const carrier = {};
|
|
678
|
+
_opentelemetry_api.propagation.inject(_opentelemetry_api.context.active(), carrier);
|
|
679
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : void 0));
|
|
680
|
+
for (const [key, value] of Object.entries(carrier)) headers.set(key, value);
|
|
681
|
+
for (const name of SAFE_REQUEST_HEADERS) {
|
|
682
|
+
const value = headers.get(name);
|
|
683
|
+
if (value !== null) span.setAttribute(`http.request.header.${name}`, value);
|
|
684
|
+
}
|
|
685
|
+
const requestBodySize = getBodyByteSize(init?.body ?? (input instanceof Request ? input.body : void 0));
|
|
686
|
+
if (requestBodySize !== void 0) span.setAttribute("http.request.body.size", requestBodySize);
|
|
687
|
+
const response = await capturedFetch(input, {
|
|
688
|
+
...init,
|
|
689
|
+
headers
|
|
690
|
+
});
|
|
691
|
+
span.setAttribute("http.response.status_code", response.status);
|
|
692
|
+
const contentLength = response.headers.get("content-length");
|
|
693
|
+
if (contentLength !== null) {
|
|
694
|
+
const size = parseInt(contentLength, 10);
|
|
695
|
+
if (!Number.isNaN(size)) span.setAttribute("http.response.body.size", size);
|
|
696
|
+
}
|
|
697
|
+
for (const name of SAFE_RESPONSE_HEADERS) {
|
|
698
|
+
const value = response.headers.get(name);
|
|
699
|
+
if (value !== null) span.setAttribute(`http.response.header.${name}`, value);
|
|
700
|
+
}
|
|
701
|
+
if (response.status >= 400) {
|
|
702
|
+
span.setAttribute("error.type", String(response.status));
|
|
703
|
+
span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
|
|
704
|
+
} else span.setStatus({ code: _opentelemetry_api.SpanStatusCode.OK });
|
|
705
|
+
return response;
|
|
706
|
+
} catch (error) {
|
|
707
|
+
span.setAttribute("error.type", error.name ?? "Error");
|
|
708
|
+
span.setStatus({
|
|
709
|
+
code: _opentelemetry_api.SpanStatusCode.ERROR,
|
|
710
|
+
message: error.message
|
|
711
|
+
});
|
|
712
|
+
span.recordException(error);
|
|
713
|
+
throw error;
|
|
714
|
+
} finally {
|
|
715
|
+
span.end();
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Restore globalThis.fetch to its original implementation.
|
|
722
|
+
*/
|
|
723
|
+
function unpatchGlobalFetch() {
|
|
724
|
+
if (originalFetch) {
|
|
725
|
+
globalThis.fetch = originalFetch;
|
|
726
|
+
originalFetch = null;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/telemetry-system/utils.ts
|
|
732
|
+
/**
|
|
733
|
+
* Parse a numeric environment variable with optional minimum bound.
|
|
734
|
+
*/
|
|
735
|
+
function parseNumberEnv(value, minimum = 0) {
|
|
736
|
+
if (value === void 0) return void 0;
|
|
737
|
+
const parsed = Number(value);
|
|
738
|
+
if (!Number.isFinite(parsed) || parsed < minimum) return void 0;
|
|
739
|
+
return parsed;
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* Parse an integer environment variable with optional minimum bound.
|
|
743
|
+
*/
|
|
744
|
+
function parseIntegerEnv(value, minimum = 0) {
|
|
745
|
+
const parsed = parseNumberEnv(value, minimum);
|
|
746
|
+
if (parsed === void 0 || !Number.isInteger(parsed)) return void 0;
|
|
747
|
+
return parsed;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
//#endregion
|
|
751
|
+
//#region src/telemetry-system/span-ops.ts
|
|
752
|
+
/** High-level span operations so consumers don't need `@opentelemetry/api`. */
|
|
753
|
+
/** Returns `false` when there is no active span or the sampler dropped it. */
|
|
754
|
+
function currentSpanIsRecording() {
|
|
755
|
+
const span = _opentelemetry_api.trace.getActiveSpan();
|
|
756
|
+
return span ? span.isRecording() : false;
|
|
757
|
+
}
|
|
758
|
+
/** No-op when the current span is not recording. */
|
|
759
|
+
function setCurrentSpanAttribute(key, value) {
|
|
760
|
+
const span = _opentelemetry_api.trace.getActiveSpan();
|
|
761
|
+
if (!span || !span.isRecording()) return;
|
|
762
|
+
span.setAttribute(key, value);
|
|
763
|
+
}
|
|
764
|
+
/** No-op when there is no active span. */
|
|
765
|
+
function setCurrentSpanError(message) {
|
|
766
|
+
const span = _opentelemetry_api.trace.getActiveSpan();
|
|
767
|
+
if (!span) return;
|
|
768
|
+
span.setStatus({
|
|
769
|
+
code: _opentelemetry_api.SpanStatusCode.ERROR,
|
|
770
|
+
message
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
/** No-op when the current span is not recording. */
|
|
774
|
+
function recordSpanEvent(name, attrs) {
|
|
775
|
+
const span = _opentelemetry_api.trace.getActiveSpan();
|
|
776
|
+
if (!span || !span.isRecording()) return;
|
|
777
|
+
span.addEvent(name, attrs);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
//#endregion
|
|
781
|
+
//#region src/telemetry-system/payload.ts
|
|
782
|
+
/** Payload redaction + truncation for invocation event capture. */
|
|
783
|
+
const REDACTED_PLACEHOLDER = "[REDACTED]";
|
|
784
|
+
const TRUNCATION_MARKER = "...\"[TRUNCATED]\"";
|
|
785
|
+
function resolveMaxBytesFromEnv() {
|
|
786
|
+
const raw = process.env.III_TRACE_PAYLOAD_MAX_BYTES;
|
|
787
|
+
if (raw === void 0) return null;
|
|
788
|
+
const trimmed = raw.trim();
|
|
789
|
+
if (trimmed === "" || trimmed.toLowerCase() === "unlimited") return null;
|
|
790
|
+
if (!/^\d+$/.test(trimmed)) return null;
|
|
791
|
+
const parsed = Number(trimmed);
|
|
792
|
+
if (parsed <= 0) return null;
|
|
793
|
+
return parsed;
|
|
794
|
+
}
|
|
795
|
+
const SENSITIVE_FRAGMENTS = [
|
|
796
|
+
"api_key",
|
|
797
|
+
"apikey",
|
|
798
|
+
"api-key",
|
|
799
|
+
"password",
|
|
800
|
+
"secret",
|
|
801
|
+
"credential",
|
|
802
|
+
"authorization",
|
|
803
|
+
"auth_token",
|
|
804
|
+
"access_token",
|
|
805
|
+
"refresh_token",
|
|
806
|
+
"bearer",
|
|
807
|
+
"private_key",
|
|
808
|
+
"client_secret"
|
|
809
|
+
];
|
|
810
|
+
function isSensitiveKey(key) {
|
|
811
|
+
const lower = key.toLowerCase();
|
|
812
|
+
if (SENSITIVE_FRAGMENTS.some((fragment) => lower.includes(fragment))) return true;
|
|
813
|
+
return lower === "token" || lower.endsWith("_token") || lower.endsWith("-token");
|
|
814
|
+
}
|
|
815
|
+
/** Recursively redact values of sensitive keys. Returns a new value. */
|
|
816
|
+
function redact(value) {
|
|
817
|
+
if (value === null || value === void 0) return value;
|
|
818
|
+
if (Array.isArray(value)) return value.map(redact);
|
|
819
|
+
if (typeof value === "object") {
|
|
820
|
+
const out = {};
|
|
821
|
+
for (const [k, v] of Object.entries(value)) out[k] = isSensitiveKey(k) ? REDACTED_PLACEHOLDER : redact(v);
|
|
822
|
+
return out;
|
|
823
|
+
}
|
|
824
|
+
return value;
|
|
825
|
+
}
|
|
826
|
+
/** Redact then serialize to JSON, optionally capped at `maxBytes`. */
|
|
827
|
+
function redactAndTruncate(value, maxBytes = null) {
|
|
828
|
+
const redacted = redact(value);
|
|
829
|
+
let serialized;
|
|
830
|
+
try {
|
|
831
|
+
serialized = JSON.stringify(redacted) ?? "null";
|
|
832
|
+
} catch {
|
|
833
|
+
serialized = "null";
|
|
834
|
+
}
|
|
835
|
+
if (maxBytes === null || maxBytes === void 0 || maxBytes <= 0) return {
|
|
836
|
+
json: serialized,
|
|
837
|
+
truncated: false
|
|
838
|
+
};
|
|
839
|
+
if (Buffer.byteLength(serialized, "utf8") <= maxBytes) return {
|
|
840
|
+
json: serialized,
|
|
841
|
+
truncated: false
|
|
842
|
+
};
|
|
843
|
+
const markerLen = Buffer.byteLength(TRUNCATION_MARKER, "utf8");
|
|
844
|
+
if (maxBytes <= markerLen) return {
|
|
845
|
+
json: TRUNCATION_MARKER.slice(0, maxBytes),
|
|
846
|
+
truncated: true
|
|
847
|
+
};
|
|
848
|
+
const cap = maxBytes - markerLen;
|
|
849
|
+
const buf = Buffer.from(serialized, "utf8");
|
|
850
|
+
let cut = Math.min(cap, buf.length);
|
|
851
|
+
while (cut > 0 && (buf[cut] & 192) === 128) cut -= 1;
|
|
852
|
+
return {
|
|
853
|
+
json: buf.subarray(0, cut).toString("utf8") + TRUNCATION_MARKER,
|
|
854
|
+
truncated: true
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
//#endregion
|
|
859
|
+
//#region src/telemetry-system/index.ts
|
|
860
|
+
/**
|
|
861
|
+
* OpenTelemetry initialization for the III Node SDK.
|
|
862
|
+
*
|
|
863
|
+
* This module provides trace, metrics, and log export to the III Engine
|
|
864
|
+
* via a shared WebSocket connection using OTLP JSON format.
|
|
865
|
+
*/
|
|
866
|
+
/**
|
|
867
|
+
* Normalize an engine WebSocket URL into the dedicated OTEL endpoint.
|
|
868
|
+
* The engine exposes `/otel` for telemetry-only WS connections; routing
|
|
869
|
+
* there keeps this socket out of the worker registry (otherwise it shows
|
|
870
|
+
* up as a ghost null-metadata worker).
|
|
871
|
+
*/
|
|
872
|
+
function appendOtelPath(base) {
|
|
873
|
+
const url = new URL(base);
|
|
874
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
875
|
+
url.pathname = path.endsWith("/otel") ? path : `${path}/otel`;
|
|
876
|
+
return url.toString();
|
|
877
|
+
}
|
|
878
|
+
let sharedConnection = null;
|
|
879
|
+
let tracerProvider = null;
|
|
880
|
+
let meterProvider = null;
|
|
881
|
+
let loggerProvider = null;
|
|
882
|
+
let tracer = null;
|
|
883
|
+
let meter = null;
|
|
884
|
+
let logger = null;
|
|
885
|
+
let serviceName = "iii-node-iii";
|
|
886
|
+
/**
|
|
887
|
+
* Initialize OpenTelemetry with the given configuration.
|
|
888
|
+
* This should be called once at application startup.
|
|
889
|
+
*/
|
|
890
|
+
function initOtel(config = {}) {
|
|
891
|
+
if (!(config.enabled ?? parseBoolEnv(process.env.OTEL_ENABLED, DEFAULT_OTEL_CONFIG.enabled))) {
|
|
892
|
+
console.debug("[OTel] OpenTelemetry is disabled. To enable, remove OTEL_ENABLED=false or set enabled: true in config.");
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
serviceName = config.serviceName ?? process.env.OTEL_SERVICE_NAME ?? DEFAULT_OTEL_CONFIG.serviceName;
|
|
896
|
+
const serviceVersion = config.serviceVersion ?? process.env.SERVICE_VERSION ?? DEFAULT_OTEL_CONFIG.serviceVersion;
|
|
897
|
+
const serviceNamespace = config.serviceNamespace ?? process.env.SERVICE_NAMESPACE;
|
|
898
|
+
const serviceInstanceId = config.serviceInstanceId ?? process.env.SERVICE_INSTANCE_ID ?? (0, node_crypto.randomUUID)();
|
|
899
|
+
const engineWsUrl = config.engineWsUrl ?? process.env.III_URL ?? DEFAULT_OTEL_CONFIG.engineWsUrl;
|
|
900
|
+
const resourceAttributes = {
|
|
901
|
+
[_opentelemetry_semantic_conventions.ATTR_SERVICE_NAME]: serviceName,
|
|
902
|
+
[ATTR_SERVICE_VERSION]: serviceVersion,
|
|
903
|
+
[ATTR_SERVICE_INSTANCE_ID]: serviceInstanceId
|
|
904
|
+
};
|
|
905
|
+
if (serviceNamespace) resourceAttributes[ATTR_SERVICE_NAMESPACE] = serviceNamespace;
|
|
906
|
+
const resource = new _opentelemetry_resources.Resource(resourceAttributes);
|
|
907
|
+
sharedConnection = new SharedEngineConnection(appendOtelPath(engineWsUrl), config.reconnectionConfig);
|
|
908
|
+
const spanExporter = new EngineSpanExporter(sharedConnection);
|
|
909
|
+
tracerProvider = new _opentelemetry_sdk_trace_node.NodeTracerProvider({
|
|
910
|
+
resource,
|
|
911
|
+
spanProcessors: [new BaggageSpanProcessor(), new _opentelemetry_sdk_trace_base.BatchSpanProcessor(spanExporter)]
|
|
912
|
+
});
|
|
913
|
+
_opentelemetry_api.propagation.setGlobalPropagator(new _opentelemetry_core.CompositePropagator({ propagators: [new _opentelemetry_core.W3CTraceContextPropagator(), new _opentelemetry_core.W3CBaggagePropagator()] }));
|
|
914
|
+
tracerProvider.register();
|
|
915
|
+
tracer = _opentelemetry_api.trace.getTracer(serviceName);
|
|
916
|
+
console.debug(`[OTel] Traces initialized: engine=${engineWsUrl}, service=${serviceName}`);
|
|
917
|
+
if (config.metricsEnabled ?? parseBoolEnv(process.env.OTEL_METRICS_ENABLED, DEFAULT_OTEL_CONFIG.metricsEnabled)) {
|
|
918
|
+
const metricsExporter = new EngineMetricsExporter(sharedConnection);
|
|
919
|
+
const exportIntervalMs = config.metricsExportIntervalMs ?? DEFAULT_OTEL_CONFIG.metricsExportIntervalMs;
|
|
920
|
+
meterProvider = new _opentelemetry_sdk_metrics.MeterProvider({
|
|
921
|
+
resource,
|
|
922
|
+
readers: [new _opentelemetry_sdk_metrics.PeriodicExportingMetricReader({
|
|
923
|
+
exporter: metricsExporter,
|
|
924
|
+
exportIntervalMillis: exportIntervalMs
|
|
925
|
+
})]
|
|
926
|
+
});
|
|
927
|
+
_opentelemetry_api.metrics.setGlobalMeterProvider(meterProvider);
|
|
928
|
+
meter = meterProvider.getMeter(serviceName);
|
|
929
|
+
console.debug(`[OTel] Metrics initialized: interval=${exportIntervalMs}ms`);
|
|
930
|
+
}
|
|
931
|
+
const instrumentations = [...config.instrumentations ?? []];
|
|
932
|
+
if (instrumentations.length > 0) {
|
|
933
|
+
(0, _opentelemetry_instrumentation.registerInstrumentations)({
|
|
934
|
+
instrumentations,
|
|
935
|
+
tracerProvider,
|
|
936
|
+
meterProvider: meterProvider ?? void 0
|
|
937
|
+
});
|
|
938
|
+
console.debug(`[OTel] Instrumentations registered: ${instrumentations.length} total`);
|
|
939
|
+
}
|
|
940
|
+
if (config.fetchInstrumentationEnabled ?? DEFAULT_OTEL_CONFIG.fetchInstrumentationEnabled) {
|
|
941
|
+
patchGlobalFetch(tracer);
|
|
942
|
+
console.debug("[OTel] Global fetch instrumentation enabled");
|
|
943
|
+
}
|
|
944
|
+
const logExporter = new EngineLogExporter(sharedConnection);
|
|
945
|
+
const logsScheduledDelayMillis = config.logsFlushIntervalMs ?? parseNumberEnv(process.env.OTEL_LOGS_FLUSH_INTERVAL_MS, 0) ?? DEFAULT_OTEL_CONFIG.logsFlushIntervalMs;
|
|
946
|
+
const logsMaxExportBatchSize = config.logsBatchSize ?? parseIntegerEnv(process.env.OTEL_LOGS_BATCH_SIZE, 1) ?? DEFAULT_OTEL_CONFIG.logsBatchSize;
|
|
947
|
+
loggerProvider = new _opentelemetry_sdk_logs.LoggerProvider({ resource });
|
|
948
|
+
loggerProvider.addLogRecordProcessor(new _opentelemetry_sdk_logs.BatchLogRecordProcessor(logExporter, {
|
|
949
|
+
scheduledDelayMillis: logsScheduledDelayMillis,
|
|
950
|
+
maxExportBatchSize: logsMaxExportBatchSize
|
|
951
|
+
}));
|
|
952
|
+
logger = loggerProvider.getLogger(serviceName);
|
|
953
|
+
console.debug(`[OTel] Logs initialized: delay=${logsScheduledDelayMillis}ms, batch=${logsMaxExportBatchSize}`);
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Shutdown OpenTelemetry, flushing any pending data.
|
|
957
|
+
*/
|
|
958
|
+
async function shutdownOtel() {
|
|
959
|
+
if (tracerProvider) {
|
|
960
|
+
await tracerProvider.forceFlush();
|
|
961
|
+
await tracerProvider.shutdown();
|
|
962
|
+
tracerProvider = null;
|
|
963
|
+
}
|
|
964
|
+
if (meterProvider) {
|
|
965
|
+
await meterProvider.forceFlush();
|
|
966
|
+
await meterProvider.shutdown();
|
|
967
|
+
meterProvider = null;
|
|
968
|
+
}
|
|
969
|
+
if (loggerProvider) {
|
|
970
|
+
await loggerProvider.forceFlush();
|
|
971
|
+
await loggerProvider.shutdown();
|
|
972
|
+
loggerProvider = null;
|
|
973
|
+
}
|
|
974
|
+
if (sharedConnection) {
|
|
975
|
+
await sharedConnection.shutdown();
|
|
976
|
+
sharedConnection = null;
|
|
977
|
+
}
|
|
978
|
+
unpatchGlobalFetch();
|
|
979
|
+
tracer = null;
|
|
980
|
+
meter = null;
|
|
981
|
+
logger = null;
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Force-flush all OTel providers without tearing them down.
|
|
985
|
+
*
|
|
986
|
+
* Counterpart to {@link shutdownOtel}. Use before short-lived process exits
|
|
987
|
+
* where you want pending spans/metrics/logs delivered but plan to keep using
|
|
988
|
+
* OTel afterwards.
|
|
989
|
+
*/
|
|
990
|
+
async function flushOtel() {
|
|
991
|
+
await Promise.all([
|
|
992
|
+
tracerProvider?.forceFlush(),
|
|
993
|
+
meterProvider?.forceFlush(),
|
|
994
|
+
loggerProvider?.forceFlush()
|
|
995
|
+
].filter(Boolean));
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Get the OpenTelemetry tracer instance.
|
|
999
|
+
*/
|
|
1000
|
+
function getTracer() {
|
|
1001
|
+
return tracer;
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Get the OpenTelemetry meter instance.
|
|
1005
|
+
*/
|
|
1006
|
+
function getMeter() {
|
|
1007
|
+
return meter;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Get the OpenTelemetry logger instance.
|
|
1011
|
+
*/
|
|
1012
|
+
function getLogger() {
|
|
1013
|
+
return logger;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Start a new span with the given name and run the callback within it.
|
|
1017
|
+
*/
|
|
1018
|
+
async function withSpan(name, options, fn) {
|
|
1019
|
+
if (!tracer) {
|
|
1020
|
+
const noopSpan = {
|
|
1021
|
+
spanContext: () => ({
|
|
1022
|
+
traceId: "",
|
|
1023
|
+
spanId: "",
|
|
1024
|
+
traceFlags: 0
|
|
1025
|
+
}),
|
|
1026
|
+
setAttribute: () => noopSpan,
|
|
1027
|
+
setAttributes: () => noopSpan,
|
|
1028
|
+
addEvent: () => noopSpan,
|
|
1029
|
+
addLink: () => noopSpan,
|
|
1030
|
+
setStatus: () => noopSpan,
|
|
1031
|
+
updateName: () => noopSpan,
|
|
1032
|
+
end: () => {},
|
|
1033
|
+
isRecording: () => false,
|
|
1034
|
+
recordException: () => {},
|
|
1035
|
+
addLinks: () => noopSpan
|
|
1036
|
+
};
|
|
1037
|
+
return fn(noopSpan);
|
|
1038
|
+
}
|
|
1039
|
+
const parentContext = options.traceparent ? extractTraceparent(options.traceparent) : _opentelemetry_api.context.active();
|
|
1040
|
+
return tracer.startActiveSpan(name, { kind: options.kind ?? _opentelemetry_api.SpanKind.INTERNAL }, parentContext, async (span) => {
|
|
1041
|
+
try {
|
|
1042
|
+
const result = await fn(span);
|
|
1043
|
+
span.setStatus({ code: _opentelemetry_api.SpanStatusCode.OK });
|
|
1044
|
+
return result;
|
|
1045
|
+
} catch (error) {
|
|
1046
|
+
span.setStatus({
|
|
1047
|
+
code: _opentelemetry_api.SpanStatusCode.ERROR,
|
|
1048
|
+
message: error.message
|
|
1049
|
+
});
|
|
1050
|
+
span.recordException(error);
|
|
1051
|
+
throw error;
|
|
1052
|
+
} finally {
|
|
1053
|
+
span.end();
|
|
1054
|
+
}
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
//#endregion
|
|
1059
|
+
Object.defineProperty(exports, 'BaggageSpanProcessor', {
|
|
1060
|
+
enumerable: true,
|
|
1061
|
+
get: function () {
|
|
1062
|
+
return BaggageSpanProcessor;
|
|
1063
|
+
}
|
|
1064
|
+
});
|
|
1065
|
+
Object.defineProperty(exports, 'DEFAULT_ALLOWLIST', {
|
|
1066
|
+
enumerable: true,
|
|
1067
|
+
get: function () {
|
|
1068
|
+
return DEFAULT_ALLOWLIST;
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1071
|
+
Object.defineProperty(exports, 'REDACTED_PLACEHOLDER', {
|
|
1072
|
+
enumerable: true,
|
|
1073
|
+
get: function () {
|
|
1074
|
+
return REDACTED_PLACEHOLDER;
|
|
1075
|
+
}
|
|
1076
|
+
});
|
|
1077
|
+
Object.defineProperty(exports, 'currentSpanId', {
|
|
1078
|
+
enumerable: true,
|
|
1079
|
+
get: function () {
|
|
1080
|
+
return currentSpanId;
|
|
1081
|
+
}
|
|
1082
|
+
});
|
|
1083
|
+
Object.defineProperty(exports, 'currentSpanIsRecording', {
|
|
1084
|
+
enumerable: true,
|
|
1085
|
+
get: function () {
|
|
1086
|
+
return currentSpanIsRecording;
|
|
1087
|
+
}
|
|
1088
|
+
});
|
|
1089
|
+
Object.defineProperty(exports, 'currentTraceId', {
|
|
1090
|
+
enumerable: true,
|
|
1091
|
+
get: function () {
|
|
1092
|
+
return currentTraceId;
|
|
1093
|
+
}
|
|
1094
|
+
});
|
|
1095
|
+
Object.defineProperty(exports, 'extractBaggage', {
|
|
1096
|
+
enumerable: true,
|
|
1097
|
+
get: function () {
|
|
1098
|
+
return extractBaggage;
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
Object.defineProperty(exports, 'extractContext', {
|
|
1102
|
+
enumerable: true,
|
|
1103
|
+
get: function () {
|
|
1104
|
+
return extractContext;
|
|
1105
|
+
}
|
|
1106
|
+
});
|
|
1107
|
+
Object.defineProperty(exports, 'extractTraceparent', {
|
|
1108
|
+
enumerable: true,
|
|
1109
|
+
get: function () {
|
|
1110
|
+
return extractTraceparent;
|
|
1111
|
+
}
|
|
1112
|
+
});
|
|
1113
|
+
Object.defineProperty(exports, 'flushOtel', {
|
|
1114
|
+
enumerable: true,
|
|
1115
|
+
get: function () {
|
|
1116
|
+
return flushOtel;
|
|
1117
|
+
}
|
|
1118
|
+
});
|
|
1119
|
+
Object.defineProperty(exports, 'getAllBaggage', {
|
|
1120
|
+
enumerable: true,
|
|
1121
|
+
get: function () {
|
|
1122
|
+
return getAllBaggage;
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
Object.defineProperty(exports, 'getBaggageEntry', {
|
|
1126
|
+
enumerable: true,
|
|
1127
|
+
get: function () {
|
|
1128
|
+
return getBaggageEntry;
|
|
1129
|
+
}
|
|
1130
|
+
});
|
|
1131
|
+
Object.defineProperty(exports, 'getLogger', {
|
|
1132
|
+
enumerable: true,
|
|
1133
|
+
get: function () {
|
|
1134
|
+
return getLogger;
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
Object.defineProperty(exports, 'getMeter', {
|
|
1138
|
+
enumerable: true,
|
|
1139
|
+
get: function () {
|
|
1140
|
+
return getMeter;
|
|
1141
|
+
}
|
|
1142
|
+
});
|
|
1143
|
+
Object.defineProperty(exports, 'getTracer', {
|
|
1144
|
+
enumerable: true,
|
|
1145
|
+
get: function () {
|
|
1146
|
+
return getTracer;
|
|
1147
|
+
}
|
|
1148
|
+
});
|
|
1149
|
+
Object.defineProperty(exports, 'initOtel', {
|
|
1150
|
+
enumerable: true,
|
|
1151
|
+
get: function () {
|
|
1152
|
+
return initOtel;
|
|
1153
|
+
}
|
|
1154
|
+
});
|
|
1155
|
+
Object.defineProperty(exports, 'injectBaggage', {
|
|
1156
|
+
enumerable: true,
|
|
1157
|
+
get: function () {
|
|
1158
|
+
return injectBaggage;
|
|
1159
|
+
}
|
|
1160
|
+
});
|
|
1161
|
+
Object.defineProperty(exports, 'injectTraceparent', {
|
|
1162
|
+
enumerable: true,
|
|
1163
|
+
get: function () {
|
|
1164
|
+
return injectTraceparent;
|
|
1165
|
+
}
|
|
1166
|
+
});
|
|
1167
|
+
Object.defineProperty(exports, 'patchGlobalFetch', {
|
|
1168
|
+
enumerable: true,
|
|
1169
|
+
get: function () {
|
|
1170
|
+
return patchGlobalFetch;
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
Object.defineProperty(exports, 'recordSpanEvent', {
|
|
1174
|
+
enumerable: true,
|
|
1175
|
+
get: function () {
|
|
1176
|
+
return recordSpanEvent;
|
|
1177
|
+
}
|
|
1178
|
+
});
|
|
1179
|
+
Object.defineProperty(exports, 'redact', {
|
|
1180
|
+
enumerable: true,
|
|
1181
|
+
get: function () {
|
|
1182
|
+
return redact;
|
|
1183
|
+
}
|
|
1184
|
+
});
|
|
1185
|
+
Object.defineProperty(exports, 'redactAndTruncate', {
|
|
1186
|
+
enumerable: true,
|
|
1187
|
+
get: function () {
|
|
1188
|
+
return redactAndTruncate;
|
|
1189
|
+
}
|
|
1190
|
+
});
|
|
1191
|
+
Object.defineProperty(exports, 'removeBaggageEntry', {
|
|
1192
|
+
enumerable: true,
|
|
1193
|
+
get: function () {
|
|
1194
|
+
return removeBaggageEntry;
|
|
1195
|
+
}
|
|
1196
|
+
});
|
|
1197
|
+
Object.defineProperty(exports, 'resolveMaxBytesFromEnv', {
|
|
1198
|
+
enumerable: true,
|
|
1199
|
+
get: function () {
|
|
1200
|
+
return resolveMaxBytesFromEnv;
|
|
1201
|
+
}
|
|
1202
|
+
});
|
|
1203
|
+
Object.defineProperty(exports, 'setBaggageEntry', {
|
|
1204
|
+
enumerable: true,
|
|
1205
|
+
get: function () {
|
|
1206
|
+
return setBaggageEntry;
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
Object.defineProperty(exports, 'setCurrentSpanAttribute', {
|
|
1210
|
+
enumerable: true,
|
|
1211
|
+
get: function () {
|
|
1212
|
+
return setCurrentSpanAttribute;
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
Object.defineProperty(exports, 'setCurrentSpanError', {
|
|
1216
|
+
enumerable: true,
|
|
1217
|
+
get: function () {
|
|
1218
|
+
return setCurrentSpanError;
|
|
1219
|
+
}
|
|
1220
|
+
});
|
|
1221
|
+
Object.defineProperty(exports, 'shutdownOtel', {
|
|
1222
|
+
enumerable: true,
|
|
1223
|
+
get: function () {
|
|
1224
|
+
return shutdownOtel;
|
|
1225
|
+
}
|
|
1226
|
+
});
|
|
1227
|
+
Object.defineProperty(exports, 'unpatchGlobalFetch', {
|
|
1228
|
+
enumerable: true,
|
|
1229
|
+
get: function () {
|
|
1230
|
+
return unpatchGlobalFetch;
|
|
1231
|
+
}
|
|
1232
|
+
});
|
|
1233
|
+
Object.defineProperty(exports, 'withSpan', {
|
|
1234
|
+
enumerable: true,
|
|
1235
|
+
get: function () {
|
|
1236
|
+
return withSpan;
|
|
1237
|
+
}
|
|
1238
|
+
});
|
|
1239
|
+
//# sourceMappingURL=telemetry-system-BNjGWyzY.cjs.map
|