-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Description
Link to the code that reproduces this issue
https://github.com/TrevorBurnham/nextjs-aws-opentelemetry
To Reproduce
- Create an AWS profile if you don't already have one
- Run the app with
AWS_PROFILE=<your profile> npm run dev - Visit http://localhost:3000/api/test
- Check the console and wait a few seconds
Current vs. Expected behavior
Several OpenTelemetry spans are emitted to the console, but not the expected one from the AWS instrumentation (look for name: 'STS.GetCallerIdentity').
I investigated and discovered that the problem is the server bundling behavior. The @opentelemetry/instrumentation-aws-sdk package relies on require hooks, which break when the AWS SDK modules are bundled. I found that adding this to the next.config.js fixes the issue:
// Exclude packages used by AWS SDK v3 from server bundling so OpenTelemetry instrumentation can patch them.
serverExternalPackages: ["@smithy/middleware-stack", "@smithy/smithy-client"],
Perhaps those modules should be excluded from bundling by default? I notice that @aws-sdk/client-s3 is already excluded, though for different reasons (#51969).
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:40 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6000
Available memory (MB): 32768
Available CPU cores: 10
Binaries:
Node: 22.17.1
npm: 10.9.2
Yarn: N/A
pnpm: 10.22.0
Relevant Packages:
next: 16.1.1-canary.13 // Latest available version is detected (16.1.1-canary.13).
eslint-config-next: N/A
react: 19.2.3
react-dom: 19.2.3
typescript: 5.9.3
Next.js Config:
output: N/AWhich area(s) are affected? (Select all that apply)
Instrumentation
Which stage(s) are affected? (Select all that apply)
Other (Deployed), Vercel (Deployed), next start (local), next dev (local)
Additional context
I'm running into this issue now because AWS recently started recommending migrating from X-Ray instrumentation to OpenTelemetry. Everyone else who uses Next.js with AWS X-Ray is going to get bitten by this issue as they migrate.