Skip to content

startNewTrace does not use same trace id for spans in callback #19952

Description

@mattjennings

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

10.45.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

https://codesandbox.io/p/devbox/vigilant-rain-9f43ql?file=%2Findex.mjs

import crypto from "node:crypto";
import * as Sentry from "@sentry/node";

Sentry.init({
  tracesSampleRate: 1.0,
});

// sequential spans inside startNewTrace get different trace IDs
await Sentry.startNewTrace(async () => {
  const span1 = Sentry.startInactiveSpan({ op: "step.one", name: "Step One" });
  const traceId1 = span1.spanContext().traceId;
  span1.end();

  const span2 = Sentry.startInactiveSpan({ op: "step.two", name: "Step Two" });
  const traceId2 = span2.spanContext().traceId;
  span2.end();

  console.log("startNewTrace:");
  console.log("  span1 traceId:", traceId1);
  console.log("  span2 traceId:", traceId2);
  console.log("  same trace:", traceId1 === traceId2, "(expected true)");
});

// Workaround: continueTrace with a self-generated sentryTrace header
await Sentry.continueTrace(
  {
    sentryTrace: `${crypto.randomBytes(16).toString("hex")}-${crypto.randomBytes(8).toString("hex")}-1`,
    baggage: undefined,
  },
  async () => {
    const span1 = Sentry.startInactiveSpan({
      op: "step.one",
      name: "Step One",
    });
    const traceId1 = span1.spanContext().traceId;
    span1.end();

    const span2 = Sentry.startInactiveSpan({
      op: "step.two",
      name: "Step Two",
    });
    const traceId2 = span2.spanContext().traceId;
    span2.end();

    console.log("\ncontinueTrace (workaround):");
    console.log("  span1 traceId:", traceId1);
    console.log("  span2 traceId:", traceId2);
    console.log("  same trace:", traceId1 === traceId2, "(expected true)");
  },
);

await Sentry.close();

Steps to Reproduce

Run the provided js snippet in node with @sentry/node@10.45.0

Expected Result

All spans in the startNewTrace callback should share the same trace

Actual Result

Each span is assigned a new trace

Additional Context

I encountered this while trying to group a series of spans under a manually created trace. Used Claude to help debug and create this repro, the workaround is something I came up with that seemed to work but felt unintended.

Happy to provide any other information if needed.

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions