Skip to content

Server dependency bundling breaks AWS OpenTelemetry instrumentation #88161

@TrevorBurnham

Description

@TrevorBurnham

Link to the code that reproduces this issue

https://github.com/TrevorBurnham/nextjs-aws-opentelemetry

To Reproduce

  1. Create an AWS profile if you don't already have one
  2. Run the app with AWS_PROFILE=<your profile> npm run dev
  3. Visit http://localhost:3000/api/test
  4. 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/A

Which 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    InstrumentationRelated to Next.js Instrumentation.TurbopackRelated to Turbopack with Next.js.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions