autotel-devtools 5.0.0 → 5.1.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.
@@ -6,8 +6,13 @@ 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 _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
13
+
14
+ var protobuf__default = /*#__PURE__*/_interopDefault(protobuf);
15
+
11
16
  // src/server/server.ts
12
17
 
13
18
  // src/server/error-aggregator.ts
@@ -987,7 +992,7 @@ var SPAN_KIND_MAP = {
987
992
  function normalizeHexId(id) {
988
993
  if (!id) return "";
989
994
  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);
995
+ const isLikelyBase64Id = isBase64Like && (id.length === 12 || id.length === 24 || id.length === 28 || id.length === 44 || id.length === 48);
991
996
  if (isLikelyBase64Id) {
992
997
  try {
993
998
  const bytes = Buffer.from(id, "base64");
@@ -1119,13 +1124,255 @@ async function readJsonBody(req) {
1119
1124
  req.on("error", reject);
1120
1125
  });
1121
1126
  }
1127
+ async function readRawBody(req) {
1128
+ return new Promise((resolve2, reject) => {
1129
+ const chunks = [];
1130
+ req.on("data", (chunk) => chunks.push(chunk));
1131
+ req.on("end", () => resolve2(Buffer.concat(chunks)));
1132
+ req.on("error", reject);
1133
+ });
1134
+ }
1135
+ function isProtobufContentType(contentType) {
1136
+ if (!contentType) return false;
1137
+ const value = contentType.toLowerCase();
1138
+ return value.includes("application/x-protobuf") || value.includes("application/protobuf");
1139
+ }
1122
1140
  function sendJson(res, status, data) {
1123
1141
  const body = JSON.stringify(data);
1124
1142
  res.writeHead(status, { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(body) });
1125
1143
  res.end(body);
1126
1144
  }
1145
+ var COMMON_PROTO = `
1146
+ syntax = "proto3";
1147
+ package opentelemetry.proto.common.v1;
1148
+
1149
+ message AnyValue {
1150
+ oneof value {
1151
+ string string_value = 1;
1152
+ bool bool_value = 2;
1153
+ int64 int_value = 3;
1154
+ double double_value = 4;
1155
+ ArrayValue array_value = 5;
1156
+ KeyValueList kvlist_value = 6;
1157
+ bytes bytes_value = 7;
1158
+ }
1159
+ }
1160
+ message ArrayValue { repeated AnyValue values = 1; }
1161
+ message KeyValueList { repeated KeyValue values = 1; }
1162
+ message KeyValue {
1163
+ string key = 1;
1164
+ AnyValue value = 2;
1165
+ }
1166
+ message InstrumentationScope {
1167
+ string name = 1;
1168
+ string version = 2;
1169
+ repeated KeyValue attributes = 3;
1170
+ uint32 dropped_attributes_count = 4;
1171
+ }
1172
+ `;
1173
+ var RESOURCE_PROTO = `
1174
+ syntax = "proto3";
1175
+ package opentelemetry.proto.resource.v1;
1176
+
1177
+ message Resource {
1178
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 1;
1179
+ uint32 dropped_attributes_count = 2;
1180
+ }
1181
+ `;
1182
+ var TRACE_PROTO = `
1183
+ syntax = "proto3";
1184
+ package opentelemetry.proto.trace.v1;
1185
+
1186
+ message ResourceSpans {
1187
+ opentelemetry.proto.resource.v1.Resource resource = 1;
1188
+ repeated ScopeSpans scope_spans = 2;
1189
+ string schema_url = 3;
1190
+ }
1191
+ message ScopeSpans {
1192
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
1193
+ repeated Span spans = 2;
1194
+ string schema_url = 3;
1195
+ }
1196
+ message Span {
1197
+ bytes trace_id = 1;
1198
+ bytes span_id = 2;
1199
+ string trace_state = 3;
1200
+ bytes parent_span_id = 4;
1201
+ fixed32 flags = 16;
1202
+ string name = 5;
1203
+ SpanKind kind = 6;
1204
+ fixed64 start_time_unix_nano = 7;
1205
+ fixed64 end_time_unix_nano = 8;
1206
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 9;
1207
+ uint32 dropped_attributes_count = 10;
1208
+ repeated Event events = 11;
1209
+ uint32 dropped_events_count = 12;
1210
+ repeated Link links = 13;
1211
+ uint32 dropped_links_count = 14;
1212
+ Status status = 15;
1213
+
1214
+ enum SpanKind {
1215
+ SPAN_KIND_UNSPECIFIED = 0;
1216
+ SPAN_KIND_INTERNAL = 1;
1217
+ SPAN_KIND_SERVER = 2;
1218
+ SPAN_KIND_CLIENT = 3;
1219
+ SPAN_KIND_PRODUCER = 4;
1220
+ SPAN_KIND_CONSUMER = 5;
1221
+ }
1222
+ message Event {
1223
+ fixed64 time_unix_nano = 1;
1224
+ string name = 2;
1225
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 3;
1226
+ uint32 dropped_attributes_count = 4;
1227
+ }
1228
+ message Link {
1229
+ bytes trace_id = 1;
1230
+ bytes span_id = 2;
1231
+ string trace_state = 3;
1232
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 4;
1233
+ uint32 dropped_attributes_count = 5;
1234
+ fixed32 flags = 6;
1235
+ }
1236
+ }
1237
+ message Status {
1238
+ reserved 1;
1239
+ string message = 2;
1240
+ StatusCode code = 3;
1241
+
1242
+ enum StatusCode {
1243
+ STATUS_CODE_UNSET = 0;
1244
+ STATUS_CODE_OK = 1;
1245
+ STATUS_CODE_ERROR = 2;
1246
+ }
1247
+ }
1248
+ message ExportTraceServiceRequest {
1249
+ repeated ResourceSpans resource_spans = 1;
1250
+ }
1251
+ `;
1252
+ var LOGS_PROTO = `
1253
+ syntax = "proto3";
1254
+ package opentelemetry.proto.logs.v1;
1255
+
1256
+ enum SeverityNumber {
1257
+ SEVERITY_NUMBER_UNSPECIFIED = 0;
1258
+ SEVERITY_NUMBER_TRACE = 1;
1259
+ SEVERITY_NUMBER_TRACE2 = 2;
1260
+ SEVERITY_NUMBER_TRACE3 = 3;
1261
+ SEVERITY_NUMBER_TRACE4 = 4;
1262
+ SEVERITY_NUMBER_DEBUG = 5;
1263
+ SEVERITY_NUMBER_DEBUG2 = 6;
1264
+ SEVERITY_NUMBER_DEBUG3 = 7;
1265
+ SEVERITY_NUMBER_DEBUG4 = 8;
1266
+ SEVERITY_NUMBER_INFO = 9;
1267
+ SEVERITY_NUMBER_INFO2 = 10;
1268
+ SEVERITY_NUMBER_INFO3 = 11;
1269
+ SEVERITY_NUMBER_INFO4 = 12;
1270
+ SEVERITY_NUMBER_WARN = 13;
1271
+ SEVERITY_NUMBER_WARN2 = 14;
1272
+ SEVERITY_NUMBER_WARN3 = 15;
1273
+ SEVERITY_NUMBER_WARN4 = 16;
1274
+ SEVERITY_NUMBER_ERROR = 17;
1275
+ SEVERITY_NUMBER_ERROR2 = 18;
1276
+ SEVERITY_NUMBER_ERROR3 = 19;
1277
+ SEVERITY_NUMBER_ERROR4 = 20;
1278
+ SEVERITY_NUMBER_FATAL = 21;
1279
+ SEVERITY_NUMBER_FATAL2 = 22;
1280
+ SEVERITY_NUMBER_FATAL3 = 23;
1281
+ SEVERITY_NUMBER_FATAL4 = 24;
1282
+ }
1283
+ message ResourceLogs {
1284
+ opentelemetry.proto.resource.v1.Resource resource = 1;
1285
+ repeated ScopeLogs scope_logs = 2;
1286
+ string schema_url = 3;
1287
+ }
1288
+ message ScopeLogs {
1289
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
1290
+ repeated LogRecord log_records = 2;
1291
+ string schema_url = 3;
1292
+ }
1293
+ message LogRecord {
1294
+ reserved 4;
1295
+ fixed64 time_unix_nano = 1;
1296
+ fixed64 observed_time_unix_nano = 11;
1297
+ SeverityNumber severity_number = 2;
1298
+ string severity_text = 3;
1299
+ opentelemetry.proto.common.v1.AnyValue body = 5;
1300
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 6;
1301
+ uint32 dropped_attributes_count = 7;
1302
+ fixed32 flags = 8;
1303
+ bytes trace_id = 9;
1304
+ bytes span_id = 10;
1305
+ }
1306
+ message ExportLogsServiceRequest {
1307
+ repeated ResourceLogs resource_logs = 1;
1308
+ }
1309
+ `;
1310
+ var METRICS_PROTO = `
1311
+ syntax = "proto3";
1312
+ package opentelemetry.proto.metrics.v1;
1313
+
1314
+ message ResourceMetrics {
1315
+ opentelemetry.proto.resource.v1.Resource resource = 1;
1316
+ repeated ScopeMetrics scope_metrics = 2;
1317
+ string schema_url = 3;
1318
+ }
1319
+ message ScopeMetrics {
1320
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
1321
+ repeated Metric metrics = 2;
1322
+ string schema_url = 3;
1323
+ }
1324
+ message Metric {
1325
+ string name = 1;
1326
+ string description = 2;
1327
+ string unit = 3;
1328
+ }
1329
+ message ExportMetricsServiceRequest {
1330
+ repeated ResourceMetrics resource_metrics = 1;
1331
+ }
1332
+ `;
1333
+ var TO_OBJECT_OPTIONS = {
1334
+ longs: String,
1335
+ bytes: String,
1336
+ defaults: false
1337
+ };
1338
+ var cachedRoot = null;
1339
+ function getRoot() {
1340
+ if (cachedRoot) return cachedRoot;
1341
+ const root = new protobuf__default.default.Root();
1342
+ for (const source of [COMMON_PROTO, RESOURCE_PROTO, TRACE_PROTO, LOGS_PROTO, METRICS_PROTO]) {
1343
+ protobuf__default.default.parse(source, root, { keepCase: false });
1344
+ }
1345
+ root.resolveAll();
1346
+ cachedRoot = root;
1347
+ return root;
1348
+ }
1349
+ function decodeRequest(typeName, body) {
1350
+ const messageType = getRoot().lookupType(typeName);
1351
+ const message = messageType.decode(body);
1352
+ return messageType.toObject(message, TO_OBJECT_OPTIONS);
1353
+ }
1354
+ function decodeOtlpTraceRequest(body) {
1355
+ return decodeRequest("opentelemetry.proto.trace.v1.ExportTraceServiceRequest", body);
1356
+ }
1357
+ function decodeOtlpLogsRequest(body) {
1358
+ return decodeRequest("opentelemetry.proto.logs.v1.ExportLogsServiceRequest", body);
1359
+ }
1360
+ function decodeOtlpMetricsRequest(body) {
1361
+ return decodeRequest("opentelemetry.proto.metrics.v1.ExportMetricsServiceRequest", body);
1362
+ }
1127
1363
 
1128
1364
  // src/server/http.ts
1365
+ var PROTOBUF_DECODERS = {
1366
+ traces: decodeOtlpTraceRequest,
1367
+ logs: decodeOtlpLogsRequest,
1368
+ metrics: decodeOtlpMetricsRequest
1369
+ };
1370
+ async function readOtlpPayload(req, signal) {
1371
+ if (isProtobufContentType(req.headers["content-type"])) {
1372
+ return PROTOBUF_DECODERS[signal](await readRawBody(req));
1373
+ }
1374
+ return readJsonBody(req);
1375
+ }
1129
1376
  function findPackageRoot() {
1130
1377
  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
1378
  for (let i = 0; i < 5; i++) {
@@ -1194,33 +1441,33 @@ function attachDevtoolsRoutes(httpServer, devtools) {
1194
1441
  }
1195
1442
  if (req.method === "POST" && url === "/v1/traces") {
1196
1443
  try {
1197
- const payload = await readJsonBody(req);
1444
+ const payload = await readOtlpPayload(req, "traces");
1198
1445
  const traces = parseOtlpTraces(payload);
1199
1446
  devtools.addTraces(traces);
1200
1447
  sendJson(res, 200, { acceptedTraces: traces.length });
1201
1448
  } catch (e) {
1202
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1449
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
1203
1450
  }
1204
1451
  return;
1205
1452
  }
1206
1453
  if (req.method === "POST" && url === "/v1/logs") {
1207
1454
  try {
1208
- const payload = await readJsonBody(req);
1455
+ const payload = await readOtlpPayload(req, "logs");
1209
1456
  const logs = parseOtlpLogs(payload);
1210
1457
  devtools.addLogs(logs);
1211
1458
  sendJson(res, 200, { acceptedLogs: logs.length });
1212
1459
  } catch (e) {
1213
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1460
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
1214
1461
  }
1215
1462
  return;
1216
1463
  }
1217
1464
  if (req.method === "POST" && url === "/v1/metrics") {
1218
1465
  try {
1219
- const payload = await readJsonBody(req);
1466
+ const payload = await readOtlpPayload(req, "metrics");
1220
1467
  const count = countOtlpMetrics(payload);
1221
1468
  sendJson(res, 200, { acceptedMetrics: count });
1222
1469
  } catch (e) {
1223
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1470
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
1224
1471
  }
1225
1472
  return;
1226
1473
  }
@@ -1243,6 +1490,10 @@ exports.appendWithLimit = appendWithLimit;
1243
1490
  exports.applyTelemetryLimits = applyTelemetryLimits;
1244
1491
  exports.attachDevtoolsRoutes = attachDevtoolsRoutes;
1245
1492
  exports.createDevtoolsHttpServer = createDevtoolsHttpServer;
1493
+ exports.decodeOtlpLogsRequest = decodeOtlpLogsRequest;
1494
+ exports.decodeOtlpMetricsRequest = decodeOtlpMetricsRequest;
1495
+ exports.decodeOtlpTraceRequest = decodeOtlpTraceRequest;
1496
+ exports.isProtobufContentType = isProtobufContentType;
1246
1497
  exports.parseOtlpLogs = parseOtlpLogs;
1247
1498
  exports.parseOtlpTraces = parseOtlpTraces;
1248
1499
  exports.resolveTelemetryLimits = resolveTelemetryLimits;