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

feat: add instrumentation for aws-sdk S3 client #3287

Merged
merged 34 commits into from
May 3, 2023

Conversation

david-luna
Copy link
Member

@david-luna david-luna commented Apr 26, 2023

Description

This change-set add instrumentation for @aws-sdk/client-s3 which is available in v3. The approach is not to instrument the S3Client directly but the base Client class so we can add specific middleware functions based on each client without having to instrument them.

Fixes: #2955

Checklist

  • Implement code
  • Add tests
  • Update TypeScript typings
  • Update documentation
  • Add CHANGELOG.asciidoc entry
  • Commit message follows commit guidelines

@david-luna david-luna requested a review from trentm April 26, 2023 15:06
@github-actions github-actions bot added the agent-nodejs Make available for APM Agents project planning. label Apr 26, 2023
@elastic-apm-tech elastic-apm-tech added this to In Progress in APM-Agents (OLD) Apr 26, 2023
Copy link
Member

@trentm trentm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start.

Eventually, another thing to add to the v3 instrumentation is span.setHttpContext(...) as is done in the v2 instrumentation for S3, which adds this to the span:

    {
        "span": {
            // ...
            "context": {
                "http": {
                    "status_code": 200,
                    "response": {
                        "encoded_body_size": 11500
                    }
                }
            },
        }
    }

lib/instrumentation/index.js Outdated Show resolved Hide resolved
lib/instrumentation/modules/aws-sdk-v3.js Outdated Show resolved Hide resolved
lib/instrumentation/modules/aws-sdk-v3.js Outdated Show resolved Hide resolved
lib/instrumentation/modules/aws-sdk-v3.js Outdated Show resolved Hide resolved
lib/instrumentation/modules/@aws-sdk/client-s3.js Outdated Show resolved Hide resolved
lib/instrumentation/modules/@aws-sdk/client-s3.js Outdated Show resolved Hide resolved
lib/instrumentation/modules/@aws-sdk/client-s3.js Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I got this error when running this with my current AWS config:

$ node ./test/instrumentation/modules/@aws-sdk/fixtures/use-s3-v3.js | ecslog
...
[2023-04-26T21:35:04.728Z]  INFO (elastic-apm-node): createBucket
    event.module: "app"
    bucketName: "elasticapmtest-bucket-20230426213504"
    data: null
    error: {
        "type": "S3ServiceException",
        "message":
            The async () => {
                        if (typeof region === "string") {
                            return (0, getRealRegion_1.getRealRegion)(region);
                        }
                        const providedRegion = await region();
                        return (0, getRealRegion_1.getRealRegion)(providedRegion);
                    } location constraint is incompatible for the region specific endpoint this request was sent to.,
        "stack_trace":
            IllegalLocationConstraintException: The async () => {
                        if (typeof region === "string") {
                            return (0, getRealRegion_1.getRealRegion)(region);
                        }
                        const providedRegion = await region();
                        return (0, getRealRegion_1.getRealRegion)(providedRegion);
                    } location constraint is incompatible for the region specific endpoint this request was sent to.
                at throwDefaultError (/Users/trentm/el/apm-agent-nodejs11/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:8:22)
                at /Users/trentm/el/apm-agent-nodejs11/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:18:39
                at de_CreateBucketCommandError (/Users/trentm/el/apm-agent-nodejs11/node_modules/@aws-sdk/client-s3/dist-cjs/protocols/Aws_restXml.js:3162:20)
                at processTicksAndRejections (node:internal/process/task_queues:96:5)
    }

I get this even when the APM agent isn't involved:

ELASTIC_APM_ACTIVE=false node ./test/instrumentation/modules/@aws-sdk/fixtures/use-s3-v3.js | ecslog

Do you know what might be causing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet. I'm going to run my own tests too and post results here

@david-luna
Copy link
Member Author

This is a great start.

Eventually, another thing to add to the v3 instrumentation is span.setHttpContext(...) as is done in the v2 instrumentation for S3, which adds this to the span:

    {
        "span": {
            // ...
            "context": {
                "http": {
                    "status_code": 200,
                    "response": {
                        "encoded_body_size": 11500
                    }
                }
            },
        }
    }

For the encoded_body_size property I've took it from the response header named content-type. Would that be ok?

@david-luna david-luna marked this pull request as ready for review April 28, 2023 14:10
Copy link
Member

@trentm trentm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great.

Another couple things:

.tav.yml Outdated Show resolved Hide resolved
dev-utils/aws-sdk-s3-client-tav-versions.sh Outdated Show resolved Hide resolved
lib/instrumentation/modules/@aws-sdk/client-s3.js Outdated Show resolved Hide resolved
lib/instrumentation/modules/@aws-sdk/client-s3.js Outdated Show resolved Hide resolved
test/instrumentation/modules/@aws-sdk/s3-v3.test.js Outdated Show resolved Hide resolved
test/instrumentation/modules/@aws-sdk/s3-v3.test.js Outdated Show resolved Hide resolved
test/instrumentation/modules/@aws-sdk/s3-v3.test.js Outdated Show resolved Hide resolved
test/instrumentation/modules/@aws-sdk/s3-v3.test.js Outdated Show resolved Hide resolved
@trentm
Copy link
Member

trentm commented Apr 28, 2023

This will avoid the failures in CI tests with node less than v14 (the min supported node version for @aws-sdk/*):

diff --git a/test/instrumentation/modules/@aws-sdk/s3-v3.test.js b/test/instrumentation/modules/@aws-sdk/s3-v3.test.js
index ecfe0dc3..453502f2 100644
--- a/test/instrumentation/modules/@aws-sdk/s3-v3.test.js
+++ b/test/instrumentation/modules/@aws-sdk/s3-v3.test.js
@@ -6,10 +6,15 @@

 'use strict'

+const semver = require('semver')
 if (process.env.GITHUB_ACTIONS === 'true' && process.platform === 'win32') {
   console.log('# SKIP: GH Actions do not support docker services on Windows')
   process.exit(0)
 }
+if (semver.lt(process.version, '14.0.0')) {
+  console.log(`# SKIP @aws-sdk min supported node is v14 (node ${process.version})`)
+  process.exit()
+}

 // Test S3 instrumentation of the '@aws-sdk/client-s3' module.
 //

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR shouldn't have most (any?) of these package-lock diffs. I think some of these will be resolved by merging from recent "main" changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is adding @aws-sdk/client-s3 as a dev dependency and this is its entry in the lock file

    "node_modules/@aws-sdk/client-s3": {
      "version": "3.325.0",
      "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.325.0.tgz",
      "integrity": "sha512-S88q84bJ1Wba3jnpIrWxMD4AbDsG7l45Ey4ZDdyriEceEgIWvBCA90ZOgSdaObhFNa5pNFZG9C6UbiaCz3F3bA==",
      "dev": true,
      "dependencies": {
        "@aws-crypto/sha1-browser": "3.0.0",
        "@aws-crypto/sha256-browser": "3.0.0",
        "@aws-crypto/sha256-js": "3.0.0",
        "@aws-sdk/client-sts": "3.325.0",
        "@aws-sdk/config-resolver": "3.310.0",
        "@aws-sdk/credential-provider-node": "3.325.0",
        "@aws-sdk/eventstream-serde-browser": "3.310.0",
        "@aws-sdk/eventstream-serde-config-resolver": "3.310.0",
        "@aws-sdk/eventstream-serde-node": "3.310.0",
        "@aws-sdk/fetch-http-handler": "3.310.0",
        "@aws-sdk/hash-blob-browser": "3.310.0",
        "@aws-sdk/hash-node": "3.310.0",
        "@aws-sdk/hash-stream-node": "3.310.0",
        "@aws-sdk/invalid-dependency": "3.310.0",
        "@aws-sdk/md5-js": "3.310.0",
        "@aws-sdk/middleware-bucket-endpoint": "3.310.0",
        "@aws-sdk/middleware-content-length": "3.325.0",
        "@aws-sdk/middleware-endpoint": "3.325.0",
        "@aws-sdk/middleware-expect-continue": "3.325.0",
        "@aws-sdk/middleware-flexible-checksums": "3.325.0",
        "@aws-sdk/middleware-host-header": "3.325.0",
        "@aws-sdk/middleware-location-constraint": "3.325.0",
        "@aws-sdk/middleware-logger": "3.325.0",
        "@aws-sdk/middleware-recursion-detection": "3.325.0",
        "@aws-sdk/middleware-retry": "3.325.0",
        "@aws-sdk/middleware-sdk-s3": "3.325.0",
        "@aws-sdk/middleware-serde": "3.325.0",
        "@aws-sdk/middleware-signing": "3.325.0",
        "@aws-sdk/middleware-ssec": "3.325.0",
        "@aws-sdk/middleware-stack": "3.325.0",
        "@aws-sdk/middleware-user-agent": "3.325.0",
        "@aws-sdk/node-config-provider": "3.310.0",
        "@aws-sdk/node-http-handler": "3.321.1",
        "@aws-sdk/protocol-http": "3.310.0",
        "@aws-sdk/signature-v4-multi-region": "3.310.0",
        "@aws-sdk/smithy-client": "3.325.0",
        "@aws-sdk/types": "3.310.0",
        "@aws-sdk/url-parser": "3.310.0",
        "@aws-sdk/util-base64": "3.310.0",
        "@aws-sdk/util-body-length-browser": "3.310.0",
        "@aws-sdk/util-body-length-node": "3.310.0",
        "@aws-sdk/util-defaults-mode-browser": "3.325.0",
        "@aws-sdk/util-defaults-mode-node": "3.325.0",
        "@aws-sdk/util-endpoints": "3.319.0",
        "@aws-sdk/util-retry": "3.310.0",
        "@aws-sdk/util-stream-browser": "3.310.0",
        "@aws-sdk/util-stream-node": "3.321.1",
        "@aws-sdk/util-user-agent-browser": "3.310.0",
        "@aws-sdk/util-user-agent-node": "3.310.0",
        "@aws-sdk/util-utf8": "3.310.0",
        "@aws-sdk/util-waiter": "3.310.0",
        "@aws-sdk/xml-builder": "3.310.0",
        "fast-xml-parser": "4.1.2",
        "tslib": "^2.5.0"
      },
      "engines": {
        "node": ">=14.0.0"
      }
    },

There's a lot of dependencies and these packages also have their dependencies as well. I guess for SNS, SQS & DynamoDB it will be less impact on the lock file.

@david-luna david-luna merged commit 38ce10c into main May 3, 2023
30 checks passed
APM-Agents (OLD) automation moved this from In Progress to Done May 3, 2023
@david-luna david-luna deleted the luna/2955-instrumentation-aws-s3-v3 branch May 3, 2023 10:54
@trentm trentm mentioned this pull request May 3, 2023
5 tasks
v1v added a commit to v1v/apm-agent-nodejs that referenced this pull request May 8, 2023
…re/support-specific-modules

* 'main' of github.com:elastic/apm-agent-nodejs: (54 commits)
  chore: fix dev-utils/ci-tav-slow-jobs.sh (elastic#3319)
  test: reduce TAV test matrix for slowest jobs (elastic#3321)
  chore: sync package-lock so 'npm ci' can work (elastic#3318)
  docs: document `useElasticTraceparentHeader` config var (elastic#3316)
  chore, test: test driver improvements (elastic#3293)
  test: drop node 14 from RC tests now that it is EOL (elastic#3315)
  test: fix running fastify.test.js with node v8 (elastic#3317)
  feat: add @apollo/server@4 support (elastic#3203)
  chore: update nvm (elastic#3309)
  tests: stop testing 'express-graphql' instrumentation (elastic#3304)
  chore: fix bitrot.js dev util for recent changes (elastic#3308)
  test: restore testing of Azure Functions on node >=18.x (elastic#3307)
  fix: support Lambda instrumentation for `contextManager: 'patch'`; refactor Lambda tests (elastic#3305)
  test: fix fastify TAV test failures (elastic#3314)
  test: fix @aws-sdk/client-s3 TAV test failures (elastic#3312)
  feat: add instrumentation for aws-sdk S3 client (elastic#3287)
  feat(fastify): add captureBody support (elastic#2681)
  feat: mysql2@3 support (elastic#3301)
  chore(deps): bump @opentelemetry/exporter-prometheus from 0.37.0 to 0.38.0 in /test/opentelemetry-metrics/fixtures (elastic#3295)
  chore(deps-dev): bump fastify from 4.16.3 to 4.17.0 (elastic#3296)
  ...
trentm added a commit that referenced this pull request May 16, 2023
* feat: add instrumentation for aws-sdk S3 client
---------

Co-authored-by: Trent Mick <trent.mick@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning.
Projects
Development

Successfully merging this pull request may close these issues.

AWS instrumentation for SDK v3, S3
2 participants