From f5851e72512117dbce571a42930a90c560dbf63d Mon Sep 17 00:00:00 2001 From: Amir Blum Date: Tue, 19 Oct 2021 10:21:44 -0700 Subject: [PATCH] feat(instrumentation-aws-sdk): upstream aws-sdk instrumentation from ext-js (#678) * feat(instrumentation-aws-sdk): upstream aws-sdk instrumentation to contrib * fix(instrumentation-aws-sdk): test-all-versions script * chore(instrumentation-aws-sdk): lint * chore(instrumentation-aws-sdk): pin api package * fix(instrumentation-aws-sdk): bump testing-utils to link in repo * fix: use otel api that match peer deps * docs(instrumentation-aws-sdk): remove provider in README * fix(aws-sdk): fix package name to otel org prefix * chore(instrumentation-aws-sdk): downgrade mocha for node v8 support * fix: use hook info interface for config hooks * build: downgrade expect package to support node8 * fix: don't run v3 tests on unsupported node8 * Revert "fix: don't run v3 tests on unsupported node8" This reverts commit 09ee529d1a8dc7361a660a10a0fe42c7ba0bd92c. * ci: exclude aws-sdk from node8 tests * ci: move lerna ignore to the right place * feat: add propagation utils package for messaging systems * chore(propagation-utils): resolve all lint issues and add types * chore(instrumentaiton-aws-sdk): lint * style(instrumentation-aws-sdk): fix lint issues * fix(propagation-utils): remove codecov from package * chore(aws-sdk): docs fixes from code review * chore: add aws-sdk to release please config * chore(aws-sdk): upgrade sdk to v1.0.0/v0.26.0 * fix(aws-sdk): support for ^3.36.0 which changed dist folder name * fix(aws-sdk): use correct dist folder for v3.35.0 * chore(aws-sdk): test less versions with tav * chore(aws-sdk): test with latest versions of package * chore(aws-sdk): update support of v2 to ^2.308.0 * fix(aws-sdk): don't crash if service name is missing * chore: lint fix * docs(aws-sdk): add link to GH issue for the receive operation and semantic conventions Co-authored-by: Rauno Viskus --- .github/component_owners.yml | 4 + .github/workflows/unit-test.yml | 6 +- README.md | 1 + .../.eslintignore | 1 + .../.eslintrc.js | 7 + .../opentelemetry-propagation-utils/LICENSE | 201 ++++++ .../opentelemetry-propagation-utils/README.md | 59 ++ .../package.json | 48 ++ .../src/index.ts | 16 + .../src/pubsub-propagation.ts | 246 +++++++ .../tsconfig.esm.json | 11 + .../tsconfig.json | 11 + .../instrumentation-singelton.ts | 3 + .../.eslintignore | 1 + .../.eslintrc.js | 7 + .../.tav.yml | 22 + .../LICENSE | 201 ++++++ .../README.md | 138 ++++ .../doc/sqs.md | 68 ++ .../package.json | 80 +++ .../src/aws-sdk.ts | 635 ++++++++++++++++++ .../src/enums.ts | 26 + .../src/index.ts | 17 + .../src/services/ServiceExtension.ts | 44 ++ .../src/services/ServicesExtensions.ts | 58 ++ .../src/services/dynamodb.ts | 74 ++ .../src/services/index.ts | 16 + .../src/services/sqs.ts | 218 ++++++ .../src/types.ts | 83 +++ .../src/utils.ts | 90 +++ .../test/aws-sdk-v2.test.ts | 388 +++++++++++ .../test/aws-sdk-v3.test.ts | 361 ++++++++++ .../test/dynamodb.test.ts | 191 ++++++ .../test/mock-responses/invalid-bucket.xml | 2 + .../test/mock-responses/s3-put-object.xml | 2 + .../test/mock-responses/sqs-receive.xml | 1 + .../test/mock-responses/sqs-send.xml | 1 + .../test/sqs.test.ts | 432 ++++++++++++ .../test/testing-utils.ts | 88 +++ .../tsconfig.json | 12 + release-please-config.json | 7 +- 41 files changed, 3874 insertions(+), 3 deletions(-) create mode 100644 packages/opentelemetry-propagation-utils/.eslintignore create mode 100644 packages/opentelemetry-propagation-utils/.eslintrc.js create mode 100644 packages/opentelemetry-propagation-utils/LICENSE create mode 100644 packages/opentelemetry-propagation-utils/README.md create mode 100644 packages/opentelemetry-propagation-utils/package.json create mode 100644 packages/opentelemetry-propagation-utils/src/index.ts create mode 100644 packages/opentelemetry-propagation-utils/src/pubsub-propagation.ts create mode 100644 packages/opentelemetry-propagation-utils/tsconfig.esm.json create mode 100644 packages/opentelemetry-propagation-utils/tsconfig.json create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintignore create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintrc.js create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/.tav.yml create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/LICENSE create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/README.md create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/doc/sqs.md create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/package.json create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/enums.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/index.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServiceExtension.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServicesExtensions.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/dynamodb.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/index.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/types.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/src/utils.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v2.test.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v3.test.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/invalid-bucket.xml create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/s3-put-object.xml create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-receive.xml create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-send.xml create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-sdk/tsconfig.json diff --git a/.github/component_owners.yml b/.github/component_owners.yml index 23a7e14ae..f67921075 100644 --- a/.github/component_owners.yml +++ b/.github/component_owners.yml @@ -17,6 +17,10 @@ components: plugins/node/opentelemetry-instrumentation-aws-lambda: - NathanielRN - willarmiros + plugins/node/opentelemetry-instrumentation-aws-sdk: + - NathanielRN + - willarmiros + - blumamir plugins/node/opentelemetry-instrumentation-generic-pool: - rauno56 plugins/node/opentelemetry-instrumentation-graphql: diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index ba63f2c35..1b1364012 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -10,6 +10,10 @@ jobs: fail-fast: false matrix: container: ["node:8", "node:10", "node:12", "node:14", "node:16"] + include: + - container: "node:8" + lerna-extra-args: >- + --ignore @opentelemetry/instrumentation-aws-sdk runs-on: ubuntu-latest container: image: ${{ matrix.container }} @@ -115,7 +119,7 @@ jobs: - name: Bootstrap Dependencies run: npx lerna bootstrap --no-ci --hoist --nohoist='zone.js' --nohoist='mocha' --nohoist='ts-mocha' - name: Unit tests - run: npm run test:ci:changed + run: npm run test:ci:changed -- ${{ matrix.lerna-extra-args }} - name: Report Coverage if: matrix.container == 'node:14' run: npm run codecov:ci:changed diff --git a/README.md b/README.md index 07fa12d8f..3d0cde26e 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information. [otel-instrumentation-xml-http-request]: https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-xml-http-request [otel-contrib-instrumentation-aws-lambda]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-aws-lambda +[otel-contrib-instrumentation-aws-sdk]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-aws-sdk [otel-contrib-instrumentation-bunyan]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-bunyan [otel-contrib-instrumentation-cassandra]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-cassandra [otel-contrib-instrumentation-connect]: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-connect diff --git a/packages/opentelemetry-propagation-utils/.eslintignore b/packages/opentelemetry-propagation-utils/.eslintignore new file mode 100644 index 000000000..378eac25d --- /dev/null +++ b/packages/opentelemetry-propagation-utils/.eslintignore @@ -0,0 +1 @@ +build diff --git a/packages/opentelemetry-propagation-utils/.eslintrc.js b/packages/opentelemetry-propagation-utils/.eslintrc.js new file mode 100644 index 000000000..f726f3bec --- /dev/null +++ b/packages/opentelemetry-propagation-utils/.eslintrc.js @@ -0,0 +1,7 @@ +module.exports = { + "env": { + "mocha": true, + "node": true + }, + ...require('../../eslint.config.js') +} diff --git a/packages/opentelemetry-propagation-utils/LICENSE b/packages/opentelemetry-propagation-utils/LICENSE new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/packages/opentelemetry-propagation-utils/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/opentelemetry-propagation-utils/README.md b/packages/opentelemetry-propagation-utils/README.md new file mode 100644 index 000000000..9ca9cd6ac --- /dev/null +++ b/packages/opentelemetry-propagation-utils/README.md @@ -0,0 +1,59 @@ +# OpenTelemetry Propagation Utils +[![NPM Published Version][npm-img]][npm-url] +[![dependencies][dependencies-image]][dependencies-url] +[![devDependencies][devDependencies-image]][devDependencies-url] +[![Apache License][license-image]][license-image] + +A collection of propagation utils for opentelemetry. + +## Install +```sh +npm install --save @opentelemetry/propagation-utils +``` + +## Usage + +### PubSub + +To make sure each message handled by pubsub creates a new `process` span, and propagates to any internal operation, do as follow: + +```ts +import { pubsubPropagation } from '@opentelemetry/propagation-utils'; +import { Span, propagation, trace, Context } from '@opentelemetry/api'; + +const patch = (message: Message[], rootSpan: Span) => { + const tracer = trace.getTracer('my-tracer'); + pubsubPropagation.patchArrayForProcessSpans(messages, tracer); + + pubsubPropagation.patchMessagesArrayToStartProcessSpans({ + messages, + tracer, + parentSpan: rootSpan, + messageToSpanDetails: (message) => ({ + attributes: { ... }, + name: 'some-name', + parentContext: propagation.extract(....) as Context + }), + }); +} +``` + +## Useful links + +- For more information on OpenTelemetry, visit: +- For more about OpenTelemetry JavaScript: +- For help or feedback on this project, join us in [GitHub Discussions][discussions-url] + +### License + +Apache 2.0 - See [LICENSE][license-url] for more information. + +[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions +[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/LICENSE +[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat +[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js-contrib.svg?path=packages%2Fopentelemetry-propagation-utils +[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=packages%2Fopentelemetry-propagation-utils +[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js-contrib.svg?path=packages%2Fopentelemetry-propagation-utils&type=dev +[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=packages%2Fopentelemetry-propagation-utils&type=dev +[npm-url]: https://www.npmjs.com/package/@opentelemetry/propagation-utils +[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fpropagation-utils.svg \ No newline at end of file diff --git a/packages/opentelemetry-propagation-utils/package.json b/packages/opentelemetry-propagation-utils/package.json new file mode 100644 index 000000000..95d1b2161 --- /dev/null +++ b/packages/opentelemetry-propagation-utils/package.json @@ -0,0 +1,48 @@ +{ + "name": "@opentelemetry/propagation-utils", + "version": "0.26.0", + "description": "Propagation utilities for opentelemetry instrumentations", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "publishConfig": { + "access": "public" + }, + "scripts": { + "clean": "tsc --build --clean tsconfig.json tsconfig.esm.json", + "compile": "tsc --build tsconfig.json tsconfig.esm.json", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "precompile": "tsc --version && lerna run version --scope @opentelemetry/propagation-utils --include-filtered-dependencies", + "prepare": "npm run compile", + "prewatch": "npm run precompile", + "version": "node ../../scripts/version-update.js", + "watch": "tsc --build --watch tsconfig.json tsconfig.esm.json" + }, + "repository": "open-telemetry/opentelemetry-js-contrib", + "keywords": [ + "tracing", + "instrumentation" + ], + "files": [ + "build/**/*.js", + "build/**/*.js.map", + "build/**/*.d.ts", + "LICENSE", + "README.md" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues" + }, + "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme", + "peerDependencies": { + "@opentelemetry/api": "^1.0.1" + }, + "devDependencies": { + "@opentelemetry/api": "1.0.1", + "@types/node": "14.17.9", + "gts": "3.1.0", + "typescript": "4.3.5" + } +} \ No newline at end of file diff --git a/packages/opentelemetry-propagation-utils/src/index.ts b/packages/opentelemetry-propagation-utils/src/index.ts new file mode 100644 index 000000000..2823683cc --- /dev/null +++ b/packages/opentelemetry-propagation-utils/src/index.ts @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export { default as pubsubPropagation } from './pubsub-propagation'; diff --git a/packages/opentelemetry-propagation-utils/src/pubsub-propagation.ts b/packages/opentelemetry-propagation-utils/src/pubsub-propagation.ts new file mode 100644 index 000000000..8302011ad --- /dev/null +++ b/packages/opentelemetry-propagation-utils/src/pubsub-propagation.ts @@ -0,0 +1,246 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + Tracer, + SpanKind, + Span, + Context, + Link, + context, + trace, + diag, + SpanAttributes, +} from '@opentelemetry/api'; + +const START_SPAN_FUNCTION = Symbol( + 'opentelemetry.pubsub-propagation.start_span' +); +const END_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.end_span'); + +interface OtelProcessedMessage { + [START_SPAN_FUNCTION]?: () => Span; + [END_SPAN_FUNCTION]?: () => void; +} + +const patchArrayFilter = ( + messages: unknown[], + tracer: Tracer, + loopContext: Context +) => { + const origFunc = messages.filter; + const patchedFunc = function ( + this: unknown, + ...args: Parameters + ) { + const newArray = origFunc.apply(this, args); + patchArrayForProcessSpans(newArray, tracer, loopContext); + return newArray; + }; + + Object.defineProperty(messages, 'filter', { + enumerable: false, + value: patchedFunc, + }); +}; + +const patchArrayFunction = ( + messages: OtelProcessedMessage[], + functionName: 'forEach' | 'map', + tracer: Tracer, + loopContext: Context +) => { + const origFunc = messages[functionName] as typeof messages.map; + const patchedFunc = function ( + this: unknown, + ...arrFuncArgs: Parameters + ) { + const callback = arrFuncArgs[0]; + const wrappedCallback = function ( + this: unknown, + ...callbackArgs: Parameters + ) { + const message = callbackArgs[0]; + const messageSpan = message?.[START_SPAN_FUNCTION]?.(); + if (!messageSpan) return callback.apply(this, callbackArgs); + + const res = context.with(trace.setSpan(loopContext, messageSpan), () => { + try { + return callback.apply(this, callbackArgs); + } finally { + message[END_SPAN_FUNCTION]?.(); + } + }); + + if (typeof res === 'object') { + const startSpanFunction = Object.getOwnPropertyDescriptor( + message, + START_SPAN_FUNCTION + ); + startSpanFunction && + Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction); + + const endSpanFunction = Object.getOwnPropertyDescriptor( + message, + END_SPAN_FUNCTION + ); + endSpanFunction && + Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction); + } + return res; + }; + arrFuncArgs[0] = wrappedCallback; + const funcResult = origFunc.apply(this, arrFuncArgs); + if (Array.isArray(funcResult)) + patchArrayForProcessSpans(funcResult, tracer, loopContext); + return funcResult; + }; + + Object.defineProperty(messages, functionName, { + enumerable: false, + value: patchedFunc, + }); +}; + +const patchArrayForProcessSpans = ( + messages: unknown[], + tracer: Tracer, + loopContext: Context = context.active() +) => { + patchArrayFunction( + messages as OtelProcessedMessage[], + 'forEach', + tracer, + loopContext + ); + patchArrayFunction( + messages as OtelProcessedMessage[], + 'map', + tracer, + loopContext + ); + patchArrayFilter(messages, tracer, loopContext); +}; + +const startMessagingProcessSpan = ( + message: T, + name: string, + attributes: SpanAttributes, + parentContext: Context, + propagatedContext: Context, + tracer: Tracer, + processHook?: ProcessHook +): Span => { + const links: Link[] = []; + const spanContext = trace.getSpanContext(propagatedContext); + if (spanContext) { + links.push({ + context: spanContext, + } as Link); + } + + const spanName = `${name} process`; + const processSpan = tracer.startSpan( + spanName, + { + kind: SpanKind.CONSUMER, + attributes: { + ...attributes, + ['messaging.operation']: 'process', + }, + links, + }, + parentContext + ); + + Object.defineProperty(message, START_SPAN_FUNCTION, { + enumerable: false, + writable: true, + value: () => processSpan, + }); + + Object.defineProperty(message, END_SPAN_FUNCTION, { + enumerable: false, + writable: true, + value: () => { + processSpan.end(); + Object.defineProperty(message, END_SPAN_FUNCTION, { + enumerable: false, + writable: true, + value: () => {}, + }); + }, + }); + + try { + processHook?.(processSpan, message); + } catch (err) { + diag.error('opentelemetry-pubsub-propagation: process hook error', err); + } + + return processSpan; +}; + +interface SpanDetails { + attributes: SpanAttributes; + parentContext: Context; + name: string; +} + +type ProcessHook = (processSpan: Span, message: T) => void; + +interface PatchForProcessingPayload { + messages: T[]; + tracer: Tracer; + parentContext: Context; + messageToSpanDetails: (message: T) => SpanDetails; + processHook?: ProcessHook; +} + +const patchMessagesArrayToStartProcessSpans = ({ + messages, + tracer, + parentContext, + messageToSpanDetails, + processHook, +}: PatchForProcessingPayload) => { + messages.forEach(message => { + const { + attributes, + name, + parentContext: propagatedContext, + } = messageToSpanDetails(message); + + Object.defineProperty(message, START_SPAN_FUNCTION, { + enumerable: false, + writable: true, + value: () => + startMessagingProcessSpan( + message, + name, + attributes, + parentContext, + propagatedContext, + tracer, + processHook + ), + }); + }); +}; + +export default { + patchMessagesArrayToStartProcessSpans, + patchArrayForProcessSpans, +}; diff --git a/packages/opentelemetry-propagation-utils/tsconfig.esm.json b/packages/opentelemetry-propagation-utils/tsconfig.esm.json new file mode 100644 index 000000000..a94adff6a --- /dev/null +++ b/packages/opentelemetry-propagation-utils/tsconfig.esm.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.base.esm.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build/esm", + "tsBuildInfoFile": "build/esm/tsconfig.esm.tsbuildinfo" + }, + "include": [ + "src/**/*.ts" + ] +} diff --git a/packages/opentelemetry-propagation-utils/tsconfig.json b/packages/opentelemetry-propagation-utils/tsconfig.json new file mode 100644 index 000000000..4078877ce --- /dev/null +++ b/packages/opentelemetry-propagation-utils/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +} diff --git a/packages/opentelemetry-test-utils/src/instrumentations/instrumentation-singelton.ts b/packages/opentelemetry-test-utils/src/instrumentations/instrumentation-singelton.ts index 732fed16d..ebdcc4cb8 100644 --- a/packages/opentelemetry-test-utils/src/instrumentations/instrumentation-singelton.ts +++ b/packages/opentelemetry-test-utils/src/instrumentations/instrumentation-singelton.ts @@ -35,6 +35,9 @@ export const registerInstrumentationTesting = ( ): T => { const existing = getInstrumentation(); if (existing) { + // we want to have just a single active instrumentation instance, + // so in case we do, we disable the current one so it will not get any events + instrumentation.disable(); return existing; } _global[OTEL_TESTING_INSTRUMENTATION_SINGLETON] = instrumentation; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintignore b/plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintignore new file mode 100644 index 000000000..378eac25d --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintignore @@ -0,0 +1 @@ +build diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintrc.js b/plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintrc.js new file mode 100644 index 000000000..f756f4488 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/.eslintrc.js @@ -0,0 +1,7 @@ +module.exports = { + "env": { + "mocha": true, + "node": true + }, + ...require('../../../eslint.config.js') +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/.tav.yml b/plugins/node/opentelemetry-instrumentation-aws-sdk/.tav.yml new file mode 100644 index 000000000..2f41cf862 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/.tav.yml @@ -0,0 +1,22 @@ +"aws-sdk": + # there are so many version to test, it can take forever. + # we will just sample few of them + versions: ">=2.1004.0 || 2.1002.0 || 2.950.0 || 2.903.0 || 2.880.0 || 2.706.0 || 2.608.0 || 2.518.0 || 2.422.0 || 2.308.0" + commands: + - npm run test + # Fix missing `contrib-test-utils` package + pretest: npm run --prefix ../../../ lerna:link + +"@aws-sdk/client-s3": + versions: ">=3.33.0 || 3.27.0 || 3.18.0 || 3.6.1 || 3.3.0" + commands: + - npm run test + # Fix missing `contrib-test-utils` package + pretest: npm run --prefix ../../../ lerna:link + +"@aws-sdk/client-sqs": + versions: ">=3.33.0 || 3.24.0 || 3.19.0 || 3.1.0" + commands: + - npm run test + # Fix missing `contrib-test-utils` package + pretest: npm run --prefix ../../../ lerna:link diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/LICENSE b/plugins/node/opentelemetry-instrumentation-aws-sdk/LICENSE new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/README.md b/plugins/node/opentelemetry-instrumentation-aws-sdk/README.md new file mode 100644 index 000000000..04587729e --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/README.md @@ -0,0 +1,138 @@ +# OpenTelemetry aws-sdk Instrumentation for Node.js + +[![NPM Published Version][npm-img]][npm-url] +[![dependencies][dependencies-image]][dependencies-url] +[![devDependencies][devdependencies-image]][devdependencies-url] +[![Apache License][license-image]][license-image] + +[component owners](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/.github/component_owners.yml): @willarmiros @NathanielRN @blumamir + +This module provides automatic instrumentation for [`aws-sdk` v2](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/) and [`@aws-sdk` v3](https://github.com/aws/aws-sdk-js-v3) + +## Installation + +```bash +npm install --save @opentelemetry/instrumentation-aws-sdk +``` + +## Usage + +For further automatic instrumentation instruction see the [@opentelemetry/instrumentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation) package. + +```js +const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node"); +const { registerInstrumentations } = require("@opentelemetry/instrumentation"); +const { + AwsInstrumentation, +} = require("@opentelemetry/instrumentation-aws-sdk"); + +const provider = new NodeTracerProvider(); +provider.register(); + +registerInstrumentations({ + instrumentations: [ + new AwsInstrumentation({ + // see under for available configuration + }), + ], +}); +``` + +### aws-sdk Instrumentation Options + +aws-sdk instrumentation has few options available to choose from. You can set the following: + +| Options | Type | Description | +| --------------------------------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| `preRequestHook` | `AwsSdkRequestCustomAttributeFunction` | Hook called before request send, which allow to add custom attributes to span. | +| `responseHook` | `AwsSdkResponseCustomAttributeFunction` | Hook for adding custom attributes when response is received from aws. | +| `sqsProcessHook` | `AwsSdkSqsProcessCustomAttributeFunction` | Hook called after starting sqs `process` span (for each sqs received message), which allow to add custom attributes to it. | +| `suppressInternalInstrumentation` | `boolean` | Most aws operation use http requests under the hood. Set this to `true` to hide all underlying http spans. | + +## Span Attributes + +Both V2 and V3 instrumentations are collecting the following attributes: +| Attribute Name | Type | Description | Example | +| -------------- | ---- | ----------- | ------- | +| `rpc.system` | string | Always equals "aws-api" | +| `rpc.method` | string | he name of the operation corresponding to the request, as returned by the AWS SDK. If the SDK does not provide a way to retrieve a name, the name of the command SHOULD be used, removing the suffix `Command` if present, resulting in a PascalCase name with no spaces. | `PutObject` | +| `rpc.service` | string | The name of the service to which a request is made, as returned by the AWS SDK. If the SDK does not provide a away to retrieve a name, the name of the SDK's client interface for a service SHOULD be used, removing the suffix `Client` if present, resulting in a PascalCase name with no spaces. | `S3`, `DynamoDB`, `Route53` | +| `aws.region` | string | Region name for the request | "eu-west-1" | + +### V2 attributes + +In addition to the above attributes, the instrumentation also collect the following for V2 ONLY: +| Attribute Name | Type | Description | Example | +| -------------- | ---- | ----------- | ------- | +| `aws.operation` | string | The method name for the request. | for `SQS.sendMessage(...)` the operation is "sendMessage" | +| `aws.signature.version` | string | AWS version of authentication signature on the request. | "v4" | +| `aws.service.api` | string | The sdk class name for the service | "SQS" | +| `aws.service.identifier` | string | Identifier for the service in the sdk | "sqs" | +| `aws.service.name` | string | Abbreviation name for the service | "Amazon SQS" | +| `aws.request.id` | uuid | Request unique id, as returned from aws on response | "01234567-89ab-cdef-0123-456789abcdef" | +| `aws.error` | string | information about a service or networking error, as returned from AWS | "UriParameterError: Expected uri parameter to have length >= 1, but found "" for params.Bucket" | + +### Custom User Attributes + +The instrumentation user can configure a `preRequestHook` function which will be called before each request, with a normalized request object (across v2 and v3) and the corresponding span. +This hook can be used to add custom attributes to the span with any logic. +For example, user can add interesting attributes from the `request.params`, and write custom logic based on the service and operation. +Usage example: + +```js +awsInstrumentationConfig = { + preRequestHook: (span, request) => { + if (span.serviceName === "s3") { + span.setAttribute("s3.bucket.name", request.commandInput["Bucket"]); + } + }, +}; +``` + +### Specific Service Logic + +AWS contains dozens of services accessible with the JS SDK. For many services, the default attributes specified above are enough, but other services have specific [trace semantic conventions](https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions), or need to inject/extract intra-process context, or set intra-process context correctly. + +Specific service logic currently implemented for: + +- [SQS](./docs/sqs.md) +- DynamoDb + +--- + +This instrumentation is a work in progress. We implemented some of the specific trace semantics for some of the services, and strive to support more services and extend the already supported services in the future. You can [Open an Issue](https://github.com/aspecto-io/opentelemetry-ext-js/issues), or [Submit a Pull Request](https://github.com/aspecto-io/opentelemetry-ext-js/pulls) if you want to contribute. + +## Potential Side Effects + +The instrumentation is doing best effort to support the trace specification of OpenTelemetry. For SQS, it involves defining new attributes on the `Messages` array, as well as on the manipulated types generated from this array (to set correct trace context for a single SQS message operation). Those properties are defined as [non-enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties) properties, so they have minimum side effect on the app. They will, however, show when using the `Object.getOwnPropertyDescriptors` and `Reflect.ownKeys` functions on SQS `Messages` array and for each `Message` in the array. + +## Migration From opentelemetry-instrumentation-aws-sdk + +This instrumentation was originally published under the name `"opentelemetry-instrumentation-aws-sdk"` in [this repo](https://github.com/aspecto-io/opentelemetry-ext-js). Few breaking changes were made during porting to the contrib repo to align with conventions: + +### Hook Info + +The instrumentation's config `preRequestHook`, `responseHook` and `sqsProcessHook` functions signature changed, so the second function parameter is info object, containing the relevant hook data. + +### `moduleVersionAttributeName` config option + +The `moduleVersionAttributeName` config option is removed. To add the aws-sdk package version to spans, use the `moduleVersion` attribute in hook info for `preRequestHook` and `responseHook` functions. + +## Useful links + +- For more information on OpenTelemetry, visit: +- For more about OpenTelemetry JavaScript: +- For help or feedback on this project, join us in [GitHub Discussions][discussions-url] + +## License + +Apache 2.0 - See [LICENSE][license-url] for more information. + +[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions +[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/LICENSE +[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat +[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=plugins%2Fnode%2Fopentelemetry-instrumentation-aws-sdk +[devdependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js-contrib.svg?path=plugins%2Fnode%2Fopentelemetry-instrumentation-aws-sdk&type=dev +[devdependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=plugins%2Fnode%2Fopentelemetry-instrumentation-aws-sdk&type=dev +[npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-aws-sdk +[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-aws-sdk.svg diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/doc/sqs.md b/plugins/node/opentelemetry-instrumentation-aws-sdk/doc/sqs.md new file mode 100644 index 000000000..5631a8a47 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/doc/sqs.md @@ -0,0 +1,68 @@ +# SQS + +SQS is amazon's managed message queue. Thus, it should follow the [OpenTelemetry specification for Messaging systems](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md). + +## Specific trace semantic + +The following methods are automatically enhanced: + +### sendMessage / sendMessageBatch + +- [Messaging Attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#messaging-attributes) are added by this instrumentation according to the spec. +- OpenTelemetry trace context is injected as SQS MessageAttributes, so the service receiving the message can link cascading spans to the trace which created the message. + +### receiveMessage + +- [Messaging Attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#messaging-attributes) are added by this instrumentation according to the spec. +- Additional "processing spans" are created for each message received by the application. + If an application invoked `receiveMessage`, and received a 10 messages batch, a single `messaging.operation` = `receive` span will be created for the `receiveMessage` operation, and 10 `messaging.operation` = `process` spans will be created, one for each message. + Those processing spans are created by the library. This behavior is partially implemented, [See discussion below](#processing-spans). +- Sets the inter process context correctly, so that additional spans created through the process will be linked to parent spans correctly. + This behavior is partially implemented, [See discussion below](#processing-spans). +- Extract trace context from SQS MessageAttributes, and set span's `parent` and `links` correctly according to the spec. + +#### Processing Spans + +See GH issue [here](https://github.com/open-telemetry/opentelemetry-js-contrib/issues/707) + +According to OpenTelemetry specification (and to reasonable expectation for trace structure), user of this library would expect to see one span for the operation of receiving messages batch from SQS, and then, **for each message**, a span with it's own sub-tree for the processing of this specific message. + +For example, if a `receiveMessages` returned 2 messages: + +- `msg1` resulting in storing something to a DB. +- `msg2` resulting in calling an external HTTP endpoint. + +This will result in a creating a DB span that would be the child of `msg1` process span, and an HTTP span that would be the child of `msg2` process span (in opposed to mixing all those operations under the single `receive` span, or start a new trace for each of them). + +Unfortunately, this is not so easy to implement in JS: + +1. The SDK is calling a single callback for the messages batch, and it's not straightforward to understand when each individual message processing starts and ends (and set the context correctly for cascading spans). +2. If async/await is used, context can be lost when returning data from async functions, for example: + +```js +async function asyncRecv() { + const data = await sqs.receiveMessage(recvParams).promise(); + // context of receiveMessage is set here + return data; +} + +async function poll() { + const result = await asyncRecv(); + // context is lost when asyncRecv returns. following spans are created with root context. + await Promise.all( + result.Messages.map((message) => this.processMessage(message)) + ); +} +``` + +Current implementation partially solves this issue by patching the `map` \ `forEach` \ `Filter` functions on the `Messages` array of `receiveMessage` result. This handles issues like the one above, but will not handle situations where the processing is done in other patterns (multiple map\forEach calls, index access to the array, other array operations, etc). This is currently an open issue in the instrumentation. + +User can add custom attributes to the `process` span, by setting a function to `sqsProcessHook` in instrumentation config. For example: + +```js +awsInstrumentationConfig = { + sqsProcessHook: (span, message) => { + span.setAttribute("sqs.receipt_handle", message.params?.ReceiptHandle); + }, +}; +``` diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/package.json b/plugins/node/opentelemetry-instrumentation-aws-sdk/package.json new file mode 100644 index 000000000..9892f4cf1 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/package.json @@ -0,0 +1,80 @@ +{ + "name": "@opentelemetry/instrumentation-aws-sdk", + "version": "0.25.0", + "description": "OpenTelemetry automatic instrumentation for the `aws-sdk` package", + "keywords": [ + "aws", + "opentelemetry", + "aws-sdk" + ], + "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme", + "license": "Apache-2.0", + "author": "OpenTelemetry Authors", + "bugs": { + "url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues" + }, + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "files": [ + "build/src/**/*.js", + "build/src/**/*.js.map", + "build/src/**/*.d.ts", + "doc", + "LICENSE", + "README.md" + ], + "publishConfig": { + "access": "public" + }, + "repository": "open-telemetry/opentelemetry-js-contrib", + "scripts": { + "clean": "rimraf build/*", + "codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", + "compile": "npm run version:update && tsc -p .", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "precompile": "tsc --version && lerna run version --scope $(npm pkg get name) --include-filtered-dependencies", + "prewatch": "npm run precompile", + "prepare": "npm run compile", + "tdd": "npm run test -- --watch-extensions ts --watch", + "test": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/*.test.ts'", + "test-all-versions": "tav", + "version:update": "node ../../../scripts/version-update.js", + "watch": "tsc -w" + }, + "peerDependencies": { + "@opentelemetry/api": "^1.0.1" + }, + "dependencies": { + "@opentelemetry/core": "^1.0.0", + "@opentelemetry/instrumentation": "^0.26.0", + "@opentelemetry/semantic-conventions": "^1.0.0", + "@opentelemetry/propagation-utils": "^0.26.0" + }, + "devDependencies": { + "@aws-sdk/client-dynamodb": "3.37.0", + "@aws-sdk/client-s3": "3.37.0", + "@aws-sdk/client-sqs": "3.37.0", + "@aws-sdk/types": "3.37.0", + "@opentelemetry/api": "1.0.1", + "@opentelemetry/sdk-trace-base": "1.0.0", + "@types/mocha": "^8.2.2", + "@types/node": "^14.0.0", + "aws-sdk": "2.1008.0", + "expect": "^25", + "mocha": "7.2.0", + "ts-mocha": "8.0.0", + "nock": "^13.0.11", + "gts": "3.1.0", + "@opentelemetry/contrib-test-utils": "^0.26.0", + "test-all-versions": "^5.0.1", + "ts-node": "^9.1.1", + "typescript": "4.3.4", + "eslint": "^7.32.0", + "nyc": "^15.1.0", + "rimraf": "^3.0.2" + }, + "engines": { + "node": ">=8.5.0" + } +} \ No newline at end of file diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts new file mode 100644 index 000000000..31d3ff87e --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts @@ -0,0 +1,635 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + Span, + SpanKind, + context, + trace, + Context, + diag, + SpanStatusCode, +} from '@opentelemetry/api'; +import { suppressTracing } from '@opentelemetry/core'; +import type * as AWS from 'aws-sdk'; +import { AttributeNames } from './enums'; +import { ServicesExtensions } from './services'; +import { + AwsSdkInstrumentationConfig, + AwsSdkRequestHookInformation, + AwsSdkResponseHookInformation, + NormalizedRequest, + NormalizedResponse, +} from './types'; +import { VERSION } from './version'; +import { + InstrumentationBase, + InstrumentationModuleDefinition, + InstrumentationNodeModuleDefinition, + InstrumentationNodeModuleFile, + isWrapped, + safeExecuteInTheMiddle, +} from '@opentelemetry/instrumentation'; +import type { + MiddlewareStack, + HandlerExecutionContext, + Command as AwsV3Command, + Handler as AwsV3MiddlewareHandler, + InitializeHandlerArguments, +} from '@aws-sdk/types'; +import { + bindPromise, + extractAttributesFromNormalizedRequest, + normalizeV2Request, + normalizeV3Request, + removeSuffixFromStringIfExists, +} from './utils'; +import { RequestMetadata } from './services/ServiceExtension'; + +const V3_CLIENT_CONFIG_KEY = Symbol( + 'opentelemetry.instrumentation.aws-sdk.client.config' +); +type V3PluginCommand = AwsV3Command & { + [V3_CLIENT_CONFIG_KEY]?: any; +}; + +const REQUEST_SPAN_KEY = Symbol('opentelemetry.instrumentation.aws-sdk.span'); +type V2PluginRequest = AWS.Request & { + [REQUEST_SPAN_KEY]?: Span; +}; + +export class AwsInstrumentation extends InstrumentationBase { + static readonly component = 'aws-sdk'; + protected override _config!: AwsSdkInstrumentationConfig; + private servicesExtensions: ServicesExtensions = new ServicesExtensions(); + + constructor(config: AwsSdkInstrumentationConfig = {}) { + super( + '@opentelemetry/instrumentation-aws-sdk', + VERSION, + Object.assign({}, config) + ); + } + + override setConfig(config: AwsSdkInstrumentationConfig = {}) { + this._config = Object.assign({}, config); + } + + protected init(): InstrumentationModuleDefinition[] { + const v3MiddlewareStackFileOldVersions = new InstrumentationNodeModuleFile( + '@aws-sdk/middleware-stack/dist/cjs/MiddlewareStack.js', + ['>=3.1.0 <3.35.0'], + this.patchV3ConstructStack.bind(this), + this.unpatchV3ConstructStack.bind(this) + ); + const v3MiddlewareStackFileNewVersions = new InstrumentationNodeModuleFile( + '@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js', + ['>=3.35.0'], + this.patchV3ConstructStack.bind(this), + this.unpatchV3ConstructStack.bind(this) + ); + + // as for aws-sdk v3.13.1, constructStack is exported from @aws-sdk/middleware-stack as + // getter instead of function, which fails shimmer. + // so we are patching the MiddlewareStack.js file directly to get around it. + const v3MiddlewareStack = new InstrumentationNodeModuleDefinition< + typeof AWS + >('@aws-sdk/middleware-stack', ['^3.1.0'], undefined, undefined, [ + v3MiddlewareStackFileOldVersions, + v3MiddlewareStackFileNewVersions, + ]); + + const v3SmithyClient = new InstrumentationNodeModuleDefinition( + '@aws-sdk/smithy-client', + ['^3.1.0'], + this.patchV3SmithyClient.bind(this), + this.unpatchV3SmithyClient.bind(this) + ); + + const v2Request = new InstrumentationNodeModuleFile( + 'aws-sdk/lib/core.js', + ['^2.308.0'], + this.patchV2.bind(this), + this.unpatchV2.bind(this) + ); + + const v2Module = new InstrumentationNodeModuleDefinition( + 'aws-sdk', + ['^2.308.0'], + undefined, + undefined, + [v2Request] + ); + + return [v2Module, v3MiddlewareStack, v3SmithyClient]; + } + + protected patchV3ConstructStack(moduleExports: any, moduleVersion?: string) { + diag.debug( + 'aws-sdk instrumentation: applying patch to aws-sdk v3 constructStack' + ); + this._wrap( + moduleExports, + 'constructStack', + this._getV3ConstructStackPatch.bind(this, moduleVersion) + ); + return moduleExports; + } + + protected unpatchV3ConstructStack(moduleExports: any) { + diag.debug( + 'aws-sdk instrumentation: applying unpatch to aws-sdk v3 constructStack' + ); + this._unwrap(moduleExports, 'constructStack'); + return moduleExports; + } + + protected patchV3SmithyClient(moduleExports: any) { + diag.debug( + 'aws-sdk instrumentation: applying patch to aws-sdk v3 client send' + ); + this._wrap( + moduleExports.Client.prototype, + 'send', + this._getV3SmithyClientSendPatch.bind(this) + ); + return moduleExports; + } + + protected unpatchV3SmithyClient(moduleExports: any) { + diag.debug( + 'aws-sdk instrumentation: applying patch to aws-sdk v3 constructStack' + ); + this._unwrap(moduleExports.Client.prototype, 'send'); + return moduleExports; + } + + protected patchV2(moduleExports: typeof AWS, moduleVersion?: string) { + diag.debug( + `aws-sdk instrumentation: applying patch to ${AwsInstrumentation.component}` + ); + this.unpatchV2(moduleExports); + this._wrap( + moduleExports?.Request.prototype, + 'send', + this._getRequestSendPatch.bind(this, moduleVersion) + ); + this._wrap( + moduleExports?.Request.prototype, + 'promise', + this._getRequestPromisePatch.bind(this, moduleVersion) + ); + + return moduleExports; + } + + protected unpatchV2(moduleExports?: typeof AWS) { + if (isWrapped(moduleExports?.Request.prototype.send)) { + this._unwrap(moduleExports!.Request.prototype, 'send'); + } + if (isWrapped(moduleExports?.Request.prototype.promise)) { + this._unwrap(moduleExports!.Request.prototype, 'promise'); + } + } + + private _startAwsV3Span( + normalizedRequest: NormalizedRequest, + metadata: RequestMetadata + ): Span { + const name = + metadata.spanName ?? + `${normalizedRequest.serviceName}.${normalizedRequest.commandName}`; + const newSpan = this.tracer.startSpan(name, { + kind: metadata.spanKind, + attributes: { + ...extractAttributesFromNormalizedRequest(normalizedRequest), + ...metadata.spanAttributes, + }, + }); + + return newSpan; + } + + private _startAwsV2Span( + request: AWS.Request, + metadata: RequestMetadata, + normalizedRequest: NormalizedRequest + ): Span { + const operation = (request as any).operation; + const service = (request as any).service; + const serviceIdentifier = service?.serviceIdentifier; + const name = + metadata.spanName ?? + `${normalizedRequest.serviceName}.${normalizedRequest.commandName}`; + + const newSpan = this.tracer.startSpan(name, { + kind: metadata.spanKind ?? SpanKind.CLIENT, + attributes: { + [AttributeNames.AWS_OPERATION]: operation, + [AttributeNames.AWS_SIGNATURE_VERSION]: + service?.config?.signatureVersion, + [AttributeNames.AWS_SERVICE_API]: service?.api?.className, + [AttributeNames.AWS_SERVICE_IDENTIFIER]: serviceIdentifier, + [AttributeNames.AWS_SERVICE_NAME]: service?.api?.abbreviation, + ...extractAttributesFromNormalizedRequest(normalizedRequest), + ...metadata.spanAttributes, + }, + }); + + return newSpan; + } + + private _callUserPreRequestHook( + span: Span, + request: NormalizedRequest, + moduleVersion: string | undefined + ) { + if (this._config?.preRequestHook) { + const requestInfo: AwsSdkRequestHookInformation = { + moduleVersion, + request, + }; + safeExecuteInTheMiddle( + () => this._config.preRequestHook!(span, requestInfo), + (e: Error | undefined) => { + if (e) + diag.error( + `${AwsInstrumentation.component} instrumentation: preRequestHook error`, + e + ); + }, + true + ); + } + } + + private _callUserResponseHook(span: Span, response: NormalizedResponse) { + const responseHook = this._config?.responseHook; + if (!responseHook) return; + + const responseInfo: AwsSdkResponseHookInformation = { + response, + }; + safeExecuteInTheMiddle( + () => responseHook(span, responseInfo), + (e: Error | undefined) => { + if (e) + diag.error( + `${AwsInstrumentation.component} instrumentation: responseHook error`, + e + ); + }, + true + ); + } + + private _registerV2CompletedEvent( + span: Span, + v2Request: V2PluginRequest, + normalizedRequest: NormalizedRequest, + completedEventContext: Context + ) { + const self = this; + v2Request.on('complete', response => { + // read issue https://github.com/aspecto-io/opentelemetry-ext-js/issues/60 + context.with(completedEventContext, () => { + if (!v2Request[REQUEST_SPAN_KEY]) { + return; + } + delete v2Request[REQUEST_SPAN_KEY]; + + const normalizedResponse: NormalizedResponse = { + data: response.data, + request: normalizedRequest, + }; + + self._callUserResponseHook(span, normalizedResponse); + if (response.error) { + span.setAttribute(AttributeNames.AWS_ERROR, response.error); + } else { + this.servicesExtensions.responseHook( + normalizedResponse, + span, + self.tracer, + self._config + ); + } + + span.setAttribute(AttributeNames.AWS_REQUEST_ID, response.requestId); + span.end(); + }); + }); + } + + private _getV3ConstructStackPatch( + moduleVersion: string | undefined, + original: (...args: unknown[]) => MiddlewareStack + ) { + const self = this; + return function constructStack( + this: any, + ...args: unknown[] + ): MiddlewareStack { + const stack: MiddlewareStack = original.apply(this, args); + self.patchV3MiddlewareStack(moduleVersion, stack); + return stack; + }; + } + + private _getV3SmithyClientSendPatch( + original: (...args: unknown[]) => Promise + ) { + return function send( + this: any, + command: V3PluginCommand, + ...args: unknown[] + ): Promise { + command[V3_CLIENT_CONFIG_KEY] = this.config; + return original.apply(this, [command, ...args]); + }; + } + + private patchV3MiddlewareStack( + moduleVersion: string | undefined, + middlewareStackToPatch: MiddlewareStack + ) { + if (!isWrapped(middlewareStackToPatch.resolve)) { + this._wrap( + middlewareStackToPatch, + 'resolve', + this._getV3MiddlewareStackResolvePatch.bind(this, moduleVersion) + ); + } + + // 'clone' and 'concat' functions are internally calling 'constructStack' which is in same + // module, thus not patched, and we need to take care of it specifically. + this._wrap( + middlewareStackToPatch, + 'clone', + this._getV3MiddlewareStackClonePatch.bind(this, moduleVersion) + ); + this._wrap( + middlewareStackToPatch, + 'concat', + this._getV3MiddlewareStackClonePatch.bind(this, moduleVersion) + ); + } + + private _getV3MiddlewareStackClonePatch( + moduleVersion: string | undefined, + original: (...args: any[]) => MiddlewareStack + ) { + const self = this; + return function (this: any, ...args: any[]) { + const newStack = original.apply(this, args); + self.patchV3MiddlewareStack(moduleVersion, newStack); + return newStack; + }; + } + + private _getV3MiddlewareStackResolvePatch( + moduleVersion: string | undefined, + original: ( + _handler: any, + context: HandlerExecutionContext + ) => AwsV3MiddlewareHandler + ) { + const self = this; + return function ( + this: any, + _handler: any, + awsExecutionContext: HandlerExecutionContext + ): AwsV3MiddlewareHandler { + const origHandler = original.call(this, _handler, awsExecutionContext); + const patchedHandler = function ( + this: any, + command: InitializeHandlerArguments & { + [V3_CLIENT_CONFIG_KEY]?: any; + } + ): Promise { + const clientConfig = command[V3_CLIENT_CONFIG_KEY]; + const regionPromise = clientConfig?.region?.(); + const serviceName = + clientConfig?.serviceId ?? + removeSuffixFromStringIfExists( + awsExecutionContext.clientName, + 'Client' + ); + const commandName = + awsExecutionContext.commandName ?? command.constructor?.name; + const normalizedRequest = normalizeV3Request( + serviceName, + commandName, + command.input, + undefined + ); + const requestMetadata = + self.servicesExtensions.requestPreSpanHook(normalizedRequest); + const span = self._startAwsV3Span(normalizedRequest, requestMetadata); + const activeContextWithSpan = trace.setSpan(context.active(), span); + + const handlerPromise = new Promise((resolve, reject) => { + Promise.resolve(regionPromise) + .then(resolvedRegion => { + normalizedRequest.region = resolvedRegion; + span.setAttribute(AttributeNames.AWS_REGION, resolvedRegion); + }) + .catch(e => { + // there is nothing much we can do in this case. + // we'll just continue without region + diag.debug( + `${AwsInstrumentation.component} instrumentation: failed to extract region from async function`, + e + ); + }) + .finally(() => { + self._callUserPreRequestHook( + span, + normalizedRequest, + moduleVersion + ); + const resultPromise = context.with(activeContextWithSpan, () => { + self.servicesExtensions.requestPostSpanHook(normalizedRequest); + return self._callOriginalFunction(() => + origHandler.call(this, command) + ); + }); + const promiseWithResponseLogic = resultPromise + .then(response => { + const requestId = response.output?.$metadata?.requestId; + if (requestId) { + span.setAttribute(AttributeNames.AWS_REQUEST_ID, requestId); + } + const extendedRequestId = + response.output?.$metadata?.extendedRequestId; + if (extendedRequestId) { + span.setAttribute( + AttributeNames.AWS_REQUEST_EXTENDED_ID, + extendedRequestId + ); + } + + const normalizedResponse: NormalizedResponse = { + data: response.output, + request: normalizedRequest, + }; + self.servicesExtensions.responseHook( + normalizedResponse, + span, + self.tracer, + self._config + ); + self._callUserResponseHook(span, normalizedResponse); + return response; + }) + .catch(err => { + const requestId = err?.RequestId; + if (requestId) { + span.setAttribute(AttributeNames.AWS_REQUEST_ID, requestId); + } + const extendedRequestId = err?.extendedRequestId; + if (extendedRequestId) { + span.setAttribute( + AttributeNames.AWS_REQUEST_EXTENDED_ID, + extendedRequestId + ); + } + + span.setStatus({ + code: SpanStatusCode.ERROR, + message: err.message, + }); + span.recordException(err); + throw err; + }) + .finally(() => { + span.end(); + }); + promiseWithResponseLogic + .then(res => { + resolve(res); + }) + .catch(err => reject(err)); + }); + }); + + return requestMetadata.isIncoming + ? bindPromise(handlerPromise, activeContextWithSpan, 2) + : handlerPromise; + }; + return patchedHandler; + }; + } + + private _getRequestSendPatch( + moduleVersion: string | undefined, + original: (callback?: (err: any, data: any) => void) => void + ) { + const self = this; + return function ( + this: V2PluginRequest, + callback?: (err: any, data: any) => void + ) { + /* + if the span was already started, we don't want to start a new one + when Request.promise() is called + */ + if (this[REQUEST_SPAN_KEY]) { + return original.call(this, callback); + } + + const normalizedRequest = normalizeV2Request(this); + const requestMetadata = + self.servicesExtensions.requestPreSpanHook(normalizedRequest); + const span = self._startAwsV2Span( + this, + requestMetadata, + normalizedRequest + ); + this[REQUEST_SPAN_KEY] = span; + const activeContextWithSpan = trace.setSpan(context.active(), span); + const callbackWithContext = context.bind(activeContextWithSpan, callback); + + self._callUserPreRequestHook(span, normalizedRequest, moduleVersion); + self._registerV2CompletedEvent( + span, + this, + normalizedRequest, + activeContextWithSpan + ); + + return context.with(activeContextWithSpan, () => { + self.servicesExtensions.requestPostSpanHook(normalizedRequest); + return self._callOriginalFunction(() => + original.call(this, callbackWithContext) + ); + }); + }; + } + + private _getRequestPromisePatch( + moduleVersion: string | undefined, + original: (...args: unknown[]) => Promise + ) { + const self = this; + return function (this: V2PluginRequest, ...args: unknown[]): Promise { + // if the span was already started, we don't want to start a new one when Request.promise() is called + if (this[REQUEST_SPAN_KEY]) { + return original.apply(this, args); + } + + const normalizedRequest = normalizeV2Request(this); + const requestMetadata = + self.servicesExtensions.requestPreSpanHook(normalizedRequest); + const span = self._startAwsV2Span( + this, + requestMetadata, + normalizedRequest + ); + this[REQUEST_SPAN_KEY] = span; + + const activeContextWithSpan = trace.setSpan(context.active(), span); + self._callUserPreRequestHook(span, normalizedRequest, moduleVersion); + self._registerV2CompletedEvent( + span, + this, + normalizedRequest, + activeContextWithSpan + ); + + const origPromise: Promise = context.with( + activeContextWithSpan, + () => { + self.servicesExtensions.requestPostSpanHook(normalizedRequest); + return self._callOriginalFunction(() => + original.call(this, arguments) + ); + } + ); + + return requestMetadata.isIncoming + ? bindPromise(origPromise, activeContextWithSpan) + : origPromise; + }; + } + + private _callOriginalFunction(originalFunction: (...args: any[]) => T): T { + if (this._config?.suppressInternalInstrumentation) { + return context.with(suppressTracing(context.active()), originalFunction); + } else { + return originalFunction(); + } + } +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/enums.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/enums.ts new file mode 100644 index 000000000..8d8f2d297 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/enums.ts @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export enum AttributeNames { + AWS_ERROR = 'aws.error', + AWS_OPERATION = 'aws.operation', + AWS_REGION = 'aws.region', + AWS_SERVICE_API = 'aws.service.api', + AWS_SERVICE_NAME = 'aws.service.name', + AWS_SERVICE_IDENTIFIER = 'aws.service.identifier', + AWS_REQUEST_ID = 'aws.request.id', + AWS_REQUEST_EXTENDED_ID = 'aws.request.extended_id', + AWS_SIGNATURE_VERSION = 'aws.signature.version', +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/index.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/index.ts new file mode 100644 index 000000000..2abc3b4f7 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export * from './aws-sdk'; +export * from './types'; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServiceExtension.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServiceExtension.ts new file mode 100644 index 000000000..5a08fecf6 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServiceExtension.ts @@ -0,0 +1,44 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Span, SpanAttributes, SpanKind, Tracer } from '@opentelemetry/api'; +import { + AwsSdkInstrumentationConfig, + NormalizedRequest, + NormalizedResponse, +} from '../types'; + +export interface RequestMetadata { + // isIncoming - if true, then the operation callback / promise should be bind with the operation's span + isIncoming: boolean; + spanAttributes?: SpanAttributes; + spanKind?: SpanKind; + spanName?: string; +} + +export interface ServiceExtension { + // called before request is sent, and before span is started + requestPreSpanHook: (request: NormalizedRequest) => RequestMetadata; + + // called before request is sent, and after span is started + requestPostSpanHook?: (request: NormalizedRequest) => void; + + responseHook?: ( + response: NormalizedResponse, + span: Span, + tracer: Tracer, + config: AwsSdkInstrumentationConfig + ) => void; +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServicesExtensions.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServicesExtensions.ts new file mode 100644 index 000000000..48c1e389b --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/ServicesExtensions.ts @@ -0,0 +1,58 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Tracer, Span } from '@opentelemetry/api'; +import { ServiceExtension, RequestMetadata } from './ServiceExtension'; +import { SqsServiceExtension } from './sqs'; +import { + AwsSdkInstrumentationConfig, + NormalizedRequest, + NormalizedResponse, +} from '../types'; +import { DynamodbServiceExtension } from './dynamodb'; + +export class ServicesExtensions implements ServiceExtension { + services: Map = new Map(); + + constructor() { + this.services.set('SQS', new SqsServiceExtension()); + this.services.set('DynamoDB', new DynamodbServiceExtension()); + } + + requestPreSpanHook(request: NormalizedRequest): RequestMetadata { + const serviceExtension = this.services.get(request.serviceName); + if (!serviceExtension) + return { + isIncoming: false, + }; + return serviceExtension.requestPreSpanHook(request); + } + + requestPostSpanHook(request: NormalizedRequest) { + const serviceExtension = this.services.get(request.serviceName); + if (!serviceExtension?.requestPostSpanHook) return; + return serviceExtension.requestPostSpanHook(request); + } + + responseHook( + response: NormalizedResponse, + span: Span, + tracer: Tracer, + config: AwsSdkInstrumentationConfig + ) { + const serviceExtension = this.services.get(response.request.serviceName); + serviceExtension?.responseHook?.(response, span, tracer, config); + } +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/dynamodb.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/dynamodb.ts new file mode 100644 index 000000000..e6f23bd42 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/dynamodb.ts @@ -0,0 +1,74 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Span, SpanKind, Tracer } from '@opentelemetry/api'; +import { RequestMetadata, ServiceExtension } from './ServiceExtension'; +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + AwsSdkInstrumentationConfig, + NormalizedRequest, + NormalizedResponse, +} from '../types'; + +export class DynamodbServiceExtension implements ServiceExtension { + requestPreSpanHook(normalizedRequest: NormalizedRequest): RequestMetadata { + const spanKind: SpanKind = SpanKind.CLIENT; + let spanName: string | undefined; + const isIncoming = false; + const operation = normalizedRequest.commandName; + + const spanAttributes = { + [SemanticAttributes.DB_SYSTEM]: 'dynamodb', + [SemanticAttributes.DB_NAME]: normalizedRequest.commandInput?.TableName, + [SemanticAttributes.DB_OPERATION]: operation, + [SemanticAttributes.DB_STATEMENT]: JSON.stringify( + normalizedRequest.commandInput + ), + }; + + if (operation == 'BatchGetItem') { + spanAttributes[SemanticAttributes.AWS_DYNAMODB_TABLE_NAMES] = Object.keys( + normalizedRequest.commandInput.RequestItems + ); + } + + return { + isIncoming, + spanAttributes, + spanKind, + spanName, + }; + } + + responseHook( + response: NormalizedResponse, + span: Span, + tracer: Tracer, + config: AwsSdkInstrumentationConfig + ) { + const operation = response.request.commandName; + + if (operation === 'BatchGetItem') { + if ('ConsumedCapacity' in response.data) { + span.setAttribute( + SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY, + response.data.ConsumedCapacity.map( + (x: { [DictionaryKey: string]: any }) => JSON.stringify(x) + ) + ); + } + } + } +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/index.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/index.ts new file mode 100644 index 000000000..7a77f3400 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/index.ts @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export { ServicesExtensions } from './ServicesExtensions'; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts new file mode 100644 index 000000000..09dd30054 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts @@ -0,0 +1,218 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + Tracer, + SpanKind, + Span, + propagation, + diag, + TextMapGetter, + TextMapSetter, + trace, + context, + ROOT_CONTEXT, +} from '@opentelemetry/api'; +import { pubsubPropagation } from '@opentelemetry/propagation-utils'; +import { RequestMetadata, ServiceExtension } from './ServiceExtension'; +import type { SQS } from 'aws-sdk'; +import { + AwsSdkInstrumentationConfig, + NormalizedRequest, + NormalizedResponse, +} from '../types'; +import { + MessagingDestinationKindValues, + SemanticAttributes, +} from '@opentelemetry/semantic-conventions'; + +export const START_SPAN_FUNCTION = Symbol( + 'opentelemetry.instrumentation.aws-sdk.sqs.start_span' +); + +export const END_SPAN_FUNCTION = Symbol( + 'opentelemetry.instrumentation.aws-sdk.sqs.end_span' +); + +// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html +const SQS_MAX_MESSAGE_ATTRIBUTES = 10; +class SqsContextSetter implements TextMapSetter { + set(carrier: SQS.MessageBodyAttributeMap, key: string, value: string) { + carrier[key] = { + DataType: 'String', + StringValue: value as string, + }; + } +} +const sqsContextSetter = new SqsContextSetter(); + +class SqsContextGetter implements TextMapGetter { + keys(carrier: SQS.MessageBodyAttributeMap): string[] { + return Object.keys(carrier); + } + + get( + carrier: SQS.MessageBodyAttributeMap, + key: string + ): undefined | string | string[] { + return carrier?.[key]?.StringValue; + } +} +const sqsContextGetter = new SqsContextGetter(); + +export class SqsServiceExtension implements ServiceExtension { + requestPreSpanHook(request: NormalizedRequest): RequestMetadata { + const queueUrl = this.extractQueueUrl(request.commandInput); + const queueName = this.extractQueueNameFromUrl(queueUrl); + let spanKind: SpanKind = SpanKind.CLIENT; + let spanName: string | undefined; + + const spanAttributes = { + [SemanticAttributes.MESSAGING_SYSTEM]: 'aws.sqs', + [SemanticAttributes.MESSAGING_DESTINATION_KIND]: + MessagingDestinationKindValues.QUEUE, + [SemanticAttributes.MESSAGING_DESTINATION]: queueName, + [SemanticAttributes.MESSAGING_URL]: queueUrl, + }; + + let isIncoming = false; + + switch (request.commandName) { + case 'ReceiveMessage': + { + isIncoming = true; + spanKind = SpanKind.CONSUMER; + spanName = `${queueName} receive`; + spanAttributes[SemanticAttributes.MESSAGING_OPERATION] = 'receive'; + + request.commandInput.MessageAttributeNames = ( + request.commandInput.MessageAttributeNames ?? [] + ).concat(propagation.fields()); + } + break; + + case 'SendMessage': + case 'SendMessageBatch': + spanKind = SpanKind.PRODUCER; + spanName = `${queueName} send`; + break; + } + + return { + isIncoming, + spanAttributes, + spanKind, + spanName, + }; + } + + requestPostSpanHook = (request: NormalizedRequest) => { + switch (request.commandName) { + case 'SendMessage': + { + const origMessageAttributes = + request.commandInput['MessageAttributes'] ?? {}; + if (origMessageAttributes) { + request.commandInput['MessageAttributes'] = + this.InjectPropagationContext(origMessageAttributes); + } + } + break; + + case 'SendMessageBatch': + { + request.commandInput?.Entries?.forEach( + (messageParams: SQS.SendMessageBatchRequestEntry) => { + messageParams.MessageAttributes = this.InjectPropagationContext( + messageParams.MessageAttributes ?? {} + ); + } + ); + } + break; + } + }; + + responseHook = ( + response: NormalizedResponse, + span: Span, + tracer: Tracer, + config: AwsSdkInstrumentationConfig + ) => { + const messages: SQS.Message[] = response?.data?.Messages; + if (messages) { + const queueUrl = this.extractQueueUrl(response.request.commandInput); + const queueName = this.extractQueueNameFromUrl(queueUrl); + + pubsubPropagation.patchMessagesArrayToStartProcessSpans({ + messages, + parentContext: trace.setSpan(context.active(), span), + tracer, + messageToSpanDetails: (message: SQS.Message) => ({ + name: queueName ?? 'unknown', + parentContext: propagation.extract( + ROOT_CONTEXT, + message.MessageAttributes, + sqsContextGetter + ), + attributes: { + [SemanticAttributes.MESSAGING_SYSTEM]: 'aws.sqs', + [SemanticAttributes.MESSAGING_DESTINATION]: queueName, + [SemanticAttributes.MESSAGING_DESTINATION_KIND]: + MessagingDestinationKindValues.QUEUE, + [SemanticAttributes.MESSAGING_MESSAGE_ID]: message.MessageId, + [SemanticAttributes.MESSAGING_URL]: queueUrl, + [SemanticAttributes.MESSAGING_OPERATION]: 'process', + }, + }), + processHook: (span: Span, message: SQS.Message) => + config.sqsProcessHook?.(span, { message }), + }); + + pubsubPropagation.patchArrayForProcessSpans( + messages, + tracer, + context.active() + ); + } + }; + + extractQueueUrl = (commandInput: Record): string => { + return commandInput?.QueueUrl; + }; + + extractQueueNameFromUrl = (queueUrl: string): string | undefined => { + if (!queueUrl) return undefined; + + const segments = queueUrl.split('/'); + if (segments.length === 0) return undefined; + + return segments[segments.length - 1]; + }; + + InjectPropagationContext( + attributesMap?: SQS.MessageBodyAttributeMap + ): SQS.MessageBodyAttributeMap { + const attributes = attributesMap ?? {}; + if (Object.keys(attributes).length < SQS_MAX_MESSAGE_ATTRIBUTES) { + propagation.inject(context.active(), attributes, sqsContextSetter); + } else { + diag.warn( + 'aws-sdk instrumentation: cannot set context propagation on SQS message due to maximum amount of MessageAttributes' + ); + } + return attributes; + } +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/types.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/types.ts new file mode 100644 index 000000000..c58260306 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/types.ts @@ -0,0 +1,83 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Span } from '@opentelemetry/api'; +import { InstrumentationConfig } from '@opentelemetry/instrumentation'; +import type * as AWS from 'aws-sdk'; + +/** + * These are normalized request and response, which are used by both sdk v2 and v3. + * They organize the relevant data in one interface which can be processed in a + * uniform manner in hooks + */ +export interface NormalizedRequest { + serviceName: string; + commandName: string; + commandInput: Record; + region?: string; +} +export interface NormalizedResponse { + data: any; + request: NormalizedRequest; +} + +export interface AwsSdkRequestHookInformation { + moduleVersion?: string; + request: NormalizedRequest; +} +export interface AwsSdkRequestCustomAttributeFunction { + (span: Span, requestInfo: AwsSdkRequestHookInformation): void; +} + +export interface AwsSdkResponseHookInformation { + moduleVersion?: string; + response: NormalizedResponse; +} +/** + * span can be used to add custom attributes, or for any other need. + * response is the object that is returned to the user calling the aws-sdk operation. + * The response type and attributes on the response are client-specific. + */ +export interface AwsSdkResponseCustomAttributeFunction { + (span: Span, responseInfo: AwsSdkResponseHookInformation): void; +} + +export interface AwsSdkSqsProcessHookInformation { + message: AWS.SQS.Message; +} +export interface AwsSdkSqsProcessCustomAttributeFunction { + (span: Span, sqsProcessInfo: AwsSdkSqsProcessHookInformation): void; +} + +export interface AwsSdkInstrumentationConfig extends InstrumentationConfig { + /** hook for adding custom attributes before request is sent to aws */ + preRequestHook?: AwsSdkRequestCustomAttributeFunction; + + /** hook for adding custom attributes when response is received from aws */ + responseHook?: AwsSdkResponseCustomAttributeFunction; + + /** hook for adding custom attribute when an sqs process span is started */ + sqsProcessHook?: AwsSdkSqsProcessCustomAttributeFunction; + + /** + * Most aws operation use http request under the hood. + * if http instrumentation is enabled, each aws operation will also create + * an http/s child describing the communication with amazon servers. + * Setting the `suppressInternalInstrumentation` config value to `true` will + * cause the instrumentation to suppress instrumentation of underlying operations, + * effectively causing those http spans to be non-recordable. + */ + suppressInternalInstrumentation?: boolean; +} diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/utils.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/utils.ts new file mode 100644 index 000000000..07be46549 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/utils.ts @@ -0,0 +1,90 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { NormalizedRequest } from './types'; +import type { Request } from 'aws-sdk'; +import { Context, SpanAttributes, context } from '@opentelemetry/api'; +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { AttributeNames } from './enums'; + +const toPascalCase = (str: string): string => + typeof str === 'string' ? str.charAt(0).toUpperCase() + str.slice(1) : str; + +export const removeSuffixFromStringIfExists = ( + str: string, + suffixToRemove: string +): string => { + const suffixLength = suffixToRemove.length; + return str?.slice(-suffixLength) === suffixToRemove + ? str.slice(0, str.length - suffixLength) + : str; +}; + +export const normalizeV2Request = ( + awsV2Request: Request +): NormalizedRequest => { + const service = (awsV2Request as any)?.service; + return { + serviceName: service?.api?.serviceId?.replace(/\s+/g, ''), + commandName: toPascalCase((awsV2Request as any)?.operation), + commandInput: (awsV2Request as any).params, + region: service?.config?.region, + }; +}; + +export const normalizeV3Request = ( + serviceName: string, + commandNameWithSuffix: string, + commandInput: Record, + region: string | undefined +): NormalizedRequest => { + return { + serviceName: serviceName?.replace(/\s+/g, ''), + commandName: removeSuffixFromStringIfExists( + commandNameWithSuffix, + 'Command' + ), + commandInput, + region, + }; +}; + +export const extractAttributesFromNormalizedRequest = ( + normalizedRequest: NormalizedRequest +): SpanAttributes => { + return { + [SemanticAttributes.RPC_SYSTEM]: 'aws-api', + [SemanticAttributes.RPC_METHOD]: normalizedRequest.commandName, + [SemanticAttributes.RPC_SERVICE]: normalizedRequest.serviceName, + [AttributeNames.AWS_REGION]: normalizedRequest.region, + }; +}; + +export const bindPromise = ( + target: Promise, + contextForCallbacks: Context, + rebindCount = 1 +): Promise => { + const origThen = target.then; + target.then = function (onFulfilled, onRejected) { + const newOnFulfilled = context.bind(contextForCallbacks, onFulfilled); + const newOnRejected = context.bind(contextForCallbacks, onRejected); + const patchedPromise = origThen.call(this, newOnFulfilled, newOnRejected); + return rebindCount > 1 + ? bindPromise(patchedPromise, contextForCallbacks, rebindCount - 1) + : patchedPromise; + }; + return target; +}; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v2.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v2.test.ts new file mode 100644 index 000000000..7a0caa3e1 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v2.test.ts @@ -0,0 +1,388 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + AwsInstrumentation, + AwsSdkRequestHookInformation, + AwsSdkResponseHookInformation, +} from '../src'; +import { + getTestSpans, + registerInstrumentationTesting, +} from '@opentelemetry/contrib-test-utils'; +const instrumentation = registerInstrumentationTesting( + new AwsInstrumentation() +); +import * as AWS from 'aws-sdk'; + +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; +import { SpanStatusCode, Span } from '@opentelemetry/api'; +import { AttributeNames } from '../src/enums'; +import { mockV2AwsSend } from './testing-utils'; +import * as expect from 'expect'; + +describe('instrumentation-aws-sdk-v2', () => { + const responseMockSuccess = { + requestId: '0000000000000', + error: null, + }; + + const responseMockWithError = { + requestId: '0000000000000', + error: 'something went wrong', + }; + + const getAwsSpans = (): ReadableSpan[] => { + return getTestSpans().filter(s => + s.instrumentationLibrary.name.includes('aws-sdk') + ); + }; + + before(() => { + AWS.config.credentials = { + accessKeyId: 'test key id', + expired: false, + expireTime: new Date(), + secretAccessKey: 'test acc key', + sessionToken: 'test token', + }; + }); + + describe('functional', () => { + describe('successful send', () => { + before(() => { + mockV2AwsSend(responseMockSuccess); + }); + + it('adds proper number of spans with correct attributes', async () => { + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + const keyName = 'aws-test-object.txt'; + await new Promise(resolve => { + // span 1 + s3.createBucket({ Bucket: bucketName }, async (err, data) => { + const params = { + Bucket: bucketName, + Key: keyName, + Body: 'Hello World!', + }; + // span 2 + s3.putObject(params, (err, data) => { + if (err) console.log(err); + resolve({}); + }); + }); + }); + + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(2); + const [spanCreateBucket, spanPutObject] = awsSpans; + + expect(spanCreateBucket.attributes[AttributeNames.AWS_OPERATION]).toBe( + 'createBucket' + ); + expect( + spanCreateBucket.attributes[AttributeNames.AWS_SIGNATURE_VERSION] + ).toBe('s3'); + expect( + spanCreateBucket.attributes[AttributeNames.AWS_SERVICE_API] + ).toBe('S3'); + expect( + spanCreateBucket.attributes[AttributeNames.AWS_SERVICE_IDENTIFIER] + ).toBe('s3'); + expect( + spanCreateBucket.attributes[AttributeNames.AWS_SERVICE_NAME] + ).toBe('Amazon S3'); + expect(spanCreateBucket.attributes[AttributeNames.AWS_REQUEST_ID]).toBe( + responseMockSuccess.requestId + ); + expect(spanCreateBucket.attributes[AttributeNames.AWS_REGION]).toBe( + 'us-east-1' + ); + + expect(spanCreateBucket.name).toBe('S3.CreateBucket'); + + expect(spanPutObject.attributes[AttributeNames.AWS_OPERATION]).toBe( + 'putObject' + ); + expect( + spanPutObject.attributes[AttributeNames.AWS_SIGNATURE_VERSION] + ).toBe('s3'); + expect(spanPutObject.attributes[AttributeNames.AWS_SERVICE_API]).toBe( + 'S3' + ); + expect( + spanPutObject.attributes[AttributeNames.AWS_SERVICE_IDENTIFIER] + ).toBe('s3'); + expect(spanPutObject.attributes[AttributeNames.AWS_SERVICE_NAME]).toBe( + 'Amazon S3' + ); + expect(spanPutObject.attributes[AttributeNames.AWS_REQUEST_ID]).toBe( + responseMockSuccess.requestId + ); + expect(spanPutObject.attributes[AttributeNames.AWS_REGION]).toBe( + 'us-east-1' + ); + expect(spanPutObject.name).toBe('S3.PutObject'); + }); + + it('adds proper number of spans with correct attributes if both, promise and callback were used', async () => { + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + const keyName = 'aws-test-object.txt'; + await new Promise(resolve => { + // span 1 + s3.createBucket({ Bucket: bucketName }, async (err, data) => { + const params = { + Bucket: bucketName, + Key: keyName, + Body: 'Hello World!', + }; + + let reqPromise: Promise | null = null; + let numberOfCalls = 0; + const cbPromise = new Promise(resolveCb => { + // span 2 + const request = s3.putObject(params, (err, data) => { + if (err) console.log(err); + numberOfCalls++; + if (numberOfCalls === 2) { + resolveCb({}); + } + }); + // NO span + reqPromise = request.promise(); + }); + + await Promise.all([cbPromise, reqPromise]).then(() => { + resolve({}); + }); + }); + }); + + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(2); + const [spanCreateBucket, spanPutObjectCb] = awsSpans; + expect(spanCreateBucket.attributes[AttributeNames.AWS_OPERATION]).toBe( + 'createBucket' + ); + expect(spanPutObjectCb.attributes[AttributeNames.AWS_OPERATION]).toBe( + 'putObject' + ); + expect(spanPutObjectCb.attributes[AttributeNames.AWS_REGION]).toBe( + 'us-east-1' + ); + }); + + it('adds proper number of spans with correct attributes if only promise was used', async () => { + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + const keyName = 'aws-test-object.txt'; + await new Promise(resolve => { + // span 1 + s3.createBucket({ Bucket: bucketName }, async (err, data) => { + const params = { + Bucket: bucketName, + Key: keyName, + Body: 'Hello World!', + }; + + // NO span + const request = s3.putObject(params); + // span 2 + await request.promise(); + resolve({}); + }); + }); + + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(2); + const [spanCreateBucket, spanPutObjectCb] = awsSpans; + expect(spanCreateBucket.attributes[AttributeNames.AWS_OPERATION]).toBe( + 'createBucket' + ); + expect(spanPutObjectCb.attributes[AttributeNames.AWS_OPERATION]).toBe( + 'putObject' + ); + expect(spanPutObjectCb.attributes[AttributeNames.AWS_REGION]).toBe( + 'us-east-1' + ); + }); + + it('should create span if no callback is supplied', done => { + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + + s3.putObject({ + Bucket: bucketName, + Key: 'key name from tests', + Body: 'Hello World!', + }).send(); + + setImmediate(() => { + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(1); + done(); + }); + }); + }); + + describe('send return error', () => { + before(() => { + mockV2AwsSend(responseMockWithError); + }); + + it('adds error attribute properly', async () => { + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + await new Promise(resolve => { + s3.createBucket({ Bucket: bucketName }, async () => { + resolve({}); + }); + }); + + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(1); + const [spanCreateBucket] = awsSpans; + expect(spanCreateBucket.attributes[AttributeNames.AWS_ERROR]).toBe( + responseMockWithError.error + ); + }); + }); + }); + + describe('instrumentation config', () => { + it('preRequestHook called and add request attribute to span', done => { + mockV2AwsSend(responseMockSuccess, 'data returned from operation'); + const config = { + preRequestHook: ( + span: Span, + requestInfo: AwsSdkRequestHookInformation + ) => { + span.setAttribute( + 'attribute from hook', + requestInfo.request.commandInput['Bucket'] + ); + }, + }; + + instrumentation.disable(); + instrumentation.setConfig(config); + instrumentation.enable(); + + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + + s3.createBucket({ Bucket: bucketName }, async (err, data) => { + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(1); + expect(awsSpans[0].attributes['attribute from hook']).toStrictEqual( + bucketName + ); + done(); + }); + }); + + it('preRequestHook throws does not fail span', done => { + mockV2AwsSend(responseMockSuccess, 'data returned from operation'); + const config = { + preRequestHook: (span: Span, request: any) => { + throw new Error('error from request hook'); + }, + }; + + instrumentation.disable(); + instrumentation.setConfig(config); + instrumentation.enable(); + + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + + s3.createBucket({ Bucket: bucketName }, async (err, data) => { + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(1); + expect(awsSpans[0].status.code).toStrictEqual(SpanStatusCode.UNSET); + done(); + }); + }); + + it('responseHook called and add response attribute to span', done => { + mockV2AwsSend(responseMockSuccess, 'data returned from operation'); + const config = { + responseHook: ( + span: Span, + responseInfo: AwsSdkResponseHookInformation + ) => { + span.setAttribute( + 'attribute from response hook', + responseInfo.response['data'] + ); + }, + }; + + instrumentation.disable(); + instrumentation.setConfig(config); + instrumentation.enable(); + + const s3 = new AWS.S3(); + const bucketName = 'aws-test-bucket'; + + s3.createBucket({ Bucket: bucketName }, async (err, data) => { + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(1); + expect( + awsSpans[0].attributes['attribute from response hook'] + ).toStrictEqual('data returned from operation'); + done(); + }); + }); + + it('suppressInternalInstrumentation set to true with send()', done => { + mockV2AwsSend(responseMockSuccess, 'data returned from operation', true); + const config = { + suppressInternalInstrumentation: true, + }; + + instrumentation.disable(); + instrumentation.setConfig(config); + instrumentation.enable(); + + const s3 = new AWS.S3(); + + s3.createBucket({ Bucket: 'aws-test-bucket' }, (err, data) => { + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(1); + done(); + }); + }); + + it('suppressInternalInstrumentation set to true with promise()', async () => { + mockV2AwsSend(responseMockSuccess, 'data returned from operation', true); + const config = { + suppressInternalInstrumentation: true, + }; + + instrumentation.disable(); + instrumentation.setConfig(config); + instrumentation.enable(); + + const s3 = new AWS.S3(); + + await s3.createBucket({ Bucket: 'aws-test-bucket' }).promise(); + const awsSpans = getAwsSpans(); + expect(awsSpans.length).toBe(1); + }); + }); +}); diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v3.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v3.test.ts new file mode 100644 index 000000000..e49ee9726 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/aws-sdk-v3.test.ts @@ -0,0 +1,361 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + AwsInstrumentation, + AwsSdkRequestHookInformation, + AwsSdkResponseHookInformation, +} from '../src'; +import { + getTestSpans, + registerInstrumentationTesting, +} from '@opentelemetry/contrib-test-utils'; +const instrumentation = registerInstrumentationTesting( + new AwsInstrumentation() +); +import { + PutObjectCommand, + PutObjectCommandOutput, + S3, + S3Client, +} from '@aws-sdk/client-s3'; +import { SQS } from '@aws-sdk/client-sqs'; + +// set aws environment variables, so tests in non aws environment are able to run +process.env.AWS_ACCESS_KEY_ID = 'testing'; +process.env.AWS_SECRET_ACCESS_KEY = 'testing'; + +import 'mocha'; +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; +import { context, SpanStatusCode, trace, Span } from '@opentelemetry/api'; +import { + MessagingDestinationKindValues, + MessagingOperationValues, + SemanticAttributes, +} from '@opentelemetry/semantic-conventions'; +import { AttributeNames } from '../src/enums'; +import * as expect from 'expect'; +import * as fs from 'fs'; +import * as nock from 'nock'; + +const region = 'us-east-1'; + +describe('instrumentation-aws-sdk-v3', () => { + const s3Client = new S3({ region }); + + describe('functional', () => { + it('promise await', async () => { + nock(`https://ot-demo-test.s3.${region}.amazonaws.com/`) + .put('/aws-ot-s3-test-object.txt?x-id=PutObject') + .reply( + 200, + fs.readFileSync('./test/mock-responses/s3-put-object.xml', 'utf8') + ); + + const params = { + Bucket: 'ot-demo-test', + Key: 'aws-ot-s3-test-object.txt', + }; + await s3Client.putObject(params); + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + expect(span.attributes[SemanticAttributes.RPC_SYSTEM]).toEqual('aws-api'); + expect(span.attributes[SemanticAttributes.RPC_METHOD]).toEqual( + 'PutObject' + ); + expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('S3'); + expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region); + expect(span.name).toEqual('S3.PutObject'); + }); + + it('callback interface', done => { + nock(`https://ot-demo-test.s3.${region}.amazonaws.com/`) + .put('/aws-ot-s3-test-object.txt?x-id=PutObject') + .reply( + 200, + fs.readFileSync('./test/mock-responses/s3-put-object.xml', 'utf8') + ); + + const params = { + Bucket: 'ot-demo-test', + Key: 'aws-ot-s3-test-object.txt', + }; + s3Client.putObject(params, (err: any, data?: PutObjectCommandOutput) => { + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + expect(span.attributes[SemanticAttributes.RPC_SYSTEM]).toEqual( + 'aws-api' + ); + expect(span.attributes[SemanticAttributes.RPC_METHOD]).toEqual( + 'PutObject' + ); + expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('S3'); + expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region); + expect(span.name).toEqual('S3.PutObject'); + done(); + }); + }); + + it('use the sdk client style to perform operation', async () => { + nock(`https://ot-demo-test.s3.${region}.amazonaws.com/`) + .put('/aws-ot-s3-test-object.txt?x-id=PutObject') + .reply( + 200, + fs.readFileSync('./test/mock-responses/s3-put-object.xml', 'utf8') + ); + + const params = { + Bucket: 'ot-demo-test', + Key: 'aws-ot-s3-test-object.txt', + }; + const client = new S3Client({ region }); + await client.send(new PutObjectCommand(params)); + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + expect(span.attributes[SemanticAttributes.RPC_SYSTEM]).toEqual('aws-api'); + expect(span.attributes[SemanticAttributes.RPC_METHOD]).toEqual( + 'PutObject' + ); + expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('S3'); + expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region); + expect(span.name).toEqual('S3.PutObject'); + }); + + it('aws error', async () => { + nock(`https://invalid-bucket-name.s3.${region}.amazonaws.com/`) + .put('/aws-ot-s3-test-object.txt?x-id=PutObject') + .reply( + 403, + fs.readFileSync('./test/mock-responses/invalid-bucket.xml', 'utf8') + ); + + const params = { + Bucket: 'invalid-bucket-name', + Key: 'aws-ot-s3-test-object.txt', + }; + + try { + await s3Client.putObject(params); + } catch { + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + + // expect error attributes + expect(span.status.code).toEqual(SpanStatusCode.ERROR); + expect(span.status.message).toEqual('Access Denied'); + expect(span.events.length).toBe(1); + expect(span.events[0].name).toEqual('exception'); + + expect(span.attributes[SemanticAttributes.RPC_SYSTEM]).toEqual( + 'aws-api' + ); + expect(span.attributes[SemanticAttributes.RPC_METHOD]).toEqual( + 'PutObject' + ); + expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('S3'); + expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region); + expect(span.attributes[AttributeNames.AWS_REQUEST_ID]).toEqual( + 'MS95GTS7KXQ34X2S' + ); + expect(span.name).toEqual('S3.PutObject'); + } + }); + }); + + describe('instrumentation config', () => { + describe('hooks', () => { + it('verify request and response hooks are called with right params', async () => { + instrumentation.disable(); + instrumentation.setConfig({ + preRequestHook: ( + span: Span, + requestInfo: AwsSdkRequestHookInformation + ) => { + span.setAttribute( + 'attribute.from.request.hook', + requestInfo.request.commandInput.Bucket + ); + }, + + responseHook: ( + span: Span, + responseInfo: AwsSdkResponseHookInformation + ) => { + span.setAttribute( + 'attribute.from.response.hook', + 'data from response hook' + ); + }, + + suppressInternalInstrumentation: true, + }); + instrumentation.enable(); + + nock(`https://ot-demo-test.s3.${region}.amazonaws.com/`) + .put('/aws-ot-s3-test-object.txt?x-id=PutObject') + .reply( + 200, + fs.readFileSync('./test/mock-responses/s3-put-object.xml', 'utf8') + ); + + const params = { + Bucket: 'ot-demo-test', + Key: 'aws-ot-s3-test-object.txt', + }; + await s3Client.putObject(params); + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + expect(span.attributes['attribute.from.request.hook']).toEqual( + params.Bucket + ); + expect(span.attributes['attribute.from.response.hook']).toEqual( + 'data from response hook' + ); + }); + + it('handle throw in request and response hooks', async () => { + instrumentation.disable(); + instrumentation.setConfig({ + preRequestHook: ( + span: Span, + requestInfo: AwsSdkRequestHookInformation + ) => { + span.setAttribute( + 'attribute.from.request.hook', + requestInfo.request.commandInput.Bucket + ); + throw new Error('error from request hook in unittests'); + }, + + responseHook: ( + span: Span, + responseInfo: AwsSdkResponseHookInformation + ) => { + throw new Error('error from response hook in unittests'); + }, + + suppressInternalInstrumentation: true, + }); + instrumentation.enable(); + + nock(`https://ot-demo-test.s3.${region}.amazonaws.com/`) + .put('/aws-ot-s3-test-object.txt?x-id=PutObject') + .reply( + 200, + fs.readFileSync('./test/mock-responses/s3-put-object.xml', 'utf8') + ); + + const params = { + Bucket: 'ot-demo-test', + Key: 'aws-ot-s3-test-object.txt', + }; + await s3Client.putObject(params); + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + expect(span.attributes['attribute.from.request.hook']).toEqual( + params.Bucket + ); + }); + }); + }); + + describe('custom service behavior', () => { + describe('SQS', () => { + const sqsClient = new SQS({ region }); + + it('sqs send add messaging attributes', async () => { + nock(`https://sqs.${region}.amazonaws.com/`) + .post('/') + .reply( + 200, + fs.readFileSync('./test/mock-responses/sqs-send.xml', 'utf8') + ); + + const params = { + QueueUrl: + 'https://sqs.us-east-1.amazonaws.com/731241200085/otel-demo-aws-sdk', + MessageBody: 'payload example from v3 without batch', + }; + await sqsClient.sendMessage(params); + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + + // make sure we have the general aws attributes: + expect(span.attributes[SemanticAttributes.RPC_SYSTEM]).toEqual( + 'aws-api' + ); + expect(span.attributes[SemanticAttributes.RPC_METHOD]).toEqual( + 'SendMessage' + ); + expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual('SQS'); + expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region); + + // custom messaging attributes + expect(span.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toEqual( + 'aws.sqs' + ); + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION_KIND] + ).toEqual(MessagingDestinationKindValues.QUEUE); + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION] + ).toEqual('otel-demo-aws-sdk'); + expect(span.attributes[SemanticAttributes.MESSAGING_URL]).toEqual( + params.QueueUrl + ); + }); + + it('sqs receive add messaging attributes and context', done => { + nock(`https://sqs.${region}.amazonaws.com/`) + .post('/') + .reply( + 200, + fs.readFileSync('./test/mock-responses/sqs-receive.xml', 'utf8') + ); + + const params = { + QueueUrl: + 'https://sqs.us-east-1.amazonaws.com/731241200085/otel-demo-aws-sdk', + MaxNumberOfMessages: 3, + }; + sqsClient.receiveMessage(params).then(res => { + expect(getTestSpans().length).toBe(1); + const [span] = getTestSpans(); + + // make sure we have the general aws attributes: + expect(span.attributes[SemanticAttributes.RPC_SYSTEM]).toEqual( + 'aws-api' + ); + expect(span.attributes[SemanticAttributes.RPC_METHOD]).toEqual( + 'ReceiveMessage' + ); + expect(span.attributes[SemanticAttributes.RPC_SERVICE]).toEqual( + 'SQS' + ); + expect(span.attributes[AttributeNames.AWS_REGION]).toEqual(region); + + const receiveCallbackSpan = trace.getSpan(context.active()); + expect(receiveCallbackSpan).toBeDefined(); + const attributes = (receiveCallbackSpan as unknown as ReadableSpan) + .attributes; + expect(attributes[SemanticAttributes.MESSAGING_OPERATION]).toMatch( + MessagingOperationValues.RECEIVE + ); + done(); + }); + }); + }); + }); +}); diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts new file mode 100644 index 000000000..dab0144f4 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts @@ -0,0 +1,191 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { AwsInstrumentation } from '../src'; +import { + getTestSpans, + registerInstrumentationTesting, +} from '@opentelemetry/contrib-test-utils'; +registerInstrumentationTesting(new AwsInstrumentation()); +import * as AWS from 'aws-sdk'; +import { AWSError } from 'aws-sdk'; + +import { mockV2AwsSend } from './testing-utils'; +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import * as expect from 'expect'; +import type { ConsumedCapacity as ConsumedCapacityV2 } from 'aws-sdk/clients/dynamodb'; +import type { ConsumedCapacity as ConsumedCapacityV3 } from '@aws-sdk/client-dynamodb'; + +type ConsumedCapacity = ConsumedCapacityV2 | ConsumedCapacityV3; + +const responseMockSuccess = { + requestId: '0000000000000', + error: null, +}; + +describe('DynamoDB', () => { + before(() => { + AWS.config.credentials = { + accessKeyId: 'test key id', + expired: false, + expireTime: new Date(), + secretAccessKey: 'test acc key', + sessionToken: 'test token', + }; + }); + + describe('Query', () => { + beforeEach(() => { + mockV2AwsSend(responseMockSuccess, { + Items: [{ key1: 'val1' }, { key2: 'val2' }], + Count: 2, + ScannedCount: 5, + } as AWS.DynamoDB.Types.QueryOutput); + }); + + it('should populate specific Query attributes', done => { + const dynamodb = new AWS.DynamoDB.DocumentClient(); + const params = { + TableName: 'test-table', + KeyConditionExpression: '#k = :v', + ExpressionAttributeNames: { + '#k': 'key1', + }, + ExpressionAttributeValues: { + ':v': 'val1', + }, + }; + dynamodb.query( + params, + (err: AWSError, data: AWS.DynamoDB.DocumentClient.QueryOutput) => { + const spans = getTestSpans(); + expect(spans.length).toStrictEqual(1); + const attrs = spans[0].attributes; + expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb'); + expect(attrs[SemanticAttributes.DB_NAME]).toStrictEqual('test-table'); + expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual('Query'); + expect( + JSON.parse(attrs[SemanticAttributes.DB_STATEMENT] as string) + ).toEqual(params); + expect(err).toBeFalsy(); + done(); + } + ); + }); + }); + + describe('BatchGetItem', () => { + const consumedCapacityResponseMockData: ConsumedCapacity[] = [ + { + TableName: 'test-table', + CapacityUnits: 0.5, + Table: { CapacityUnits: 0.5 }, + }, + ]; + + it('should populate BatchGetIem default attributes', done => { + mockV2AwsSend(responseMockSuccess, { + Responses: { 'test-table': [{ key1: { S: 'val1' } }] }, + UnprocessedKeys: {}, + } as AWS.DynamoDB.Types.BatchGetItemOutput); + + const dynamodb = new AWS.DynamoDB.DocumentClient(); + const dynamodb_params = { + RequestItems: { + 'test-table': { + Keys: [{ key1: { S: 'val1' } }], + ProjectionExpression: 'id', + }, + }, + ReturnConsumedCapacity: 'INDEXES', + }; + dynamodb.batchGet( + dynamodb_params, + ( + err: AWSError, + data: AWS.DynamoDB.DocumentClient.BatchGetItemOutput + ) => { + const spans = getTestSpans(); + expect(spans.length).toStrictEqual(1); + const attrs = spans[0].attributes; + expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb'); + expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual( + 'BatchGetItem' + ); + expect( + attrs[SemanticAttributes.AWS_DYNAMODB_TABLE_NAMES] + ).toStrictEqual(['test-table']); + expect( + attrs[SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY] + ).toBeUndefined(); + expect( + JSON.parse(attrs[SemanticAttributes.DB_STATEMENT] as string) + ).toEqual(dynamodb_params); + expect(err).toBeFalsy(); + done(); + } + ); + }); + + it('should populate BatchGetIem optional attributes', done => { + mockV2AwsSend(responseMockSuccess, { + Responses: { 'test-table': [{ key1: { S: 'val1' } }] }, + UnprocessedKeys: {}, + ConsumedCapacity: consumedCapacityResponseMockData, + } as AWS.DynamoDB.Types.BatchGetItemOutput); + + const dynamodb = new AWS.DynamoDB.DocumentClient(); + const dynamodb_params = { + RequestItems: { + 'test-table': { + Keys: [{ key1: { S: 'val1' } }], + ProjectionExpression: 'id', + }, + }, + ReturnConsumedCapacity: 'INDEXES', + }; + dynamodb.batchGet( + dynamodb_params, + ( + err: AWSError, + data: AWS.DynamoDB.DocumentClient.BatchGetItemOutput + ) => { + const spans = getTestSpans(); + expect(spans.length).toStrictEqual(1); + const attrs = spans[0].attributes; + expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb'); + expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual( + 'BatchGetItem' + ); + expect( + attrs[SemanticAttributes.AWS_DYNAMODB_TABLE_NAMES] + ).toStrictEqual(['test-table']); + expect( + attrs[SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY] + ).toStrictEqual( + consumedCapacityResponseMockData.map((x: ConsumedCapacity) => + JSON.stringify(x) + ) + ); + expect( + JSON.parse(attrs[SemanticAttributes.DB_STATEMENT] as string) + ).toEqual(dynamodb_params); + expect(err).toBeFalsy(); + done(); + } + ); + }); + }); +}); diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/invalid-bucket.xml b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/invalid-bucket.xml new file mode 100644 index 000000000..d2a64d376 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/invalid-bucket.xml @@ -0,0 +1,2 @@ + +AccessDeniedAccess DeniedMS95GTS7KXQ34X2SVAHljC+Y071YSnIUz18CciSkVa2UFrz+XPwg4cULITTWfAkoLtl1VwRzvYc+uX9Uo8K/wdicb4I= \ No newline at end of file diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/s3-put-object.xml b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/s3-put-object.xml new file mode 100644 index 000000000..69d715e7f --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/s3-put-object.xml @@ -0,0 +1,2 @@ + + e8b7247768fa8b2d077fb1ffb57840ffbe9c101ba73ef4613ac2b4633399a0a5michaelaccess-logs-prd-aspecto-nlb-private2021-02-10T10:17:40.000Zapp.aspecto.io2019-05-09T17:27:55.000Zapp.topsight.io2018-11-09T09:08:58.000Zaspecto-cf-templates-public2020-01-02T09:21:50.000Zaspecto-cost-billing-legacy2020-02-06T16:30:23.000Zaspecto-docs-customers2019-08-26T08:27:15.000Zaspecto-ecs-logs2020-01-29T14:59:50.000Zaspecto-http-firehose-lambda-converter2019-07-22T12:30:17.000Zaspecto-lake-formation-test2019-08-18T17:35:31.000Zaspecto-message-payload2020-03-18T15:44:09.000Zaspecto-sagemaker-test2019-07-15T09:00:47.000Zaspecto-static2020-03-17T16:40:26.000Zaspecto-test-dev-serverlessdeploymentbucket-fms1k6d62kyw2019-05-22T12:00:40.000Zaspecto-version-pipeline-aspectoversionpipelinela-1ngrp5ccewdhs2019-07-30T15:39:08.000Zaws-athena-query-results-731241200085-eu-west-12019-01-27T10:51:45.000Zaws-athena-query-results-eu-west-1-7312412000852019-05-28T06:22:27.000Zaws-glue-scripts-731241200085-eu-west-12019-03-03T10:47:15.000Zaws-glue-temporary-731241200085-eu-west-12019-03-03T10:47:17.000Zaws-logs-731241200085-eu-west-12019-04-22T05:32:40.000Zawscommunityday-s3100bedfb-1eawtiwjim1662019-12-07T12:02:33.000Zawscommunityday-s3100bedfb-1uh1znis3tkmh2019-12-07T11:36:50.000Zawscommunityday-s3100bedfb-8kp6cmynq3gr2019-12-07T13:05:11.000Zcdktoolkit-stagingbucket-1rssn0syfm7ra2020-12-13T14:19:54.000Zcdktoolkit-stagingbucket-2fvubvo96j3o2019-12-07T10:14:08.000Zcdktoolkit-stagingbucket-ofrmroh9nrld2019-07-30T15:37:37.000Zcf-templates-z21r2x6dm98j-eu-west-12018-11-13T12:47:16.000Zcloud-trail-logs-aspecto2019-04-23T05:07:26.000Zcr.aspecto.io2019-10-07T09:46:38.000Zdemo.topsight.io2018-10-17T07:56:04.000Zdev-amir2020-07-12T06:59:21.000Zfrom-template-dev-serverlessdeploymentbucket-1u09z7kheeztg2020-11-08T12:16:35.000Zfrom-template-dev-serverlessdeploymentbucket-a063v3bs3jo32020-11-16T11:07:39.000Zhttp-raw-events2019-12-01T12:13:34.000Zjava.aspecto.io2020-01-30T11:08:44.000Zlogo.aspecto.io2019-05-09T17:51:20.000Zlogo.topsight.io2018-12-11T15:18:57.000Zmyservice-dev-serverlessdeploymentbucket-1jjf9xlsdxepz2020-08-31T09:10:42.000Zmyservice-dev-serverlessdeploymentbucket-z2w1pc6x8awl2020-11-08T11:02:17.000Zot-demo-test2020-05-21T11:36:31.000Zprd-topsight-client-events-athena-results2019-02-03T11:23:09.000Zprd-topsight-node-client-events2019-01-21T13:44:17.000Zproto.aspecto.io2020-06-17T13:56:34.000Zresources.aspecto.io2020-09-09T10:57:44.000Zserverless-pg-dev-serverlessdeploymentbucket-1jhcozv2hx5802021-01-18T12:36:55.000Zsls-test-dev-serverlessdeploymentbucket-h8cdsqmecqw32020-11-08T09:53:30.000Ztom-replication-dest2021-01-11T15:25:09.000Ztom-replication-test2021-01-11T14:33:21.000Ztopsight-general-logs2018-12-03T12:33:06.000Ztopsight-ml-test2019-01-05T20:15:26.000Ztopsight-reports-images2018-12-11T11:27:11.000Zus-east-2.aspecto-message-payload2021-01-18T11:00:36.000Zus-east-2.java.aspecto.io2021-01-12T11:56:50.000Zus-east-2.logo.aspecto.io2021-01-12T10:31:42.000Zus-east-2.resources.aspecto.io2021-01-12T12:01:10.000Z \ No newline at end of file diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-receive.xml b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-receive.xml new file mode 100644 index 000000000..2e2e61de1 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-receive.xml @@ -0,0 +1 @@ +e8cc48a4-3716-409f-a82c-f6acfa9b9032AQEBZSQ0BFOc7EQ+HS3OAtqeXwyg8ZWW8ha3xHiCZWTkXAuCpsC3b5mzwd3OKRETs9eJK7jS63RudsNKvCEpE+m1zkeG5JBFbR/tG7i0o8HfmCdrhbRotdhxlC2edEetgXYuvWFZ+EY/HiiJfOz0xTK7gJOo+vqPtko221fa3SEmUt5zeYZTZ+NGHMx4n6+yXWRm72eQ1zB9ooGhNRl0lDcthmIDAaqZK+s+wuIDhxemOP6PnUW6yN4EpA16I1T/xPg/S0MnSsDOhTm2zzNtkrxeszMC7O25PUc6Mk/6eVVRVkfZ8NFHNDdjaB3GlbEVn3yxz2HoFjo+JLpkSnhvvJ4XFzmiZofSrHgru9gI/Se0vvUdAPS2a/n7LRf0gY7gTyOTrUw4ryr4WXj0Vxau3KW7Zg==89ca049a00b657d53acb784d93c83ee9832519a023850b48b5ebea9a6ac0a04epayload example from v3 without batchaspecto-traceparent00-a1d050b7c8ad93c405e7a0d94cda5b03-23a485dc98b24027-01Stringtraceparent00-a1d050b7c8ad93c405e7a0d94cda5b03-23a485dc98b24027-01Stringbb205142-a48e-45e5-aae6-596c24919443AQEBY3/BwD+pjORg/O0lQtumFxfwEASaLrpRetl6WSMI7Fev7huBy0FG10lBTfSMIar0NYCBKz3oG+risizey4kWxp1aj10ze5ru+7abQCZfDHH3wTFuky3OGhAU1YtmiWElTUiA3yHP5p08y23Ayg+WZ7JSt7ESHZ5nwebJ9Hg9IE2E/DCXAHQFnZte6txN2eIDNfUeaO1OuLS7uYIdH/QKs884r+KsJTLrAwBNyuqn+mJBEMMJZEqvUqZ1w0dFhrEFVhAflKqd52f8wrqN4Hs9c60S3FzATeT+Ebe5hRxZwepAqrhGqpCXgw61oSBH0tlSDXzyps7NeSCWXiYWZLOIb1wk2qMLe/mgSbKOQ5ZUFvWkwmkw+qM7Nq6QvJ6JjfzZzox3d1/tq23ex4FU+M9s6g==89ca049a00b657d53acb784d93c83ee9b6ec266e272f5e65e093eded331e667fpayload example from v3 without batchaspecto-traceparent00-b5f436bcdd06457acc1be749f789e3cd-23d8385b3653b0ce-01Stringtraceparent00-b5f436bcdd06457acc1be749f789e3cd-23d8385b3653b0ce-01Stringcacd8413-edf6-5ebe-bc51-dcd829297449 \ No newline at end of file diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-send.xml b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-send.xml new file mode 100644 index 000000000..6709a2d5f --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/mock-responses/sqs-send.xml @@ -0,0 +1 @@ +35de59a8-cdcc-4f55-9734-d7343405862289ca049a00b657d53acb784d93c83ee94fdeb9d03aa9063f7b9eccb7429d23f45490ab59-e06c-540e-9644-f39254d6bcf7 \ No newline at end of file diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts new file mode 100644 index 000000000..42a66f7db --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts @@ -0,0 +1,432 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { AwsInstrumentation, AwsSdkSqsProcessHookInformation } from '../src'; +import { + getTestSpans, + registerInstrumentationTesting, +} from '@opentelemetry/contrib-test-utils'; +const instrumentation = registerInstrumentationTesting( + new AwsInstrumentation() +); +import * as AWS from 'aws-sdk'; +import { AWSError } from 'aws-sdk'; + +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + context, + SpanKind, + SpanStatusCode, + trace, + Span, +} from '@opentelemetry/api'; +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; +import { mockV2AwsSend } from './testing-utils'; +import { Message } from 'aws-sdk/clients/sqs'; +import * as expect from 'expect'; + +const responseMockSuccess = { + requestId: '0000000000000', + error: null, +}; + +describe('SQS', () => { + before(() => { + AWS.config.credentials = { + accessKeyId: 'test key id', + expired: false, + expireTime: new Date(), + secretAccessKey: 'test acc key', + sessionToken: 'test token', + }; + }); + + beforeEach(() => { + mockV2AwsSend(responseMockSuccess, { + Messages: [{ Body: 'msg 1 payload' }, { Body: 'msg 2 payload' }], + } as AWS.SQS.Types.ReceiveMessageResult); + }); + + describe('receive context', () => { + const createReceiveChildSpan = () => { + const childSpan = trace + .getTracerProvider() + .getTracer('default') + .startSpan('child span of SQS.ReceiveMessage'); + childSpan.end(); + }; + + const expectReceiverWithChildSpan = (spans: ReadableSpan[]) => { + const awsReceiveSpan = spans.filter(s => s.kind === SpanKind.CONSUMER); + expect(awsReceiveSpan.length).toBe(1); + const internalSpan = spans.filter(s => s.kind === SpanKind.INTERNAL); + expect(internalSpan.length).toBe(1); + expect(internalSpan[0].parentSpanId).toStrictEqual( + awsReceiveSpan[0].spanContext().spanId + ); + }; + + it('should set parent context in sqs receive callback', done => { + const sqs = new AWS.SQS(); + sqs.receiveMessage( + { + QueueUrl: 'queue/url/for/unittests', + }, + (err: AWSError, data: AWS.SQS.Types.ReceiveMessageResult) => { + expect(err).toBeFalsy(); + createReceiveChildSpan(); + expectReceiverWithChildSpan(getTestSpans()); + done(); + } + ); + }); + + it("should set parent context in sqs receive 'send' callback", done => { + const sqs = new AWS.SQS(); + sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .send((err: AWSError, data: AWS.SQS.Types.ReceiveMessageResult) => { + expect(err).toBeFalsy(); + createReceiveChildSpan(); + expectReceiverWithChildSpan(getTestSpans()); + done(); + }); + }); + + it('should set parent context in sqs receive promise then', async () => { + const sqs = new AWS.SQS(); + await sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .promise() + .then(() => { + createReceiveChildSpan(); + expectReceiverWithChildSpan(getTestSpans()); + }); + }); + + it.skip('should set parent context in sqs receive after await', async () => { + const sqs = new AWS.SQS(); + await sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .promise(); + + createReceiveChildSpan(); + expectReceiverWithChildSpan(getTestSpans()); + }); + + it.skip('should set parent context in sqs receive from async function', async () => { + const asycnReceive = async () => { + const sqs = new AWS.SQS(); + return await sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .promise(); + }; + + await asycnReceive(); + createReceiveChildSpan(); + expectReceiverWithChildSpan(getTestSpans()); + }); + }); + + describe('process spans', () => { + let receivedMessages: Message[]; + + const createProcessChildSpan = (msgContext: any) => { + const processChildSpan = trace + .getTracerProvider() + .getTracer('default') + .startSpan(`child span of sqs processing span of msg ${msgContext}`); + processChildSpan.end(); + }; + + const expectReceiver2ProcessWithNChildrenEach = ( + spans: ReadableSpan[], + numChildPerProcessSpan: number + ) => { + const awsReceiveSpan = spans.filter( + s => s.attributes[SemanticAttributes.MESSAGING_OPERATION] === 'receive' + ); + expect(awsReceiveSpan.length).toBe(1); + + const processSpans = spans.filter( + s => s.attributes[SemanticAttributes.MESSAGING_OPERATION] === 'process' + ); + expect(processSpans.length).toBe(2); + expect(processSpans[0].parentSpanId).toStrictEqual( + awsReceiveSpan[0].spanContext().spanId + ); + expect(processSpans[1].parentSpanId).toStrictEqual( + awsReceiveSpan[0].spanContext().spanId + ); + + const processChildSpans = spans.filter(s => s.kind === SpanKind.INTERNAL); + expect(processChildSpans.length).toBe(2 * numChildPerProcessSpan); + for (let i = 0; i < numChildPerProcessSpan; i++) { + expect(processChildSpans[2 * i + 0].parentSpanId).toStrictEqual( + processSpans[0].spanContext().spanId + ); + expect(processChildSpans[2 * i + 1].parentSpanId).toStrictEqual( + processSpans[1].spanContext().spanId + ); + } + }; + + const expectReceiver2ProcessWith1ChildEach = (spans: ReadableSpan[]) => { + expectReceiver2ProcessWithNChildrenEach(spans, 1); + }; + + const expectReceiver2ProcessWith2ChildEach = (spans: ReadableSpan[]) => { + expectReceiver2ProcessWithNChildrenEach(spans, 2); + }; + + const contextKeyFromTest = Symbol('context key from test'); + const contextValueFromTest = 'context value from test'; + + beforeEach(async () => { + const sqs = new AWS.SQS(); + await context.with( + context.active().setValue(contextKeyFromTest, contextValueFromTest), + async () => { + const res = await sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .promise(); + receivedMessages = res.Messages!; + } + ); + }); + + it('should create processing child with forEach', async () => { + receivedMessages.forEach(msg => { + createProcessChildSpan(msg.Body); + }); + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it('should create processing child with map', async () => { + receivedMessages.map(msg => { + createProcessChildSpan(msg.Body); + }); + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it('should not fail when mapping to non-object type', async () => { + receivedMessages + .map(msg => 'map result is string') + .map(s => 'some other string'); + }); + + it('should not fail when mapping to undefined type', async () => { + receivedMessages.map(msg => undefined).map(s => 'some other string'); + }); + + it('should create one processing child when throws in map', async () => { + try { + receivedMessages.map(msg => { + createProcessChildSpan(msg.Body); + throw Error('error from array.map'); + }); + } catch (err) {} + + const processChildSpans = getTestSpans().filter( + s => s.kind === SpanKind.INTERNAL + ); + expect(processChildSpans.length).toBe(1); + }); + + it('should create processing child with two forEach', async () => { + receivedMessages.forEach(msg => { + createProcessChildSpan(msg.Body); + }); + receivedMessages.forEach(msg => { + createProcessChildSpan(msg.Body); + }); + expectReceiver2ProcessWith2ChildEach(getTestSpans()); + }); + + it('should forward all parameters to forEach callback', async () => { + const objectForThis = {}; + receivedMessages.forEach(function (this: any, msg, index, array) { + expect(msg).not.toBeUndefined(); + expect(index).toBeLessThan(2); + expect(index).toBeGreaterThanOrEqual(0); + expect(array).toBe(receivedMessages); + expect(this).toBe(objectForThis); + }, objectForThis); + }); + + it('should create one processing child with forEach that throws', async () => { + try { + receivedMessages.forEach(msg => { + createProcessChildSpan(msg.Body); + throw Error('error from forEach'); + }); + } catch (err) {} + const processChildSpans = getTestSpans().filter( + s => s.kind === SpanKind.INTERNAL + ); + expect(processChildSpans.length).toBe(1); + }); + + it.skip('should create processing child with array index access', async () => { + for (let i = 0; i < receivedMessages.length; i++) { + const msg = receivedMessages[i]; + createProcessChildSpan(msg.Body); + } + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it('should create processing child with map and forEach calls', async () => { + receivedMessages + .map(msg => ({ payload: msg.Body })) + .forEach(msgBody => { + createProcessChildSpan(msgBody); + }); + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it('should create processing child with filter and forEach', async () => { + receivedMessages + .filter(msg => msg) + .forEach(msgBody => { + createProcessChildSpan(msgBody); + }); + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it.skip('should create processing child with for(msg of messages)', () => { + for (const msg of receivedMessages) { + createProcessChildSpan(msg.Body); + } + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it.skip('should create processing child with array.values() for loop', () => { + for (const msg of receivedMessages.values()) { + createProcessChildSpan(msg.Body); + } + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it.skip('should create processing child with array.values() for loop and awaits in process', async () => { + for (const msg of receivedMessages.values()) { + await new Promise(resolve => setImmediate(resolve)); + createProcessChildSpan(msg.Body); + } + expectReceiver2ProcessWith1ChildEach(getTestSpans()); + }); + + it('should propagate the context of the receive call in process spans loop', async () => { + receivedMessages.forEach(() => { + expect(context.active().getValue(contextKeyFromTest)).toStrictEqual( + contextValueFromTest + ); + }); + }); + }); + + describe('hooks', () => { + it('sqsProcessHook called and add message attribute to span', async () => { + const config = { + sqsProcessHook: ( + span: Span, + sqsProcessInfo: AwsSdkSqsProcessHookInformation + ) => { + span.setAttribute( + 'attribute from sqs process hook', + sqsProcessInfo.message.Body! + ); + }, + }; + + instrumentation.setConfig(config); + + const sqs = new AWS.SQS(); + const res = await sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .promise(); + res.Messages?.map( + message => 'some mapping to create child process spans' + ); + + const processSpans = getTestSpans().filter( + s => s.attributes[SemanticAttributes.MESSAGING_OPERATION] === 'process' + ); + expect(processSpans.length).toBe(2); + expect( + processSpans[0].attributes['attribute from sqs process hook'] + ).toBe('msg 1 payload'); + expect( + processSpans[1].attributes['attribute from sqs process hook'] + ).toBe('msg 2 payload'); + }); + + it('sqsProcessHook not set in config', async () => { + const sqs = new AWS.SQS(); + const res = await sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .promise(); + res.Messages?.map( + message => 'some mapping to create child process spans' + ); + const processSpans = getTestSpans().filter( + s => s.attributes[SemanticAttributes.MESSAGING_OPERATION] === 'process' + ); + expect(processSpans.length).toBe(2); + }); + + it('sqsProcessHook throws does not fail span', async () => { + const config = { + sqsProcessHook: ( + span: Span, + sqsProcessInfo: AwsSdkSqsProcessHookInformation + ) => { + throw new Error('error from sqsProcessHook hook'); + }, + }; + instrumentation.setConfig(config); + + const sqs = new AWS.SQS(); + const res = await sqs + .receiveMessage({ + QueueUrl: 'queue/url/for/unittests', + }) + .promise(); + res.Messages?.map( + message => 'some mapping to create child process spans' + ); + + const processSpans = getTestSpans().filter( + s => s.attributes[SemanticAttributes.MESSAGING_OPERATION] === 'process' + ); + expect(processSpans.length).toBe(2); + expect(processSpans[0].status.code).toStrictEqual(SpanStatusCode.UNSET); + expect(processSpans[1].status.code).toStrictEqual(SpanStatusCode.UNSET); + }); + }); +}); diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts new file mode 100644 index 000000000..a08f3e123 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts @@ -0,0 +1,88 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { context } from '@opentelemetry/api'; +import { isTracingSuppressed } from '@opentelemetry/core'; +import { getInstrumentation } from '@opentelemetry/contrib-test-utils'; +import * as expect from 'expect'; +import * as AWS from 'aws-sdk'; + +// we want to mock the request object and trigger events on it's events emitter. +// the event emitter is not part of the public interface, so we create a type +// for the mock to use it. +type CompleteEventHandler = (response: AWS.Response) => void; +type RequestWithEvents = AWS.Request & { + _events: { complete: CompleteEventHandler[] }; +}; + +export const mockV2AwsSend = ( + sendResult: any, + data: any = undefined, + expectedInstrumentationSuppressed = false +) => { + // since we are setting a new value to a function being patched by the instrumentation, + // we need to disable and enable again to make the patch for the new function. + // I would like to see another pattern for this in the future, for example - patching only + // once and just setting the result and data, or patching the http layer instead with nock package. + getInstrumentation()?.disable(); + AWS.Request.prototype.send = function ( + this: RequestWithEvents, + cb?: (error: any, response: any) => void + ) { + expect(isTracingSuppressed(context.active())).toStrictEqual( + expectedInstrumentationSuppressed + ); + if (cb) { + (this as AWS.Request).on('complete', response => { + cb(response.error, response); + }); + } + const response = { + ...sendResult, + data, + request: this, + }; + setImmediate(() => { + this._events.complete.forEach( + (handler: (response: AWS.Response) => void) => + handler(response) + ); + }); + return response; + }; + + AWS.Request.prototype.promise = function (this: RequestWithEvents) { + expect(isTracingSuppressed(context.active())).toStrictEqual( + expectedInstrumentationSuppressed + ); + const response = { + ...sendResult, + data, + request: this, + }; + setImmediate(() => { + this._events.complete.forEach( + (handler: (response: AWS.Response) => void) => + handler(response) + ); + }); + return new Promise(resolve => + setImmediate(() => { + resolve(data); + }) + ); + }; + getInstrumentation()?.enable(); +}; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/tsconfig.json b/plugins/node/opentelemetry-instrumentation-aws-sdk/tsconfig.json new file mode 100644 index 000000000..505b67a24 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] + } + \ No newline at end of file diff --git a/release-please-config.json b/release-please-config.json index edf8116fe..5af239e30 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -1,6 +1,8 @@ { "bootstrap-sha": "798fb2d5c585f6fdee10146bfe15e03409bdddfe", - "plugins": ["node-workspace"], + "plugins": [ + "node-workspace" + ], "bump-minor-pre-major": true, "packages": { "detectors/node/opentelemetry-resource-detector-alibaba-cloud": {}, @@ -14,6 +16,7 @@ "packages/opentelemetry-id-generator-aws-xray": {}, "packages/opentelemetry-test-utils": {}, "plugins/node/opentelemetry-instrumentation-aws-lambda": {}, + "plugins/node/opentelemetry-instrumentation-aws-sdk": {}, "plugins/node/opentelemetry-instrumentation-bunyan": {}, "plugins/node/opentelemetry-instrumentation-cassandra": {}, "plugins/node/opentelemetry-instrumentation-connect": {}, @@ -44,4 +47,4 @@ "propagators/opentelemetry-propagator-grpc-census-binary": {}, "propagators/opentelemetry-propagator-ot-trace": {} } -} +} \ No newline at end of file