autotel-devtools 8.1.1 → 10.0.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.
Files changed (63) hide show
  1. package/dist/cli.cjs +108 -1429
  2. package/dist/cli.cjs.map +1 -1
  3. package/dist/cli.d.cts +1 -1
  4. package/dist/cli.d.ts +1 -1
  5. package/dist/cli.js +109 -1422
  6. package/dist/cli.js.map +1 -1
  7. package/dist/error-aggregator-BvNmgn7E.d.ts +120 -0
  8. package/dist/error-aggregator-nnfbpSR7.d.cts +120 -0
  9. package/dist/exporter-1Y3GmLVS.d.cts +182 -0
  10. package/dist/exporter-CZ5HdD3o.d.ts +182 -0
  11. package/dist/genai/index.cjs +650 -537
  12. package/dist/genai/index.cjs.map +1 -1
  13. package/dist/genai/index.d.cts +164 -157
  14. package/dist/genai/index.d.ts +164 -157
  15. package/dist/genai/index.js +649 -536
  16. package/dist/genai/index.js.map +1 -1
  17. package/dist/http-BkkKa9C_.js +1128 -0
  18. package/dist/http-BkkKa9C_.js.map +1 -0
  19. package/dist/http-Yj6iSrMX.cjs +1275 -0
  20. package/dist/http-Yj6iSrMX.cjs.map +1 -0
  21. package/dist/index.cjs +50 -1728
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +21 -23
  24. package/dist/index.d.ts +21 -23
  25. package/dist/index.js +44 -1716
  26. package/dist/index.js.map +1 -1
  27. package/dist/listen-BBsxO0wm.cjs +125 -0
  28. package/dist/listen-BBsxO0wm.cjs.map +1 -0
  29. package/dist/listen-DfOCquUq.js +120 -0
  30. package/dist/listen-DfOCquUq.js.map +1 -0
  31. package/dist/resource-utils-B4UVvfnH.js +18 -0
  32. package/dist/resource-utils-B4UVvfnH.js.map +1 -0
  33. package/dist/resource-utils-DjHJB6uc.cjs +24 -0
  34. package/dist/resource-utils-DjHJB6uc.cjs.map +1 -0
  35. package/dist/server/exporter.cjs +135 -159
  36. package/dist/server/exporter.cjs.map +1 -1
  37. package/dist/server/exporter.d.cts +2 -4
  38. package/dist/server/exporter.d.ts +2 -4
  39. package/dist/server/exporter.js +134 -158
  40. package/dist/server/exporter.js.map +1 -1
  41. package/dist/server/index.cjs +29 -1660
  42. package/dist/server/index.d.cts +34 -31
  43. package/dist/server/index.d.ts +34 -31
  44. package/dist/server/index.js +5 -1630
  45. package/dist/server/log-exporter.cjs +75 -102
  46. package/dist/server/log-exporter.cjs.map +1 -1
  47. package/dist/server/log-exporter.d.cts +27 -46
  48. package/dist/server/log-exporter.d.ts +27 -46
  49. package/dist/server/log-exporter.js +74 -100
  50. package/dist/server/log-exporter.js.map +1 -1
  51. package/dist/server/remote-exporter.cjs +171 -213
  52. package/dist/server/remote-exporter.cjs.map +1 -1
  53. package/dist/server/remote-exporter.d.cts +62 -82
  54. package/dist/server/remote-exporter.d.ts +62 -82
  55. package/dist/server/remote-exporter.js +170 -212
  56. package/dist/server/remote-exporter.js.map +1 -1
  57. package/package.json +5 -5
  58. package/dist/error-aggregator-D0Uu5r38.d.ts +0 -147
  59. package/dist/error-aggregator-D1Mr221Y.d.cts +0 -147
  60. package/dist/exporter-De6p4iAD.d.cts +0 -182
  61. package/dist/exporter-De6p4iAD.d.ts +0 -182
  62. package/dist/server/index.cjs.map +0 -1
  63. package/dist/server/index.js.map +0 -1
@@ -0,0 +1,1275 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+ const require_resource_utils = require('./resource-utils-DjHJB6uc.cjs');
29
+ let node_http = require("node:http");
30
+ let ws = require("ws");
31
+ let node_fs = require("node:fs");
32
+ let node_path = require("node:path");
33
+ let node_url = require("node:url");
34
+ let protobufjs = require("protobufjs");
35
+ protobufjs = __toESM(protobufjs, 1);
36
+
37
+ //#region src/server/error-aggregator.ts
38
+ var ErrorAggregator = class {
39
+ errorGroups = /* @__PURE__ */ new Map();
40
+ options;
41
+ constructor(options = {}) {
42
+ this.options = {
43
+ maxGroups: options.maxGroups ?? 100,
44
+ maxAffectedTraces: options.maxAffectedTraces ?? 10,
45
+ maxAffectedSpans: options.maxAffectedSpans ?? 5,
46
+ stackFramesForFingerprint: options.stackFramesForFingerprint ?? 5
47
+ };
48
+ }
49
+ /**
50
+ * Add an error occurrence to the aggregator
51
+ */
52
+ addError(occurrence) {
53
+ const fingerprint = this.generateFingerprint(occurrence);
54
+ const existing = this.errorGroups.get(fingerprint);
55
+ if (existing) {
56
+ existing.count++;
57
+ existing.lastSeen = occurrence.timestamp;
58
+ if (!existing.affectedTraces.includes(occurrence.traceId)) {
59
+ existing.affectedTraces.push(occurrence.traceId);
60
+ if (existing.affectedTraces.length > this.options.maxAffectedTraces) existing.affectedTraces.shift();
61
+ }
62
+ if (!existing.affectedSpans.includes(occurrence.spanName)) {
63
+ existing.affectedSpans.push(occurrence.spanName);
64
+ if (existing.affectedSpans.length > this.options.maxAffectedSpans) existing.affectedSpans.shift();
65
+ }
66
+ return existing;
67
+ }
68
+ const newGroup = {
69
+ fingerprint,
70
+ type: occurrence.error.type,
71
+ message: occurrence.error.message,
72
+ stackTrace: this.normalizeStackTrace(occurrence.error.stackTrace),
73
+ count: 1,
74
+ firstSeen: occurrence.timestamp,
75
+ lastSeen: occurrence.timestamp,
76
+ affectedTraces: [occurrence.traceId],
77
+ affectedSpans: [occurrence.spanName],
78
+ service: occurrence.service,
79
+ attributes: occurrence.attributes
80
+ };
81
+ if (this.errorGroups.size >= this.options.maxGroups) this.evictOldestGroup();
82
+ this.errorGroups.set(fingerprint, newGroup);
83
+ return newGroup;
84
+ }
85
+ /**
86
+ * Extract errors from a trace and add them to the aggregator
87
+ */
88
+ addErrorsFromTrace(trace) {
89
+ const addedGroups = [];
90
+ for (const span of trace.spans) if (span.status.code === "ERROR") {
91
+ const occurrence = this.extractErrorFromSpan(span, trace);
92
+ if (occurrence) {
93
+ const group = this.addError(occurrence);
94
+ addedGroups.push(group);
95
+ }
96
+ }
97
+ return addedGroups;
98
+ }
99
+ /**
100
+ * Extract error occurrence from a span
101
+ */
102
+ extractErrorFromSpan(span, trace) {
103
+ const exceptionEvent = span.events?.find((e) => e.name === "exception");
104
+ const errorType = span.attributes["exception.type"] || span.attributes["error.type"] || exceptionEvent?.attributes?.["exception.type"] || "Error";
105
+ const errorMessage = span.status.message || span.attributes["exception.message"] || span.attributes["error.message"] || "Unknown error";
106
+ const stackTrace = span.attributes["exception.stacktrace"] || span.attributes["exception.stack"] || this.extractStackFromEvents(span);
107
+ return {
108
+ traceId: trace.traceId,
109
+ spanId: span.spanId,
110
+ spanName: span.name,
111
+ service: trace.service,
112
+ timestamp: span.endTime,
113
+ error: {
114
+ type: errorType,
115
+ message: errorMessage,
116
+ stackTrace
117
+ },
118
+ attributes: this.extractRelevantAttributes(span.attributes)
119
+ };
120
+ }
121
+ /**
122
+ * Extract stack trace from span events (exception events)
123
+ */
124
+ extractStackFromEvents(span) {
125
+ if (!span.events) return void 0;
126
+ const exceptionEvent = span.events.find((e) => e.name === "exception");
127
+ if (exceptionEvent?.attributes) return exceptionEvent.attributes["exception.stacktrace"] || exceptionEvent.attributes["exception.stack"];
128
+ }
129
+ /**
130
+ * Extract relevant attributes for error context
131
+ */
132
+ extractRelevantAttributes(attributes) {
133
+ const relevant = {};
134
+ for (const key of [
135
+ "http.method",
136
+ "http.url",
137
+ "http.route",
138
+ "http.status_code",
139
+ "db.system",
140
+ "db.operation",
141
+ "rpc.method",
142
+ "rpc.service",
143
+ "code.function",
144
+ "code.filepath",
145
+ "user.id",
146
+ "operation.name"
147
+ ]) if (key in attributes) relevant[key] = attributes[key];
148
+ return relevant;
149
+ }
150
+ /**
151
+ * Generate a fingerprint for error grouping
152
+ *
153
+ * Uses error type + first N stack frames (normalized)
154
+ */
155
+ generateFingerprint(occurrence) {
156
+ const parts = [occurrence.error.type];
157
+ if (occurrence.error.stackTrace) {
158
+ const frames = this.extractStackFrames(occurrence.error.stackTrace, this.options.stackFramesForFingerprint);
159
+ parts.push(...frames);
160
+ } else parts.push(this.normalizeMessage(occurrence.error.message));
161
+ return this.simpleHash(parts.join("|"));
162
+ }
163
+ /**
164
+ * Extract and normalize stack frames from a stack trace
165
+ */
166
+ extractStackFrames(stackTrace, count) {
167
+ const lines = stackTrace.split("\n");
168
+ const frames = [];
169
+ for (const line of lines) {
170
+ if (frames.length >= count) break;
171
+ const trimmed = line.trim();
172
+ const nodeMatch = trimmed.match(/^at\s+(.+?)\s+\((.+?):(\d+):\d+\)$/);
173
+ if (nodeMatch) {
174
+ frames.push(`${nodeMatch[1]}@${this.normalizeFilePath(nodeMatch[2])}`);
175
+ continue;
176
+ }
177
+ const anonMatch = trimmed.match(/^at\s+(.+?):(\d+):\d+$/);
178
+ if (anonMatch) {
179
+ frames.push(`anonymous@${this.normalizeFilePath(anonMatch[1])}`);
180
+ continue;
181
+ }
182
+ const browserMatch = trimmed.match(/^(.+?)@(.+?):(\d+):\d+$/);
183
+ if (browserMatch) {
184
+ frames.push(`${browserMatch[1]}@${this.normalizeFilePath(browserMatch[2])}`);
185
+ continue;
186
+ }
187
+ }
188
+ return frames;
189
+ }
190
+ /**
191
+ * Normalize file path by removing absolute path prefixes and node_modules paths
192
+ */
193
+ normalizeFilePath(filePath) {
194
+ const nodeModulesMatch = filePath.match(/node_modules\/(@[^/]+\/[^/]+|[^/]+)/);
195
+ if (nodeModulesMatch) return `[npm]/${nodeModulesMatch[1]}`;
196
+ return filePath.replace(/^.*?\/src\//, "src/").replace(/^.*?\/dist\//, "dist/").replace(/^.*?\/lib\//, "lib/").replace(/^file:\/\//, "");
197
+ }
198
+ /**
199
+ * Normalize error message by removing dynamic parts
200
+ */
201
+ normalizeMessage(message) {
202
+ return message.replaceAll(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, "[UUID]").replaceAll(/\b[0-9a-f]{16,}\b/gi, "[ID]").replaceAll(/\b\d+\b/g, "[N]").replaceAll(/"[^"]*"/g, "\"[STR]\"").replaceAll(/'[^']*'/g, "'[STR]'").slice(0, 200);
203
+ }
204
+ /**
205
+ * Normalize stack trace for display
206
+ */
207
+ normalizeStackTrace(stackTrace) {
208
+ if (!stackTrace) return void 0;
209
+ return stackTrace.split("\n").slice(0, 10).join("\n");
210
+ }
211
+ /**
212
+ * Simple hash function for fingerprinting
213
+ */
214
+ simpleHash(str) {
215
+ let hash = 0;
216
+ for (let i = 0; i < str.length; i++) {
217
+ const char = str.charCodeAt(i);
218
+ hash = (hash << 5) - hash + char;
219
+ hash = hash & hash;
220
+ }
221
+ return Math.abs(hash).toString(16).padStart(8, "0");
222
+ }
223
+ /**
224
+ * Evict the oldest error group
225
+ */
226
+ evictOldestGroup() {
227
+ let oldest = null;
228
+ for (const [fingerprint, group] of this.errorGroups) if (!oldest || group.lastSeen < oldest.lastSeen) oldest = {
229
+ fingerprint,
230
+ lastSeen: group.lastSeen
231
+ };
232
+ if (oldest) this.errorGroups.delete(oldest.fingerprint);
233
+ }
234
+ /**
235
+ * Get all error groups, sorted by most recent
236
+ */
237
+ getErrorGroups() {
238
+ return [...this.errorGroups.values()].sort((a, b) => b.lastSeen - a.lastSeen);
239
+ }
240
+ /**
241
+ * Get error groups sorted by count (most frequent first)
242
+ */
243
+ getErrorGroupsByFrequency() {
244
+ return [...this.errorGroups.values()].sort((a, b) => b.count - a.count);
245
+ }
246
+ /**
247
+ * Get a specific error group by fingerprint
248
+ */
249
+ getErrorGroup(fingerprint) {
250
+ return this.errorGroups.get(fingerprint);
251
+ }
252
+ /**
253
+ * Get error groups for a specific service
254
+ */
255
+ getErrorGroupsByService(service) {
256
+ return this.getErrorGroups().filter((g) => g.service === service);
257
+ }
258
+ /**
259
+ * Get total error count across all groups
260
+ */
261
+ getTotalErrorCount() {
262
+ let total = 0;
263
+ for (const group of this.errorGroups.values()) total += group.count;
264
+ return total;
265
+ }
266
+ /**
267
+ * Get error statistics
268
+ */
269
+ getStats() {
270
+ const oneHourAgo = Date.now() - 3600 * 1e3;
271
+ let recentErrors = 0;
272
+ const typeCount = /* @__PURE__ */ new Map();
273
+ for (const group of this.errorGroups.values()) {
274
+ if (group.lastSeen > oneHourAgo) recentErrors += group.count;
275
+ typeCount.set(group.type, (typeCount.get(group.type) || 0) + group.count);
276
+ }
277
+ const topErrorTypes = [...typeCount.entries()].map(([type, count]) => ({
278
+ type,
279
+ count
280
+ })).sort((a, b) => b.count - a.count).slice(0, 5);
281
+ return {
282
+ totalGroups: this.errorGroups.size,
283
+ totalErrors: this.getTotalErrorCount(),
284
+ recentErrors,
285
+ topErrorTypes
286
+ };
287
+ }
288
+ /**
289
+ * Clear all error groups
290
+ */
291
+ clear() {
292
+ this.errorGroups.clear();
293
+ }
294
+ /**
295
+ * Clear old error groups (not seen in given time window)
296
+ */
297
+ clearOlderThan(maxAgeMs) {
298
+ const cutoff = Date.now() - maxAgeMs;
299
+ let cleared = 0;
300
+ for (const [fingerprint, group] of this.errorGroups) if (group.lastSeen < cutoff) {
301
+ this.errorGroups.delete(fingerprint);
302
+ cleared++;
303
+ }
304
+ return cleared;
305
+ }
306
+ };
307
+
308
+ //#endregion
309
+ //#region src/server/telemetry-limits.ts
310
+ const defaultLimit = 100;
311
+ function parseLimit(value) {
312
+ if (!value) return void 0;
313
+ const parsed = Number.parseInt(value, 10);
314
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : void 0;
315
+ }
316
+ function resolveTelemetryLimits(args = {}) {
317
+ const env = args.env ?? process.env;
318
+ const fallback = args.maxHistory ?? defaultLimit;
319
+ return {
320
+ maxTraceCount: args.maxTraceCount ?? parseLimit(env.AUTOTEL_MAX_TRACE_COUNT) ?? fallback,
321
+ maxLogCount: args.maxLogCount ?? parseLimit(env.AUTOTEL_MAX_LOG_COUNT) ?? fallback,
322
+ maxMetricCount: args.maxMetricCount ?? parseLimit(env.AUTOTEL_MAX_METRIC_COUNT) ?? fallback
323
+ };
324
+ }
325
+ function appendWithLimit(items, item, limit) {
326
+ if (limit <= 0) return [];
327
+ const next = [...items, item];
328
+ return next.length > limit ? next.slice(next.length - limit) : next;
329
+ }
330
+ function appendManyWithLimit(items, incoming, limit) {
331
+ if (limit <= 0 || incoming.length === 0) return limit <= 0 ? [] : items;
332
+ const next = [...items, ...incoming];
333
+ return next.length > limit ? next.slice(next.length - limit) : next;
334
+ }
335
+ function applyTelemetryLimits(data, limits) {
336
+ return {
337
+ ...data,
338
+ traces: data.traces.slice(-limits.maxTraceCount),
339
+ logs: data.logs.slice(-limits.maxLogCount),
340
+ metrics: data.metrics.slice(-limits.maxMetricCount)
341
+ };
342
+ }
343
+
344
+ //#endregion
345
+ //#region src/server/origin-guard.ts
346
+ const LOOPBACK_IPV6 = new Set(["::1", "0:0:0:0:0:0:0:1"]);
347
+ /** True for `localhost`, any `127.x.x.x`, and IPv6 loopback. Case-insensitive. */
348
+ function isLoopbackHostname(hostname) {
349
+ const h = hostname.toLowerCase().replace(/^\[|\]$/g, "");
350
+ return h === "localhost" || /^127\./.test(h) || LOOPBACK_IPV6.has(h);
351
+ }
352
+ /** Hostname from a `Host` header (`host`, `host:port`, `[::1]:port`). */
353
+ function hostnameFromHostHeader(host) {
354
+ const h = host.trim();
355
+ if (h.startsWith("[")) {
356
+ const end = h.indexOf("]");
357
+ return end > 0 ? h.slice(1, end) : h;
358
+ }
359
+ const colon = h.indexOf(":");
360
+ return colon === -1 ? h : h.slice(0, colon);
361
+ }
362
+ /** True when the `Host` header names a loopback host. */
363
+ function hostHeaderIsLoopback(host) {
364
+ return isLoopbackHostname(hostnameFromHostHeader(host));
365
+ }
366
+ /** True when an `Origin` header names a loopback origin. A malformed or opaque
367
+ * origin (e.g. the literal `null` from a sandboxed iframe) is treated as
368
+ * non-loopback. */
369
+ function originIsLoopback(origin) {
370
+ try {
371
+ return isLoopbackHostname(new URL(origin).hostname);
372
+ } catch {
373
+ return false;
374
+ }
375
+ }
376
+ /**
377
+ * Decide whether a request to a sensitive (read/mutate) endpoint is allowed.
378
+ * - A present, non-loopback `Origin` is always rejected (cross-origin read).
379
+ * - When `loopbackOnly`, a present, non-loopback `Host` is rejected (DNS
380
+ * rebinding). Skipped when the receiver is bound to a non-loopback host.
381
+ */
382
+ function allowSensitiveRequest(headers, loopbackOnly) {
383
+ const { origin, host } = headers;
384
+ if (origin && origin.length > 0 && !originIsLoopback(origin)) return false;
385
+ if (loopbackOnly && host && host.length > 0 && !hostHeaderIsLoopback(host)) return false;
386
+ return true;
387
+ }
388
+
389
+ //#endregion
390
+ //#region src/server/server.ts
391
+ var DevtoolsServer = class {
392
+ wss;
393
+ clients = /* @__PURE__ */ new Set();
394
+ httpServer;
395
+ traces = [];
396
+ logs = [];
397
+ metrics = [];
398
+ errorAggregator = new ErrorAggregator();
399
+ limits;
400
+ verbose;
401
+ _port;
402
+ onData;
403
+ constructor(options = {}) {
404
+ this.limits = resolveTelemetryLimits(options);
405
+ this.verbose = options.verbose ?? false;
406
+ this._port = options.port ?? 4318;
407
+ this.onData = options.onData;
408
+ this.httpServer = options.server ?? (0, node_http.createServer)();
409
+ const loopbackOnly = options.host == null || hostHeaderIsLoopback(options.host);
410
+ this.wss = new ws.WebSocketServer({
411
+ server: this.httpServer,
412
+ path: options.path ?? "/ws",
413
+ verifyClient: ({ origin, req }) => allowSensitiveRequest({
414
+ origin,
415
+ host: req.headers.host
416
+ }, loopbackOnly)
417
+ });
418
+ this.wss.on("error", (err) => {
419
+ if (this.httpServer.listening) throw err;
420
+ });
421
+ this.wss.on("connection", (ws$1) => {
422
+ this.clients.add(ws$1);
423
+ this.log(`Client connected (${this.clients.size} total)`);
424
+ const data = this.getCurrentData();
425
+ if (data.traces.length > 0 || data.logs.length > 0 || data.errors.length > 0) ws$1.send(JSON.stringify(data));
426
+ ws$1.on("close", () => {
427
+ this.clients.delete(ws$1);
428
+ this.log(`Client disconnected (${this.clients.size} total)`);
429
+ });
430
+ });
431
+ if (!options.server) this.httpServer.listen(this._port, () => {
432
+ const addr = this.httpServer.address();
433
+ if (addr && typeof addr === "object") this._port = addr.port;
434
+ this.log(`WebSocket server listening on port ${this._port}`);
435
+ });
436
+ }
437
+ get port() {
438
+ const addr = this.httpServer.address();
439
+ if (addr && typeof addr === "object") return addr.port;
440
+ return this._port;
441
+ }
442
+ get clientCount() {
443
+ return this.clients.size;
444
+ }
445
+ addTrace(trace) {
446
+ const existing = this.traces.find((t) => t.traceId === trace.traceId);
447
+ const merged = existing ?? trace;
448
+ if (existing) {
449
+ const existingSpanIds = new Set(existing.spans.map((s) => s.spanId));
450
+ for (const span of trace.spans) if (!existingSpanIds.has(span.spanId)) existing.spans.push(span);
451
+ existing.startTime = Math.min(existing.startTime, trace.startTime);
452
+ existing.endTime = Math.max(existing.endTime, trace.endTime);
453
+ existing.duration = existing.endTime - existing.startTime;
454
+ if (trace.status === "ERROR") existing.status = "ERROR";
455
+ const root = existing.spans.find((s) => !s.parentSpanId);
456
+ if (root) {
457
+ existing.rootSpan = root;
458
+ const rootService = root.attributes?.["service.name"];
459
+ if (typeof rootService === "string" && rootService.length > 0) existing.service = rootService;
460
+ }
461
+ } else this.traces = appendWithLimit(this.traces, trace, this.limits.maxTraceCount);
462
+ this.errorAggregator.addErrorsFromTrace(trace);
463
+ this.broadcast({
464
+ traces: [merged],
465
+ metrics: [],
466
+ logs: [],
467
+ errors: this.errorAggregator.getErrorGroups()
468
+ });
469
+ }
470
+ addTraces(traces) {
471
+ for (const trace of traces) this.addTrace(trace);
472
+ }
473
+ addLog(log) {
474
+ this.logs = appendWithLimit(this.logs, log, this.limits.maxLogCount);
475
+ this.broadcast({
476
+ traces: [],
477
+ metrics: [],
478
+ logs: [log],
479
+ errors: this.errorAggregator.getErrorGroups()
480
+ });
481
+ }
482
+ addLogs(logs) {
483
+ this.logs = appendManyWithLimit(this.logs, logs, this.limits.maxLogCount);
484
+ this.broadcast({
485
+ traces: [],
486
+ metrics: [],
487
+ logs,
488
+ errors: this.errorAggregator.getErrorGroups()
489
+ });
490
+ }
491
+ addMetric(metric) {
492
+ this.metrics = appendWithLimit(this.metrics, metric, this.limits.maxMetricCount);
493
+ this.broadcast({
494
+ traces: [],
495
+ metrics: [metric],
496
+ logs: [],
497
+ errors: this.errorAggregator.getErrorGroups()
498
+ });
499
+ }
500
+ getCurrentData() {
501
+ return {
502
+ traces: this.traces,
503
+ metrics: this.metrics,
504
+ logs: this.logs,
505
+ errors: this.errorAggregator.getErrorGroups()
506
+ };
507
+ }
508
+ clearData() {
509
+ this.traces = [];
510
+ this.logs = [];
511
+ this.metrics = [];
512
+ this.errorAggregator.clear();
513
+ }
514
+ broadcast(data) {
515
+ const msg = JSON.stringify(data);
516
+ for (const client of this.clients) if (client.readyState === ws.WebSocket.OPEN) client.send(msg);
517
+ if (this.onData) try {
518
+ this.onData(data);
519
+ } catch {}
520
+ }
521
+ log(message) {
522
+ if (this.verbose) console.log(`[autotel-devtools] ${message}`);
523
+ }
524
+ async close() {
525
+ for (const client of this.clients) client.close();
526
+ this.clients.clear();
527
+ this.wss.close();
528
+ await new Promise((resolve) => this.httpServer.close(() => resolve()));
529
+ }
530
+ };
531
+
532
+ //#endregion
533
+ //#region src/server/otlp.ts
534
+ function resolveOtlpValue(v) {
535
+ if (!v) return void 0;
536
+ if (v.stringValue !== void 0) return v.stringValue;
537
+ if (v.boolValue !== void 0) return v.boolValue;
538
+ if (v.intValue !== void 0) return typeof v.intValue === "string" ? Number(v.intValue) : v.intValue;
539
+ if (v.doubleValue !== void 0) return v.doubleValue;
540
+ if (v.bytesValue !== void 0) return v.bytesValue;
541
+ if (v.arrayValue?.values) return v.arrayValue.values.map(resolveOtlpValue);
542
+ if (v.kvlistValue?.values) return flattenAttributes(v.kvlistValue.values);
543
+ }
544
+ function flattenAttributes(attrs) {
545
+ const out = {};
546
+ if (!attrs) return out;
547
+ for (const { key, value } of attrs) out[key] = resolveOtlpValue(value);
548
+ return out;
549
+ }
550
+ function nanoToMs(nano) {
551
+ if (!nano) return 0;
552
+ const ns = BigInt(nano);
553
+ const ms = ns / 1000000n;
554
+ const remNs = ns % 1000000n;
555
+ return Number(ms) + Number(remNs) / 1e6;
556
+ }
557
+ const SPAN_KIND_MAP = {
558
+ 0: "INTERNAL",
559
+ 1: "INTERNAL",
560
+ 2: "SERVER",
561
+ 3: "CLIENT",
562
+ 4: "PRODUCER",
563
+ 5: "CONSUMER",
564
+ SPAN_KIND_INTERNAL: "INTERNAL",
565
+ SPAN_KIND_SERVER: "SERVER",
566
+ SPAN_KIND_CLIENT: "CLIENT",
567
+ SPAN_KIND_PRODUCER: "PRODUCER",
568
+ SPAN_KIND_CONSUMER: "CONSUMER"
569
+ };
570
+ function normalizeHexId(id) {
571
+ if (!id) return "";
572
+ if (/^[A-Za-z0-9+/=]+$/.test(id) && !/^[0-9a-f]+$/i.test(id) && (id.length === 12 || id.length === 24 || id.length === 28 || id.length === 44 || id.length === 48)) try {
573
+ return Buffer.from(id, "base64").toString("hex");
574
+ } catch {}
575
+ return id;
576
+ }
577
+ function parseOtlpTraces(payload) {
578
+ if (!payload || typeof payload !== "object") return [];
579
+ const { resourceSpans } = payload;
580
+ if (!Array.isArray(resourceSpans) || resourceSpans.length === 0) return [];
581
+ const traceMap = /* @__PURE__ */ new Map();
582
+ for (const rs of resourceSpans) {
583
+ const resourceAttrs = flattenAttributes(rs.resource?.attributes);
584
+ const service = String(resourceAttrs["service.name"] || "unknown");
585
+ const scopeSpans = rs.scopeSpans || [];
586
+ for (const ss of scopeSpans) {
587
+ const scope = ss.scope?.name ? {
588
+ name: ss.scope.name,
589
+ version: ss.scope.version || void 0
590
+ } : void 0;
591
+ for (const span of ss.spans || []) {
592
+ const traceId = normalizeHexId(span.traceId);
593
+ if (!traceId) continue;
594
+ const startMs = nanoToMs(span.startTimeUnixNano);
595
+ const endMs = nanoToMs(span.endTimeUnixNano);
596
+ const statusCode = span.status?.code;
597
+ let status = "UNSET";
598
+ if (statusCode === 1 || statusCode === "STATUS_CODE_OK") status = "OK";
599
+ if (statusCode === 2 || statusCode === "STATUS_CODE_ERROR") status = "ERROR";
600
+ const spanData = {
601
+ traceId,
602
+ spanId: normalizeHexId(span.spanId),
603
+ parentSpanId: normalizeHexId(span.parentSpanId) || void 0,
604
+ name: span.name || "unknown",
605
+ kind: SPAN_KIND_MAP[span.kind ?? 0] || "INTERNAL",
606
+ startTime: startMs,
607
+ endTime: endMs,
608
+ duration: endMs - startMs,
609
+ attributes: {
610
+ ...resourceAttrs,
611
+ ...flattenAttributes(span.attributes)
612
+ },
613
+ status: {
614
+ code: status,
615
+ message: span.status?.message
616
+ },
617
+ events: (span.events || []).map((e) => ({
618
+ name: e.name || "",
619
+ timestamp: nanoToMs(e.timeUnixNano),
620
+ attributes: flattenAttributes(e.attributes)
621
+ })),
622
+ links: (span.links || []).map((l) => ({
623
+ traceId: normalizeHexId(l.traceId),
624
+ spanId: normalizeHexId(l.spanId),
625
+ attributes: flattenAttributes(l.attributes)
626
+ })),
627
+ scope
628
+ };
629
+ const existing = traceMap.get(traceId);
630
+ if (existing) existing.spans.push(spanData);
631
+ else traceMap.set(traceId, {
632
+ spans: [spanData],
633
+ service
634
+ });
635
+ }
636
+ }
637
+ }
638
+ const traces = [];
639
+ for (const [traceId, { spans, service }] of traceMap) {
640
+ const sorted = spans.sort((a, b) => a.startTime - b.startTime);
641
+ const rootSpan = sorted.find((s) => !s.parentSpanId) || sorted[0];
642
+ const startTime = Math.min(...sorted.map((s) => s.startTime));
643
+ const endTime = Math.max(...sorted.map((s) => s.endTime));
644
+ const hasError = sorted.some((s) => s.status.code === "ERROR");
645
+ traces.push({
646
+ traceId,
647
+ correlationId: traceId.slice(0, 16),
648
+ rootSpan,
649
+ spans: sorted,
650
+ startTime,
651
+ endTime,
652
+ duration: endTime - startTime,
653
+ status: hasError ? "ERROR" : "OK",
654
+ service
655
+ });
656
+ }
657
+ return traces;
658
+ }
659
+ function parseOtlpLogs(payload) {
660
+ if (!payload || typeof payload !== "object") return [];
661
+ const { resourceLogs } = payload;
662
+ if (!Array.isArray(resourceLogs)) return [];
663
+ const logs = [];
664
+ for (const rl of resourceLogs) {
665
+ const resourceAttrs = flattenAttributes(rl.resource?.attributes);
666
+ for (const sl of rl.scopeLogs || []) for (const rec of sl.logRecords || []) {
667
+ const timestamp = nanoToMs(rec.timeUnixNano || rec.observedTimeUnixNano);
668
+ const traceId = normalizeHexId(rec.traceId) || void 0;
669
+ const spanId = normalizeHexId(rec.spanId) || void 0;
670
+ const body = rec.body ? resolveOtlpValue(rec.body) : "";
671
+ logs.push({
672
+ id: `${traceId || "no-trace"}:${spanId || "no-span"}:${timestamp}:${rec.severityNumber || 0}`,
673
+ traceId,
674
+ spanId,
675
+ resourceName: require_resource_utils.getResourceName(resourceAttrs),
676
+ severityText: rec.severityText,
677
+ severityNumber: rec.severityNumber,
678
+ body: typeof body === "string" ? body : body,
679
+ timestamp,
680
+ attributes: flattenAttributes(rec.attributes),
681
+ resource: resourceAttrs
682
+ });
683
+ }
684
+ }
685
+ return logs;
686
+ }
687
+ function countOtlpMetrics(payload) {
688
+ if (!payload || typeof payload !== "object") return 0;
689
+ const { resourceMetrics } = payload;
690
+ if (!Array.isArray(resourceMetrics)) return 0;
691
+ let count = 0;
692
+ for (const rm of resourceMetrics) for (const sm of rm.scopeMetrics || []) count += (sm.metrics || []).length;
693
+ return count;
694
+ }
695
+ async function readJsonBody(req) {
696
+ return new Promise((resolve, reject) => {
697
+ const chunks = [];
698
+ req.on("data", (chunk) => chunks.push(chunk));
699
+ req.on("end", () => {
700
+ try {
701
+ resolve(JSON.parse(Buffer.concat(chunks).toString()));
702
+ } catch {
703
+ reject(/* @__PURE__ */ new Error("Invalid JSON"));
704
+ }
705
+ });
706
+ req.on("error", reject);
707
+ });
708
+ }
709
+ async function readRawBody(req) {
710
+ return new Promise((resolve, reject) => {
711
+ const chunks = [];
712
+ req.on("data", (chunk) => chunks.push(chunk));
713
+ req.on("end", () => resolve(Buffer.concat(chunks)));
714
+ req.on("error", reject);
715
+ });
716
+ }
717
+ /**
718
+ * True for OTLP/protobuf bodies. The OpenTelemetry Python/Java/Go SDKs default to
719
+ * `http/protobuf` over OTLP HTTP, sending `application/x-protobuf`; some clients use
720
+ * `application/protobuf`. Anything else (JSON, unset) is treated as OTLP/JSON.
721
+ */
722
+ function isProtobufContentType(contentType) {
723
+ if (!contentType) return false;
724
+ const value = contentType.toLowerCase();
725
+ return value.includes("application/x-protobuf") || value.includes("application/protobuf");
726
+ }
727
+ function sendJson(res, status, data) {
728
+ const body = JSON.stringify(data);
729
+ res.writeHead(status, {
730
+ "Content-Type": "application/json",
731
+ "Content-Length": Buffer.byteLength(body)
732
+ });
733
+ res.end(body);
734
+ }
735
+
736
+ //#endregion
737
+ //#region src/server/otlp-proto.ts
738
+ const COMMON_PROTO = `
739
+ syntax = "proto3";
740
+ package opentelemetry.proto.common.v1;
741
+
742
+ message AnyValue {
743
+ oneof value {
744
+ string string_value = 1;
745
+ bool bool_value = 2;
746
+ int64 int_value = 3;
747
+ double double_value = 4;
748
+ ArrayValue array_value = 5;
749
+ KeyValueList kvlist_value = 6;
750
+ bytes bytes_value = 7;
751
+ }
752
+ }
753
+ message ArrayValue { repeated AnyValue values = 1; }
754
+ message KeyValueList { repeated KeyValue values = 1; }
755
+ message KeyValue {
756
+ string key = 1;
757
+ AnyValue value = 2;
758
+ }
759
+ message InstrumentationScope {
760
+ string name = 1;
761
+ string version = 2;
762
+ repeated KeyValue attributes = 3;
763
+ uint32 dropped_attributes_count = 4;
764
+ }
765
+ `;
766
+ const RESOURCE_PROTO = `
767
+ syntax = "proto3";
768
+ package opentelemetry.proto.resource.v1;
769
+
770
+ message Resource {
771
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 1;
772
+ uint32 dropped_attributes_count = 2;
773
+ }
774
+ `;
775
+ const TRACE_PROTO = `
776
+ syntax = "proto3";
777
+ package opentelemetry.proto.trace.v1;
778
+
779
+ message ResourceSpans {
780
+ opentelemetry.proto.resource.v1.Resource resource = 1;
781
+ repeated ScopeSpans scope_spans = 2;
782
+ string schema_url = 3;
783
+ }
784
+ message ScopeSpans {
785
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
786
+ repeated Span spans = 2;
787
+ string schema_url = 3;
788
+ }
789
+ message Span {
790
+ bytes trace_id = 1;
791
+ bytes span_id = 2;
792
+ string trace_state = 3;
793
+ bytes parent_span_id = 4;
794
+ fixed32 flags = 16;
795
+ string name = 5;
796
+ SpanKind kind = 6;
797
+ fixed64 start_time_unix_nano = 7;
798
+ fixed64 end_time_unix_nano = 8;
799
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 9;
800
+ uint32 dropped_attributes_count = 10;
801
+ repeated Event events = 11;
802
+ uint32 dropped_events_count = 12;
803
+ repeated Link links = 13;
804
+ uint32 dropped_links_count = 14;
805
+ Status status = 15;
806
+
807
+ enum SpanKind {
808
+ SPAN_KIND_UNSPECIFIED = 0;
809
+ SPAN_KIND_INTERNAL = 1;
810
+ SPAN_KIND_SERVER = 2;
811
+ SPAN_KIND_CLIENT = 3;
812
+ SPAN_KIND_PRODUCER = 4;
813
+ SPAN_KIND_CONSUMER = 5;
814
+ }
815
+ message Event {
816
+ fixed64 time_unix_nano = 1;
817
+ string name = 2;
818
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 3;
819
+ uint32 dropped_attributes_count = 4;
820
+ }
821
+ message Link {
822
+ bytes trace_id = 1;
823
+ bytes span_id = 2;
824
+ string trace_state = 3;
825
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 4;
826
+ uint32 dropped_attributes_count = 5;
827
+ fixed32 flags = 6;
828
+ }
829
+ }
830
+ message Status {
831
+ reserved 1;
832
+ string message = 2;
833
+ StatusCode code = 3;
834
+
835
+ enum StatusCode {
836
+ STATUS_CODE_UNSET = 0;
837
+ STATUS_CODE_OK = 1;
838
+ STATUS_CODE_ERROR = 2;
839
+ }
840
+ }
841
+ message ExportTraceServiceRequest {
842
+ repeated ResourceSpans resource_spans = 1;
843
+ }
844
+ `;
845
+ const LOGS_PROTO = `
846
+ syntax = "proto3";
847
+ package opentelemetry.proto.logs.v1;
848
+
849
+ enum SeverityNumber {
850
+ SEVERITY_NUMBER_UNSPECIFIED = 0;
851
+ SEVERITY_NUMBER_TRACE = 1;
852
+ SEVERITY_NUMBER_TRACE2 = 2;
853
+ SEVERITY_NUMBER_TRACE3 = 3;
854
+ SEVERITY_NUMBER_TRACE4 = 4;
855
+ SEVERITY_NUMBER_DEBUG = 5;
856
+ SEVERITY_NUMBER_DEBUG2 = 6;
857
+ SEVERITY_NUMBER_DEBUG3 = 7;
858
+ SEVERITY_NUMBER_DEBUG4 = 8;
859
+ SEVERITY_NUMBER_INFO = 9;
860
+ SEVERITY_NUMBER_INFO2 = 10;
861
+ SEVERITY_NUMBER_INFO3 = 11;
862
+ SEVERITY_NUMBER_INFO4 = 12;
863
+ SEVERITY_NUMBER_WARN = 13;
864
+ SEVERITY_NUMBER_WARN2 = 14;
865
+ SEVERITY_NUMBER_WARN3 = 15;
866
+ SEVERITY_NUMBER_WARN4 = 16;
867
+ SEVERITY_NUMBER_ERROR = 17;
868
+ SEVERITY_NUMBER_ERROR2 = 18;
869
+ SEVERITY_NUMBER_ERROR3 = 19;
870
+ SEVERITY_NUMBER_ERROR4 = 20;
871
+ SEVERITY_NUMBER_FATAL = 21;
872
+ SEVERITY_NUMBER_FATAL2 = 22;
873
+ SEVERITY_NUMBER_FATAL3 = 23;
874
+ SEVERITY_NUMBER_FATAL4 = 24;
875
+ }
876
+ message ResourceLogs {
877
+ opentelemetry.proto.resource.v1.Resource resource = 1;
878
+ repeated ScopeLogs scope_logs = 2;
879
+ string schema_url = 3;
880
+ }
881
+ message ScopeLogs {
882
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
883
+ repeated LogRecord log_records = 2;
884
+ string schema_url = 3;
885
+ }
886
+ message LogRecord {
887
+ reserved 4;
888
+ fixed64 time_unix_nano = 1;
889
+ fixed64 observed_time_unix_nano = 11;
890
+ SeverityNumber severity_number = 2;
891
+ string severity_text = 3;
892
+ opentelemetry.proto.common.v1.AnyValue body = 5;
893
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 6;
894
+ uint32 dropped_attributes_count = 7;
895
+ fixed32 flags = 8;
896
+ bytes trace_id = 9;
897
+ bytes span_id = 10;
898
+ }
899
+ message ExportLogsServiceRequest {
900
+ repeated ResourceLogs resource_logs = 1;
901
+ }
902
+ `;
903
+ const METRICS_PROTO = `
904
+ syntax = "proto3";
905
+ package opentelemetry.proto.metrics.v1;
906
+
907
+ message ResourceMetrics {
908
+ opentelemetry.proto.resource.v1.Resource resource = 1;
909
+ repeated ScopeMetrics scope_metrics = 2;
910
+ string schema_url = 3;
911
+ }
912
+ message ScopeMetrics {
913
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
914
+ repeated Metric metrics = 2;
915
+ string schema_url = 3;
916
+ }
917
+ message Metric {
918
+ string name = 1;
919
+ string description = 2;
920
+ string unit = 3;
921
+ }
922
+ message ExportMetricsServiceRequest {
923
+ repeated ResourceMetrics resource_metrics = 1;
924
+ }
925
+ `;
926
+ const TO_OBJECT_OPTIONS = {
927
+ longs: String,
928
+ bytes: String,
929
+ defaults: false
930
+ };
931
+ let cachedRoot = null;
932
+ function getRoot() {
933
+ if (cachedRoot) return cachedRoot;
934
+ const root = new protobufjs.default.Root();
935
+ for (const source of [
936
+ COMMON_PROTO,
937
+ RESOURCE_PROTO,
938
+ TRACE_PROTO,
939
+ LOGS_PROTO,
940
+ METRICS_PROTO
941
+ ]) protobufjs.default.parse(source, root, { keepCase: false });
942
+ root.resolveAll();
943
+ cachedRoot = root;
944
+ return root;
945
+ }
946
+ function decodeRequest(typeName, body) {
947
+ const messageType = getRoot().lookupType(typeName);
948
+ const message = messageType.decode(body);
949
+ return messageType.toObject(message, TO_OBJECT_OPTIONS);
950
+ }
951
+ /** Decode an OTLP/protobuf `ExportTraceServiceRequest` into the OTLP/JSON object shape. */
952
+ function decodeOtlpTraceRequest(body) {
953
+ return decodeRequest("opentelemetry.proto.trace.v1.ExportTraceServiceRequest", body);
954
+ }
955
+ /** Decode an OTLP/protobuf `ExportLogsServiceRequest` into the OTLP/JSON object shape. */
956
+ function decodeOtlpLogsRequest(body) {
957
+ return decodeRequest("opentelemetry.proto.logs.v1.ExportLogsServiceRequest", body);
958
+ }
959
+ /** Decode an OTLP/protobuf `ExportMetricsServiceRequest` into the OTLP/JSON object shape. */
960
+ function decodeOtlpMetricsRequest(body) {
961
+ return decodeRequest("opentelemetry.proto.metrics.v1.ExportMetricsServiceRequest", body);
962
+ }
963
+
964
+ //#endregion
965
+ //#region src/server/identity.ts
966
+ /** Value of the `x-autotel-devtools` response header and the /healthz `service` field. */
967
+ const DEVTOOLS_IDENTITY = "autotel-devtools";
968
+ /**
969
+ * Probe `host:port` over HTTP and classify what is listening. Used when our
970
+ * requested port is busy: it lets us tell "a stale autotel-devtools is still
971
+ * up" (benign) apart from "a foreign collector owns this port" — the latter is
972
+ * the silent footgun where apps keep exporting OTLP to the busy port and reach
973
+ * the wrong process, so the devtools UI stays empty and the app sees errors.
974
+ */
975
+ async function probePortHolder(host, port, timeoutMs = 500) {
976
+ const authority = host.includes(":") ? `[${host}]` : host;
977
+ const controller = new AbortController();
978
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
979
+ try {
980
+ const res = await fetch(`http://${authority}:${port}/healthz`, { signal: controller.signal });
981
+ if (res.headers.get("x-autotel-devtools")) return "autotel-devtools";
982
+ try {
983
+ const body = await res.json();
984
+ if (body && body.service === "autotel-devtools") return "autotel-devtools";
985
+ } catch {}
986
+ return "foreign";
987
+ } catch {
988
+ return "none";
989
+ } finally {
990
+ clearTimeout(timer);
991
+ }
992
+ }
993
+
994
+ //#endregion
995
+ //#region src/server/http.ts
996
+ function sendOtlpError(res, req, e) {
997
+ sendJson(res, 400, {
998
+ error: "Invalid OTLP payload",
999
+ message: e instanceof Error ? e.message : String(e),
1000
+ contentType: req.headers["content-type"] ?? null
1001
+ });
1002
+ }
1003
+ const PROTOBUF_DECODERS = {
1004
+ traces: decodeOtlpTraceRequest,
1005
+ logs: decodeOtlpLogsRequest,
1006
+ metrics: decodeOtlpMetricsRequest
1007
+ };
1008
+ async function readOtlpPayload(req, signal) {
1009
+ if (isProtobufContentType(req.headers["content-type"])) return PROTOBUF_DECODERS[signal](await readRawBody(req));
1010
+ return readJsonBody(req);
1011
+ }
1012
+ function findPackageRoot() {
1013
+ let dir = (0, node_path.dirname)((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
1014
+ for (let i = 0; i < 5; i++) {
1015
+ if ((0, node_fs.existsSync)((0, node_path.resolve)(dir, "package.json"))) return dir;
1016
+ dir = (0, node_path.dirname)(dir);
1017
+ }
1018
+ return dir;
1019
+ }
1020
+ const DEVTOOLS_FAVICON_SVG = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 64 64\"><rect width=\"64\" height=\"64\" rx=\"14\" fill=\"#0f172a\"/><text x=\"32\" y=\"41\" text-anchor=\"middle\" font-size=\"32\">🛰️</text></svg>";
1021
+ const FULLPAGE_HTML = `<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>autotel-devtools</title><link rel="icon" href="/favicon.svg" type="image/svg+xml"><style>*{margin:0;padding:0;box-sizing:border-box}html,body{height:100%;width:100%;overflow:hidden}</style></head><body><script src="/widget.js?mode=fullpage"><\/script></body></html>`;
1022
+ let cachedVersion = null;
1023
+ function getVersion() {
1024
+ if (cachedVersion !== null) return cachedVersion;
1025
+ let version = "unknown";
1026
+ try {
1027
+ const pkg = JSON.parse((0, node_fs.readFileSync)((0, node_path.resolve)(findPackageRoot(), "package.json"), "utf8"));
1028
+ if (typeof pkg.version === "string") version = pkg.version;
1029
+ } catch {}
1030
+ cachedVersion = version;
1031
+ return version;
1032
+ }
1033
+ let cachedWidgetJs = null;
1034
+ function getWidgetJs() {
1035
+ if (!cachedWidgetJs) {
1036
+ const pkgRoot = findPackageRoot();
1037
+ const candidates = [(0, node_path.resolve)(pkgRoot, "dist", "widget.global.js"), (0, node_path.resolve)(pkgRoot, "widget.global.js")];
1038
+ for (const candidate of candidates) try {
1039
+ cachedWidgetJs = (0, node_fs.readFileSync)(candidate, "utf8");
1040
+ break;
1041
+ } catch {}
1042
+ if (!cachedWidgetJs) cachedWidgetJs = "// widget bundle not found - run pnpm build first";
1043
+ }
1044
+ return cachedWidgetJs;
1045
+ }
1046
+ function attachDevtoolsRoutes(httpServer, devtools, options = {}) {
1047
+ const loopbackOnly = options.loopbackOnly ?? true;
1048
+ httpServer.on("request", async (req, res) => {
1049
+ if (req.headers.upgrade?.toLowerCase() === "websocket") return;
1050
+ res.setHeader("Access-Control-Allow-Origin", "*");
1051
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
1052
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type");
1053
+ res.setHeader("x-autotel-devtools", getVersion());
1054
+ res.setHeader("Access-Control-Expose-Headers", "x-autotel-devtools");
1055
+ if (req.method === "OPTIONS") {
1056
+ res.writeHead(204);
1057
+ res.end();
1058
+ return;
1059
+ }
1060
+ const url = req.url || "/";
1061
+ if (req.method === "GET" && url === "/") {
1062
+ res.writeHead(200, {
1063
+ "Content-Type": "text/html; charset=utf-8",
1064
+ "Content-Length": Buffer.byteLength(FULLPAGE_HTML)
1065
+ });
1066
+ res.end(FULLPAGE_HTML);
1067
+ return;
1068
+ }
1069
+ if (req.method === "GET" && url.startsWith("/widget.js")) {
1070
+ const js = getWidgetJs();
1071
+ res.writeHead(200, {
1072
+ "Content-Type": "application/javascript; charset=utf-8",
1073
+ "Content-Length": Buffer.byteLength(js)
1074
+ });
1075
+ res.end(js);
1076
+ return;
1077
+ }
1078
+ if (req.method === "GET" && (url === "/favicon.svg" || url === "/favicon.ico")) {
1079
+ res.writeHead(200, {
1080
+ "Content-Type": "image/svg+xml; charset=utf-8",
1081
+ "Cache-Control": "public, max-age=86400",
1082
+ "Content-Length": Buffer.byteLength(DEVTOOLS_FAVICON_SVG)
1083
+ });
1084
+ res.end(DEVTOOLS_FAVICON_SVG);
1085
+ return;
1086
+ }
1087
+ if (req.method === "GET" && url === "/healthz") {
1088
+ sendJson(res, 200, {
1089
+ ok: true,
1090
+ service: DEVTOOLS_IDENTITY,
1091
+ version: getVersion(),
1092
+ clients: devtools.clientCount
1093
+ });
1094
+ return;
1095
+ }
1096
+ if (req.method === "GET" && url === "/v1/traces") {
1097
+ if (!allowSensitiveRequest(req.headers, loopbackOnly)) {
1098
+ sendJson(res, 403, { error: "Forbidden" });
1099
+ return;
1100
+ }
1101
+ const data = devtools.getCurrentData();
1102
+ sendJson(res, 200, {
1103
+ traces: data.traces,
1104
+ count: data.traces.length
1105
+ });
1106
+ return;
1107
+ }
1108
+ if (req.method === "DELETE" && url === "/v1/traces") {
1109
+ if (!allowSensitiveRequest(req.headers, loopbackOnly)) {
1110
+ sendJson(res, 403, { error: "Forbidden" });
1111
+ return;
1112
+ }
1113
+ devtools.clearData();
1114
+ sendJson(res, 200, { cleared: true });
1115
+ return;
1116
+ }
1117
+ if (req.method === "POST" && url === "/v1/traces") {
1118
+ try {
1119
+ const traces = parseOtlpTraces(await readOtlpPayload(req, "traces"));
1120
+ devtools.addTraces(traces);
1121
+ sendJson(res, 200, { acceptedTraces: traces.length });
1122
+ } catch (e) {
1123
+ sendOtlpError(res, req, e);
1124
+ }
1125
+ return;
1126
+ }
1127
+ if (req.method === "POST" && url === "/v1/logs") {
1128
+ try {
1129
+ const logs = parseOtlpLogs(await readOtlpPayload(req, "logs"));
1130
+ devtools.addLogs(logs);
1131
+ sendJson(res, 200, { acceptedLogs: logs.length });
1132
+ } catch (e) {
1133
+ sendOtlpError(res, req, e);
1134
+ }
1135
+ return;
1136
+ }
1137
+ if (req.method === "POST" && url === "/v1/metrics") {
1138
+ try {
1139
+ sendJson(res, 200, { acceptedMetrics: countOtlpMetrics(await readOtlpPayload(req, "metrics")) });
1140
+ } catch (e) {
1141
+ sendOtlpError(res, req, e);
1142
+ }
1143
+ return;
1144
+ }
1145
+ sendJson(res, 404, { error: "Not found" });
1146
+ });
1147
+ }
1148
+ function createDevtoolsHttpServer(devtools, _options = {}) {
1149
+ const server = (0, node_http.createServer)();
1150
+ attachDevtoolsRoutes(server, devtools);
1151
+ return server;
1152
+ }
1153
+
1154
+ //#endregion
1155
+ Object.defineProperty(exports, 'DEVTOOLS_IDENTITY', {
1156
+ enumerable: true,
1157
+ get: function () {
1158
+ return DEVTOOLS_IDENTITY;
1159
+ }
1160
+ });
1161
+ Object.defineProperty(exports, 'DevtoolsServer', {
1162
+ enumerable: true,
1163
+ get: function () {
1164
+ return DevtoolsServer;
1165
+ }
1166
+ });
1167
+ Object.defineProperty(exports, 'ErrorAggregator', {
1168
+ enumerable: true,
1169
+ get: function () {
1170
+ return ErrorAggregator;
1171
+ }
1172
+ });
1173
+ Object.defineProperty(exports, 'allowSensitiveRequest', {
1174
+ enumerable: true,
1175
+ get: function () {
1176
+ return allowSensitiveRequest;
1177
+ }
1178
+ });
1179
+ Object.defineProperty(exports, 'appendManyWithLimit', {
1180
+ enumerable: true,
1181
+ get: function () {
1182
+ return appendManyWithLimit;
1183
+ }
1184
+ });
1185
+ Object.defineProperty(exports, 'appendWithLimit', {
1186
+ enumerable: true,
1187
+ get: function () {
1188
+ return appendWithLimit;
1189
+ }
1190
+ });
1191
+ Object.defineProperty(exports, 'applyTelemetryLimits', {
1192
+ enumerable: true,
1193
+ get: function () {
1194
+ return applyTelemetryLimits;
1195
+ }
1196
+ });
1197
+ Object.defineProperty(exports, 'attachDevtoolsRoutes', {
1198
+ enumerable: true,
1199
+ get: function () {
1200
+ return attachDevtoolsRoutes;
1201
+ }
1202
+ });
1203
+ Object.defineProperty(exports, 'createDevtoolsHttpServer', {
1204
+ enumerable: true,
1205
+ get: function () {
1206
+ return createDevtoolsHttpServer;
1207
+ }
1208
+ });
1209
+ Object.defineProperty(exports, 'decodeOtlpLogsRequest', {
1210
+ enumerable: true,
1211
+ get: function () {
1212
+ return decodeOtlpLogsRequest;
1213
+ }
1214
+ });
1215
+ Object.defineProperty(exports, 'decodeOtlpMetricsRequest', {
1216
+ enumerable: true,
1217
+ get: function () {
1218
+ return decodeOtlpMetricsRequest;
1219
+ }
1220
+ });
1221
+ Object.defineProperty(exports, 'decodeOtlpTraceRequest', {
1222
+ enumerable: true,
1223
+ get: function () {
1224
+ return decodeOtlpTraceRequest;
1225
+ }
1226
+ });
1227
+ Object.defineProperty(exports, 'hostHeaderIsLoopback', {
1228
+ enumerable: true,
1229
+ get: function () {
1230
+ return hostHeaderIsLoopback;
1231
+ }
1232
+ });
1233
+ Object.defineProperty(exports, 'isLoopbackHostname', {
1234
+ enumerable: true,
1235
+ get: function () {
1236
+ return isLoopbackHostname;
1237
+ }
1238
+ });
1239
+ Object.defineProperty(exports, 'isProtobufContentType', {
1240
+ enumerable: true,
1241
+ get: function () {
1242
+ return isProtobufContentType;
1243
+ }
1244
+ });
1245
+ Object.defineProperty(exports, 'originIsLoopback', {
1246
+ enumerable: true,
1247
+ get: function () {
1248
+ return originIsLoopback;
1249
+ }
1250
+ });
1251
+ Object.defineProperty(exports, 'parseOtlpLogs', {
1252
+ enumerable: true,
1253
+ get: function () {
1254
+ return parseOtlpLogs;
1255
+ }
1256
+ });
1257
+ Object.defineProperty(exports, 'parseOtlpTraces', {
1258
+ enumerable: true,
1259
+ get: function () {
1260
+ return parseOtlpTraces;
1261
+ }
1262
+ });
1263
+ Object.defineProperty(exports, 'probePortHolder', {
1264
+ enumerable: true,
1265
+ get: function () {
1266
+ return probePortHolder;
1267
+ }
1268
+ });
1269
+ Object.defineProperty(exports, 'resolveTelemetryLimits', {
1270
+ enumerable: true,
1271
+ get: function () {
1272
+ return resolveTelemetryLimits;
1273
+ }
1274
+ });
1275
+ //# sourceMappingURL=http-Yj6iSrMX.cjs.map