Skip to content

Sentry AWS lambda distributed tracing does not work #18073

@anthonyma94

Description

@anthonyma94

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/aws-serverless

SDK Version

10.22.0

Framework Version

@sentry/react 10.22.0

Link to Sentry event

API: https://jervis-webb-company.sentry.io/issues/6987688505/events/2bec74dd8d694a35a0dbebb4152e627e/, client: https://jervis-webb-company.sentry.io/issues/6987688558/events/565d36470aca4183802799c63bf41426/

Reproduction Example/SDK Setup

Lambda code hosts a TRPC server, is bundled via esbuild and deployed behind API Gateway and routed via a Cloudfront function to have the same origin as frontend.

Lambda init

// Creates TRPC context
const createContext = async ({ event, context }: CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>) => {
  ...
};

Sentry.init({
    enabled: true,
    dsn: process.env.SENTRY_DSN,
    sendDefaultPii: true,

    sampleRate: 1.0,
    tracesSampleRate: 1.0,
});

// TRPC lambda adapter
const inner = awsLambdaRequestHandler({
    router: appRouter,
    createContext,
    onError: (opts) => {
        const { error } = opts;
        logger.error(error, "TRPC error");
        if (error.code === "INTERNAL_SERVER_ERROR") {
            throw error;
        }
    },
});

export const handler = Sentry.wrapHandler(inner);

Frontend init

Sentry.init({
  dsn: import.meta.env.VITE_SENTRY_DSN,
  sendDefaultPii: true,
  integrations: [
    Sentry.replayIntegration({
      blockAllMedia: false,
      maskAllText: false,
    }),
    Sentry.tanstackRouterBrowserTracingIntegration(router),
  ],
  tracesSampleRate: 1.0,
  replaysSessionSampleRate: 1.0,
  replaysOnErrorSampleRate: 1.0,
});

Steps to Reproduce

  1. Deploy a lambda TRPC function behind API gateway and Cloudfront function for routing.
  2. Create a client that calls a TRPC route that errors out.

Expected Result

Distributed tracing should work out of the box.

Actual Result

Distributed tracing doesn't work.

You can see in the Sentry issues linked above, that the server event receives the necessary headers (entire event logged as context), and that it matches the trace ID sent from client. However, the API issue creates a new trace ID anyways. Notably, the url tag is also empty, but I don't know if this is because it's not something in the Lambda event.

Additional Context

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions