Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Duplicate middleware name 'NewRelicSqsMiddleware' when using v10 with the AWS SQS client in an ESM project #1688

Closed
barryhagan opened this issue Jun 22, 2023 · 5 comments

Comments

@barryhagan
Copy link

barryhagan commented Jun 22, 2023

Description

When running an ESM node project with the aws-sdk v3 SQS client, it is not possible to use v10 of the new relic agent. The agent causes a crash when the SQSClient is constructed if @aws-sdk/client-sqs is imported in more than one source file.

Error: Duplicate middleware name 'NewRelicSqsMiddleware'
    at Object.add (/deploy/common/temp/node_modules/.pnpm/@[email protected]/node_modules/@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js:130:31)
    at Object.applyToStack (/deploy/common/temp/node_modules/.pnpm/@[email protected][email protected]/node_modules/@newrelic/aws-sdk/lib/v3/sqs.js:39:19)
    at Object.use (/deploy/common/temp/node_modules/.pnpm/@[email protected]/node_modules/@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js:167:20)
    at SQSClient.wrapShim (/deploy/common/temp/node_modules/.pnpm/@[email protected][email protected]/node_modules/@newrelic/aws-sdk/lib/v3/util.js:33:26)
    at SQSClient.wrappedReturn (/deploy/common/temp/node_modules/.pnpm/@[email protected][email protected]/node_modules/@newrelic/aws-sdk/lib/v3/util.js:53:27)
    at Object.constructTrap [as construct] (/deploy/common/temp/node_modules/.pnpm/[email protected]/node_modules/newrelic/lib/shim/shim.js:672:29)
    at Object.constructTrap [as construct] (/deploy/common/temp/node_modules/.pnpm/[email protected]/node_modules/newrelic/lib/shim/shim.js:663:27)
    at new SqsQueueProducer (file:///deploy/libs/queue/lib/src/sqs-queue/producer.js:6:26)
    at createAppServices (file:///deploy/libs/domain-logic/lib/src/domain-logic/app-services.js:141:29)
    at appStart (file:///deploy/apps/api/lib/src/index.js:27:31)

This is happening with the latest v5.0.5 version of @newrelic/aws-sdk, which addresses a similar issue with duplicate middleware registrations.

Expected Behavior

The agent properly instruments @aws-sdk/client-sqs without attempting to add multiple middlewares.

Troubleshooting or NR Diag results

This only occurs if you have imported @aws-sdk/client-sqs in more than one source file and are using the ESM loader.

Steps to Reproduce

https://github.com/barryhagan/newrelic-sqs-repro

Your Environment

Node 18 ESM project
Newrelic Agent v10.x
@aws-sdk/client-sqs v3.357.0

Additional context

Rolling back to v9.15.0 works, but we are stuck on that version now.

@workato-integration
Copy link

@RobinWijnant
Copy link

RobinWijnant commented Jun 26, 2023

We are having the same issue when using the AWS lambda layer.

Environment

  • AWS Lamba
  • NodeJS v14.x
  • New Relic AWS Lambda layer 107/v10.3.1 (the error starts at version 101 v10.0.0)
  • Relevant dependencies
    • "@aws-sdk/client-s3": "3.293.0",
    • "@aws-sdk/client-secrets-manager": "3.56.0",
{
    "errorType": "Runtime.UnhandledPromiseRejection",
    "errorMessage": "Error: Duplicate middleware name 'NewRelicHeader'",
    "reason": {
        "errorType": "Error",
        "errorMessage": "Duplicate middleware name 'NewRelicHeader'",
        "stack": [
            "Error: Duplicate middleware name 'NewRelicHeader'",
            "    at Object.add (/var/task/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js:130:31)",
            "    at Object.applyToStack (/opt/nodejs/node_modules/@newrelic/aws-sdk/lib/v3/smithy-client.js:45:19)",
            "    at Object.use (/var/task/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js:167:20)",
            "    at S3Client.wrapShim (/opt/nodejs/node_modules/@newrelic/aws-sdk/lib/v3/util.js:33:26)",
            "    at new WrappedClass (/opt/nodejs/node_modules/newrelic/lib/shim/shim.js:2064:30)",
            "    at new S3Client (/var/task/node_modules/@aws-sdk/client-s3/dist-cjs/S3Client.js:31:9)",
            "    at Object.<anonymous> (/var/task/dist/handleEvent/firmware.js:12:18)",
            "    at Module._compile (internal/modules/cjs/loader.js:1114:14)",
            "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)",
            "    at Module.load (internal/modules/cjs/loader.js:979:32)"
        ]
    },
    "promise": {},
    "stack": [
        "Runtime.UnhandledPromiseRejection: Error: Duplicate middleware name 'NewRelicHeader'",
        "    at process.<anonymous> (/var/runtime/index.js:35:15)",
        "    at process.emit (events.js:400:28)",
        "    at process.wrappedEmit [as emit] (/opt/nodejs/node_modules/newrelic/lib/instrumentation/core/globals.js:54:23)",
        "    at processPromiseRejections (internal/process/promises.js:245:33)",
        "    at processTicksAndRejections (internal/process/task_queues.js:96:32)"
    ]
}

@mikeshot
Copy link

mikeshot commented Jun 27, 2023

We are having the same issue when using serverless-newrelic-lambda-layers

Environment

  • AWS Lamba
  • NodeJS v18.x
  • AWS Lambda layer arn:aws:lambda:eu-west-1:451483290750:layer:NewRelicNodeJS18X:29 (the error starts at version NewRelicNodeJS18X:28)
  • Relevant dependencies
    • "@aws-sdk/client-dynamodb": "^3.358.0",
    • "@aws-sdk/client-lambda": "^3.358.0",
    • "@aws-sdk/client-rds-data": "^3.358.0",
    • "@aws-sdk/client-ssm": "^3.358.0",
    • "@aws-sdk/lib-dynamodb": "^3.358.0",

Error

{
  "timestamp": "2023-06-26T15:10:37.505Z",
  "level": "error",
  "message": "[RdsDataService] ERROR: Duplicate middleware name 'NewRelicHeader'",
  "data": "Error: Duplicate middleware name 'NewRelicHeader'
      at Object.add (/var/runtime/node_modules/@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js:130:31)
      at Object.applyToStack (/opt/nodejs/node_modules/@newrelic/aws-sdk/lib/v3/smithy-client.js:45:19)
      at Object.use (/var/runtime/node_modules/@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js:167:20)
      at RDSData.wrapShim (/opt/nodejs/node_modules/@newrelic/aws-sdk/lib/v3/util.js:33:26)
      at new WrappedClass (/opt/nodejs/node_modules/newrelic/lib/shim/shim.js:2085:30)
      at new RDSDataClient (/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/RDSDataClient.js:23:9)
      at new RDSData (/var/runtime/node_modules/@aws-sdk/client-rds-data/dist-cjs/RDSData.js:11:1)"
}

Workaround
As a workaround, we used arn:aws:lambda:eu-west-1:451483290750:layer:NewRelicNodeJS18X:27 and is working fine.

@nthornton2010
Copy link

We've also forced ourselves to version 27 as the later versions are broken (we tested version 32, 31 and 30). 31 and 32 were actually broken due to this - newrelic/serverless-newrelic-lambda-layers#358 so I don't know if the Middleware issue is also present is in versions higher than 30.

@barryhagan
Copy link
Author

This appears to be resolved as of 10.5.0 because of this #1729

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

No branches or pull requests

5 participants