sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Using Express on Lambda fails to add tracing data into Sentry

Open joshhornby opened this issue 2 months ago • 9 comments

Is there an existing issue for this?

  • [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [x] I have reviewed the documentation https://docs.sentry.io/
  • [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/aws-serverless

SDK Version

10.22.0

Framework Version

Express 4.21.1

Link to Sentry event

No response

Reproduction Example/SDK Setup

Running an Express application on AWS Lambda using codegenie/serverless-express

This is being deployed as a CJS application

import * as Sentry from '@sentry/aws-serverless';

Sentry.init({
  debug: true,
  dsn: process.env.SENTRY_DSN,
  environment: 'staging',
  integrations: [
    Sentry.expressIntegration()
  ],
  tracesSampleRate: 1.0,
});

import express from 'express';
import serverlessExpress from '@codegenie/serverless-express';

const app = express();
app.get('/debug-sentry', (_req, res) => res.status(200).send('OK'));

app.use(Sentry.expressErrorHandler());

const serverlessExpressInstance = serverlessExpress({ app });

export const handler = Sentry.wrapHandler((event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  return serverlessExpressInstance(event, context, callback);
});

Steps to Reproduce

Deploying the above on a Lambda produces the following debug logs when sending a GET request to the route:

2025-11-07T20:16:18.262+00:00
INIT_START Runtime Version: nodejs:22.v59 Runtime Version ARN: arn:aws:lambda:eu-west-1::runtime:9ff8926a0b8f846a2bd170efff02668e0f0f00b647d56d4fdc31c22b5cef37a8
2025-11-07T20:16:18.571+00:00
2025-11-07T20:16:18.571Z undefined INFO Sentry Logger [log]: Initializing Sentry: process: 2, thread: main.
2025-11-07T20:16:18.586+00:00
2025-11-07T20:16:18.586Z undefined INFO Sentry Logger [log]: Integration installed: InboundFilters
2025-11-07T20:16:18.587+00:00
2025-11-07T20:16:18.587Z undefined INFO Sentry Logger [log]: Integration installed: FunctionToString
2025-11-07T20:16:18.587+00:00
2025-11-07T20:16:18.587Z undefined INFO Sentry Logger [log]: Integration installed: LinkedErrors
2025-11-07T20:16:18.587+00:00
2025-11-07T20:16:18.587Z undefined INFO Sentry Logger [log]: Integration installed: RequestData
2025-11-07T20:16:18.588+00:00
2025-11-07T20:16:18.588Z undefined INFO Sentry Logger [log]: Integration installed: NodeSystemError
2025-11-07T20:16:18.588+00:00
2025-11-07T20:16:18.588Z undefined INFO Sentry Logger [log]: Integration installed: Console
2025-11-07T20:16:18.588+00:00
2025-11-07T20:16:18.588Z undefined INFO Sentry Logger [log]: Integration installed: OnUncaughtException
2025-11-07T20:16:18.588+00:00
2025-11-07T20:16:18.588Z undefined INFO Sentry Logger [log]: Integration installed: OnUnhandledRejection
2025-11-07T20:16:18.589+00:00
2025-11-07T20:16:18.589Z undefined INFO Sentry Logger [log]: Integration installed: ContextLines
2025-11-07T20:16:18.589+00:00
2025-11-07T20:16:18.589Z undefined INFO Sentry Logger [log]: Integration installed: LocalVariablesAsync
2025-11-07T20:16:18.589+00:00
2025-11-07T20:16:18.589Z undefined INFO Sentry Logger [log]: Integration installed: Context
2025-11-07T20:16:18.589+00:00
2025-11-07T20:16:18.589Z undefined INFO Sentry Logger [log]: Integration installed: ChildProcess
2025-11-07T20:16:18.590+00:00
2025-11-07T20:16:18.590Z undefined INFO Sentry Logger [log]: Integration installed: ProcessSession
2025-11-07T20:16:18.590+00:00
2025-11-07T20:16:18.590Z undefined INFO Sentry Logger [log]: Integration installed: Modules
2025-11-07T20:16:18.593+00:00
2025-11-07T20:16:18.593Z undefined INFO Sentry Logger [log]: Integration installed: Http
2025-11-07T20:16:18.594+00:00
2025-11-07T20:16:18.594Z undefined INFO Sentry Logger [log]: Integration installed: NodeFetch
2025-11-07T20:16:18.598+00:00
2025-11-07T20:16:18.598Z undefined INFO Sentry Logger [log]: Integration installed: Aws
2025-11-07T20:16:18.600+00:00
2025-11-07T20:16:18.600Z undefined INFO Sentry Logger [log]: Integration installed: AwsLambda
2025-11-07T20:16:18.600+00:00
2025-11-07T20:16:18.600Z undefined INFO Sentry Logger [log]: Integration installed: Express
2025-11-07T20:16:18.601+00:00
2025-11-07T20:16:18.601Z undefined INFO Sentry Logger [log]: SDK initialized from CommonJS
2025-11-07T20:16:18.601+00:00
2025-11-07T20:16:18.601Z undefined INFO Sentry Logger [log]: @opentelemetry/api: Registered a global for diag v1.9.0.
2025-11-07T20:16:18.603+00:00
2025-11-07T20:16:18.603Z undefined INFO Sentry Logger [log]: @opentelemetry/api: Registered a global for trace v1.9.0.
2025-11-07T20:16:18.604+00:00
2025-11-07T20:16:18.604Z undefined INFO Sentry Logger [log]: @opentelemetry/api: Registered a global for propagation v1.9.0.
2025-11-07T20:16:18.604+00:00
2025-11-07T20:16:18.604Z undefined INFO Sentry Logger [log]: @opentelemetry/api: Registered a global for context v1.9.0.
2025-11-07T20:16:18.612+00:00
START RequestId: 18dcc334-9cf5-4ea9-9147-89820d5cf69b Version: $LATEST
2025-11-07T20:16:18.625+00:00
2025-11-07T20:16:18.625Z 18dcc334-9cf5-4ea9-9147-89820d5cf69b INFO Sentry Logger [log]: SpanExporter exported 0 spans, 0 spans are waiting for their parent spans to finish
2025-11-07T20:16:18.625+00:00
2025-11-07T20:16:18.625Z 18dcc334-9cf5-4ea9-9147-89820d5cf69b INFO Sentry Logger [log]: Flushing outcomes...
2025-11-07T20:16:18.625+00:00
2025-11-07T20:16:18.625Z 18dcc334-9cf5-4ea9-9147-89820d5cf69b INFO Sentry Logger [log]: No outcomes to send
2025-11-07T20:16:18.630+00:00
END RequestId: 18dcc334-9cf5-4ea9-9147-89820d5cf69b
2025-11-07T20:16:18.630+00:00
REPORT RequestId: 18dcc334-9cf5-4ea9-9147-89820d5cf69b Duration: 17.34 ms Billed Duration: 365 ms Memory Size: 1024 MB Max Memory Used: 108 MB Init Duration: 346.86 ms

Expected Result

Traces to show in Sentry UI

Actual Result

Nothing can be seen in backend traces in Sentry UI. This is confirmed by the debug logs showing SpanExporter exported 0 spans, 0 spans are waiting for their parent spans to finish

I've successfully been able to see an error arrive in Sentry with the above setup.

Additional Context

This feels related to this comment https://github.com/getsentry/sentry-javascript/issues/13871#issuecomment-2694869834

joshhornby avatar Nov 07 '25 20:11 joshhornby

JS-1127

linear[bot] avatar Nov 07 '25 20:11 linear[bot]

Hi @joshhornby, thanks for writing in.

Could you check if express is bundled into your app bundle? If so, you'll need to mark express external in your build process. If you provide a reproduction repo I can take a closer look.

andreiborza avatar Nov 10 '25 11:11 andreiborza

Hi @andreiborza

Thanks for the response.

See here for a reproduction repo - https://github.com/joshhornby/sentry-express-bug

joshhornby avatar Nov 10 '25 20:11 joshhornby

@joshhornby From your repro it looks like express is being bundled, along with the instrumentation logic. In order for the instrumentation to work you should make sure instrumented libraries (e.g: express) are not bundled.

You may want to follow the docs for setting the NODE_OPTIONS or by setting up the instrument.js|ts file manually.

logaretm avatar Nov 11 '25 10:11 logaretm

@logaretm Can you confirm what you mean by express should not be bundled?

If I update the CDK to

bundling: {
        target: 'es2022',
        sourceMap: true,
        externalModules: [
          'express',
        ],
      },

I get the following error Error: Cannot find module 'express'

joshhornby avatar Nov 11 '25 12:11 joshhornby

@joshhornby you need to ensure express is available to your application, most likely by deploying it in a lambda layer alongside your application. I think you can define a lambda layer that has express and all its dependencies installed, but it's been a while since I looked at CDK.

andreiborza avatar Nov 11 '25 12:11 andreiborza

@andreiborza @logaretm So good news, I've managed to get this logging to Sentry - see this commit https://github.com/joshhornby/sentry-express-bug/commit/7b2870795337dfb8ea56118f52d67b64d074a182

Although all traces are coming through on a span with the name of the Lambda, eg SentryExpressLambdaStack-SentryExpressLambdaD912B7-hiXcXrPJKVn5. In other words it's not tracing based on the express route.

If I was to wrap in a newTrace and manually span I lose the Express waterfall tracing.

Example logs ``` 2025-11-11T14:37:12.862+00:00 INIT_START Runtime Version: nodejs:22.v65 Runtime Version ARN: arn:aws:lambda:eu-west-1::runtime:32854065d48ca9c5e6b3046b96919e02f1be0a9623e600f07b963bb7f531b7d5 2025-11-11T14:37:13.645+00:00 Sentry Logger [log]: Initializing Sentry: process: 2, thread: main. 2025-11-11T14:37:13.647+00:00 Sentry Logger [log]: Integration installed: InboundFilters 2025-11-11T14:37:13.647+00:00 Sentry Logger [log]: Integration installed: FunctionToString 2025-11-11T14:37:13.647+00:00 Sentry Logger [log]: Integration installed: LinkedErrors 2025-11-11T14:37:13.647+00:00 Sentry Logger [log]: Integration installed: RequestData 2025-11-11T14:37:13.647+00:00 Sentry Logger [log]: Integration installed: NodeSystemError 2025-11-11T14:37:13.648+00:00 Sentry Logger [log]: Integration installed: Console 2025-11-11T14:37:13.648+00:00 Sentry Logger [log]: Integration installed: OnUncaughtException 2025-11-11T14:37:13.648+00:00 Sentry Logger [log]: Integration installed: OnUnhandledRejection 2025-11-11T14:37:13.649+00:00 Sentry Logger [log]: Integration installed: ContextLines 2025-11-11T14:37:13.649+00:00 Sentry Logger [log]: Integration installed: LocalVariablesAsync 2025-11-11T14:37:13.649+00:00 Sentry Logger [log]: Integration installed: Context 2025-11-11T14:37:13.649+00:00 Sentry Logger [log]: Integration installed: ChildProcess 2025-11-11T14:37:13.650+00:00 Sentry Logger [log]: Integration installed: ProcessSession 2025-11-11T14:37:13.650+00:00 Sentry Logger [log]: Integration installed: Modules 2025-11-11T14:37:13.653+00:00 Sentry Logger [log]: Integration installed: Http 2025-11-11T14:37:13.654+00:00 Sentry Logger [log]: Integration installed: NodeFetch 2025-11-11T14:37:13.660+00:00 Sentry Logger [log]: Integration installed: Aws 2025-11-11T14:37:13.662+00:00 Sentry Logger [log]: Integration installed: AwsLambda 2025-11-11T14:37:13.663+00:00 Sentry Logger [log]: Integration installed: Express 2025-11-11T14:37:13.664+00:00 Sentry Logger [log]: SDK initialized from CommonJS 2025-11-11T14:37:13.664+00:00 Sentry Logger [log]: @opentelemetry/api: Registered a global for diag v1.9.0. 2025-11-11T14:37:13.666+00:00 Sentry Logger [log]: @opentelemetry/api: Registered a global for trace v1.9.0. 2025-11-11T14:37:13.667+00:00 Sentry Logger [log]: @opentelemetry/api: Registered a global for propagation v1.9.0. 2025-11-11T14:37:13.667+00:00 Sentry Logger [log]: @opentelemetry/api: Registered a global for context v1.9.0. 2025-11-11T14:37:13.676+00:00 Sentry Logger [log]: @sentry/instrumentation-http Applying instrumentation patch for nodejs core module on require hook { module: 'http' } 2025-11-11T14:37:13.676+00:00 Sentry Logger [log]: @opentelemetry_sentry-patched/instrumentation-http Applying instrumentation patch for nodejs core module on require hook { module: 'http' } 2025-11-11T14:37:13.891+00:00 Sentry Logger [log]: @opentelemetry/instrumentation-express Applying instrumentation patch for module on require hook { 2025-11-11T14:37:13.891+00:00 module: 'express', 2025-11-11T14:37:13.891+00:00 version: '4.21.2', 2025-11-11T14:37:13.891+00:00 baseDir: '/var/task/node_modules/express' 2025-11-11T14:37:13.891+00:00 } 2025-11-11T14:37:13.912+00:00 Sentry Logger [log]: @sentry/instrumentation-aws-lambda Applying instrumentation patch for nodejs module file on require hook { 2025-11-11T14:37:13.912+00:00 module: '/var/task/index.js', 2025-11-11T14:37:13.912+00:00 version: undefined, 2025-11-11T14:37:13.912+00:00 fileName: 'index', 2025-11-11T14:37:13.912+00:00 baseDir: '/var/task' 2025-11-11T14:37:13.912+00:00 } 2025-11-11T14:37:13.912+00:00 Sentry Logger [log]: patch handler function 2025-11-11T14:37:13.917+00:00 START RequestId: c6faa7a1-d313-4dda-b95c-c63f4e8c7805 Version: $LATEST 2025-11-11T14:37:13.924+00:00 Sentry Logger [log]: [Tracing] Starting sampled root span 2025-11-11T14:37:13.924+00:00 op: 2025-11-11T14:37:13.924+00:00 name: SentryExpressLambdaStack-SentryExpressLambdaD912B7-hiXcXrPJKVn5 2025-11-11T14:37:13.924+00:00 ID: ece94c3a9163f7b1 2025-11-11T14:37:13.941+00:00 Sentry Logger [warn]: Isolation scope is still default isolation scope - skipping setting transactionName 2025-11-11T14:37:13.942+00:00 Sentry Logger [log]: [Tracing] Inheriting parent's sampled decision for middleware - query: true 2025-11-11T14:37:13.942+00:00 Sentry Logger [log]: [Tracing] Starting sampled span 2025-11-11T14:37:13.942+00:00 op: 2025-11-11T14:37:13.942+00:00 name: middleware - query 2025-11-11T14:37:13.942+00:00 ID: ac229af18bf8b041 2025-11-11T14:37:13.942+00:00 parent ID: ece94c3a9163f7b1 2025-11-11T14:37:13.942+00:00 root ID: ece94c3a9163f7b1 2025-11-11T14:37:13.942+00:00 root op: function.aws.lambda 2025-11-11T14:37:13.942+00:00 root description: SentryExpressLambdaStack-SentryExpressLambdaD912B7-hiXcXrPJKVn5 2025-11-11T14:37:13.943+00:00 Sentry Logger [log]: [Tracing] Finishing "middleware.express" span "query" with ID ac229af18bf8b041 2025-11-11T14:37:13.944+00:00 Sentry Logger [warn]: Isolation scope is still default isolation scope - skipping setting transactionName 2025-11-11T14:37:13.944+00:00 Sentry Logger [log]: [Tracing] Inheriting parent's sampled decision for middleware - expressInit: true 2025-11-11T14:37:13.944+00:00 Sentry Logger [log]: [Tracing] Starting sampled span 2025-11-11T14:37:13.944+00:00 op: 2025-11-11T14:37:13.944+00:00 name: middleware - expressInit 2025-11-11T14:37:13.944+00:00 ID: 1a8770289d44aea6 2025-11-11T14:37:13.944+00:00 parent ID: ece94c3a9163f7b1 2025-11-11T14:37:13.944+00:00 root ID: ece94c3a9163f7b1 2025-11-11T14:37:13.944+00:00 root op: function.aws.lambda 2025-11-11T14:37:13.944+00:00 root description: SentryExpressLambdaStack-SentryExpressLambdaD912B7-hiXcXrPJKVn5 2025-11-11T14:37:13.961+00:00 Sentry Logger [log]: [Tracing] Finishing "middleware.express" span "expressInit" with ID 1a8770289d44aea6 2025-11-11T14:37:13.962+00:00 Sentry Logger [warn]: Isolation scope is still default isolation scope - skipping setting transactionName 2025-11-11T14:37:13.962+00:00 Sentry Logger [log]: [Tracing] Inheriting parent's sampled decision for request handler - /debug-sentry: true 2025-11-11T14:37:13.962+00:00 Sentry Logger [log]: [Tracing] Starting sampled span 2025-11-11T14:37:13.962+00:00 op: 2025-11-11T14:37:13.962+00:00 name: request handler - /debug-sentry 2025-11-11T14:37:13.962+00:00 ID: 09b6371cab4b8cf1 2025-11-11T14:37:13.962+00:00 parent ID: ece94c3a9163f7b1 2025-11-11T14:37:13.962+00:00 root ID: ece94c3a9163f7b1 2025-11-11T14:37:13.962+00:00 root op: function.aws.lambda 2025-11-11T14:37:13.962+00:00 root description: SentryExpressLambdaStack-SentryExpressLambdaD912B7-hiXcXrPJKVn5 2025-11-11T14:37:13.985+00:00 Sentry Logger [log]: SpanExporter exported 0 spans, 2 spans are waiting for their parent spans to finish 2025-11-11T14:37:13.985+00:00 Sentry Logger [log]: Flushing outcomes... 2025-11-11T14:37:13.985+00:00 Sentry Logger [log]: No outcomes to send 2025-11-11T14:37:13.988+00:00 Sentry Logger [log]: [Tracing] Finishing "function.aws.lambda" root span "SentryExpressLambdaStack-SentryExpressLambdaD912B7-hiXcXrPJKVn5" with ID ece94c3a9163f7b1 2025-11-11T14:37:13.988+00:00 Sentry Logger [log]: Spans may not be exported for the lambda function because we are not force flushing before callback. 2025-11-11T14:37:13.988+00:00 Sentry Logger [log]: Metrics may not be exported for the lambda function because we are not force flushing before callback. 2025-11-11T14:37:13.992+00:00 Sentry Logger [log]: SpanExporter exported 3 spans, 0 spans are waiting for their parent spans to finish 2025-11-11T14:37:14.001+00:00 Sentry Logger [log]: Flushing outcomes... 2025-11-11T14:37:14.001+00:00 Sentry Logger [log]: No outcomes to send 2025-11-11T14:37:14.286+00:00 Sentry Logger [log]: @sentry/instrumentation-http Handling finished outgoing request 2025-11-11T14:37:14.291+00:00 END RequestId: c6faa7a1-d313-4dda-b95c-c63f4e8c7805 2025-11-11T14:37:14.291+00:00 REPORT RequestId: c6faa7a1-d313-4dda-b95c-c63f4e8c7805 Duration: 373.01 ms Billed Duration: 1425 ms Memory Size: 512 MB Max Memory Used: 132 MB Init Duration: 1051.84 ms ```

joshhornby avatar Nov 11 '25 14:11 joshhornby

@joshhornby this is a known issue, we haven't found a good way around it yet as the name is set via the aws lambda instrumentation. See https://github.com/getsentry/sentry-javascript/issues/15788

Maybe https://github.com/getsentry/sentry-javascript/issues/15788#issuecomment-2923719562 can help you a bit.

andreiborza avatar Nov 11 '25 15:11 andreiborza

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

getsantry[bot] avatar Dec 04 '25 08:12 getsantry[bot]