autotel-devtools 8.1.1 → 9.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.
- package/dist/cli.cjs +108 -1429
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.cts +1 -1
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +109 -1422
- package/dist/cli.js.map +1 -1
- package/dist/error-aggregator-BvNmgn7E.d.ts +120 -0
- package/dist/error-aggregator-nnfbpSR7.d.cts +120 -0
- package/dist/exporter-1Y3GmLVS.d.cts +182 -0
- package/dist/exporter-CZ5HdD3o.d.ts +182 -0
- package/dist/genai/index.cjs +650 -537
- package/dist/genai/index.cjs.map +1 -1
- package/dist/genai/index.d.cts +164 -157
- package/dist/genai/index.d.ts +164 -157
- package/dist/genai/index.js +649 -536
- package/dist/genai/index.js.map +1 -1
- package/dist/http-BkkKa9C_.js +1128 -0
- package/dist/http-BkkKa9C_.js.map +1 -0
- package/dist/http-Yj6iSrMX.cjs +1275 -0
- package/dist/http-Yj6iSrMX.cjs.map +1 -0
- package/dist/index.cjs +50 -1728
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -23
- package/dist/index.d.ts +21 -23
- package/dist/index.js +44 -1716
- package/dist/index.js.map +1 -1
- package/dist/listen-BBsxO0wm.cjs +125 -0
- package/dist/listen-BBsxO0wm.cjs.map +1 -0
- package/dist/listen-DfOCquUq.js +120 -0
- package/dist/listen-DfOCquUq.js.map +1 -0
- package/dist/resource-utils-B4UVvfnH.js +18 -0
- package/dist/resource-utils-B4UVvfnH.js.map +1 -0
- package/dist/resource-utils-DjHJB6uc.cjs +24 -0
- package/dist/resource-utils-DjHJB6uc.cjs.map +1 -0
- package/dist/server/exporter.cjs +135 -159
- package/dist/server/exporter.cjs.map +1 -1
- package/dist/server/exporter.d.cts +2 -4
- package/dist/server/exporter.d.ts +2 -4
- package/dist/server/exporter.js +134 -158
- package/dist/server/exporter.js.map +1 -1
- package/dist/server/index.cjs +29 -1660
- package/dist/server/index.d.cts +34 -31
- package/dist/server/index.d.ts +34 -31
- package/dist/server/index.js +5 -1630
- package/dist/server/log-exporter.cjs +75 -102
- package/dist/server/log-exporter.cjs.map +1 -1
- package/dist/server/log-exporter.d.cts +27 -46
- package/dist/server/log-exporter.d.ts +27 -46
- package/dist/server/log-exporter.js +74 -100
- package/dist/server/log-exporter.js.map +1 -1
- package/dist/server/remote-exporter.cjs +171 -213
- package/dist/server/remote-exporter.cjs.map +1 -1
- package/dist/server/remote-exporter.d.cts +62 -82
- package/dist/server/remote-exporter.d.ts +62 -82
- package/dist/server/remote-exporter.js +170 -212
- package/dist/server/remote-exporter.js.map +1 -1
- package/package.json +5 -5
- package/dist/error-aggregator-D0Uu5r38.d.ts +0 -147
- package/dist/error-aggregator-D1Mr221Y.d.cts +0 -147
- package/dist/exporter-De6p4iAD.d.cts +0 -182
- package/dist/exporter-De6p4iAD.d.ts +0 -182
- package/dist/server/index.cjs.map +0 -1
- package/dist/server/index.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,1719 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
|
|
11
|
-
// src/server/error-aggregator.ts
|
|
12
|
-
var ErrorAggregator = class {
|
|
13
|
-
errorGroups = /* @__PURE__ */ new Map();
|
|
14
|
-
options;
|
|
15
|
-
constructor(options = {}) {
|
|
16
|
-
this.options = {
|
|
17
|
-
maxGroups: options.maxGroups ?? 100,
|
|
18
|
-
maxAffectedTraces: options.maxAffectedTraces ?? 10,
|
|
19
|
-
maxAffectedSpans: options.maxAffectedSpans ?? 5,
|
|
20
|
-
stackFramesForFingerprint: options.stackFramesForFingerprint ?? 5
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Add an error occurrence to the aggregator
|
|
25
|
-
*/
|
|
26
|
-
addError(occurrence) {
|
|
27
|
-
const fingerprint = this.generateFingerprint(occurrence);
|
|
28
|
-
const existing = this.errorGroups.get(fingerprint);
|
|
29
|
-
if (existing) {
|
|
30
|
-
existing.count++;
|
|
31
|
-
existing.lastSeen = occurrence.timestamp;
|
|
32
|
-
if (!existing.affectedTraces.includes(occurrence.traceId)) {
|
|
33
|
-
existing.affectedTraces.push(occurrence.traceId);
|
|
34
|
-
if (existing.affectedTraces.length > this.options.maxAffectedTraces) {
|
|
35
|
-
existing.affectedTraces.shift();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (!existing.affectedSpans.includes(occurrence.spanName)) {
|
|
39
|
-
existing.affectedSpans.push(occurrence.spanName);
|
|
40
|
-
if (existing.affectedSpans.length > this.options.maxAffectedSpans) {
|
|
41
|
-
existing.affectedSpans.shift();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return existing;
|
|
45
|
-
}
|
|
46
|
-
const newGroup = {
|
|
47
|
-
fingerprint,
|
|
48
|
-
type: occurrence.error.type,
|
|
49
|
-
message: occurrence.error.message,
|
|
50
|
-
stackTrace: this.normalizeStackTrace(occurrence.error.stackTrace),
|
|
51
|
-
count: 1,
|
|
52
|
-
firstSeen: occurrence.timestamp,
|
|
53
|
-
lastSeen: occurrence.timestamp,
|
|
54
|
-
affectedTraces: [occurrence.traceId],
|
|
55
|
-
affectedSpans: [occurrence.spanName],
|
|
56
|
-
service: occurrence.service,
|
|
57
|
-
attributes: occurrence.attributes
|
|
58
|
-
};
|
|
59
|
-
if (this.errorGroups.size >= this.options.maxGroups) {
|
|
60
|
-
this.evictOldestGroup();
|
|
61
|
-
}
|
|
62
|
-
this.errorGroups.set(fingerprint, newGroup);
|
|
63
|
-
return newGroup;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Extract errors from a trace and add them to the aggregator
|
|
67
|
-
*/
|
|
68
|
-
addErrorsFromTrace(trace) {
|
|
69
|
-
const addedGroups = [];
|
|
70
|
-
for (const span of trace.spans) {
|
|
71
|
-
if (span.status.code === "ERROR") {
|
|
72
|
-
const occurrence = this.extractErrorFromSpan(span, trace);
|
|
73
|
-
if (occurrence) {
|
|
74
|
-
const group = this.addError(occurrence);
|
|
75
|
-
addedGroups.push(group);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return addedGroups;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Extract error occurrence from a span
|
|
83
|
-
*/
|
|
84
|
-
extractErrorFromSpan(span, trace) {
|
|
85
|
-
const exceptionEvent = span.events?.find((e) => e.name === "exception");
|
|
86
|
-
const errorType = span.attributes["exception.type"] || span.attributes["error.type"] || exceptionEvent?.attributes?.["exception.type"] || "Error";
|
|
87
|
-
const errorMessage = span.status.message || span.attributes["exception.message"] || span.attributes["error.message"] || "Unknown error";
|
|
88
|
-
const stackTrace = span.attributes["exception.stacktrace"] || span.attributes["exception.stack"] || this.extractStackFromEvents(span);
|
|
89
|
-
return {
|
|
90
|
-
traceId: trace.traceId,
|
|
91
|
-
spanId: span.spanId,
|
|
92
|
-
spanName: span.name,
|
|
93
|
-
service: trace.service,
|
|
94
|
-
timestamp: span.endTime,
|
|
95
|
-
error: {
|
|
96
|
-
type: errorType,
|
|
97
|
-
message: errorMessage,
|
|
98
|
-
stackTrace
|
|
99
|
-
},
|
|
100
|
-
attributes: this.extractRelevantAttributes(span.attributes)
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Extract stack trace from span events (exception events)
|
|
105
|
-
*/
|
|
106
|
-
extractStackFromEvents(span) {
|
|
107
|
-
if (!span.events) return void 0;
|
|
108
|
-
const exceptionEvent = span.events.find((e) => e.name === "exception");
|
|
109
|
-
if (exceptionEvent?.attributes) {
|
|
110
|
-
return exceptionEvent.attributes["exception.stacktrace"] || exceptionEvent.attributes["exception.stack"];
|
|
111
|
-
}
|
|
112
|
-
return void 0;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Extract relevant attributes for error context
|
|
116
|
-
*/
|
|
117
|
-
extractRelevantAttributes(attributes) {
|
|
118
|
-
const relevant = {};
|
|
119
|
-
const keepKeys = [
|
|
120
|
-
"http.method",
|
|
121
|
-
"http.url",
|
|
122
|
-
"http.route",
|
|
123
|
-
"http.status_code",
|
|
124
|
-
"db.system",
|
|
125
|
-
"db.operation",
|
|
126
|
-
"rpc.method",
|
|
127
|
-
"rpc.service",
|
|
128
|
-
"code.function",
|
|
129
|
-
"code.filepath",
|
|
130
|
-
"user.id",
|
|
131
|
-
"operation.name"
|
|
132
|
-
];
|
|
133
|
-
for (const key of keepKeys) {
|
|
134
|
-
if (key in attributes) {
|
|
135
|
-
relevant[key] = attributes[key];
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return relevant;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Generate a fingerprint for error grouping
|
|
142
|
-
*
|
|
143
|
-
* Uses error type + first N stack frames (normalized)
|
|
144
|
-
*/
|
|
145
|
-
generateFingerprint(occurrence) {
|
|
146
|
-
const parts = [occurrence.error.type];
|
|
147
|
-
if (occurrence.error.stackTrace) {
|
|
148
|
-
const frames = this.extractStackFrames(
|
|
149
|
-
occurrence.error.stackTrace,
|
|
150
|
-
this.options.stackFramesForFingerprint
|
|
151
|
-
);
|
|
152
|
-
parts.push(...frames);
|
|
153
|
-
} else {
|
|
154
|
-
parts.push(this.normalizeMessage(occurrence.error.message));
|
|
155
|
-
}
|
|
156
|
-
return this.simpleHash(parts.join("|"));
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Extract and normalize stack frames from a stack trace
|
|
160
|
-
*/
|
|
161
|
-
extractStackFrames(stackTrace, count) {
|
|
162
|
-
const lines = stackTrace.split("\n");
|
|
163
|
-
const frames = [];
|
|
164
|
-
for (const line of lines) {
|
|
165
|
-
if (frames.length >= count) break;
|
|
166
|
-
const trimmed = line.trim();
|
|
167
|
-
const nodeMatch = trimmed.match(/^at\s+(.+?)\s+\((.+?):(\d+):\d+\)$/);
|
|
168
|
-
if (nodeMatch) {
|
|
169
|
-
frames.push(`${nodeMatch[1]}@${this.normalizeFilePath(nodeMatch[2])}`);
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
const anonMatch = trimmed.match(/^at\s+(.+?):(\d+):\d+$/);
|
|
173
|
-
if (anonMatch) {
|
|
174
|
-
frames.push(`anonymous@${this.normalizeFilePath(anonMatch[1])}`);
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
const browserMatch = trimmed.match(/^(.+?)@(.+?):(\d+):\d+$/);
|
|
178
|
-
if (browserMatch) {
|
|
179
|
-
frames.push(
|
|
180
|
-
`${browserMatch[1]}@${this.normalizeFilePath(browserMatch[2])}`
|
|
181
|
-
);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return frames;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Normalize file path by removing absolute path prefixes and node_modules paths
|
|
189
|
-
*/
|
|
190
|
-
normalizeFilePath(filePath) {
|
|
191
|
-
const nodeModulesMatch = filePath.match(
|
|
192
|
-
/node_modules\/(@[^/]+\/[^/]+|[^/]+)/
|
|
193
|
-
);
|
|
194
|
-
if (nodeModulesMatch) {
|
|
195
|
-
return `[npm]/${nodeModulesMatch[1]}`;
|
|
196
|
-
}
|
|
197
|
-
return filePath.replace(/^.*?\/src\//, "src/").replace(/^.*?\/dist\//, "dist/").replace(/^.*?\/lib\//, "lib/").replace(/^file:\/\//, "");
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Normalize error message by removing dynamic parts
|
|
201
|
-
*/
|
|
202
|
-
normalizeMessage(message) {
|
|
203
|
-
return message.replaceAll(
|
|
204
|
-
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,
|
|
205
|
-
"[UUID]"
|
|
206
|
-
).replaceAll(/\b[0-9a-f]{16,}\b/gi, "[ID]").replaceAll(/\b\d+\b/g, "[N]").replaceAll(/"[^"]*"/g, '"[STR]"').replaceAll(/'[^']*'/g, "'[STR]'").slice(0, 200);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Normalize stack trace for display
|
|
210
|
-
*/
|
|
211
|
-
normalizeStackTrace(stackTrace) {
|
|
212
|
-
if (!stackTrace) return void 0;
|
|
213
|
-
const lines = stackTrace.split("\n").slice(0, 10);
|
|
214
|
-
return lines.join("\n");
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Simple hash function for fingerprinting
|
|
218
|
-
*/
|
|
219
|
-
simpleHash(str) {
|
|
220
|
-
let hash = 0;
|
|
221
|
-
for (let i = 0; i < str.length; i++) {
|
|
222
|
-
const char = str.charCodeAt(i);
|
|
223
|
-
hash = (hash << 5) - hash + char;
|
|
224
|
-
hash = hash & hash;
|
|
225
|
-
}
|
|
226
|
-
return Math.abs(hash).toString(16).padStart(8, "0");
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Evict the oldest error group
|
|
230
|
-
*/
|
|
231
|
-
evictOldestGroup() {
|
|
232
|
-
let oldest = null;
|
|
233
|
-
for (const [fingerprint, group] of this.errorGroups) {
|
|
234
|
-
if (!oldest || group.lastSeen < oldest.lastSeen) {
|
|
235
|
-
oldest = { fingerprint, lastSeen: group.lastSeen };
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (oldest) {
|
|
239
|
-
this.errorGroups.delete(oldest.fingerprint);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Get all error groups, sorted by most recent
|
|
244
|
-
*/
|
|
245
|
-
getErrorGroups() {
|
|
246
|
-
return [...this.errorGroups.values()].sort(
|
|
247
|
-
(a, b) => b.lastSeen - a.lastSeen
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Get error groups sorted by count (most frequent first)
|
|
252
|
-
*/
|
|
253
|
-
getErrorGroupsByFrequency() {
|
|
254
|
-
return [...this.errorGroups.values()].sort(
|
|
255
|
-
(a, b) => b.count - a.count
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Get a specific error group by fingerprint
|
|
260
|
-
*/
|
|
261
|
-
getErrorGroup(fingerprint) {
|
|
262
|
-
return this.errorGroups.get(fingerprint);
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Get error groups for a specific service
|
|
266
|
-
*/
|
|
267
|
-
getErrorGroupsByService(service) {
|
|
268
|
-
return this.getErrorGroups().filter((g) => g.service === service);
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Get total error count across all groups
|
|
272
|
-
*/
|
|
273
|
-
getTotalErrorCount() {
|
|
274
|
-
let total = 0;
|
|
275
|
-
for (const group of this.errorGroups.values()) {
|
|
276
|
-
total += group.count;
|
|
277
|
-
}
|
|
278
|
-
return total;
|
|
279
|
-
}
|
|
280
|
-
/**
|
|
281
|
-
* Get error statistics
|
|
282
|
-
*/
|
|
283
|
-
getStats() {
|
|
284
|
-
const now = Date.now();
|
|
285
|
-
const oneHourAgo = now - 60 * 60 * 1e3;
|
|
286
|
-
let recentErrors = 0;
|
|
287
|
-
const typeCount = /* @__PURE__ */ new Map();
|
|
288
|
-
for (const group of this.errorGroups.values()) {
|
|
289
|
-
if (group.lastSeen > oneHourAgo) {
|
|
290
|
-
recentErrors += group.count;
|
|
291
|
-
}
|
|
292
|
-
typeCount.set(group.type, (typeCount.get(group.type) || 0) + group.count);
|
|
293
|
-
}
|
|
294
|
-
const topErrorTypes = [...typeCount.entries()].map(([type, count]) => ({ type, count })).sort((a, b) => b.count - a.count).slice(0, 5);
|
|
295
|
-
return {
|
|
296
|
-
totalGroups: this.errorGroups.size,
|
|
297
|
-
totalErrors: this.getTotalErrorCount(),
|
|
298
|
-
recentErrors,
|
|
299
|
-
topErrorTypes
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Clear all error groups
|
|
304
|
-
*/
|
|
305
|
-
clear() {
|
|
306
|
-
this.errorGroups.clear();
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Clear old error groups (not seen in given time window)
|
|
310
|
-
*/
|
|
311
|
-
clearOlderThan(maxAgeMs) {
|
|
312
|
-
const cutoff = Date.now() - maxAgeMs;
|
|
313
|
-
let cleared = 0;
|
|
314
|
-
for (const [fingerprint, group] of this.errorGroups) {
|
|
315
|
-
if (group.lastSeen < cutoff) {
|
|
316
|
-
this.errorGroups.delete(fingerprint);
|
|
317
|
-
cleared++;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
return cleared;
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
// src/server/telemetry-limits.ts
|
|
325
|
-
var defaultLimit = 100;
|
|
326
|
-
function parseLimit(value) {
|
|
327
|
-
if (!value) return void 0;
|
|
328
|
-
const parsed = Number.parseInt(value, 10);
|
|
329
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : void 0;
|
|
330
|
-
}
|
|
331
|
-
function resolveTelemetryLimits(args = {}) {
|
|
332
|
-
const env = args.env ?? process.env;
|
|
333
|
-
const fallback = args.maxHistory ?? defaultLimit;
|
|
334
|
-
return {
|
|
335
|
-
maxTraceCount: args.maxTraceCount ?? parseLimit(env.AUTOTEL_MAX_TRACE_COUNT) ?? fallback,
|
|
336
|
-
maxLogCount: args.maxLogCount ?? parseLimit(env.AUTOTEL_MAX_LOG_COUNT) ?? fallback,
|
|
337
|
-
maxMetricCount: args.maxMetricCount ?? parseLimit(env.AUTOTEL_MAX_METRIC_COUNT) ?? fallback
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
function appendWithLimit(items, item, limit) {
|
|
341
|
-
if (limit <= 0) return [];
|
|
342
|
-
const next = [...items, item];
|
|
343
|
-
return next.length > limit ? next.slice(next.length - limit) : next;
|
|
344
|
-
}
|
|
345
|
-
function appendManyWithLimit(items, incoming, limit) {
|
|
346
|
-
if (limit <= 0 || incoming.length === 0) return limit <= 0 ? [] : items;
|
|
347
|
-
const next = [...items, ...incoming];
|
|
348
|
-
return next.length > limit ? next.slice(next.length - limit) : next;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// src/server/origin-guard.ts
|
|
352
|
-
var LOOPBACK_IPV6 = /* @__PURE__ */ new Set(["::1", "0:0:0:0:0:0:0:1"]);
|
|
353
|
-
function isLoopbackHostname(hostname) {
|
|
354
|
-
const h = hostname.toLowerCase().replace(/^\[|\]$/g, "");
|
|
355
|
-
return h === "localhost" || /^127\./.test(h) || LOOPBACK_IPV6.has(h);
|
|
356
|
-
}
|
|
357
|
-
function hostnameFromHostHeader(host) {
|
|
358
|
-
const h = host.trim();
|
|
359
|
-
if (h.startsWith("[")) {
|
|
360
|
-
const end = h.indexOf("]");
|
|
361
|
-
return end > 0 ? h.slice(1, end) : h;
|
|
362
|
-
}
|
|
363
|
-
const colon = h.indexOf(":");
|
|
364
|
-
return colon === -1 ? h : h.slice(0, colon);
|
|
365
|
-
}
|
|
366
|
-
function hostHeaderIsLoopback(host) {
|
|
367
|
-
return isLoopbackHostname(hostnameFromHostHeader(host));
|
|
368
|
-
}
|
|
369
|
-
function originIsLoopback(origin) {
|
|
370
|
-
try {
|
|
371
|
-
return isLoopbackHostname(new URL(origin).hostname);
|
|
372
|
-
} catch {
|
|
373
|
-
return false;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
function allowSensitiveRequest(headers, loopbackOnly) {
|
|
377
|
-
const { origin, host } = headers;
|
|
378
|
-
if (origin && origin.length > 0 && !originIsLoopback(origin)) return false;
|
|
379
|
-
if (loopbackOnly && host && host.length > 0 && !hostHeaderIsLoopback(host)) {
|
|
380
|
-
return false;
|
|
381
|
-
}
|
|
382
|
-
return true;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// src/server/server.ts
|
|
386
|
-
var DevtoolsServer = class {
|
|
387
|
-
wss;
|
|
388
|
-
clients = /* @__PURE__ */ new Set();
|
|
389
|
-
httpServer;
|
|
390
|
-
traces = [];
|
|
391
|
-
logs = [];
|
|
392
|
-
metrics = [];
|
|
393
|
-
errorAggregator = new ErrorAggregator();
|
|
394
|
-
limits;
|
|
395
|
-
verbose;
|
|
396
|
-
_port;
|
|
397
|
-
onData;
|
|
398
|
-
constructor(options = {}) {
|
|
399
|
-
this.limits = resolveTelemetryLimits(options);
|
|
400
|
-
this.verbose = options.verbose ?? false;
|
|
401
|
-
this._port = options.port ?? 4318;
|
|
402
|
-
this.onData = options.onData;
|
|
403
|
-
this.httpServer = options.server ?? createServer();
|
|
404
|
-
const loopbackOnly = options.host == null || hostHeaderIsLoopback(options.host);
|
|
405
|
-
this.wss = new WebSocketServer({
|
|
406
|
-
server: this.httpServer,
|
|
407
|
-
path: options.path ?? "/ws",
|
|
408
|
-
verifyClient: ({ origin, req }) => allowSensitiveRequest({ origin, host: req.headers.host }, loopbackOnly)
|
|
409
|
-
});
|
|
410
|
-
this.wss.on("error", (err) => {
|
|
411
|
-
if (this.httpServer.listening) throw err;
|
|
412
|
-
});
|
|
413
|
-
this.wss.on("connection", (ws) => {
|
|
414
|
-
this.clients.add(ws);
|
|
415
|
-
this.log(`Client connected (${this.clients.size} total)`);
|
|
416
|
-
const data = this.getCurrentData();
|
|
417
|
-
if (data.traces.length > 0 || data.logs.length > 0 || data.errors.length > 0) {
|
|
418
|
-
ws.send(JSON.stringify(data));
|
|
419
|
-
}
|
|
420
|
-
ws.on("close", () => {
|
|
421
|
-
this.clients.delete(ws);
|
|
422
|
-
this.log(`Client disconnected (${this.clients.size} total)`);
|
|
423
|
-
});
|
|
424
|
-
});
|
|
425
|
-
if (!options.server) {
|
|
426
|
-
this.httpServer.listen(this._port, () => {
|
|
427
|
-
const addr = this.httpServer.address();
|
|
428
|
-
if (addr && typeof addr === "object") this._port = addr.port;
|
|
429
|
-
this.log(`WebSocket server listening on port ${this._port}`);
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
get port() {
|
|
434
|
-
const addr = this.httpServer.address();
|
|
435
|
-
if (addr && typeof addr === "object") return addr.port;
|
|
436
|
-
return this._port;
|
|
437
|
-
}
|
|
438
|
-
get clientCount() {
|
|
439
|
-
return this.clients.size;
|
|
440
|
-
}
|
|
441
|
-
addTrace(trace) {
|
|
442
|
-
const existing = this.traces.find((t) => t.traceId === trace.traceId);
|
|
443
|
-
const merged = existing ?? trace;
|
|
444
|
-
if (existing) {
|
|
445
|
-
const existingSpanIds = new Set(existing.spans.map((s) => s.spanId));
|
|
446
|
-
for (const span of trace.spans) {
|
|
447
|
-
if (!existingSpanIds.has(span.spanId)) {
|
|
448
|
-
existing.spans.push(span);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
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) {
|
|
460
|
-
existing.service = rootService;
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
} else {
|
|
464
|
-
this.traces = appendWithLimit(
|
|
465
|
-
this.traces,
|
|
466
|
-
trace,
|
|
467
|
-
this.limits.maxTraceCount
|
|
468
|
-
);
|
|
469
|
-
}
|
|
470
|
-
this.errorAggregator.addErrorsFromTrace(trace);
|
|
471
|
-
this.broadcast({ traces: [merged], metrics: [], logs: [], errors: this.errorAggregator.getErrorGroups() });
|
|
472
|
-
}
|
|
473
|
-
addTraces(traces) {
|
|
474
|
-
for (const trace of traces) this.addTrace(trace);
|
|
475
|
-
}
|
|
476
|
-
// `errors` is full-state on every broadcast (the client replaces, not appends),
|
|
477
|
-
// so non-trace broadcasts must echo the current error groups rather than `[]` —
|
|
478
|
-
// otherwise a log/metric arriving after an error would wipe it from the UI.
|
|
479
|
-
addLog(log) {
|
|
480
|
-
this.logs = appendWithLimit(this.logs, log, this.limits.maxLogCount);
|
|
481
|
-
this.broadcast({ traces: [], metrics: [], logs: [log], errors: this.errorAggregator.getErrorGroups() });
|
|
482
|
-
}
|
|
483
|
-
addLogs(logs) {
|
|
484
|
-
this.logs = appendManyWithLimit(this.logs, logs, this.limits.maxLogCount);
|
|
485
|
-
this.broadcast({ traces: [], metrics: [], logs, errors: this.errorAggregator.getErrorGroups() });
|
|
486
|
-
}
|
|
487
|
-
addMetric(metric) {
|
|
488
|
-
this.metrics = appendWithLimit(
|
|
489
|
-
this.metrics,
|
|
490
|
-
metric,
|
|
491
|
-
this.limits.maxMetricCount
|
|
492
|
-
);
|
|
493
|
-
this.broadcast({ traces: [], metrics: [metric], logs: [], errors: this.errorAggregator.getErrorGroups() });
|
|
494
|
-
}
|
|
495
|
-
getCurrentData() {
|
|
496
|
-
return {
|
|
497
|
-
traces: this.traces,
|
|
498
|
-
metrics: this.metrics,
|
|
499
|
-
logs: this.logs,
|
|
500
|
-
errors: this.errorAggregator.getErrorGroups()
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
clearData() {
|
|
504
|
-
this.traces = [];
|
|
505
|
-
this.logs = [];
|
|
506
|
-
this.metrics = [];
|
|
507
|
-
this.errorAggregator.clear();
|
|
508
|
-
}
|
|
509
|
-
broadcast(data) {
|
|
510
|
-
const msg = JSON.stringify(data);
|
|
511
|
-
for (const client of this.clients) {
|
|
512
|
-
if (client.readyState === WebSocket.OPEN) {
|
|
513
|
-
client.send(msg);
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
if (this.onData) {
|
|
517
|
-
try {
|
|
518
|
-
this.onData(data);
|
|
519
|
-
} catch {
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
log(message) {
|
|
524
|
-
if (this.verbose) console.log(`[autotel-devtools] ${message}`);
|
|
525
|
-
}
|
|
526
|
-
async close() {
|
|
527
|
-
for (const client of this.clients) client.close();
|
|
528
|
-
this.clients.clear();
|
|
529
|
-
this.wss.close();
|
|
530
|
-
await new Promise((resolve2) => this.httpServer.close(() => resolve2()));
|
|
531
|
-
}
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
// src/server/resource-utils.ts
|
|
535
|
-
function getResourceName(resource, fallback = "unknown") {
|
|
536
|
-
if (!resource) return fallback;
|
|
537
|
-
const candidates = [
|
|
538
|
-
resource["service.name"],
|
|
539
|
-
resource["service.namespace"],
|
|
540
|
-
resource["deployment.environment.name"],
|
|
541
|
-
resource["host.name"],
|
|
542
|
-
resource["container.name"],
|
|
543
|
-
resource["process.executable.name"]
|
|
544
|
-
];
|
|
545
|
-
for (const candidate of candidates) {
|
|
546
|
-
if (typeof candidate === "string" && candidate.trim().length > 0) {
|
|
547
|
-
return candidate;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
return fallback;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
// src/server/otlp.ts
|
|
554
|
-
function resolveOtlpValue(v) {
|
|
555
|
-
if (!v) return void 0;
|
|
556
|
-
if (v.stringValue !== void 0) return v.stringValue;
|
|
557
|
-
if (v.boolValue !== void 0) return v.boolValue;
|
|
558
|
-
if (v.intValue !== void 0) return typeof v.intValue === "string" ? Number(v.intValue) : v.intValue;
|
|
559
|
-
if (v.doubleValue !== void 0) return v.doubleValue;
|
|
560
|
-
if (v.bytesValue !== void 0) return v.bytesValue;
|
|
561
|
-
if (v.arrayValue?.values) return v.arrayValue.values.map(resolveOtlpValue);
|
|
562
|
-
if (v.kvlistValue?.values) return flattenAttributes(v.kvlistValue.values);
|
|
563
|
-
return void 0;
|
|
564
|
-
}
|
|
565
|
-
function flattenAttributes(attrs) {
|
|
566
|
-
const out = {};
|
|
567
|
-
if (!attrs) return out;
|
|
568
|
-
for (const { key, value } of attrs) {
|
|
569
|
-
out[key] = resolveOtlpValue(value);
|
|
570
|
-
}
|
|
571
|
-
return out;
|
|
572
|
-
}
|
|
573
|
-
function nanoToMs(nano) {
|
|
574
|
-
if (!nano) return 0;
|
|
575
|
-
const ns = BigInt(nano);
|
|
576
|
-
const ms = ns / 1000000n;
|
|
577
|
-
const remNs = ns % 1000000n;
|
|
578
|
-
return Number(ms) + Number(remNs) / 1e6;
|
|
579
|
-
}
|
|
580
|
-
var SPAN_KIND_MAP = {
|
|
581
|
-
0: "INTERNAL",
|
|
582
|
-
1: "INTERNAL",
|
|
583
|
-
2: "SERVER",
|
|
584
|
-
3: "CLIENT",
|
|
585
|
-
4: "PRODUCER",
|
|
586
|
-
5: "CONSUMER",
|
|
587
|
-
SPAN_KIND_INTERNAL: "INTERNAL",
|
|
588
|
-
SPAN_KIND_SERVER: "SERVER",
|
|
589
|
-
SPAN_KIND_CLIENT: "CLIENT",
|
|
590
|
-
SPAN_KIND_PRODUCER: "PRODUCER",
|
|
591
|
-
SPAN_KIND_CONSUMER: "CONSUMER"
|
|
592
|
-
};
|
|
593
|
-
function normalizeHexId(id) {
|
|
594
|
-
if (!id) return "";
|
|
595
|
-
const isBase64Like = /^[A-Za-z0-9+/=]+$/.test(id) && !/^[0-9a-f]+$/i.test(id);
|
|
596
|
-
const isLikelyBase64Id = isBase64Like && (id.length === 12 || id.length === 24 || id.length === 28 || id.length === 44 || id.length === 48);
|
|
597
|
-
if (isLikelyBase64Id) {
|
|
598
|
-
try {
|
|
599
|
-
const bytes = Buffer.from(id, "base64");
|
|
600
|
-
return bytes.toString("hex");
|
|
601
|
-
} catch {
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
return id;
|
|
605
|
-
}
|
|
606
|
-
function parseOtlpTraces(payload) {
|
|
607
|
-
if (!payload || typeof payload !== "object") return [];
|
|
608
|
-
const { resourceSpans } = payload;
|
|
609
|
-
if (!Array.isArray(resourceSpans) || resourceSpans.length === 0) return [];
|
|
610
|
-
const traceMap = /* @__PURE__ */ new Map();
|
|
611
|
-
for (const rs of resourceSpans) {
|
|
612
|
-
const resourceAttrs = flattenAttributes(rs.resource?.attributes);
|
|
613
|
-
const service = String(resourceAttrs["service.name"] || "unknown");
|
|
614
|
-
const scopeSpans = rs.scopeSpans || [];
|
|
615
|
-
for (const ss of scopeSpans) {
|
|
616
|
-
const scope = ss.scope?.name ? { name: ss.scope.name, version: ss.scope.version || void 0 } : void 0;
|
|
617
|
-
for (const span of ss.spans || []) {
|
|
618
|
-
const traceId = normalizeHexId(span.traceId);
|
|
619
|
-
if (!traceId) continue;
|
|
620
|
-
const startMs = nanoToMs(span.startTimeUnixNano);
|
|
621
|
-
const endMs = nanoToMs(span.endTimeUnixNano);
|
|
622
|
-
const statusCode = span.status?.code;
|
|
623
|
-
let status = "UNSET";
|
|
624
|
-
if (statusCode === 1 || statusCode === "STATUS_CODE_OK") status = "OK";
|
|
625
|
-
if (statusCode === 2 || statusCode === "STATUS_CODE_ERROR") status = "ERROR";
|
|
626
|
-
const spanData = {
|
|
627
|
-
traceId,
|
|
628
|
-
spanId: normalizeHexId(span.spanId),
|
|
629
|
-
parentSpanId: normalizeHexId(span.parentSpanId) || void 0,
|
|
630
|
-
name: span.name || "unknown",
|
|
631
|
-
kind: SPAN_KIND_MAP[span.kind ?? 0] || "INTERNAL",
|
|
632
|
-
startTime: startMs,
|
|
633
|
-
endTime: endMs,
|
|
634
|
-
duration: endMs - startMs,
|
|
635
|
-
attributes: { ...resourceAttrs, ...flattenAttributes(span.attributes) },
|
|
636
|
-
status: { code: status, message: span.status?.message },
|
|
637
|
-
events: (span.events || []).map((e) => ({
|
|
638
|
-
name: e.name || "",
|
|
639
|
-
timestamp: nanoToMs(e.timeUnixNano),
|
|
640
|
-
attributes: flattenAttributes(e.attributes)
|
|
641
|
-
})),
|
|
642
|
-
links: (span.links || []).map((l) => ({
|
|
643
|
-
traceId: normalizeHexId(l.traceId),
|
|
644
|
-
spanId: normalizeHexId(l.spanId),
|
|
645
|
-
attributes: flattenAttributes(l.attributes)
|
|
646
|
-
})),
|
|
647
|
-
scope
|
|
648
|
-
};
|
|
649
|
-
const existing = traceMap.get(traceId);
|
|
650
|
-
if (existing) {
|
|
651
|
-
existing.spans.push(spanData);
|
|
652
|
-
} else {
|
|
653
|
-
traceMap.set(traceId, { spans: [spanData], service });
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
const traces = [];
|
|
659
|
-
for (const [traceId, { spans, service }] of traceMap) {
|
|
660
|
-
const sorted = spans.sort((a, b) => a.startTime - b.startTime);
|
|
661
|
-
const rootSpan = sorted.find((s) => !s.parentSpanId) || sorted[0];
|
|
662
|
-
const startTime = Math.min(...sorted.map((s) => s.startTime));
|
|
663
|
-
const endTime = Math.max(...sorted.map((s) => s.endTime));
|
|
664
|
-
const hasError = sorted.some((s) => s.status.code === "ERROR");
|
|
665
|
-
traces.push({
|
|
666
|
-
traceId,
|
|
667
|
-
correlationId: traceId.slice(0, 16),
|
|
668
|
-
rootSpan,
|
|
669
|
-
spans: sorted,
|
|
670
|
-
startTime,
|
|
671
|
-
endTime,
|
|
672
|
-
duration: endTime - startTime,
|
|
673
|
-
status: hasError ? "ERROR" : "OK",
|
|
674
|
-
service
|
|
675
|
-
});
|
|
676
|
-
}
|
|
677
|
-
return traces;
|
|
678
|
-
}
|
|
679
|
-
function parseOtlpLogs(payload) {
|
|
680
|
-
if (!payload || typeof payload !== "object") return [];
|
|
681
|
-
const { resourceLogs } = payload;
|
|
682
|
-
if (!Array.isArray(resourceLogs)) return [];
|
|
683
|
-
const logs = [];
|
|
684
|
-
for (const rl of resourceLogs) {
|
|
685
|
-
const resourceAttrs = flattenAttributes(rl.resource?.attributes);
|
|
686
|
-
for (const sl of rl.scopeLogs || []) {
|
|
687
|
-
for (const rec of sl.logRecords || []) {
|
|
688
|
-
const timestamp = nanoToMs(rec.timeUnixNano || rec.observedTimeUnixNano);
|
|
689
|
-
const traceId = normalizeHexId(rec.traceId) || void 0;
|
|
690
|
-
const spanId = normalizeHexId(rec.spanId) || void 0;
|
|
691
|
-
const body = rec.body ? resolveOtlpValue(rec.body) : "";
|
|
692
|
-
logs.push({
|
|
693
|
-
id: `${traceId || "no-trace"}:${spanId || "no-span"}:${timestamp}:${rec.severityNumber || 0}`,
|
|
694
|
-
traceId,
|
|
695
|
-
spanId,
|
|
696
|
-
resourceName: getResourceName(resourceAttrs),
|
|
697
|
-
severityText: rec.severityText,
|
|
698
|
-
severityNumber: rec.severityNumber,
|
|
699
|
-
body: typeof body === "string" ? body : body,
|
|
700
|
-
timestamp,
|
|
701
|
-
attributes: flattenAttributes(rec.attributes),
|
|
702
|
-
resource: resourceAttrs
|
|
703
|
-
});
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
return logs;
|
|
708
|
-
}
|
|
709
|
-
function countOtlpMetrics(payload) {
|
|
710
|
-
if (!payload || typeof payload !== "object") return 0;
|
|
711
|
-
const { resourceMetrics } = payload;
|
|
712
|
-
if (!Array.isArray(resourceMetrics)) return 0;
|
|
713
|
-
let count = 0;
|
|
714
|
-
for (const rm of resourceMetrics) {
|
|
715
|
-
for (const sm of rm.scopeMetrics || []) {
|
|
716
|
-
count += (sm.metrics || []).length;
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
return count;
|
|
720
|
-
}
|
|
721
|
-
async function readJsonBody(req) {
|
|
722
|
-
return new Promise((resolve2, reject) => {
|
|
723
|
-
const chunks = [];
|
|
724
|
-
req.on("data", (chunk) => chunks.push(chunk));
|
|
725
|
-
req.on("end", () => {
|
|
726
|
-
try {
|
|
727
|
-
resolve2(JSON.parse(Buffer.concat(chunks).toString()));
|
|
728
|
-
} catch {
|
|
729
|
-
reject(new Error("Invalid JSON"));
|
|
730
|
-
}
|
|
731
|
-
});
|
|
732
|
-
req.on("error", reject);
|
|
733
|
-
});
|
|
734
|
-
}
|
|
735
|
-
async function readRawBody(req) {
|
|
736
|
-
return new Promise((resolve2, reject) => {
|
|
737
|
-
const chunks = [];
|
|
738
|
-
req.on("data", (chunk) => chunks.push(chunk));
|
|
739
|
-
req.on("end", () => resolve2(Buffer.concat(chunks)));
|
|
740
|
-
req.on("error", reject);
|
|
741
|
-
});
|
|
742
|
-
}
|
|
743
|
-
function isProtobufContentType(contentType) {
|
|
744
|
-
if (!contentType) return false;
|
|
745
|
-
const value = contentType.toLowerCase();
|
|
746
|
-
return value.includes("application/x-protobuf") || value.includes("application/protobuf");
|
|
747
|
-
}
|
|
748
|
-
function sendJson(res, status, data) {
|
|
749
|
-
const body = JSON.stringify(data);
|
|
750
|
-
res.writeHead(status, { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(body) });
|
|
751
|
-
res.end(body);
|
|
752
|
-
}
|
|
753
|
-
var COMMON_PROTO = `
|
|
754
|
-
syntax = "proto3";
|
|
755
|
-
package opentelemetry.proto.common.v1;
|
|
756
|
-
|
|
757
|
-
message AnyValue {
|
|
758
|
-
oneof value {
|
|
759
|
-
string string_value = 1;
|
|
760
|
-
bool bool_value = 2;
|
|
761
|
-
int64 int_value = 3;
|
|
762
|
-
double double_value = 4;
|
|
763
|
-
ArrayValue array_value = 5;
|
|
764
|
-
KeyValueList kvlist_value = 6;
|
|
765
|
-
bytes bytes_value = 7;
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
message ArrayValue { repeated AnyValue values = 1; }
|
|
769
|
-
message KeyValueList { repeated KeyValue values = 1; }
|
|
770
|
-
message KeyValue {
|
|
771
|
-
string key = 1;
|
|
772
|
-
AnyValue value = 2;
|
|
773
|
-
}
|
|
774
|
-
message InstrumentationScope {
|
|
775
|
-
string name = 1;
|
|
776
|
-
string version = 2;
|
|
777
|
-
repeated KeyValue attributes = 3;
|
|
778
|
-
uint32 dropped_attributes_count = 4;
|
|
779
|
-
}
|
|
780
|
-
`;
|
|
781
|
-
var RESOURCE_PROTO = `
|
|
782
|
-
syntax = "proto3";
|
|
783
|
-
package opentelemetry.proto.resource.v1;
|
|
784
|
-
|
|
785
|
-
message Resource {
|
|
786
|
-
repeated opentelemetry.proto.common.v1.KeyValue attributes = 1;
|
|
787
|
-
uint32 dropped_attributes_count = 2;
|
|
788
|
-
}
|
|
789
|
-
`;
|
|
790
|
-
var TRACE_PROTO = `
|
|
791
|
-
syntax = "proto3";
|
|
792
|
-
package opentelemetry.proto.trace.v1;
|
|
793
|
-
|
|
794
|
-
message ResourceSpans {
|
|
795
|
-
opentelemetry.proto.resource.v1.Resource resource = 1;
|
|
796
|
-
repeated ScopeSpans scope_spans = 2;
|
|
797
|
-
string schema_url = 3;
|
|
798
|
-
}
|
|
799
|
-
message ScopeSpans {
|
|
800
|
-
opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
|
|
801
|
-
repeated Span spans = 2;
|
|
802
|
-
string schema_url = 3;
|
|
803
|
-
}
|
|
804
|
-
message Span {
|
|
805
|
-
bytes trace_id = 1;
|
|
806
|
-
bytes span_id = 2;
|
|
807
|
-
string trace_state = 3;
|
|
808
|
-
bytes parent_span_id = 4;
|
|
809
|
-
fixed32 flags = 16;
|
|
810
|
-
string name = 5;
|
|
811
|
-
SpanKind kind = 6;
|
|
812
|
-
fixed64 start_time_unix_nano = 7;
|
|
813
|
-
fixed64 end_time_unix_nano = 8;
|
|
814
|
-
repeated opentelemetry.proto.common.v1.KeyValue attributes = 9;
|
|
815
|
-
uint32 dropped_attributes_count = 10;
|
|
816
|
-
repeated Event events = 11;
|
|
817
|
-
uint32 dropped_events_count = 12;
|
|
818
|
-
repeated Link links = 13;
|
|
819
|
-
uint32 dropped_links_count = 14;
|
|
820
|
-
Status status = 15;
|
|
821
|
-
|
|
822
|
-
enum SpanKind {
|
|
823
|
-
SPAN_KIND_UNSPECIFIED = 0;
|
|
824
|
-
SPAN_KIND_INTERNAL = 1;
|
|
825
|
-
SPAN_KIND_SERVER = 2;
|
|
826
|
-
SPAN_KIND_CLIENT = 3;
|
|
827
|
-
SPAN_KIND_PRODUCER = 4;
|
|
828
|
-
SPAN_KIND_CONSUMER = 5;
|
|
829
|
-
}
|
|
830
|
-
message Event {
|
|
831
|
-
fixed64 time_unix_nano = 1;
|
|
832
|
-
string name = 2;
|
|
833
|
-
repeated opentelemetry.proto.common.v1.KeyValue attributes = 3;
|
|
834
|
-
uint32 dropped_attributes_count = 4;
|
|
835
|
-
}
|
|
836
|
-
message Link {
|
|
837
|
-
bytes trace_id = 1;
|
|
838
|
-
bytes span_id = 2;
|
|
839
|
-
string trace_state = 3;
|
|
840
|
-
repeated opentelemetry.proto.common.v1.KeyValue attributes = 4;
|
|
841
|
-
uint32 dropped_attributes_count = 5;
|
|
842
|
-
fixed32 flags = 6;
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
message Status {
|
|
846
|
-
reserved 1;
|
|
847
|
-
string message = 2;
|
|
848
|
-
StatusCode code = 3;
|
|
849
|
-
|
|
850
|
-
enum StatusCode {
|
|
851
|
-
STATUS_CODE_UNSET = 0;
|
|
852
|
-
STATUS_CODE_OK = 1;
|
|
853
|
-
STATUS_CODE_ERROR = 2;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
message ExportTraceServiceRequest {
|
|
857
|
-
repeated ResourceSpans resource_spans = 1;
|
|
858
|
-
}
|
|
859
|
-
`;
|
|
860
|
-
var LOGS_PROTO = `
|
|
861
|
-
syntax = "proto3";
|
|
862
|
-
package opentelemetry.proto.logs.v1;
|
|
863
|
-
|
|
864
|
-
enum SeverityNumber {
|
|
865
|
-
SEVERITY_NUMBER_UNSPECIFIED = 0;
|
|
866
|
-
SEVERITY_NUMBER_TRACE = 1;
|
|
867
|
-
SEVERITY_NUMBER_TRACE2 = 2;
|
|
868
|
-
SEVERITY_NUMBER_TRACE3 = 3;
|
|
869
|
-
SEVERITY_NUMBER_TRACE4 = 4;
|
|
870
|
-
SEVERITY_NUMBER_DEBUG = 5;
|
|
871
|
-
SEVERITY_NUMBER_DEBUG2 = 6;
|
|
872
|
-
SEVERITY_NUMBER_DEBUG3 = 7;
|
|
873
|
-
SEVERITY_NUMBER_DEBUG4 = 8;
|
|
874
|
-
SEVERITY_NUMBER_INFO = 9;
|
|
875
|
-
SEVERITY_NUMBER_INFO2 = 10;
|
|
876
|
-
SEVERITY_NUMBER_INFO3 = 11;
|
|
877
|
-
SEVERITY_NUMBER_INFO4 = 12;
|
|
878
|
-
SEVERITY_NUMBER_WARN = 13;
|
|
879
|
-
SEVERITY_NUMBER_WARN2 = 14;
|
|
880
|
-
SEVERITY_NUMBER_WARN3 = 15;
|
|
881
|
-
SEVERITY_NUMBER_WARN4 = 16;
|
|
882
|
-
SEVERITY_NUMBER_ERROR = 17;
|
|
883
|
-
SEVERITY_NUMBER_ERROR2 = 18;
|
|
884
|
-
SEVERITY_NUMBER_ERROR3 = 19;
|
|
885
|
-
SEVERITY_NUMBER_ERROR4 = 20;
|
|
886
|
-
SEVERITY_NUMBER_FATAL = 21;
|
|
887
|
-
SEVERITY_NUMBER_FATAL2 = 22;
|
|
888
|
-
SEVERITY_NUMBER_FATAL3 = 23;
|
|
889
|
-
SEVERITY_NUMBER_FATAL4 = 24;
|
|
890
|
-
}
|
|
891
|
-
message ResourceLogs {
|
|
892
|
-
opentelemetry.proto.resource.v1.Resource resource = 1;
|
|
893
|
-
repeated ScopeLogs scope_logs = 2;
|
|
894
|
-
string schema_url = 3;
|
|
895
|
-
}
|
|
896
|
-
message ScopeLogs {
|
|
897
|
-
opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
|
|
898
|
-
repeated LogRecord log_records = 2;
|
|
899
|
-
string schema_url = 3;
|
|
900
|
-
}
|
|
901
|
-
message LogRecord {
|
|
902
|
-
reserved 4;
|
|
903
|
-
fixed64 time_unix_nano = 1;
|
|
904
|
-
fixed64 observed_time_unix_nano = 11;
|
|
905
|
-
SeverityNumber severity_number = 2;
|
|
906
|
-
string severity_text = 3;
|
|
907
|
-
opentelemetry.proto.common.v1.AnyValue body = 5;
|
|
908
|
-
repeated opentelemetry.proto.common.v1.KeyValue attributes = 6;
|
|
909
|
-
uint32 dropped_attributes_count = 7;
|
|
910
|
-
fixed32 flags = 8;
|
|
911
|
-
bytes trace_id = 9;
|
|
912
|
-
bytes span_id = 10;
|
|
913
|
-
}
|
|
914
|
-
message ExportLogsServiceRequest {
|
|
915
|
-
repeated ResourceLogs resource_logs = 1;
|
|
916
|
-
}
|
|
917
|
-
`;
|
|
918
|
-
var METRICS_PROTO = `
|
|
919
|
-
syntax = "proto3";
|
|
920
|
-
package opentelemetry.proto.metrics.v1;
|
|
921
|
-
|
|
922
|
-
message ResourceMetrics {
|
|
923
|
-
opentelemetry.proto.resource.v1.Resource resource = 1;
|
|
924
|
-
repeated ScopeMetrics scope_metrics = 2;
|
|
925
|
-
string schema_url = 3;
|
|
926
|
-
}
|
|
927
|
-
message ScopeMetrics {
|
|
928
|
-
opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
|
|
929
|
-
repeated Metric metrics = 2;
|
|
930
|
-
string schema_url = 3;
|
|
931
|
-
}
|
|
932
|
-
message Metric {
|
|
933
|
-
string name = 1;
|
|
934
|
-
string description = 2;
|
|
935
|
-
string unit = 3;
|
|
936
|
-
}
|
|
937
|
-
message ExportMetricsServiceRequest {
|
|
938
|
-
repeated ResourceMetrics resource_metrics = 1;
|
|
939
|
-
}
|
|
940
|
-
`;
|
|
941
|
-
var TO_OBJECT_OPTIONS = {
|
|
942
|
-
longs: String,
|
|
943
|
-
bytes: String,
|
|
944
|
-
defaults: false
|
|
945
|
-
};
|
|
946
|
-
var cachedRoot = null;
|
|
947
|
-
function getRoot() {
|
|
948
|
-
if (cachedRoot) return cachedRoot;
|
|
949
|
-
const root = new protobuf.Root();
|
|
950
|
-
for (const source of [COMMON_PROTO, RESOURCE_PROTO, TRACE_PROTO, LOGS_PROTO, METRICS_PROTO]) {
|
|
951
|
-
protobuf.parse(source, root, { keepCase: false });
|
|
952
|
-
}
|
|
953
|
-
root.resolveAll();
|
|
954
|
-
cachedRoot = root;
|
|
955
|
-
return root;
|
|
956
|
-
}
|
|
957
|
-
function decodeRequest(typeName, body) {
|
|
958
|
-
const messageType = getRoot().lookupType(typeName);
|
|
959
|
-
const message = messageType.decode(body);
|
|
960
|
-
return messageType.toObject(message, TO_OBJECT_OPTIONS);
|
|
961
|
-
}
|
|
962
|
-
function decodeOtlpTraceRequest(body) {
|
|
963
|
-
return decodeRequest("opentelemetry.proto.trace.v1.ExportTraceServiceRequest", body);
|
|
964
|
-
}
|
|
965
|
-
function decodeOtlpLogsRequest(body) {
|
|
966
|
-
return decodeRequest("opentelemetry.proto.logs.v1.ExportLogsServiceRequest", body);
|
|
967
|
-
}
|
|
968
|
-
function decodeOtlpMetricsRequest(body) {
|
|
969
|
-
return decodeRequest("opentelemetry.proto.metrics.v1.ExportMetricsServiceRequest", body);
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
// src/server/identity.ts
|
|
973
|
-
var DEVTOOLS_IDENTITY = "autotel-devtools";
|
|
974
|
-
|
|
975
|
-
// src/server/http.ts
|
|
976
|
-
function sendOtlpError(res, req, e) {
|
|
977
|
-
sendJson(res, 400, {
|
|
978
|
-
error: "Invalid OTLP payload",
|
|
979
|
-
message: e instanceof Error ? e.message : String(e),
|
|
980
|
-
contentType: req.headers["content-type"] ?? null
|
|
981
|
-
});
|
|
982
|
-
}
|
|
983
|
-
var PROTOBUF_DECODERS = {
|
|
984
|
-
traces: decodeOtlpTraceRequest,
|
|
985
|
-
logs: decodeOtlpLogsRequest,
|
|
986
|
-
metrics: decodeOtlpMetricsRequest
|
|
987
|
-
};
|
|
988
|
-
async function readOtlpPayload(req, signal) {
|
|
989
|
-
if (isProtobufContentType(req.headers["content-type"])) {
|
|
990
|
-
return PROTOBUF_DECODERS[signal](await readRawBody(req));
|
|
991
|
-
}
|
|
992
|
-
return readJsonBody(req);
|
|
993
|
-
}
|
|
994
|
-
function findPackageRoot() {
|
|
995
|
-
let dir = dirname(fileURLToPath(import.meta.url));
|
|
996
|
-
for (let i = 0; i < 5; i++) {
|
|
997
|
-
if (existsSync(resolve(dir, "package.json"))) return dir;
|
|
998
|
-
dir = dirname(dir);
|
|
999
|
-
}
|
|
1000
|
-
return dir;
|
|
1001
|
-
}
|
|
1002
|
-
var 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">\u{1F6F0}\uFE0F</text></svg>';
|
|
1003
|
-
var 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>`;
|
|
1004
|
-
var cachedVersion = null;
|
|
1005
|
-
function getVersion() {
|
|
1006
|
-
if (cachedVersion !== null) return cachedVersion;
|
|
1007
|
-
let version = "unknown";
|
|
1008
|
-
try {
|
|
1009
|
-
const pkg = JSON.parse(readFileSync(resolve(findPackageRoot(), "package.json"), "utf8"));
|
|
1010
|
-
if (typeof pkg.version === "string") version = pkg.version;
|
|
1011
|
-
} catch {
|
|
1012
|
-
}
|
|
1013
|
-
cachedVersion = version;
|
|
1014
|
-
return version;
|
|
1015
|
-
}
|
|
1016
|
-
var cachedWidgetJs = null;
|
|
1017
|
-
function getWidgetJs() {
|
|
1018
|
-
if (!cachedWidgetJs) {
|
|
1019
|
-
const pkgRoot = findPackageRoot();
|
|
1020
|
-
const candidates = [
|
|
1021
|
-
resolve(pkgRoot, "dist", "widget.global.js"),
|
|
1022
|
-
resolve(pkgRoot, "widget.global.js")
|
|
1023
|
-
];
|
|
1024
|
-
for (const candidate of candidates) {
|
|
1025
|
-
try {
|
|
1026
|
-
cachedWidgetJs = readFileSync(candidate, "utf8");
|
|
1027
|
-
break;
|
|
1028
|
-
} catch {
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
if (!cachedWidgetJs) {
|
|
1032
|
-
cachedWidgetJs = "// widget bundle not found - run pnpm build first";
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
return cachedWidgetJs;
|
|
1036
|
-
}
|
|
1037
|
-
function attachDevtoolsRoutes(httpServer, devtools, options = {}) {
|
|
1038
|
-
const loopbackOnly = options.loopbackOnly ?? true;
|
|
1039
|
-
httpServer.on("request", async (req, res) => {
|
|
1040
|
-
if (req.headers.upgrade?.toLowerCase() === "websocket") return;
|
|
1041
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
1042
|
-
res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
|
|
1043
|
-
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
1044
|
-
res.setHeader("x-autotel-devtools", getVersion());
|
|
1045
|
-
res.setHeader("Access-Control-Expose-Headers", "x-autotel-devtools");
|
|
1046
|
-
if (req.method === "OPTIONS") {
|
|
1047
|
-
res.writeHead(204);
|
|
1048
|
-
res.end();
|
|
1049
|
-
return;
|
|
1050
|
-
}
|
|
1051
|
-
const url = req.url || "/";
|
|
1052
|
-
if (req.method === "GET" && url === "/") {
|
|
1053
|
-
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Content-Length": Buffer.byteLength(FULLPAGE_HTML) });
|
|
1054
|
-
res.end(FULLPAGE_HTML);
|
|
1055
|
-
return;
|
|
1056
|
-
}
|
|
1057
|
-
if (req.method === "GET" && url.startsWith("/widget.js")) {
|
|
1058
|
-
const js = getWidgetJs();
|
|
1059
|
-
res.writeHead(200, { "Content-Type": "application/javascript; charset=utf-8", "Content-Length": Buffer.byteLength(js) });
|
|
1060
|
-
res.end(js);
|
|
1061
|
-
return;
|
|
1062
|
-
}
|
|
1063
|
-
if (req.method === "GET" && (url === "/favicon.svg" || url === "/favicon.ico")) {
|
|
1064
|
-
res.writeHead(200, {
|
|
1065
|
-
"Content-Type": "image/svg+xml; charset=utf-8",
|
|
1066
|
-
"Cache-Control": "public, max-age=86400",
|
|
1067
|
-
"Content-Length": Buffer.byteLength(DEVTOOLS_FAVICON_SVG)
|
|
1068
|
-
});
|
|
1069
|
-
res.end(DEVTOOLS_FAVICON_SVG);
|
|
1070
|
-
return;
|
|
1071
|
-
}
|
|
1072
|
-
if (req.method === "GET" && url === "/healthz") {
|
|
1073
|
-
sendJson(res, 200, {
|
|
1074
|
-
ok: true,
|
|
1075
|
-
service: DEVTOOLS_IDENTITY,
|
|
1076
|
-
version: getVersion(),
|
|
1077
|
-
clients: devtools.clientCount
|
|
1078
|
-
});
|
|
1079
|
-
return;
|
|
1080
|
-
}
|
|
1081
|
-
if (req.method === "GET" && url === "/v1/traces") {
|
|
1082
|
-
if (!allowSensitiveRequest(req.headers, loopbackOnly)) {
|
|
1083
|
-
sendJson(res, 403, { error: "Forbidden" });
|
|
1084
|
-
return;
|
|
1085
|
-
}
|
|
1086
|
-
const data = devtools.getCurrentData();
|
|
1087
|
-
sendJson(res, 200, { traces: data.traces, count: data.traces.length });
|
|
1088
|
-
return;
|
|
1089
|
-
}
|
|
1090
|
-
if (req.method === "DELETE" && url === "/v1/traces") {
|
|
1091
|
-
if (!allowSensitiveRequest(req.headers, loopbackOnly)) {
|
|
1092
|
-
sendJson(res, 403, { error: "Forbidden" });
|
|
1093
|
-
return;
|
|
1094
|
-
}
|
|
1095
|
-
devtools.clearData();
|
|
1096
|
-
sendJson(res, 200, { cleared: true });
|
|
1097
|
-
return;
|
|
1098
|
-
}
|
|
1099
|
-
if (req.method === "POST" && url === "/v1/traces") {
|
|
1100
|
-
try {
|
|
1101
|
-
const payload = await readOtlpPayload(req, "traces");
|
|
1102
|
-
const traces = parseOtlpTraces(payload);
|
|
1103
|
-
devtools.addTraces(traces);
|
|
1104
|
-
sendJson(res, 200, { acceptedTraces: traces.length });
|
|
1105
|
-
} catch (e) {
|
|
1106
|
-
sendOtlpError(res, req, e);
|
|
1107
|
-
}
|
|
1108
|
-
return;
|
|
1109
|
-
}
|
|
1110
|
-
if (req.method === "POST" && url === "/v1/logs") {
|
|
1111
|
-
try {
|
|
1112
|
-
const payload = await readOtlpPayload(req, "logs");
|
|
1113
|
-
const logs = parseOtlpLogs(payload);
|
|
1114
|
-
devtools.addLogs(logs);
|
|
1115
|
-
sendJson(res, 200, { acceptedLogs: logs.length });
|
|
1116
|
-
} catch (e) {
|
|
1117
|
-
sendOtlpError(res, req, e);
|
|
1118
|
-
}
|
|
1119
|
-
return;
|
|
1120
|
-
}
|
|
1121
|
-
if (req.method === "POST" && url === "/v1/metrics") {
|
|
1122
|
-
try {
|
|
1123
|
-
const payload = await readOtlpPayload(req, "metrics");
|
|
1124
|
-
const count = countOtlpMetrics(payload);
|
|
1125
|
-
sendJson(res, 200, { acceptedMetrics: count });
|
|
1126
|
-
} catch (e) {
|
|
1127
|
-
sendOtlpError(res, req, e);
|
|
1128
|
-
}
|
|
1129
|
-
return;
|
|
1130
|
-
}
|
|
1131
|
-
sendJson(res, 404, { error: "Not found" });
|
|
1132
|
-
});
|
|
1133
|
-
}
|
|
1134
|
-
var LOOPBACK = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1"]);
|
|
1135
|
-
var DEFAULT_MAX_PORT_TRIES = 20;
|
|
1136
|
-
function formatAddress(host, port) {
|
|
1137
|
-
return host.includes(":") ? `[${host}]:${port}` : `${host}:${port}`;
|
|
1138
|
-
}
|
|
1139
|
-
function listenLoopbackDualStack(args) {
|
|
1140
|
-
const { primary, port, host, attachSecondary, maxTries } = args;
|
|
1141
|
-
const maxAttempts = Math.max(1, maxTries ?? DEFAULT_MAX_PORT_TRIES);
|
|
1142
|
-
let sibling;
|
|
1143
|
-
const ready = new Promise(
|
|
1144
|
-
(resolve2, reject) => {
|
|
1145
|
-
const addresses = [];
|
|
1146
|
-
const warnings = [];
|
|
1147
|
-
const primaryHost = host === "localhost" ? "127.0.0.1" : host;
|
|
1148
|
-
let candidate = port;
|
|
1149
|
-
let attempt = 0;
|
|
1150
|
-
const bindFailed = (atPort, msg) => reject(
|
|
1151
|
-
new Error(`could not bind ${formatAddress(primaryHost, atPort)}: ${msg}`)
|
|
1152
|
-
);
|
|
1153
|
-
const onError = (e) => {
|
|
1154
|
-
if (e.code !== "EADDRINUSE") return bindFailed(candidate, e.message);
|
|
1155
|
-
if (++attempt >= maxAttempts) {
|
|
1156
|
-
reject(
|
|
1157
|
-
new Error(
|
|
1158
|
-
`could not bind ${formatAddress(primaryHost, port)}: ${maxAttempts} consecutive ports in use`
|
|
1159
|
-
)
|
|
1160
|
-
);
|
|
1161
|
-
return;
|
|
1162
|
-
}
|
|
1163
|
-
candidate++;
|
|
1164
|
-
listen();
|
|
1165
|
-
};
|
|
1166
|
-
const onListening = () => {
|
|
1167
|
-
primary.removeListener("error", onError);
|
|
1168
|
-
if (candidate !== port) {
|
|
1169
|
-
warnings.push(`port ${port} was busy; using ${candidate} instead`);
|
|
1170
|
-
}
|
|
1171
|
-
const addr = primary.address();
|
|
1172
|
-
const resolvedPort = addr && typeof addr === "object" ? addr.port : candidate;
|
|
1173
|
-
addresses.push(formatAddress(primaryHost, resolvedPort));
|
|
1174
|
-
if (!LOOPBACK.has(host)) {
|
|
1175
|
-
resolve2({ addresses, port: resolvedPort, warnings });
|
|
1176
|
-
return;
|
|
1177
|
-
}
|
|
1178
|
-
const siblingHost = primaryHost === "::1" ? "127.0.0.1" : "::1";
|
|
1179
|
-
const s = createServer();
|
|
1180
|
-
attachSecondary(s);
|
|
1181
|
-
const onSiblingError = (se) => {
|
|
1182
|
-
s.close();
|
|
1183
|
-
warnings.push(
|
|
1184
|
-
`could not also bind ${formatAddress(siblingHost, resolvedPort)} (${se.message}); clients using the ${siblingHost === "::1" ? "IPv6" : "IPv4"} form of "localhost" may not connect.`
|
|
1185
|
-
);
|
|
1186
|
-
resolve2({ addresses, port: resolvedPort, warnings });
|
|
1187
|
-
};
|
|
1188
|
-
s.once("error", onSiblingError);
|
|
1189
|
-
s.listen(resolvedPort, siblingHost, () => {
|
|
1190
|
-
s.off("error", onSiblingError);
|
|
1191
|
-
sibling = s;
|
|
1192
|
-
addresses.push(formatAddress(siblingHost, resolvedPort));
|
|
1193
|
-
resolve2({ addresses, port: resolvedPort, warnings });
|
|
1194
|
-
});
|
|
1195
|
-
};
|
|
1196
|
-
const listen = () => {
|
|
1197
|
-
try {
|
|
1198
|
-
primary.listen(candidate, primaryHost);
|
|
1199
|
-
} catch (e) {
|
|
1200
|
-
primary.removeListener("error", onError);
|
|
1201
|
-
primary.removeListener("listening", onListening);
|
|
1202
|
-
bindFailed(candidate, e.message);
|
|
1203
|
-
}
|
|
1204
|
-
};
|
|
1205
|
-
primary.on("error", onError);
|
|
1206
|
-
primary.once("listening", onListening);
|
|
1207
|
-
listen();
|
|
1208
|
-
}
|
|
1209
|
-
);
|
|
1210
|
-
return {
|
|
1211
|
-
ready,
|
|
1212
|
-
closeSibling: () => new Promise((res) => {
|
|
1213
|
-
if (!sibling) return res();
|
|
1214
|
-
sibling.close(() => res());
|
|
1215
|
-
})
|
|
1216
|
-
};
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
// src/server/exporter.ts
|
|
1220
|
-
var DevtoolsSpanExporter = class {
|
|
1221
|
-
server;
|
|
1222
|
-
serviceName;
|
|
1223
|
-
constructor(server, serviceName = "unknown-service") {
|
|
1224
|
-
this.server = server;
|
|
1225
|
-
this.serviceName = serviceName;
|
|
1226
|
-
}
|
|
1227
|
-
/**
|
|
1228
|
-
* Export spans to the WebSocket server
|
|
1229
|
-
*/
|
|
1230
|
-
async export(spans, resultCallback) {
|
|
1231
|
-
resultCallback({ code: 0 });
|
|
1232
|
-
Promise.resolve().then(() => {
|
|
1233
|
-
try {
|
|
1234
|
-
console.log(`[Autotel Exporter] Exporting ${spans.length} span(s)`);
|
|
1235
|
-
const traceMap = /* @__PURE__ */ new Map();
|
|
1236
|
-
for (const span of spans) {
|
|
1237
|
-
const traceId = span.spanContext().traceId;
|
|
1238
|
-
if (!traceMap.has(traceId)) {
|
|
1239
|
-
traceMap.set(traceId, []);
|
|
1240
|
-
}
|
|
1241
|
-
traceMap.get(traceId).push(span);
|
|
1242
|
-
}
|
|
1243
|
-
for (const [traceId, traceSpans] of traceMap) {
|
|
1244
|
-
const trace = this.convertToTraceData(traceId, traceSpans);
|
|
1245
|
-
console.log(
|
|
1246
|
-
`[Autotel Exporter] Adding trace ${traceId.slice(0, 16)} with ${traceSpans.length} spans`
|
|
1247
|
-
);
|
|
1248
|
-
this.server.addTrace(trace);
|
|
1249
|
-
}
|
|
1250
|
-
} catch (error) {
|
|
1251
|
-
console.error("[Autotel Exporter] Export error:", error);
|
|
1252
|
-
}
|
|
1253
|
-
});
|
|
1254
|
-
}
|
|
1255
|
-
/**
|
|
1256
|
-
* Shutdown the exporter
|
|
1257
|
-
*/
|
|
1258
|
-
async shutdown() {
|
|
1259
|
-
}
|
|
1260
|
-
/**
|
|
1261
|
-
* Force flush any buffered spans
|
|
1262
|
-
*/
|
|
1263
|
-
async forceFlush() {
|
|
1264
|
-
}
|
|
1265
|
-
/**
|
|
1266
|
-
* Convert OpenTelemetry spans to TraceData
|
|
1267
|
-
*/
|
|
1268
|
-
convertToTraceData(traceId, spans) {
|
|
1269
|
-
const spanData = spans.map((span) => this.convertSpan(span));
|
|
1270
|
-
const rootSpan = spanData.find((s) => !s.parentSpanId) || spanData[0];
|
|
1271
|
-
spanData.sort((a, b) => a.startTime - b.startTime);
|
|
1272
|
-
const startTime = Math.min(...spanData.map((s) => s.startTime));
|
|
1273
|
-
const endTime = Math.max(...spanData.map((s) => s.endTime));
|
|
1274
|
-
const hasError = spanData.some((s) => s.status.code === "ERROR");
|
|
1275
|
-
const status = hasError ? "ERROR" : "OK";
|
|
1276
|
-
return {
|
|
1277
|
-
traceId,
|
|
1278
|
-
correlationId: traceId.slice(0, 16),
|
|
1279
|
-
// First 16 chars
|
|
1280
|
-
rootSpan,
|
|
1281
|
-
spans: spanData,
|
|
1282
|
-
startTime,
|
|
1283
|
-
endTime,
|
|
1284
|
-
duration: endTime - startTime,
|
|
1285
|
-
status,
|
|
1286
|
-
service: this.serviceName
|
|
1287
|
-
};
|
|
1288
|
-
}
|
|
1289
|
-
/**
|
|
1290
|
-
* Convert OpenTelemetry span to SpanData
|
|
1291
|
-
*/
|
|
1292
|
-
convertSpan(span) {
|
|
1293
|
-
const spanContext = span.spanContext();
|
|
1294
|
-
const startTime = span.startTime[0] * 1e3 + span.startTime[1] / 1e6;
|
|
1295
|
-
const endTime = span.endTime[0] * 1e3 + span.endTime[1] / 1e6;
|
|
1296
|
-
const attributes = {};
|
|
1297
|
-
for (const [key, value] of Object.entries(span.attributes)) {
|
|
1298
|
-
attributes[key] = value;
|
|
1299
|
-
}
|
|
1300
|
-
const statusCode = span.status.code;
|
|
1301
|
-
let status;
|
|
1302
|
-
switch (statusCode) {
|
|
1303
|
-
case 0: {
|
|
1304
|
-
status = "UNSET";
|
|
1305
|
-
break;
|
|
1306
|
-
}
|
|
1307
|
-
case 1: {
|
|
1308
|
-
status = "OK";
|
|
1309
|
-
break;
|
|
1310
|
-
}
|
|
1311
|
-
case 2: {
|
|
1312
|
-
status = "ERROR";
|
|
1313
|
-
break;
|
|
1314
|
-
}
|
|
1315
|
-
default: {
|
|
1316
|
-
status = "UNSET";
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
const events = span.events.map((event) => ({
|
|
1320
|
-
name: event.name,
|
|
1321
|
-
timestamp: event.time[0] * 1e3 + event.time[1] / 1e6,
|
|
1322
|
-
attributes: event.attributes ? Object.fromEntries(Object.entries(event.attributes)) : void 0
|
|
1323
|
-
}));
|
|
1324
|
-
const links = span.links.map((link) => ({
|
|
1325
|
-
traceId: link.context.traceId,
|
|
1326
|
-
spanId: link.context.spanId,
|
|
1327
|
-
attributes: link.attributes ? Object.fromEntries(Object.entries(link.attributes)) : void 0
|
|
1328
|
-
}));
|
|
1329
|
-
return {
|
|
1330
|
-
traceId: spanContext.traceId,
|
|
1331
|
-
spanId: spanContext.spanId,
|
|
1332
|
-
parentSpanId: span.parentSpanId,
|
|
1333
|
-
name: span.name,
|
|
1334
|
-
kind: this.convertSpanKind(span.kind),
|
|
1335
|
-
startTime,
|
|
1336
|
-
endTime,
|
|
1337
|
-
duration: endTime - startTime,
|
|
1338
|
-
attributes,
|
|
1339
|
-
status: {
|
|
1340
|
-
code: status,
|
|
1341
|
-
message: span.status.message
|
|
1342
|
-
},
|
|
1343
|
-
events: events.length > 0 ? events : void 0,
|
|
1344
|
-
links: links.length > 0 ? links : void 0,
|
|
1345
|
-
scope: this.convertScope(span)
|
|
1346
|
-
};
|
|
1347
|
-
}
|
|
1348
|
-
convertScope(span) {
|
|
1349
|
-
const s = span.instrumentationScope ?? span.instrumentationLibrary;
|
|
1350
|
-
return s?.name ? { name: s.name, version: s.version || void 0 } : void 0;
|
|
1351
|
-
}
|
|
1352
|
-
/**
|
|
1353
|
-
* Convert OpenTelemetry SpanKind to string
|
|
1354
|
-
*/
|
|
1355
|
-
convertSpanKind(kind) {
|
|
1356
|
-
switch (kind) {
|
|
1357
|
-
case 0: {
|
|
1358
|
-
return "INTERNAL";
|
|
1359
|
-
}
|
|
1360
|
-
case 1: {
|
|
1361
|
-
return "SERVER";
|
|
1362
|
-
}
|
|
1363
|
-
case 2: {
|
|
1364
|
-
return "CLIENT";
|
|
1365
|
-
}
|
|
1366
|
-
case 3: {
|
|
1367
|
-
return "PRODUCER";
|
|
1368
|
-
}
|
|
1369
|
-
case 4: {
|
|
1370
|
-
return "CONSUMER";
|
|
1371
|
-
}
|
|
1372
|
-
default: {
|
|
1373
|
-
return "INTERNAL";
|
|
1374
|
-
}
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
};
|
|
1378
|
-
var defaultTimeout = 5e3;
|
|
1379
|
-
function hrTimeToMs(hrTime) {
|
|
1380
|
-
return hrTime[0] * 1e3 + hrTime[1] / 1e6;
|
|
1381
|
-
}
|
|
1382
|
-
function bodyToPayload(body) {
|
|
1383
|
-
if (body === void 0) return "";
|
|
1384
|
-
if (typeof body === "string") return body;
|
|
1385
|
-
if (typeof body === "object" && body !== null) return body;
|
|
1386
|
-
return String(body);
|
|
1387
|
-
}
|
|
1388
|
-
function recordToLogData(record, index) {
|
|
1389
|
-
const id = `log-${Date.now()}-${index}-${Math.random().toString(36).slice(2, 9)}`;
|
|
1390
|
-
const timestamp = hrTimeToMs(record.hrTime);
|
|
1391
|
-
const body = bodyToPayload(record.body);
|
|
1392
|
-
const attributes = record.attributes && Object.keys(record.attributes).length > 0 ? record.attributes : void 0;
|
|
1393
|
-
const resource = record.resource?.attributes && Object.keys(record.resource.attributes).length > 0 ? record.resource.attributes : void 0;
|
|
1394
|
-
const log = {
|
|
1395
|
-
id,
|
|
1396
|
-
resourceName: getResourceName(resource),
|
|
1397
|
-
severityText: record.severityText,
|
|
1398
|
-
severityNumber: record.severityNumber,
|
|
1399
|
-
body,
|
|
1400
|
-
timestamp,
|
|
1401
|
-
attributes,
|
|
1402
|
-
resource
|
|
1403
|
-
};
|
|
1404
|
-
if (record.spanContext) {
|
|
1405
|
-
log.traceId = record.spanContext.traceId;
|
|
1406
|
-
log.spanId = record.spanContext.spanId;
|
|
1407
|
-
}
|
|
1408
|
-
return log;
|
|
1409
|
-
}
|
|
1410
|
-
var DevtoolsLogExporter = class {
|
|
1411
|
-
endpoint;
|
|
1412
|
-
apiKey;
|
|
1413
|
-
timeout;
|
|
1414
|
-
isShutdown = false;
|
|
1415
|
-
constructor(options) {
|
|
1416
|
-
this.endpoint = options.endpoint.replace(/\/$/, "");
|
|
1417
|
-
this.apiKey = options.apiKey ?? "";
|
|
1418
|
-
this.timeout = options.timeout ?? defaultTimeout;
|
|
1419
|
-
}
|
|
1420
|
-
export(logs, resultCallback) {
|
|
1421
|
-
if (this.isShutdown || logs.length === 0) {
|
|
1422
|
-
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
1423
|
-
return;
|
|
1424
|
-
}
|
|
1425
|
-
const payload = { logs: logs.map((r, i) => recordToLogData(r, i)) };
|
|
1426
|
-
const url = `${this.endpoint}/ingest/logs`;
|
|
1427
|
-
const headers = {
|
|
1428
|
-
"Content-Type": "application/json"
|
|
1429
|
-
};
|
|
1430
|
-
if (this.apiKey) {
|
|
1431
|
-
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
1432
|
-
}
|
|
1433
|
-
const controller = new AbortController();
|
|
1434
|
-
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
1435
|
-
fetch(url, {
|
|
1436
|
-
method: "POST",
|
|
1437
|
-
headers,
|
|
1438
|
-
body: JSON.stringify(payload),
|
|
1439
|
-
signal: controller.signal
|
|
1440
|
-
}).then((res) => {
|
|
1441
|
-
clearTimeout(timeoutId);
|
|
1442
|
-
if (!res.ok) {
|
|
1443
|
-
throw new Error(`Devtools log ingest failed: ${res.status} ${res.statusText}`);
|
|
1444
|
-
}
|
|
1445
|
-
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
1446
|
-
}).catch((err) => {
|
|
1447
|
-
clearTimeout(timeoutId);
|
|
1448
|
-
resultCallback({
|
|
1449
|
-
code: ExportResultCode.FAILED,
|
|
1450
|
-
error: err instanceof Error ? err : new Error(String(err))
|
|
1451
|
-
});
|
|
1452
|
-
});
|
|
1453
|
-
}
|
|
1454
|
-
shutdown() {
|
|
1455
|
-
this.isShutdown = true;
|
|
1456
|
-
return Promise.resolve();
|
|
1457
|
-
}
|
|
1458
|
-
forceFlush() {
|
|
1459
|
-
return Promise.resolve();
|
|
1460
|
-
}
|
|
1461
|
-
};
|
|
1462
|
-
|
|
1463
|
-
// src/server/remote-exporter.ts
|
|
1464
|
-
var DevtoolsRemoteExporter = class {
|
|
1465
|
-
options;
|
|
1466
|
-
pendingExports = [];
|
|
1467
|
-
constructor(options) {
|
|
1468
|
-
this.options = {
|
|
1469
|
-
endpoint: options.endpoint.replace(/\/$/, ""),
|
|
1470
|
-
// Remove trailing slash
|
|
1471
|
-
apiKey: options.apiKey ?? "",
|
|
1472
|
-
serviceName: options.serviceName ?? "unknown-service",
|
|
1473
|
-
timeout: options.timeout ?? 5e3,
|
|
1474
|
-
retry: options.retry ?? true,
|
|
1475
|
-
retryCount: options.retryCount ?? 3,
|
|
1476
|
-
retryDelay: options.retryDelay ?? 1e3,
|
|
1477
|
-
verbose: options.verbose ?? false
|
|
1478
|
-
};
|
|
1479
|
-
}
|
|
1480
|
-
/**
|
|
1481
|
-
* Export spans to the remote server
|
|
1482
|
-
*/
|
|
1483
|
-
async export(spans, resultCallback) {
|
|
1484
|
-
const exportPromise = this.doExport(spans).then(() => {
|
|
1485
|
-
resultCallback({ code: 0 });
|
|
1486
|
-
}).catch((error) => {
|
|
1487
|
-
this.log(`Export failed: ${error.message}`);
|
|
1488
|
-
resultCallback({ code: 1 });
|
|
1489
|
-
});
|
|
1490
|
-
this.pendingExports.push(exportPromise);
|
|
1491
|
-
exportPromise.finally(() => {
|
|
1492
|
-
const index = this.pendingExports.indexOf(exportPromise);
|
|
1493
|
-
if (index !== -1) {
|
|
1494
|
-
this.pendingExports.splice(index, 1);
|
|
1495
|
-
}
|
|
1496
|
-
});
|
|
1497
|
-
}
|
|
1498
|
-
async doExport(spans) {
|
|
1499
|
-
if (spans.length === 0) return;
|
|
1500
|
-
this.log(`Exporting ${spans.length} span(s) to ${this.options.endpoint}`);
|
|
1501
|
-
const traceMap = /* @__PURE__ */ new Map();
|
|
1502
|
-
for (const span of spans) {
|
|
1503
|
-
const traceId = span.spanContext().traceId;
|
|
1504
|
-
if (!traceMap.has(traceId)) {
|
|
1505
|
-
traceMap.set(traceId, []);
|
|
1506
|
-
}
|
|
1507
|
-
traceMap.get(traceId).push(span);
|
|
1508
|
-
}
|
|
1509
|
-
const traces = [];
|
|
1510
|
-
for (const [traceId, traceSpans] of traceMap) {
|
|
1511
|
-
traces.push(this.convertToTraceData(traceId, traceSpans));
|
|
1512
|
-
}
|
|
1513
|
-
await this.sendWithRetry({ traces });
|
|
1514
|
-
}
|
|
1515
|
-
async sendWithRetry(payload) {
|
|
1516
|
-
let lastError = null;
|
|
1517
|
-
const maxAttempts = this.options.retry ? this.options.retryCount : 1;
|
|
1518
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
1519
|
-
try {
|
|
1520
|
-
await this.send(payload);
|
|
1521
|
-
return;
|
|
1522
|
-
} catch (error) {
|
|
1523
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
1524
|
-
this.log(
|
|
1525
|
-
`Attempt ${attempt}/${maxAttempts} failed: ${lastError.message}`
|
|
1526
|
-
);
|
|
1527
|
-
if (attempt < maxAttempts) {
|
|
1528
|
-
await this.sleep(this.options.retryDelay * attempt);
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
|
-
throw lastError || new Error("Export failed");
|
|
1533
|
-
}
|
|
1534
|
-
async send(payload) {
|
|
1535
|
-
const controller = new AbortController();
|
|
1536
|
-
const timeoutId = setTimeout(
|
|
1537
|
-
() => controller.abort(),
|
|
1538
|
-
this.options.timeout
|
|
1539
|
-
);
|
|
1540
|
-
try {
|
|
1541
|
-
const headers = {
|
|
1542
|
-
"Content-Type": "application/json"
|
|
1543
|
-
};
|
|
1544
|
-
if (this.options.apiKey) {
|
|
1545
|
-
headers["Authorization"] = `Bearer ${this.options.apiKey}`;
|
|
1546
|
-
}
|
|
1547
|
-
const response = await fetch(`${this.options.endpoint}/ingest/traces`, {
|
|
1548
|
-
method: "POST",
|
|
1549
|
-
headers,
|
|
1550
|
-
body: JSON.stringify(payload),
|
|
1551
|
-
signal: controller.signal
|
|
1552
|
-
});
|
|
1553
|
-
if (!response.ok) {
|
|
1554
|
-
const text = await response.text();
|
|
1555
|
-
throw new Error(`HTTP ${response.status}: ${text}`);
|
|
1556
|
-
}
|
|
1557
|
-
const result = await response.json();
|
|
1558
|
-
this.log(`Successfully sent ${result.processed} trace(s)`);
|
|
1559
|
-
} finally {
|
|
1560
|
-
clearTimeout(timeoutId);
|
|
1561
|
-
}
|
|
1562
|
-
}
|
|
1563
|
-
/**
|
|
1564
|
-
* Shutdown the exporter, waiting for pending exports
|
|
1565
|
-
*/
|
|
1566
|
-
async shutdown() {
|
|
1567
|
-
this.log("Shutting down, waiting for pending exports...");
|
|
1568
|
-
await Promise.allSettled(this.pendingExports);
|
|
1569
|
-
this.log("Shutdown complete");
|
|
1570
|
-
}
|
|
1571
|
-
/**
|
|
1572
|
-
* Force flush pending exports
|
|
1573
|
-
*/
|
|
1574
|
-
async forceFlush() {
|
|
1575
|
-
await Promise.allSettled(this.pendingExports);
|
|
1576
|
-
}
|
|
1577
|
-
convertToTraceData(traceId, spans) {
|
|
1578
|
-
const spanData = spans.map((span) => this.convertSpan(span));
|
|
1579
|
-
const rootSpan = spanData.find((s) => !s.parentSpanId) || spanData[0];
|
|
1580
|
-
spanData.sort((a, b) => a.startTime - b.startTime);
|
|
1581
|
-
const startTime = Math.min(...spanData.map((s) => s.startTime));
|
|
1582
|
-
const endTime = Math.max(...spanData.map((s) => s.endTime));
|
|
1583
|
-
const hasError = spanData.some((s) => s.status.code === "ERROR");
|
|
1584
|
-
const status = hasError ? "ERROR" : "OK";
|
|
1585
|
-
return {
|
|
1586
|
-
traceId,
|
|
1587
|
-
correlationId: traceId.slice(0, 16),
|
|
1588
|
-
rootSpan,
|
|
1589
|
-
spans: spanData,
|
|
1590
|
-
startTime,
|
|
1591
|
-
endTime,
|
|
1592
|
-
duration: endTime - startTime,
|
|
1593
|
-
status,
|
|
1594
|
-
service: this.options.serviceName
|
|
1595
|
-
};
|
|
1596
|
-
}
|
|
1597
|
-
convertSpan(span) {
|
|
1598
|
-
const spanContext = span.spanContext();
|
|
1599
|
-
const startTime = span.startTime[0] * 1e3 + span.startTime[1] / 1e6;
|
|
1600
|
-
const endTime = span.endTime[0] * 1e3 + span.endTime[1] / 1e6;
|
|
1601
|
-
const attributes = {};
|
|
1602
|
-
for (const [key, value] of Object.entries(span.attributes)) {
|
|
1603
|
-
attributes[key] = value;
|
|
1604
|
-
}
|
|
1605
|
-
let status;
|
|
1606
|
-
switch (span.status.code) {
|
|
1607
|
-
case 0: {
|
|
1608
|
-
status = "UNSET";
|
|
1609
|
-
break;
|
|
1610
|
-
}
|
|
1611
|
-
case 1: {
|
|
1612
|
-
status = "OK";
|
|
1613
|
-
break;
|
|
1614
|
-
}
|
|
1615
|
-
case 2: {
|
|
1616
|
-
status = "ERROR";
|
|
1617
|
-
break;
|
|
1618
|
-
}
|
|
1619
|
-
default: {
|
|
1620
|
-
status = "UNSET";
|
|
1621
|
-
}
|
|
1622
|
-
}
|
|
1623
|
-
const events = span.events.map((event) => ({
|
|
1624
|
-
name: event.name,
|
|
1625
|
-
timestamp: event.time[0] * 1e3 + event.time[1] / 1e6,
|
|
1626
|
-
attributes: event.attributes ? Object.fromEntries(Object.entries(event.attributes)) : void 0
|
|
1627
|
-
}));
|
|
1628
|
-
return {
|
|
1629
|
-
traceId: spanContext.traceId,
|
|
1630
|
-
spanId: spanContext.spanId,
|
|
1631
|
-
parentSpanId: span.parentSpanId,
|
|
1632
|
-
name: span.name,
|
|
1633
|
-
kind: this.convertSpanKind(span.kind),
|
|
1634
|
-
startTime,
|
|
1635
|
-
endTime,
|
|
1636
|
-
duration: endTime - startTime,
|
|
1637
|
-
attributes,
|
|
1638
|
-
status: {
|
|
1639
|
-
code: status,
|
|
1640
|
-
message: span.status.message
|
|
1641
|
-
},
|
|
1642
|
-
events: events.length > 0 ? events : void 0
|
|
1643
|
-
};
|
|
1644
|
-
}
|
|
1645
|
-
convertSpanKind(kind) {
|
|
1646
|
-
switch (kind) {
|
|
1647
|
-
case 0: {
|
|
1648
|
-
return "INTERNAL";
|
|
1649
|
-
}
|
|
1650
|
-
case 1: {
|
|
1651
|
-
return "SERVER";
|
|
1652
|
-
}
|
|
1653
|
-
case 2: {
|
|
1654
|
-
return "CLIENT";
|
|
1655
|
-
}
|
|
1656
|
-
case 3: {
|
|
1657
|
-
return "PRODUCER";
|
|
1658
|
-
}
|
|
1659
|
-
case 4: {
|
|
1660
|
-
return "CONSUMER";
|
|
1661
|
-
}
|
|
1662
|
-
default: {
|
|
1663
|
-
return "INTERNAL";
|
|
1664
|
-
}
|
|
1665
|
-
}
|
|
1666
|
-
}
|
|
1667
|
-
sleep(ms) {
|
|
1668
|
-
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
1669
|
-
}
|
|
1670
|
-
log(message) {
|
|
1671
|
-
if (this.options.verbose) {
|
|
1672
|
-
console.log(`[Devtools Remote Exporter] ${message}`);
|
|
1673
|
-
}
|
|
1674
|
-
}
|
|
1675
|
-
};
|
|
1676
|
-
|
|
1677
|
-
// src/index.ts
|
|
1
|
+
import { b as ErrorAggregator, d as DevtoolsServer, p as hostHeaderIsLoopback, t as attachDevtoolsRoutes } from "./http-BkkKa9C_.js";
|
|
2
|
+
import { t as listenLoopbackDualStack } from "./listen-DfOCquUq.js";
|
|
3
|
+
import { DevtoolsSpanExporter } from "./server/exporter.js";
|
|
4
|
+
import { DevtoolsLogExporter } from "./server/log-exporter.js";
|
|
5
|
+
import { DevtoolsRemoteExporter } from "./server/remote-exporter.js";
|
|
6
|
+
import { createServer } from "node:http";
|
|
7
|
+
|
|
8
|
+
//#region src/index.ts
|
|
1678
9
|
function createDevtools(options = {}) {
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
}
|
|
1716
|
-
|
|
10
|
+
const port = options.port ?? 4318;
|
|
11
|
+
const host = options.host ?? "127.0.0.1";
|
|
12
|
+
const loopbackOnly = hostHeaderIsLoopback(host);
|
|
13
|
+
const httpServer = createServer();
|
|
14
|
+
const wsServer = new DevtoolsServer({
|
|
15
|
+
server: httpServer,
|
|
16
|
+
host,
|
|
17
|
+
verbose: options.verbose,
|
|
18
|
+
maxHistory: options.maxHistory,
|
|
19
|
+
maxTraceCount: options.maxTraceCount,
|
|
20
|
+
maxLogCount: options.maxLogCount,
|
|
21
|
+
maxMetricCount: options.maxMetricCount
|
|
22
|
+
});
|
|
23
|
+
attachDevtoolsRoutes(httpServer, wsServer, { loopbackOnly });
|
|
24
|
+
const listeners = listenLoopbackDualStack({
|
|
25
|
+
primary: httpServer,
|
|
26
|
+
port,
|
|
27
|
+
host,
|
|
28
|
+
attachSecondary: (s) => attachDevtoolsRoutes(s, wsServer, { loopbackOnly })
|
|
29
|
+
});
|
|
30
|
+
if (options.verbose) listeners.ready.then(({ warnings }) => {
|
|
31
|
+
for (const w of warnings) console.warn(`[autotel-devtools] ${w}`);
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
server: wsServer,
|
|
35
|
+
httpServer,
|
|
36
|
+
exporter: new DevtoolsSpanExporter(wsServer),
|
|
37
|
+
port,
|
|
38
|
+
close: async () => {
|
|
39
|
+
await wsServer.close();
|
|
40
|
+
await listeners.closeSibling();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
1717
46
|
export { DevtoolsLogExporter, DevtoolsRemoteExporter, DevtoolsServer, DevtoolsSpanExporter, ErrorAggregator, createDevtools };
|
|
1718
|
-
//# sourceMappingURL=index.js.map
|
|
1719
47
|
//# sourceMappingURL=index.js.map
|