autotel-devtools 5.0.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,8 +6,29 @@ var core = require('@opentelemetry/core');
6
6
  var fs = require('fs');
7
7
  var path = require('path');
8
8
  var url = require('url');
9
+ var protobuf = require('protobufjs');
9
10
 
10
11
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
12
+ function _interopNamespace(e) {
13
+ if (e && e.__esModule) return e;
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n.default = e;
27
+ return Object.freeze(n);
28
+ }
29
+
30
+ var protobuf__namespace = /*#__PURE__*/_interopNamespace(protobuf);
31
+
11
32
  // src/server/server.ts
12
33
 
13
34
  // src/server/error-aggregator.ts
@@ -987,7 +1008,7 @@ var SPAN_KIND_MAP = {
987
1008
  function normalizeHexId(id) {
988
1009
  if (!id) return "";
989
1010
  const isBase64Like = /^[A-Za-z0-9+/=]+$/.test(id) && !/^[0-9a-f]+$/i.test(id);
990
- const isLikelyBase64Id = isBase64Like && (id.length === 24 || id.length === 28 || id.length === 44 || id.length === 48);
1011
+ const isLikelyBase64Id = isBase64Like && (id.length === 12 || id.length === 24 || id.length === 28 || id.length === 44 || id.length === 48);
991
1012
  if (isLikelyBase64Id) {
992
1013
  try {
993
1014
  const bytes = Buffer.from(id, "base64");
@@ -1119,13 +1140,255 @@ async function readJsonBody(req) {
1119
1140
  req.on("error", reject);
1120
1141
  });
1121
1142
  }
1143
+ async function readRawBody(req) {
1144
+ return new Promise((resolve2, reject) => {
1145
+ const chunks = [];
1146
+ req.on("data", (chunk) => chunks.push(chunk));
1147
+ req.on("end", () => resolve2(Buffer.concat(chunks)));
1148
+ req.on("error", reject);
1149
+ });
1150
+ }
1151
+ function isProtobufContentType(contentType) {
1152
+ if (!contentType) return false;
1153
+ const value = contentType.toLowerCase();
1154
+ return value.includes("application/x-protobuf") || value.includes("application/protobuf");
1155
+ }
1122
1156
  function sendJson(res, status, data) {
1123
1157
  const body = JSON.stringify(data);
1124
1158
  res.writeHead(status, { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(body) });
1125
1159
  res.end(body);
1126
1160
  }
1161
+ var COMMON_PROTO = `
1162
+ syntax = "proto3";
1163
+ package opentelemetry.proto.common.v1;
1164
+
1165
+ message AnyValue {
1166
+ oneof value {
1167
+ string string_value = 1;
1168
+ bool bool_value = 2;
1169
+ int64 int_value = 3;
1170
+ double double_value = 4;
1171
+ ArrayValue array_value = 5;
1172
+ KeyValueList kvlist_value = 6;
1173
+ bytes bytes_value = 7;
1174
+ }
1175
+ }
1176
+ message ArrayValue { repeated AnyValue values = 1; }
1177
+ message KeyValueList { repeated KeyValue values = 1; }
1178
+ message KeyValue {
1179
+ string key = 1;
1180
+ AnyValue value = 2;
1181
+ }
1182
+ message InstrumentationScope {
1183
+ string name = 1;
1184
+ string version = 2;
1185
+ repeated KeyValue attributes = 3;
1186
+ uint32 dropped_attributes_count = 4;
1187
+ }
1188
+ `;
1189
+ var RESOURCE_PROTO = `
1190
+ syntax = "proto3";
1191
+ package opentelemetry.proto.resource.v1;
1192
+
1193
+ message Resource {
1194
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 1;
1195
+ uint32 dropped_attributes_count = 2;
1196
+ }
1197
+ `;
1198
+ var TRACE_PROTO = `
1199
+ syntax = "proto3";
1200
+ package opentelemetry.proto.trace.v1;
1201
+
1202
+ message ResourceSpans {
1203
+ opentelemetry.proto.resource.v1.Resource resource = 1;
1204
+ repeated ScopeSpans scope_spans = 2;
1205
+ string schema_url = 3;
1206
+ }
1207
+ message ScopeSpans {
1208
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
1209
+ repeated Span spans = 2;
1210
+ string schema_url = 3;
1211
+ }
1212
+ message Span {
1213
+ bytes trace_id = 1;
1214
+ bytes span_id = 2;
1215
+ string trace_state = 3;
1216
+ bytes parent_span_id = 4;
1217
+ fixed32 flags = 16;
1218
+ string name = 5;
1219
+ SpanKind kind = 6;
1220
+ fixed64 start_time_unix_nano = 7;
1221
+ fixed64 end_time_unix_nano = 8;
1222
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 9;
1223
+ uint32 dropped_attributes_count = 10;
1224
+ repeated Event events = 11;
1225
+ uint32 dropped_events_count = 12;
1226
+ repeated Link links = 13;
1227
+ uint32 dropped_links_count = 14;
1228
+ Status status = 15;
1229
+
1230
+ enum SpanKind {
1231
+ SPAN_KIND_UNSPECIFIED = 0;
1232
+ SPAN_KIND_INTERNAL = 1;
1233
+ SPAN_KIND_SERVER = 2;
1234
+ SPAN_KIND_CLIENT = 3;
1235
+ SPAN_KIND_PRODUCER = 4;
1236
+ SPAN_KIND_CONSUMER = 5;
1237
+ }
1238
+ message Event {
1239
+ fixed64 time_unix_nano = 1;
1240
+ string name = 2;
1241
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 3;
1242
+ uint32 dropped_attributes_count = 4;
1243
+ }
1244
+ message Link {
1245
+ bytes trace_id = 1;
1246
+ bytes span_id = 2;
1247
+ string trace_state = 3;
1248
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 4;
1249
+ uint32 dropped_attributes_count = 5;
1250
+ fixed32 flags = 6;
1251
+ }
1252
+ }
1253
+ message Status {
1254
+ reserved 1;
1255
+ string message = 2;
1256
+ StatusCode code = 3;
1257
+
1258
+ enum StatusCode {
1259
+ STATUS_CODE_UNSET = 0;
1260
+ STATUS_CODE_OK = 1;
1261
+ STATUS_CODE_ERROR = 2;
1262
+ }
1263
+ }
1264
+ message ExportTraceServiceRequest {
1265
+ repeated ResourceSpans resource_spans = 1;
1266
+ }
1267
+ `;
1268
+ var LOGS_PROTO = `
1269
+ syntax = "proto3";
1270
+ package opentelemetry.proto.logs.v1;
1271
+
1272
+ enum SeverityNumber {
1273
+ SEVERITY_NUMBER_UNSPECIFIED = 0;
1274
+ SEVERITY_NUMBER_TRACE = 1;
1275
+ SEVERITY_NUMBER_TRACE2 = 2;
1276
+ SEVERITY_NUMBER_TRACE3 = 3;
1277
+ SEVERITY_NUMBER_TRACE4 = 4;
1278
+ SEVERITY_NUMBER_DEBUG = 5;
1279
+ SEVERITY_NUMBER_DEBUG2 = 6;
1280
+ SEVERITY_NUMBER_DEBUG3 = 7;
1281
+ SEVERITY_NUMBER_DEBUG4 = 8;
1282
+ SEVERITY_NUMBER_INFO = 9;
1283
+ SEVERITY_NUMBER_INFO2 = 10;
1284
+ SEVERITY_NUMBER_INFO3 = 11;
1285
+ SEVERITY_NUMBER_INFO4 = 12;
1286
+ SEVERITY_NUMBER_WARN = 13;
1287
+ SEVERITY_NUMBER_WARN2 = 14;
1288
+ SEVERITY_NUMBER_WARN3 = 15;
1289
+ SEVERITY_NUMBER_WARN4 = 16;
1290
+ SEVERITY_NUMBER_ERROR = 17;
1291
+ SEVERITY_NUMBER_ERROR2 = 18;
1292
+ SEVERITY_NUMBER_ERROR3 = 19;
1293
+ SEVERITY_NUMBER_ERROR4 = 20;
1294
+ SEVERITY_NUMBER_FATAL = 21;
1295
+ SEVERITY_NUMBER_FATAL2 = 22;
1296
+ SEVERITY_NUMBER_FATAL3 = 23;
1297
+ SEVERITY_NUMBER_FATAL4 = 24;
1298
+ }
1299
+ message ResourceLogs {
1300
+ opentelemetry.proto.resource.v1.Resource resource = 1;
1301
+ repeated ScopeLogs scope_logs = 2;
1302
+ string schema_url = 3;
1303
+ }
1304
+ message ScopeLogs {
1305
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
1306
+ repeated LogRecord log_records = 2;
1307
+ string schema_url = 3;
1308
+ }
1309
+ message LogRecord {
1310
+ reserved 4;
1311
+ fixed64 time_unix_nano = 1;
1312
+ fixed64 observed_time_unix_nano = 11;
1313
+ SeverityNumber severity_number = 2;
1314
+ string severity_text = 3;
1315
+ opentelemetry.proto.common.v1.AnyValue body = 5;
1316
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 6;
1317
+ uint32 dropped_attributes_count = 7;
1318
+ fixed32 flags = 8;
1319
+ bytes trace_id = 9;
1320
+ bytes span_id = 10;
1321
+ }
1322
+ message ExportLogsServiceRequest {
1323
+ repeated ResourceLogs resource_logs = 1;
1324
+ }
1325
+ `;
1326
+ var METRICS_PROTO = `
1327
+ syntax = "proto3";
1328
+ package opentelemetry.proto.metrics.v1;
1329
+
1330
+ message ResourceMetrics {
1331
+ opentelemetry.proto.resource.v1.Resource resource = 1;
1332
+ repeated ScopeMetrics scope_metrics = 2;
1333
+ string schema_url = 3;
1334
+ }
1335
+ message ScopeMetrics {
1336
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
1337
+ repeated Metric metrics = 2;
1338
+ string schema_url = 3;
1339
+ }
1340
+ message Metric {
1341
+ string name = 1;
1342
+ string description = 2;
1343
+ string unit = 3;
1344
+ }
1345
+ message ExportMetricsServiceRequest {
1346
+ repeated ResourceMetrics resource_metrics = 1;
1347
+ }
1348
+ `;
1349
+ var TO_OBJECT_OPTIONS = {
1350
+ longs: String,
1351
+ bytes: String,
1352
+ defaults: false
1353
+ };
1354
+ var cachedRoot = null;
1355
+ function getRoot() {
1356
+ if (cachedRoot) return cachedRoot;
1357
+ const root = new protobuf__namespace.Root();
1358
+ for (const source of [COMMON_PROTO, RESOURCE_PROTO, TRACE_PROTO, LOGS_PROTO, METRICS_PROTO]) {
1359
+ protobuf__namespace.parse(source, root, { keepCase: false });
1360
+ }
1361
+ root.resolveAll();
1362
+ cachedRoot = root;
1363
+ return root;
1364
+ }
1365
+ function decodeRequest(typeName, body) {
1366
+ const messageType = getRoot().lookupType(typeName);
1367
+ const message = messageType.decode(body);
1368
+ return messageType.toObject(message, TO_OBJECT_OPTIONS);
1369
+ }
1370
+ function decodeOtlpTraceRequest(body) {
1371
+ return decodeRequest("opentelemetry.proto.trace.v1.ExportTraceServiceRequest", body);
1372
+ }
1373
+ function decodeOtlpLogsRequest(body) {
1374
+ return decodeRequest("opentelemetry.proto.logs.v1.ExportLogsServiceRequest", body);
1375
+ }
1376
+ function decodeOtlpMetricsRequest(body) {
1377
+ return decodeRequest("opentelemetry.proto.metrics.v1.ExportMetricsServiceRequest", body);
1378
+ }
1127
1379
 
1128
1380
  // src/server/http.ts
1381
+ var PROTOBUF_DECODERS = {
1382
+ traces: decodeOtlpTraceRequest,
1383
+ logs: decodeOtlpLogsRequest,
1384
+ metrics: decodeOtlpMetricsRequest
1385
+ };
1386
+ async function readOtlpPayload(req, signal) {
1387
+ if (isProtobufContentType(req.headers["content-type"])) {
1388
+ return PROTOBUF_DECODERS[signal](await readRawBody(req));
1389
+ }
1390
+ return readJsonBody(req);
1391
+ }
1129
1392
  function findPackageRoot() {
1130
1393
  let dir = path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
1131
1394
  for (let i = 0; i < 5; i++) {
@@ -1194,33 +1457,33 @@ function attachDevtoolsRoutes(httpServer, devtools) {
1194
1457
  }
1195
1458
  if (req.method === "POST" && url === "/v1/traces") {
1196
1459
  try {
1197
- const payload = await readJsonBody(req);
1460
+ const payload = await readOtlpPayload(req, "traces");
1198
1461
  const traces = parseOtlpTraces(payload);
1199
1462
  devtools.addTraces(traces);
1200
1463
  sendJson(res, 200, { acceptedTraces: traces.length });
1201
1464
  } catch (e) {
1202
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1465
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
1203
1466
  }
1204
1467
  return;
1205
1468
  }
1206
1469
  if (req.method === "POST" && url === "/v1/logs") {
1207
1470
  try {
1208
- const payload = await readJsonBody(req);
1471
+ const payload = await readOtlpPayload(req, "logs");
1209
1472
  const logs = parseOtlpLogs(payload);
1210
1473
  devtools.addLogs(logs);
1211
1474
  sendJson(res, 200, { acceptedLogs: logs.length });
1212
1475
  } catch (e) {
1213
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1476
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
1214
1477
  }
1215
1478
  return;
1216
1479
  }
1217
1480
  if (req.method === "POST" && url === "/v1/metrics") {
1218
1481
  try {
1219
- const payload = await readJsonBody(req);
1482
+ const payload = await readOtlpPayload(req, "metrics");
1220
1483
  const count = countOtlpMetrics(payload);
1221
1484
  sendJson(res, 200, { acceptedMetrics: count });
1222
1485
  } catch (e) {
1223
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1486
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
1224
1487
  }
1225
1488
  return;
1226
1489
  }
@@ -1243,6 +1506,10 @@ exports.appendWithLimit = appendWithLimit;
1243
1506
  exports.applyTelemetryLimits = applyTelemetryLimits;
1244
1507
  exports.attachDevtoolsRoutes = attachDevtoolsRoutes;
1245
1508
  exports.createDevtoolsHttpServer = createDevtoolsHttpServer;
1509
+ exports.decodeOtlpLogsRequest = decodeOtlpLogsRequest;
1510
+ exports.decodeOtlpMetricsRequest = decodeOtlpMetricsRequest;
1511
+ exports.decodeOtlpTraceRequest = decodeOtlpTraceRequest;
1512
+ exports.isProtobufContentType = isProtobufContentType;
1246
1513
  exports.parseOtlpLogs = parseOtlpLogs;
1247
1514
  exports.parseOtlpTraces = parseOtlpTraces;
1248
1515
  exports.resolveTelemetryLimits = resolveTelemetryLimits;