@neat.is/core 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -337,12 +337,15 @@ function parseOtlpRequest(body) {
337
337
  const out = [];
338
338
  for (const rs of body.resourceSpans ?? []) {
339
339
  const resourceAttrs = attrsToRecord(rs.resource?.attributes);
340
- const service = typeof resourceAttrs["service.name"] === "string" ? resourceAttrs["service.name"] : "unknown";
340
+ const rawServiceName = resourceAttrs["service.name"];
341
+ const resourceServiceNamePresent = typeof rawServiceName === "string" && rawServiceName.length > 0;
342
+ const service = resourceServiceNamePresent ? rawServiceName : "unidentified";
341
343
  for (const ss of rs.scopeSpans ?? []) {
342
344
  for (const span of ss.spans ?? []) {
343
345
  const attrs = attrsToRecord(span.attributes);
344
346
  const parsed = {
345
347
  service,
348
+ resourceServiceNamePresent,
346
349
  traceId: span.traceId ?? "",
347
350
  spanId: span.spanId ?? "",
348
351
  parentSpanId: span.parentSpanId || void 0,
@@ -437,6 +440,64 @@ async function buildOtelReceiver(opts) {
437
440
  for (const s of spans) queue.push(s);
438
441
  drainPromise = drainPromise.then(() => drain());
439
442
  };
443
+ const projectQueue = [];
444
+ let projectDraining = false;
445
+ let projectDrainPromise = Promise.resolve();
446
+ const drainProject = async () => {
447
+ if (projectDraining) return;
448
+ projectDraining = true;
449
+ try {
450
+ while (projectQueue.length > 0) {
451
+ const { project, span } = projectQueue.shift();
452
+ try {
453
+ if (opts.onProjectSpan) {
454
+ await opts.onProjectSpan(project, span);
455
+ } else {
456
+ await opts.onSpan(span);
457
+ }
458
+ } catch (err) {
459
+ console.warn(`[neat] otel handler error: ${err.message}`);
460
+ }
461
+ }
462
+ } finally {
463
+ projectDraining = false;
464
+ }
465
+ };
466
+ const enqueueProject = (project, spans) => {
467
+ if (spans.length === 0) return;
468
+ for (const s of spans) projectQueue.push({ project, span: s });
469
+ projectDrainPromise = projectDrainPromise.then(() => drainProject());
470
+ };
471
+ const legacyEndpointWarned = /* @__PURE__ */ new Set();
472
+ function warnLegacyEndpoint(serviceName) {
473
+ if (legacyEndpointWarned.has(serviceName)) return;
474
+ legacyEndpointWarned.add(serviceName);
475
+ console.warn(
476
+ `[neatd] received span on the global endpoint; migrate OTEL_EXPORTER_OTLP_TRACES_ENDPOINT to /projects/<name>/v1/traces (service.name="${serviceName}").`
477
+ );
478
+ }
479
+ async function readOtlpBody(req) {
480
+ const ct = (req.headers["content-type"] ?? "").toString().split(";")[0].trim().toLowerCase();
481
+ if (ct === "application/x-protobuf") {
482
+ try {
483
+ const body = await decodeProtobufBody(req.body);
484
+ return { ok: true, body, flavor: "protobuf" };
485
+ } catch (err) {
486
+ return { ok: false, code: 400, error: `protobuf decode failed: ${err.message}` };
487
+ }
488
+ }
489
+ if (!ct || ct === "application/json") {
490
+ return { ok: true, body: req.body ?? {}, flavor: "json" };
491
+ }
492
+ return { ok: false, code: 415, error: `unsupported content-type: ${ct}` };
493
+ }
494
+ function sendOtlpSuccess(reply, flavor) {
495
+ if (flavor === "protobuf") {
496
+ const buf = encodeProtobufResponseBody();
497
+ return reply.code(200).header("content-type", "application/x-protobuf").send(buf);
498
+ }
499
+ return reply.code(200).header("content-type", "application/json").send({ partialSuccess: {} });
500
+ }
440
501
  app.addContentTypeParser(
441
502
  "application/x-protobuf",
442
503
  { parseAs: "buffer", bodyLimit: opts.bodyLimit ?? 16 * 1024 * 1024 },
@@ -446,26 +507,44 @@ async function buildOtelReceiver(opts) {
446
507
  );
447
508
  app.get("/health", async () => ({ ok: true }));
448
509
  app.post("/v1/traces", async (req, reply) => {
449
- const ct = (req.headers["content-type"] ?? "").toString().split(";")[0].trim().toLowerCase();
450
- let body;
451
- let responseFlavor;
452
- if (ct === "application/x-protobuf") {
453
- responseFlavor = "protobuf";
510
+ const result = await readOtlpBody(req);
511
+ if (!result.ok) {
512
+ return reply.code(result.code).send({ error: result.error });
513
+ }
514
+ const spans = parseOtlpRequest(result.body);
515
+ for (const s of spans) warnLegacyEndpoint(s.service);
516
+ if (opts.onErrorSpanSync) {
454
517
  try {
455
- body = await decodeProtobufBody(req.body);
518
+ for (const span of spans) {
519
+ if (span.statusCode === 2) await opts.onErrorSpanSync(span);
520
+ }
456
521
  } catch (err) {
457
- return reply.code(400).send({
458
- error: `protobuf decode failed: ${err.message}`
522
+ return reply.code(500).send({
523
+ error: `error-event write failed: ${err.message}`
459
524
  });
460
525
  }
461
- } else if (!ct || ct === "application/json") {
462
- responseFlavor = "json";
463
- body = req.body ?? {};
464
- } else {
465
- return reply.code(415).send({ error: `unsupported content-type: ${ct}` });
466
526
  }
467
- const spans = parseOtlpRequest(body);
468
- if (opts.onErrorSpanSync) {
527
+ enqueue(spans);
528
+ return sendOtlpSuccess(reply, result.flavor);
529
+ });
530
+ app.post("/projects/:project/v1/traces", async (req, reply) => {
531
+ const project = req.params.project;
532
+ const result = await readOtlpBody(req);
533
+ if (!result.ok) {
534
+ return reply.code(result.code).send({ error: result.error });
535
+ }
536
+ const spans = parseOtlpRequest(result.body);
537
+ if (opts.onProjectErrorSpanSync) {
538
+ try {
539
+ for (const span of spans) {
540
+ if (span.statusCode === 2) await opts.onProjectErrorSpanSync(project, span);
541
+ }
542
+ } catch (err) {
543
+ return reply.code(500).send({
544
+ error: `error-event write failed: ${err.message}`
545
+ });
546
+ }
547
+ } else if (opts.onErrorSpanSync) {
469
548
  try {
470
549
  for (const span of spans) {
471
550
  if (span.statusCode === 2) await opts.onErrorSpanSync(span);
@@ -476,17 +555,13 @@ async function buildOtelReceiver(opts) {
476
555
  });
477
556
  }
478
557
  }
479
- enqueue(spans);
480
- if (responseFlavor === "protobuf") {
481
- const buf = encodeProtobufResponseBody();
482
- return reply.code(200).header("content-type", "application/x-protobuf").send(buf);
483
- }
484
- return reply.code(200).header("content-type", "application/json").send({ partialSuccess: {} });
558
+ enqueueProject(project, spans);
559
+ return sendOtlpSuccess(reply, result.flavor);
485
560
  });
486
561
  const decorated = app;
487
562
  decorated.flushPending = async () => {
488
- while (queue.length > 0 || draining) {
489
- await drainPromise;
563
+ while (queue.length > 0 || draining || projectQueue.length > 0 || projectDraining) {
564
+ await Promise.all([drainPromise, projectDrainPromise]);
490
565
  }
491
566
  };
492
567
  return decorated;
@@ -1572,6 +1647,14 @@ function thresholdForEdgeType(edgeType, overrides) {
1572
1647
  function nowIso(ctx) {
1573
1648
  return new Date(ctx.now ? ctx.now() : Date.now()).toISOString();
1574
1649
  }
1650
+ var unidentifiedWarnedProjects = /* @__PURE__ */ new Set();
1651
+ function warnUnidentifiedSpan(project) {
1652
+ if (unidentifiedWarnedProjects.has(project)) return;
1653
+ unidentifiedWarnedProjects.add(project);
1654
+ console.warn(
1655
+ `[neatd] span lacked service.name; routed to 'unidentified' in project ${project}; check your OTel SDK config.`
1656
+ );
1657
+ }
1575
1658
  function pickAttr(span, ...keys) {
1576
1659
  for (const k of keys) {
1577
1660
  const v = span.attributes[k];
@@ -1826,6 +1909,9 @@ async function handleSpan(ctx, span) {
1826
1909
  const ts = span.startTimeIso ?? nowIso(ctx);
1827
1910
  const nowMs = ctx.now ? ctx.now() : Date.now();
1828
1911
  const env = span.env ?? "unknown";
1912
+ if (span.resourceServiceNamePresent === false) {
1913
+ warnUnidentifiedSpan(ctx.project ?? DEFAULT_PROJECT);
1914
+ }
1829
1915
  const sourceId = ensureServiceNode(ctx.graph, span.service, env);
1830
1916
  const isError = span.statusCode === 2;
1831
1917
  cacheSpanService(span, nowMs);
@@ -5884,6 +5970,25 @@ async function startDaemon(opts = {}) {
5884
5970
  }
5885
5971
  return slot.status === "active" ? slot : null;
5886
5972
  }
5973
+ async function resolveSlotByName(project, serviceName, traceId) {
5974
+ const liveEntries = await listProjects().catch(() => []);
5975
+ let slot = slots.get(project);
5976
+ if (!slot) {
5977
+ await recordUnroutedSpan(serviceName, traceId);
5978
+ return null;
5979
+ }
5980
+ if (slot.status === "broken") {
5981
+ const entry = liveEntries.find((e) => e.name === slot.entry.name);
5982
+ if (entry) {
5983
+ slot = await tryRecoverSlot(entry);
5984
+ }
5985
+ if (slot.status !== "active") {
5986
+ warnDroppedSpan(slot.entry.name, slot.errorReason ?? "unknown");
5987
+ return null;
5988
+ }
5989
+ }
5990
+ return slot.status === "active" ? slot : null;
5991
+ }
5887
5992
  try {
5888
5993
  otlpApp = await buildOtelReceiver({
5889
5994
  authToken: auth.otelToken,
@@ -5906,6 +6011,28 @@ async function startDaemon(opts = {}) {
5906
6011
  const slot = await resolveTargetSlot(span.service, span.traceId);
5907
6012
  if (!slot) return;
5908
6013
  await makeErrorSpanWriter(slot.paths.errorsPath)(span);
6014
+ },
6015
+ // Project-scoped route (issue #367) — the URL already named the
6016
+ // project. Resolution is a direct slot lookup; service.name resolves
6017
+ // the ServiceNode inside the slot's graph instead of which project
6018
+ // owns the span.
6019
+ onProjectSpan: async (project, span) => {
6020
+ const slot = await resolveSlotByName(project, span.service, span.traceId);
6021
+ if (!slot) return;
6022
+ await handleSpan(
6023
+ {
6024
+ graph: slot.graph,
6025
+ errorsPath: slot.paths.errorsPath,
6026
+ project: slot.entry.name,
6027
+ writeErrorEventInline: false
6028
+ },
6029
+ span
6030
+ );
6031
+ },
6032
+ onProjectErrorSpanSync: async (project, span) => {
6033
+ const slot = await resolveSlotByName(project, span.service, span.traceId);
6034
+ if (!slot) return;
6035
+ await makeErrorSpanWriter(slot.paths.errorsPath)(span);
5909
6036
  }
5910
6037
  });
5911
6038
  otlpAddress = await otlpApp.listen({ port: otlpPort, host });