From f865143d9042c41ebed6adbe906097ad7622f2c7 Mon Sep 17 00:00:00 2001 From: Motti Bechhofer Date: Tue, 22 Nov 2022 12:58:02 +0200 Subject: [PATCH 1/7] feat: add socket.io instrumentation (#1284) --- .github/component_owners.yml | 3 + .release-please-manifest.json | 2 +- .../auto-instrumentations-node/README.md | 1 + .../auto-instrumentations-node/package.json | 1 + .../auto-instrumentations-node/src/utils.ts | 2 + .../instrumentation-socket.io/.eslintignore | 1 + .../instrumentation-socket.io/.eslintrc.js | 7 + .../node/instrumentation-socket.io/.npmignore | 4 + .../node/instrumentation-socket.io/.tav.yml | 15 + .../node/instrumentation-socket.io/LICENSE | 201 ++++++++ .../node/instrumentation-socket.io/README.md | 76 +++ .../instrumentation-socket.io/package.json | 66 +++ .../src/AttributeNames.ts | 20 + .../instrumentation-socket.io/src/index.ts | 18 + .../src/socket.io.ts | 450 ++++++++++++++++++ .../instrumentation-socket.io/src/types.ts | 40 ++ .../instrumentation-socket.io/src/utils.ts | 49 ++ .../test/config.test.ts | 32 ++ .../test/socket.io.test.ts | 449 +++++++++++++++++ .../instrumentation-socket.io/test/utils.ts | 75 +++ .../instrumentation-socket.io/tsconfig.json | 11 + release-please-config.json | 1 + 22 files changed, 1523 insertions(+), 1 deletion(-) create mode 100644 plugins/node/instrumentation-socket.io/.eslintignore create mode 100644 plugins/node/instrumentation-socket.io/.eslintrc.js create mode 100644 plugins/node/instrumentation-socket.io/.npmignore create mode 100644 plugins/node/instrumentation-socket.io/.tav.yml create mode 100644 plugins/node/instrumentation-socket.io/LICENSE create mode 100644 plugins/node/instrumentation-socket.io/README.md create mode 100644 plugins/node/instrumentation-socket.io/package.json create mode 100644 plugins/node/instrumentation-socket.io/src/AttributeNames.ts create mode 100644 plugins/node/instrumentation-socket.io/src/index.ts create mode 100644 plugins/node/instrumentation-socket.io/src/socket.io.ts create mode 100644 plugins/node/instrumentation-socket.io/src/types.ts create mode 100644 plugins/node/instrumentation-socket.io/src/utils.ts create mode 100644 plugins/node/instrumentation-socket.io/test/config.test.ts create mode 100644 plugins/node/instrumentation-socket.io/test/socket.io.test.ts create mode 100644 plugins/node/instrumentation-socket.io/test/utils.ts create mode 100644 plugins/node/instrumentation-socket.io/tsconfig.json diff --git a/.github/component_owners.yml b/.github/component_owners.yml index 033888085b..acc7575d3a 100644 --- a/.github/component_owners.yml +++ b/.github/component_owners.yml @@ -56,6 +56,9 @@ components: - rauno56 plugins/node/opentelemetry-instrumentation-pg: - rauno56 + plugins/node/opentelemetry-instrumentation-socket.io: + - mottibec + - rauno56 plugins/web/opentelemetry-instrumentation-document-load: - obecny plugins/web/opentelemetry-instrumentation-long-task: diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 3abf9b2078..e41db53c94 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,2 +1,2 @@ -{"detectors/node/opentelemetry-resource-detector-alibaba-cloud":"0.27.3","detectors/node/opentelemetry-resource-detector-aws":"1.2.1","detectors/node/opentelemetry-resource-detector-container":"0.2.1","detectors/node/opentelemetry-resource-detector-gcp":"0.27.4","detectors/node/opentelemetry-resource-detector-github":"0.27.0","detectors/node/opentelemetry-resource-detector-instana":"0.4.0","metapackages/auto-instrumentations-node":"0.35.0","metapackages/auto-instrumentations-web":"0.31.0","packages/opentelemetry-host-metrics":"0.31.0","packages/opentelemetry-id-generator-aws-xray":"1.1.1","packages/opentelemetry-propagation-utils":"0.29.1","packages/opentelemetry-test-utils":"0.33.0","plugins/node/instrumentation-amqplib":"0.32.0","plugins/node/instrumentation-dataloader":"0.3.0","plugins/node/instrumentation-fs":"0.6.0","plugins/node/instrumentation-lru-memoizer":"0.32.0","plugins/node/instrumentation-mongoose":"0.32.0","plugins/node/instrumentation-tedious":"0.5.0","plugins/node/opentelemetry-instrumentation-aws-lambda":"0.34.0","plugins/node/opentelemetry-instrumentation-aws-sdk":"0.10.0","plugins/node/opentelemetry-instrumentation-bunyan":"0.31.0","plugins/node/opentelemetry-instrumentation-cassandra":"0.32.0","plugins/node/opentelemetry-instrumentation-connect":"0.31.0","plugins/node/opentelemetry-instrumentation-dns":"0.31.0","plugins/node/opentelemetry-instrumentation-express":"0.32.0","plugins/node/opentelemetry-instrumentation-fastify":"0.31.0","plugins/node/opentelemetry-instrumentation-generic-pool":"0.31.0","plugins/node/opentelemetry-instrumentation-graphql":"0.33.0","plugins/node/opentelemetry-instrumentation-hapi":"0.31.0","plugins/node/opentelemetry-instrumentation-ioredis":"0.33.0","plugins/node/opentelemetry-instrumentation-knex":"0.31.0","plugins/node/opentelemetry-instrumentation-koa":"0.34.0","plugins/node/opentelemetry-instrumentation-memcached":"0.31.0","plugins/node/opentelemetry-instrumentation-mongodb":"0.33.0","plugins/node/opentelemetry-instrumentation-mysql":"0.32.0","plugins/node/opentelemetry-instrumentation-mysql2":"0.33.0","plugins/node/opentelemetry-instrumentation-nestjs-core":"0.32.0","plugins/node/opentelemetry-instrumentation-net":"0.31.0","plugins/node/opentelemetry-instrumentation-pg":"0.33.0","plugins/node/opentelemetry-instrumentation-pino":"0.33.0","plugins/node/opentelemetry-instrumentation-redis":"0.34.0","plugins/node/opentelemetry-instrumentation-redis-4":"0.34.0","plugins/node/opentelemetry-instrumentation-restify":"0.31.0","plugins/node/opentelemetry-instrumentation-router":"0.32.0","plugins/node/opentelemetry-instrumentation-winston":"0.31.0","plugins/web/opentelemetry-instrumentation-document-load":"0.31.0","plugins/web/opentelemetry-instrumentation-long-task":"0.32.0","plugins/web/opentelemetry-instrumentation-user-interaction":"0.32.0","plugins/web/opentelemetry-plugin-react-load":"0.28.1","propagators/opentelemetry-propagator-aws-xray":"1.1.1","propagators/opentelemetry-propagator-grpc-census-binary":"0.26.0","propagators/opentelemetry-propagator-instana":"0.2.1","propagators/opentelemetry-propagator-ot-trace":"0.26.2"} +{"detectors/node/opentelemetry-resource-detector-alibaba-cloud":"0.27.3","detectors/node/opentelemetry-resource-detector-aws":"1.2.1","detectors/node/opentelemetry-resource-detector-container":"0.2.1","detectors/node/opentelemetry-resource-detector-gcp":"0.27.4","detectors/node/opentelemetry-resource-detector-github":"0.27.0","detectors/node/opentelemetry-resource-detector-instana":"0.4.0","metapackages/auto-instrumentations-node":"0.35.0","metapackages/auto-instrumentations-web":"0.31.0","packages/opentelemetry-host-metrics":"0.31.0","packages/opentelemetry-id-generator-aws-xray":"1.1.1","packages/opentelemetry-propagation-utils":"0.29.1","packages/opentelemetry-test-utils":"0.33.0","plugins/node/instrumentation-amqplib":"0.32.0","plugins/node/instrumentation-dataloader":"0.3.0","plugins/node/instrumentation-fs":"0.6.0","plugins/node/instrumentation-lru-memoizer":"0.32.0","plugins/node/instrumentation-mongoose":"0.32.0", "plugins/node/instrumentation-socket.io": "0.32.0","plugins/node/instrumentation-tedious":"0.5.0","plugins/node/opentelemetry-instrumentation-aws-lambda":"0.34.0","plugins/node/opentelemetry-instrumentation-aws-sdk":"0.10.0","plugins/node/opentelemetry-instrumentation-bunyan":"0.31.0","plugins/node/opentelemetry-instrumentation-cassandra":"0.32.0","plugins/node/opentelemetry-instrumentation-connect":"0.31.0","plugins/node/opentelemetry-instrumentation-dns":"0.31.0","plugins/node/opentelemetry-instrumentation-express":"0.32.0","plugins/node/opentelemetry-instrumentation-fastify":"0.31.0","plugins/node/opentelemetry-instrumentation-generic-pool":"0.31.0","plugins/node/opentelemetry-instrumentation-graphql":"0.33.0","plugins/node/opentelemetry-instrumentation-hapi":"0.31.0","plugins/node/opentelemetry-instrumentation-ioredis":"0.33.0","plugins/node/opentelemetry-instrumentation-knex":"0.31.0","plugins/node/opentelemetry-instrumentation-koa":"0.34.0","plugins/node/opentelemetry-instrumentation-memcached":"0.31.0","plugins/node/opentelemetry-instrumentation-mongodb":"0.33.0","plugins/node/opentelemetry-instrumentation-mysql":"0.32.0","plugins/node/opentelemetry-instrumentation-mysql2":"0.33.0","plugins/node/opentelemetry-instrumentation-nestjs-core":"0.32.0","plugins/node/opentelemetry-instrumentation-net":"0.31.0","plugins/node/opentelemetry-instrumentation-pg":"0.33.0","plugins/node/opentelemetry-instrumentation-pino":"0.33.0","plugins/node/opentelemetry-instrumentation-redis":"0.34.0","plugins/node/opentelemetry-instrumentation-redis-4":"0.34.0","plugins/node/opentelemetry-instrumentation-restify":"0.31.0","plugins/node/opentelemetry-instrumentation-router":"0.32.0","plugins/node/opentelemetry-instrumentation-winston":"0.31.0","plugins/web/opentelemetry-instrumentation-document-load":"0.31.0","plugins/web/opentelemetry-instrumentation-long-task":"0.32.0","plugins/web/opentelemetry-instrumentation-user-interaction":"0.32.0","plugins/web/opentelemetry-plugin-react-load":"0.28.1","propagators/opentelemetry-propagator-aws-xray":"1.1.1","propagators/opentelemetry-propagator-grpc-census-binary":"0.26.0","propagators/opentelemetry-propagator-instana":"0.2.1","propagators/opentelemetry-propagator-ot-trace":"0.26.2"} diff --git a/metapackages/auto-instrumentations-node/README.md b/metapackages/auto-instrumentations-node/README.md index 21b7c9c8d3..95ab8cff0a 100644 --- a/metapackages/auto-instrumentations-node/README.md +++ b/metapackages/auto-instrumentations-node/README.md @@ -88,6 +88,7 @@ registerInstrumentations({ - [@opentelemetry/instrumentation-pino](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-pino) - [@opentelemetry/instrumentation-redis](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-redis) - [@opentelemetry/instrumentation-restify](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-restify) +- [@opentelemetry/instrumentation-socket.io](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-socket.io) - [@opentelemetry/instrumentation-winston](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-winston) ## Useful links diff --git a/metapackages/auto-instrumentations-node/package.json b/metapackages/auto-instrumentations-node/package.json index 255857d49a..5ad4e86a17 100644 --- a/metapackages/auto-instrumentations-node/package.json +++ b/metapackages/auto-instrumentations-node/package.json @@ -80,6 +80,7 @@ "@opentelemetry/instrumentation-redis-4": "^0.34.0", "@opentelemetry/instrumentation-restify": "^0.31.0", "@opentelemetry/instrumentation-router": "^0.32.0", + "@opentelemetry/instrumentation-socket.io": "^0.32.0", "@opentelemetry/instrumentation-tedious": "^0.5.0", "@opentelemetry/instrumentation-winston": "^0.31.0" } diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index 2daba2146d..dfb7a511a4 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -50,6 +50,7 @@ import { RedisInstrumentation as RedisInstrumentationV2 } from '@opentelemetry/i import { RedisInstrumentation as RedisInstrumentationV4 } from '@opentelemetry/instrumentation-redis-4'; import { RestifyInstrumentation } from '@opentelemetry/instrumentation-restify'; import { RouterInstrumentation } from '@opentelemetry/instrumentation-router'; +import { SocketIoInstrumentation } from '@opentelemetry/instrumentation-socket.io'; import { TediousInstrumentation } from '@opentelemetry/instrumentation-tedious'; import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston'; @@ -88,6 +89,7 @@ const InstrumentationMap = { '@opentelemetry/instrumentation-redis-4': RedisInstrumentationV4, '@opentelemetry/instrumentation-restify': RestifyInstrumentation, '@opentelemetry/instrumentation-router': RouterInstrumentation, + '@opentelemetry/instrumentation-socket.io': SocketIoInstrumentation, '@opentelemetry/instrumentation-tedious': TediousInstrumentation, '@opentelemetry/instrumentation-winston': WinstonInstrumentation, }; diff --git a/plugins/node/instrumentation-socket.io/.eslintignore b/plugins/node/instrumentation-socket.io/.eslintignore new file mode 100644 index 0000000000..378eac25d3 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/.eslintignore @@ -0,0 +1 @@ +build diff --git a/plugins/node/instrumentation-socket.io/.eslintrc.js b/plugins/node/instrumentation-socket.io/.eslintrc.js new file mode 100644 index 0000000000..f756f4488b --- /dev/null +++ b/plugins/node/instrumentation-socket.io/.eslintrc.js @@ -0,0 +1,7 @@ +module.exports = { + "env": { + "mocha": true, + "node": true + }, + ...require('../../../eslint.config.js') +} diff --git a/plugins/node/instrumentation-socket.io/.npmignore b/plugins/node/instrumentation-socket.io/.npmignore new file mode 100644 index 0000000000..9505ba9450 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/.npmignore @@ -0,0 +1,4 @@ +/bin +/coverage +/doc +/test diff --git a/plugins/node/instrumentation-socket.io/.tav.yml b/plugins/node/instrumentation-socket.io/.tav.yml new file mode 100644 index 0000000000..2c704e4634 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/.tav.yml @@ -0,0 +1,15 @@ +'socket.io': + - versions: "2.1.1 || 2.2.0 || 2.3.0 || 2.4.0 || ^2.5.0" + peerDependencies: socket.io-client@^2 + commands: + - npm run test + + - versions: "^3.1.3" + peerDependencies: socket.io-client@^3 + commands: + - npm run test + + - versions: "4.4.1 || 4.5.1 || 4.5.2 || ^4.5.3" + peerDependencies: socket.io-client@^4 + commands: + - npm run test diff --git a/plugins/node/instrumentation-socket.io/LICENSE b/plugins/node/instrumentation-socket.io/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/plugins/node/instrumentation-socket.io/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/instrumentation-socket.io/README.md b/plugins/node/instrumentation-socket.io/README.md new file mode 100644 index 0000000000..ca242046f9 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/README.md @@ -0,0 +1,76 @@ +# OpenTelemetry socket.io Instrumentation for Node.js + +[![NPM Published Version][npm-img]][npm-url] +[![Apache License][license-image]][license-image] + +This module provides automatic instrumentation for the [`socket.io`](https://github.com/socketio/socket.io) module, which may be loaded using the [`@opentelemetry/sdk-trace-node`](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package and is included in the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle. + +If total installation size is not constrained, it is recommended to use the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle with [@opentelemetry/sdk-node](`https://www.npmjs.com/package/@opentelemetry/sdk-node`) for the most seamless instrumentation experience. + +Compatible with OpenTelemetry JS API and SDK `1.0+`. + +## Installation + +```bash +npm install --save @opentelemetry/instrumentation-socket.io +``` + +## Supported Versions + +- `>=2 <5` + +## Usage + +To load a specific plugin, specify it in the registerInstrumentations's configuration: + +```js +const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node"); +const { + SocketIoInstrumentation, +} = require("@opentelemetry/instrumentation-socket.io"); +const { registerInstrumentations } = require("@opentelemetry/instrumentation"); + +const provider = new NodeTracerProvider(); +provider.register(); + +registerInstrumentations({ + instrumentations: [new SocketIoInstrumentation()], +}); +``` + +## Optional Parameters + +| Option | Type | Default | Description | +| ------ | ---- | ------- | ----------- | +| `emitHook` | `SocketIoHookFunction` | `undefined` | hook for adding custom attributes before socket.io emits the event | +| `emitIgnoreEventList` | `string[]` | `[]` | names of emitted events to ignore tracing for | +| `onHook` | `SocketIoHookFunction` | `undefined` | hook for adding custom attributes before the event listener (callback) is invoked | +| `onIgnoreEventList` | `string[]` | `[]` | names of listened events to ignore tracing for | +| `traceReserved` | `boolean` | `false` | set to true if you want to trace socket.io reserved events (see [docs](https://socket.io/docs/v4/emit-cheatsheet/#Reserved-events)) | + +## Migration From opentelemetry-instrumentation-socket.io + +This instrumentation was originally published and maintained under the name `"opentelemetry-instrumentation-socket.io"` in [this repo](https://github.com/aspecto-io/opentelemetry-ext-js). + +Few breaking changes were made during porting to the contrib repo: + +### filterHttpTransport + +The instrumentation's config `filterHttpTransport` option was removed to decouple this instrumentation from the http instrumentation. +if you do not want to trace the socket.io http requests, add the default socket.io route (`/socket.io/`) to the `HttpInstrumentationConfig.ignoreIncomingPaths` array + +## 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 +[npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-socket.io +[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-socket.io.svg diff --git a/plugins/node/instrumentation-socket.io/package.json b/plugins/node/instrumentation-socket.io/package.json new file mode 100644 index 0000000000..f6b1191099 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/package.json @@ -0,0 +1,66 @@ +{ + "name": "@opentelemetry/instrumentation-socket.io", + "version": "0.32.0", + "description": "OpenTelemetry automatic instrumentation package for socket.io", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "repository": "open-telemetry/opentelemetry-js-contrib", + "scripts": { + "test": "ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/*.test.ts'", + "test-all-versions": "tav", + "tdd": "npm run test -- --watch-extensions ts --watch", + "clean": "rimraf build/*", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "precompile": "tsc --version && lerna run version --scope @opentelemetry/instrumentation-socket.io --include-dependencies", + "prewatch": "npm run precompile", + "prepare": "npm run compile", + "version:update": "node ../../../scripts/version-update.js", + "compile": "npm run version:update && tsc -p ." + }, + "keywords": [ + "socket.io", + "instrumentation", + "nodejs", + "opentelemetry", + "tracing" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "engines": { + "node": ">=14.0" + }, + "files": [ + "build/src/**/*.js", + "build/src/**/*.js.map", + "build/src/**/*.d.ts" + ], + "publishConfig": { + "access": "public" + }, + "peerDependencies": { + "@opentelemetry/api": "^1.3.0" + }, + "devDependencies": { + "@opentelemetry/api": "^1.3.0", + "@opentelemetry/contrib-test-utils": "^0.33.0", + "@opentelemetry/sdk-trace-base": "^1.8.0", + "@types/mocha": "8.2.3", + "@types/node": "18.11.7", + "expect": "27.4.2", + "gts": "3.1.0", + "mocha": "7.2.0", + "nyc": "15.1.0", + "rimraf": "3.0.2", + "socket.io": "^4.1.3", + "socket.io-client": "^4.1.3", + "test-all-versions": "5.0.1", + "ts-mocha": "10.0.0", + "typescript": "4.3.5" + }, + "dependencies": { + "@opentelemetry/instrumentation": "^0.34.0", + "@opentelemetry/semantic-conventions": "^1.0.0" + }, + "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-socket.io#readme" +} diff --git a/plugins/node/instrumentation-socket.io/src/AttributeNames.ts b/plugins/node/instrumentation-socket.io/src/AttributeNames.ts new file mode 100644 index 0000000000..ce46246b25 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/src/AttributeNames.ts @@ -0,0 +1,20 @@ +/* + * 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 const SocketIoInstrumentationAttributes = { + SOCKET_IO_ROOMS: 'messaging.socket.io.rooms', + SOCKET_IO_NAMESPACE: 'messaging.socket.io.namespace', + SOCKET_IO_EVENT_NAME: 'messaging.socket.io.event_name', +}; diff --git a/plugins/node/instrumentation-socket.io/src/index.ts b/plugins/node/instrumentation-socket.io/src/index.ts new file mode 100644 index 0000000000..c32b37405f --- /dev/null +++ b/plugins/node/instrumentation-socket.io/src/index.ts @@ -0,0 +1,18 @@ +/* + * 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 './socket.io'; +export * from './types'; +export * from './AttributeNames'; diff --git a/plugins/node/instrumentation-socket.io/src/socket.io.ts b/plugins/node/instrumentation-socket.io/src/socket.io.ts new file mode 100644 index 0000000000..db37e5c375 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/src/socket.io.ts @@ -0,0 +1,450 @@ +/* + * 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, + trace, + Span, + SpanKind, + SpanStatusCode, +} from '@opentelemetry/api'; +import { + InstrumentationBase, + InstrumentationNodeModuleFile, + InstrumentationNodeModuleDefinition, + isWrapped, + safeExecuteInTheMiddle, +} from '@opentelemetry/instrumentation'; +import { + SemanticAttributes, + MessagingOperationValues, + MessagingDestinationKindValues, +} from '@opentelemetry/semantic-conventions'; +import { SocketIoInstrumentationConfig } from './types'; +import { SocketIoInstrumentationAttributes } from './AttributeNames'; +import { VERSION } from './version'; +import { + extractRoomsAttributeValue, + isPromise, + normalizeConfig, +} from './utils'; + +const reservedEvents = [ + 'connect', + 'connect_error', + 'disconnect', + 'disconnecting', + 'newListener', + 'removeListener', +]; + +export class SocketIoInstrumentation extends InstrumentationBase { + protected override _config!: SocketIoInstrumentationConfig; + + constructor(config: SocketIoInstrumentationConfig = {}) { + super( + '@opentelemetry/instrumentation-socket.io', + VERSION, + normalizeConfig(config) + ); + } + + protected init() { + const socketInstrumentation = new InstrumentationNodeModuleFile( + 'socket.io/dist/socket.js', + ['>=3 <5'], + (moduleExports, moduleVersion) => { + if (moduleExports === undefined || moduleExports === null) { + return moduleExports; + } + if (moduleVersion === undefined) { + return moduleExports; + } + this._diag.debug(`applying patch to socket.io@${moduleVersion} Socket`); + if (isWrapped(moduleExports?.Socket?.prototype?.on)) { + this._unwrap(moduleExports.Socket.prototype, 'on'); + } + this._wrap( + moduleExports.Socket.prototype, + 'on', + this._patchOn(moduleVersion) + ); + if (isWrapped(moduleExports?.Socket?.prototype?.emit)) { + this._unwrap(moduleExports.Socket.prototype, 'emit'); + } + this._wrap( + moduleExports.Socket.prototype, + 'emit', + this._patchEmit(moduleVersion) + ); + return moduleExports; + }, + moduleExports => { + if (isWrapped(moduleExports?.Socket?.prototype?.on)) { + this._unwrap(moduleExports.Socket.prototype, 'on'); + } + if (isWrapped(moduleExports?.Socket?.prototype?.emit)) { + this._unwrap(moduleExports.Socket.prototype, 'emit'); + } + return moduleExports; + } + ); + + const broadcastOperatorInstrumentation = + new InstrumentationNodeModuleFile( + 'socket.io/dist/broadcast-operator.js', + ['>=4 <5'], + (moduleExports, moduleVersion) => { + if (moduleExports === undefined || moduleExports === null) { + return moduleExports; + } + if (moduleVersion === undefined) { + return moduleExports; + } + this._diag.debug( + `applying patch to socket.io@${moduleVersion} StrictEventEmitter` + ); + if (isWrapped(moduleExports?.BroadcastOperator?.prototype?.emit)) { + this._unwrap(moduleExports.BroadcastOperator.prototype, 'emit'); + } + this._wrap( + moduleExports.BroadcastOperator.prototype, + 'emit', + this._patchEmit(moduleVersion) + ); + return moduleExports; + }, + moduleExports => { + if (isWrapped(moduleExports?.BroadcastOperator?.prototype?.emit)) { + this._unwrap(moduleExports.BroadcastOperator.prototype, 'emit'); + } + return moduleExports; + } + ); + const namespaceInstrumentation = new InstrumentationNodeModuleFile( + 'socket.io/dist/namespace.js', + ['<4'], + (moduleExports, moduleVersion) => { + if (moduleExports === undefined || moduleExports === null) { + return moduleExports; + } + if (moduleVersion === undefined) { + return moduleExports; + } + this._diag.debug( + `applying patch to socket.io@${moduleVersion} Namespace` + ); + if (isWrapped(moduleExports?.Namespace?.prototype?.emit)) { + this._unwrap(moduleExports.Namespace.prototype, 'emit'); + } + this._wrap( + moduleExports.Namespace.prototype, + 'emit', + this._patchEmit(moduleVersion) + ); + return moduleExports; + }, + moduleExports => { + if (isWrapped(moduleExports?.Namespace?.prototype?.emit)) { + this._unwrap(moduleExports.Namespace.prototype, 'emit'); + } + } + ); + const socketInstrumentationLegacy = new InstrumentationNodeModuleFile( + 'socket.io/lib/socket.js', + ['2'], + (moduleExports, moduleVersion) => { + if (moduleExports === undefined || moduleExports === null) { + return moduleExports; + } + if (moduleVersion === undefined) { + return moduleExports; + } + this._diag.debug(`applying patch to socket.io@${moduleVersion} Socket`); + if (isWrapped(moduleExports.prototype?.on)) { + this._unwrap(moduleExports.prototype, 'on'); + } + this._wrap(moduleExports.prototype, 'on', this._patchOn(moduleVersion)); + if (isWrapped(moduleExports.prototype?.emit)) { + this._unwrap(moduleExports.prototype, 'emit'); + } + this._wrap( + moduleExports.prototype, + 'emit', + this._patchEmit(moduleVersion) + ); + return moduleExports; + }, + moduleExports => { + if (isWrapped(moduleExports.prototype?.on)) { + this._unwrap(moduleExports.prototype, 'on'); + } + if (isWrapped(moduleExports.prototype?.emit)) { + this._unwrap(moduleExports.prototype, 'emit'); + } + return moduleExports; + } + ); + const namespaceInstrumentationLegacy = + new InstrumentationNodeModuleFile( + 'socket.io/lib/namespace.js', + ['2'], + (moduleExports, moduleVersion) => { + if (moduleExports === undefined || moduleExports === null) { + return moduleExports; + } + if (moduleVersion === undefined) { + return moduleExports; + } + this._diag.debug( + `applying patch to socket.io@${moduleVersion} Namespace` + ); + if (isWrapped(moduleExports?.prototype?.emit)) { + this._unwrap(moduleExports.prototype, 'emit'); + } + this._wrap( + moduleExports.prototype, + 'emit', + this._patchEmit(moduleVersion) + ); + return moduleExports; + }, + moduleExports => { + if (isWrapped(moduleExports?.prototype?.emit)) { + this._unwrap(moduleExports.prototype, 'emit'); + } + } + ); + + return [ + new InstrumentationNodeModuleDefinition( + 'socket.io', + ['>=3 <5'], + (moduleExports, moduleVersion) => { + if (moduleExports === undefined || moduleExports === null) { + return moduleExports; + } + if (moduleVersion === undefined) { + return moduleExports; + } + this._diag.debug( + `applying patch to socket.io@${moduleVersion} Server` + ); + if (isWrapped(moduleExports?.Server?.prototype?.on)) { + this._unwrap(moduleExports.Server.prototype, 'on'); + } + this._wrap( + moduleExports.Server.prototype, + 'on', + this._patchOn(moduleVersion) + ); + return moduleExports; + }, + (moduleExports, moduleVersion) => { + if (isWrapped(moduleExports?.Server?.prototype?.on)) { + this._unwrap(moduleExports.Server.prototype, 'on'); + } + return moduleExports; + }, + [ + broadcastOperatorInstrumentation, + namespaceInstrumentation, + socketInstrumentation, + ] + ), + new InstrumentationNodeModuleDefinition( + 'socket.io', + ['2'], + (moduleExports, moduleVersion) => { + if (moduleExports === undefined || moduleExports === null) { + return moduleExports; + } + if (moduleVersion === undefined) { + return moduleExports; + } + this._diag.debug( + `applying patch to socket.io@${moduleVersion} Server` + ); + if (isWrapped(moduleExports?.prototype?.on)) { + this._unwrap(moduleExports.prototype, 'on'); + } + this._wrap( + moduleExports.prototype, + 'on', + this._patchOn(moduleVersion) + ); + return moduleExports; + }, + (moduleExports, moduleVersion) => { + if (isWrapped(moduleExports?.prototype?.on)) { + this._unwrap(moduleExports.prototype, 'on'); + } + return moduleExports; + }, + [namespaceInstrumentationLegacy, socketInstrumentationLegacy] + ), + ]; + } + + override setConfig(config: SocketIoInstrumentationConfig = {}) { + return super.setConfig(normalizeConfig(config)); + } + + private _patchOn(moduleVersion: string) { + const self = this; + return (original: Function) => { + return function (this: any, ev: any, originalListener: Function) { + if (!self._config.traceReserved && reservedEvents.includes(ev)) { + return original.apply(this, arguments); + } + if (self._config.onIgnoreEventList?.includes(ev)) { + return original.apply(this, arguments); + } + const wrappedListener = function (this: any, ...args: any[]) { + const eventName = ev; + const defaultNamespace = '/'; + const namespace = this.name || this.adapter?.nsp?.name; + const destination = + namespace === defaultNamespace + ? eventName + : `${namespace} ${eventName}`; + const span: Span = self.tracer.startSpan( + `${destination} ${MessagingOperationValues.RECEIVE}`, + { + kind: SpanKind.CONSUMER, + attributes: { + [SemanticAttributes.MESSAGING_SYSTEM]: 'socket.io', + [SemanticAttributes.MESSAGING_DESTINATION]: namespace, + [SemanticAttributes.MESSAGING_OPERATION]: + MessagingOperationValues.RECEIVE, + [SocketIoInstrumentationAttributes.SOCKET_IO_EVENT_NAME]: + eventName, + }, + } + ); + + if (self._config.onHook) { + safeExecuteInTheMiddle( + () => + self._config?.onHook?.(span, { moduleVersion, payload: args }), + e => { + if (e) self._diag.error('onHook error', e); + }, + true + ); + } + return context.with(trace.setSpan(context.active(), span), () => + self.endSpan(() => originalListener.apply(this, arguments), span) + ); + }; + return original.apply(this, [ev, wrappedListener]); + }; + }; + } + + private endSpan(traced: () => any | Promise, span: Span) { + try { + const result = traced(); + if (isPromise(result)) { + return result.then( + value => { + span.end(); + return value; + }, + err => { + span.recordException(err); + span.setStatus({ + code: SpanStatusCode.ERROR, + message: err?.message, + }); + span.end(); + throw err; + } + ); + } else { + span.end(); + return result; + } + } catch (error: any) { + span.recordException(error); + span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message }); + span.end(); + throw error; + } + } + + private _patchEmit(moduleVersion: string) { + const self = this; + return (original: Function) => { + return function (this: any, ev: any, ...args: any[]) { + if (!self._config.traceReserved && reservedEvents.includes(ev)) { + return original.apply(this, arguments); + } + if (self._config?.emitIgnoreEventList?.includes(ev)) { + return original.apply(this, arguments); + } + const messagingSystem = 'socket.io'; + const eventName = ev; + const attributes: any = { + [SemanticAttributes.MESSAGING_SYSTEM]: messagingSystem, + [SemanticAttributes.MESSAGING_DESTINATION_KIND]: + MessagingDestinationKindValues.TOPIC, + [SocketIoInstrumentationAttributes.SOCKET_IO_EVENT_NAME]: eventName, + }; + + const rooms = extractRoomsAttributeValue(this); + if (rooms.length) { + attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS] = rooms; + } + const namespace = + this.name || this.adapter?.nsp?.name || this.sockets?.name; + if (namespace) { + attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE] = + namespace; + attributes[SemanticAttributes.MESSAGING_DESTINATION] = namespace; + } + const spanRooms = rooms.length ? `[${rooms.join()}]` : ''; + const span = self.tracer.startSpan(`${namespace}${spanRooms} send`, { + kind: SpanKind.PRODUCER, + attributes, + }); + + if (self._config.emitHook) { + safeExecuteInTheMiddle( + () => + self._config.emitHook?.(span, { moduleVersion, payload: args }), + e => { + if (e) self._diag.error('emitHook error', e); + }, + true + ); + } + try { + return context.with(trace.setSpan(context.active(), span), () => + original.apply(this, arguments) + ); + } catch (error: any) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error.message, + }); + throw error; + } finally { + span.end(); + } + }; + }; + } +} diff --git a/plugins/node/instrumentation-socket.io/src/types.ts b/plugins/node/instrumentation-socket.io/src/types.ts new file mode 100644 index 0000000000..ec432e0566 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/src/types.ts @@ -0,0 +1,40 @@ +/* + * 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'; + +export const defaultSocketIoPath = '/socket.io/'; + +export interface SocketIoHookInfo { + moduleVersion?: string; + payload: any[]; +} +export interface SocketIoHookFunction { + (span: Span, hookInfo: SocketIoHookInfo): void; +} + +export interface SocketIoInstrumentationConfig extends InstrumentationConfig { + /** Hook for adding custom attributes before socket.io emits the event */ + emitHook?: SocketIoHookFunction; + /** list of events to ignore tracing on for socket.io emits */ + emitIgnoreEventList?: string[]; + /** Hook for adding custom attributes before the event listener (callback) is invoked */ + onHook?: SocketIoHookFunction; + /** list of events to ignore tracing on for socket.io listeners */ + onIgnoreEventList?: string[]; + /** Set to `true` if you want to trace socket.io reserved events (see https://socket.io/docs/v4/emit-cheatsheet/#Reserved-events) */ + traceReserved?: boolean; +} diff --git a/plugins/node/instrumentation-socket.io/src/utils.ts b/plugins/node/instrumentation-socket.io/src/utils.ts new file mode 100644 index 0000000000..a4ace3a485 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/src/utils.ts @@ -0,0 +1,49 @@ +/* + * 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 { SocketIoInstrumentationConfig } from './types'; + +export const isPromise = (value: any): value is Promise => { + return typeof value?.then === 'function'; +}; + +export const normalizeConfig = (config?: SocketIoInstrumentationConfig) => { + config = Object.assign({}, config); + if (!Array.isArray(config.emitIgnoreEventList)) { + config.emitIgnoreEventList = []; + } + if (!Array.isArray(config.onIgnoreEventList)) { + config.onIgnoreEventList = []; + } + return config; +}; + +export const extractRoomsAttributeValue = (self: any): any[] => { + let rooms = + self.rooms || + self._rooms || + self.sockets?._rooms || + self.sockets?.rooms || + []; + // Some of the attributes above are of Set type. Convert it. + if (!Array.isArray(rooms)) { + rooms = Array.from(rooms); + } + // only for v2: this.id is only set for v2. That's to mimic later versions which have this.id in the rooms Set. + if (rooms.length === 0 && self.id) { + rooms.push(self.id); + } + return rooms; +}; diff --git a/plugins/node/instrumentation-socket.io/test/config.test.ts b/plugins/node/instrumentation-socket.io/test/config.test.ts new file mode 100644 index 0000000000..de1b89c7ac --- /dev/null +++ b/plugins/node/instrumentation-socket.io/test/config.test.ts @@ -0,0 +1,32 @@ +/* + * 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 { SocketIoInstrumentation, SocketIoInstrumentationConfig } from '../src'; +import * as expect from 'expect'; + +describe('SocketIoInstrumentationConfig', () => { + it('forces *IgnoreEventList to be an Array', () => { + const socketIoInstrumentation = new SocketIoInstrumentation({ + onIgnoreEventList: {} as any, + emitIgnoreEventList: 1 as any, + }); + + const { onIgnoreEventList, emitIgnoreEventList } = + socketIoInstrumentation.getConfig() as SocketIoInstrumentationConfig; + + expect(Array.isArray(onIgnoreEventList)).toEqual(true); + expect(Array.isArray(emitIgnoreEventList)).toEqual(true); + }); +}); diff --git a/plugins/node/instrumentation-socket.io/test/socket.io.test.ts b/plugins/node/instrumentation-socket.io/test/socket.io.test.ts new file mode 100644 index 0000000000..1b47fd5d73 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/test/socket.io.test.ts @@ -0,0 +1,449 @@ +/* + * 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 { + MessagingDestinationKindValues, + SemanticAttributes, +} from '@opentelemetry/semantic-conventions'; +import { + SocketIoInstrumentation, + SocketIoInstrumentationAttributes, + SocketIoInstrumentationConfig, +} from '../src'; +import { SpanKind, SpanStatusCode } from '@opentelemetry/api'; +import * as expect from 'expect'; +import 'mocha'; +import { registerInstrumentationTesting } from '@opentelemetry/contrib-test-utils'; + +const instrumentation = registerInstrumentationTesting( + new SocketIoInstrumentation() +); +import { Socket } from 'socket.io'; +import { + createServer, + createServerInstance, + io, + getSocketIoSpans, + expectSpan, + isV2, +} from './utils'; + +describe('SocketIoInstrumentation', () => { + beforeEach(() => { + instrumentation.enable(); + instrumentation.setConfig({}); + }); + + afterEach(() => { + instrumentation.disable(); + }); + + describe('Server', () => { + it('emit is instrumented', () => { + const io = createServerInstance(); + io.emit('test'); + expectSpan('/ send', span => { + expect(span.kind).toEqual(SpanKind.PRODUCER); + expect(span.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toEqual( + 'socket.io' + ); + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION_KIND] + ).toEqual(MessagingDestinationKindValues.TOPIC); + }); + }); + + it('emitIgnoreEventList events are ignored', () => { + const io = createServerInstance(); + const config: SocketIoInstrumentationConfig = { + emitIgnoreEventList: ['ignored'], + }; + instrumentation.setConfig(config); + io.emit('test'); + io.emit('ignored'); + expect(getSocketIoSpans().length).toEqual(1); + }); + + it('emit reserved events error is instrumented', () => { + const config: SocketIoInstrumentationConfig = { + traceReserved: true, + }; + instrumentation.setConfig(config); + const io = createServerInstance(); + try { + io.emit('connect'); + } catch (error) {} + if (isV2) { + // only for v2: connect do not throw, but are just ignored + return expectSpan('/ send', span => { + expect(span.kind).toEqual(SpanKind.PRODUCER); + expect(span.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toEqual( + 'socket.io' + ); + }); + } + expectSpan('/ send', span => { + expect(span.status.code).toEqual(SpanStatusCode.ERROR); + expect(span.status.message).toEqual( + '"connect" is a reserved event name' + ); + }); + }); + + it('send is instrumented', () => { + const io = createServerInstance(); + io.send('test'); + expectSpan('/ send', span => { + expect(span.kind).toEqual(SpanKind.PRODUCER); + expect(span.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toEqual( + 'socket.io' + ); + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION_KIND] + ).toEqual(MessagingDestinationKindValues.TOPIC); + }); + }); + + it('emitHook is called', () => { + const config: SocketIoInstrumentationConfig = { + traceReserved: true, + emitHook: (span, hookInfo) => { + span.setAttribute('payload', JSON.stringify(hookInfo.payload)); + }, + }; + instrumentation.setConfig(config); + + const io = createServerInstance(); + io.emit('test', 1234); + expectSpan('/ send', span => { + expect(span.attributes['payload']).toEqual(JSON.stringify([1234])); + }); + }); + + it('emitHook error does not effect trace', () => { + const config: SocketIoInstrumentationConfig = { + emitHook: () => { + throw new Error('Throwing'); + }, + }; + instrumentation.setConfig(config); + const io = createServerInstance(); + io.emit('test'); + const spans = getSocketIoSpans(); + expect(spans.length).toBe(1); + }); + + it('onHook is called', done => { + const config: SocketIoInstrumentationConfig = { + onHook: (span, hookInfo) => { + span.setAttribute('payload', JSON.stringify(hookInfo.payload)); + }, + // only for v2: v2 emits connection on the client side, newer versions do not + emitIgnoreEventList: ['connection'], + }; + instrumentation.setConfig(config); + const data = { + name: 'bob', + age: 28, + }; + createServer((sio, port) => { + const client = io(`http://localhost:${port}`); + client.on('test', () => client.emit('test_reply', data)); + sio.on('connection', (socket: Socket) => { + socket.emit('test'); + socket.on('test_reply', data => { + client.close(); + sio.close(); + //trace is created after the listener method is completed + setTimeout(() => { + expectSpan( + 'test_reply receive', + span => { + try { + expect(span.kind).toEqual(SpanKind.CONSUMER); + expect( + span.attributes[SemanticAttributes.MESSAGING_SYSTEM] + ).toEqual('socket.io'); + expect(span.attributes['payload']).toEqual( + JSON.stringify([data]) + ); + done(); + } catch (e) { + done(e); + } + }, + 3 + ); + }); + }); + }); + }); + }); + + it('traceReserved:true on is instrumented', done => { + const config: SocketIoInstrumentationConfig = { + traceReserved: true, + // only for v2: v2 emits [dis]connect[ing] events which later versions do not + emitIgnoreEventList: [ + 'disconnect', + 'disconnecting', + 'connection', + 'connect', + ], + }; + instrumentation.setConfig(config); + createServer((sio, port) => { + const client = io(`http://localhost:${port}`); + sio.on('connection', () => { + client.close(); + sio.close(); + //trace is created after the listener method is completed + setTimeout(() => { + expectSpan('connection receive', span => { + expect(span.kind).toEqual(SpanKind.CONSUMER); + expect( + span.attributes[SemanticAttributes.MESSAGING_SYSTEM] + ).toEqual('socket.io'); + done(); + }); + }); + }); + }); + }); + + it('on is instrumented', done => { + const config: SocketIoInstrumentationConfig = { + // only for v2: v2 emits connection events which later versions do not + emitIgnoreEventList: ['connection'], + }; + instrumentation.setConfig(config); + createServer((sio, port) => { + const client = io(`http://localhost:${port}`); + client.on('test', () => client.emit('test_reply')); + sio.on('connection', (socket: Socket) => { + socket.emit('test'); + socket.on('test_reply', () => { + client.close(); + sio.close(); + //trace is created after the listener method is completed + setTimeout(() => { + expectSpan( + 'test_reply receive', + span => { + try { + expect(span.kind).toEqual(SpanKind.CONSUMER); + expect( + span.attributes[SemanticAttributes.MESSAGING_SYSTEM] + ).toEqual('socket.io'); + done(); + } catch (e) { + done(e); + } + }, + 3 + ); + }); + }); + }); + }); + }); + + it('onIgnoreEventList events are ignored', done => { + const config: SocketIoInstrumentationConfig = { + onIgnoreEventList: ['test_reply'], + // only for v2: v2 emits connection events which later versions do not + emitIgnoreEventList: ['connection'], + }; + instrumentation.setConfig(config); + createServer((sio, port) => { + const client = io(`http://localhost:${port}`); + client.on('test', () => client.emit('test_reply')); + sio.on('connection', (socket: Socket) => { + socket.emit('test'); + socket.on('test_reply', () => { + client.close(); + sio.close(); + //trace is created after the listener method is completed + setTimeout(() => { + try { + expect(getSocketIoSpans().length).toEqual(2); + done(); + } catch (e) { + done(e); + } + }); + }); + }); + }); + }); + + it('broadcast is instrumented', () => { + const roomName = 'room'; + const sio = createServerInstance(); + sio.to(roomName).emit('broadcast', '1234'); + expectSpan('/[room] send', span => { + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION] + ).toEqual('/'); + expect( + span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS] + ).toEqual([roomName]); + }); + }); + + it('broadcast to multiple rooms', () => { + const sio = createServerInstance(); + sio.to('room1').to('room2').emit('broadcast', '1234'); + expectSpan('/[room1,room2] send', span => { + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION] + ).toEqual('/'); + expect( + span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS] + ).toEqual(['room1', 'room2']); + }); + }); + }); + + describe('Namespace', () => { + it('emit is instrumented', () => { + const io = createServerInstance(); + const namespace = io.of('/testing'); + namespace.emit('namespace'); + expectSpan('/testing send', span => { + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION] + ).toEqual('/testing'); + expect( + span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE] + ).toEqual('/testing'); + }); + }); + + it('broadcast is instrumented', () => { + const roomName = 'room'; + const io = createServerInstance(); + const namespace = io.of('/testing'); + namespace.to(roomName).emit('broadcast', '1234'); + expectSpan('/testing[room] send', span => { + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION] + ).toEqual('/testing'); + expect( + span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS] + ).toEqual([roomName]); + expect( + span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE] + ).toEqual('/testing'); + }); + }); + + it('broadcast to multiple rooms', () => { + const io = createServerInstance(); + const namespace = io.of('/testing'); + namespace.to('room1').to('room2').emit('broadcast', '1234'); + expectSpan('/testing[room1,room2] send', span => { + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION] + ).toEqual('/testing'); + expect( + span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE] + ).toEqual('/testing'); + expect( + span.attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS] + ).toEqual(['room1', 'room2']); + }); + }); + + it('on is instrumented', done => { + const config: SocketIoInstrumentationConfig = { + // only for v2: v2 emits connection events which later versions do not + emitIgnoreEventList: ['connection'], + }; + instrumentation.setConfig(config); + createServer((sio, port) => { + const namespace = sio.of('/testing'); + const client = io(`http://localhost:${port}/testing`); + client.on('test', () => client.emit('test_reply')); + namespace.on('connection', (socket: Socket) => { + socket.emit('test'); + socket.on('test_reply', () => { + client.close(); + sio.close(); + //trace is created after the listener method is completed + setTimeout(() => { + expectSpan( + '/testing test_reply receive', + span => { + try { + expect(span.kind).toEqual(SpanKind.CONSUMER); + expect( + span.attributes[SemanticAttributes.MESSAGING_SYSTEM] + ).toEqual('socket.io'); + expect( + span.attributes[SemanticAttributes.MESSAGING_DESTINATION] + ).toEqual('/testing'); + done(); + } catch (e) { + done(e); + } + }, + 2 + ); + }); + }); + }); + }); + }); + }); + + describe('Socket', () => { + it('emit is instrumented', done => { + const config: SocketIoInstrumentationConfig = { + // only for v2: v2 emits connection events which later versions do not + emitIgnoreEventList: ['connection'], + }; + instrumentation.setConfig(config); + createServer((sio, port) => { + const client = io(`http://localhost:${port}`, { + // websockets transport disconnects without the delay of the polling interval + transports: ['websocket'], + }); + sio.on('connection', (socket: Socket) => { + socket.emit('test'); + client.close(); + sio.close(); + setTimeout(() => { + expectSpan( + `/[${socket.id}] send`, + span => { + try { + expect(span.kind).toEqual(SpanKind.PRODUCER); + expect( + span.attributes[SemanticAttributes.MESSAGING_SYSTEM] + ).toEqual('socket.io'); + done(); + } catch (e) { + done(e); + } + }, + 2 + ); + }); + }); + }); + }); + }); +}); diff --git a/plugins/node/instrumentation-socket.io/test/utils.ts b/plugins/node/instrumentation-socket.io/test/utils.ts new file mode 100644 index 0000000000..86144d5f4d --- /dev/null +++ b/plugins/node/instrumentation-socket.io/test/utils.ts @@ -0,0 +1,75 @@ +/* + * 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 { strict as assert } from 'assert'; +import * as http from 'http'; +import { AddressInfo } from 'net'; + +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; +import { getTestSpans } from '@opentelemetry/contrib-test-utils'; + +import * as expect from 'expect'; +import { Server } from 'socket.io'; +import * as socketIo from 'socket.io'; +import * as ioClient from 'socket.io-client'; +import * as path from 'path'; + +export const io = ioClient.io || ioClient; + +const packageJsonPath = (packageName: string) => + path.join(path.dirname(require.resolve(packageName)), '..', 'package.json'); +const version = require(packageJsonPath('socket.io')).version; + +assert.equal(typeof version, 'string'); + +export const isV2 = version && /^2\./.test(version); + +export const createServer = ( + callback: (server: Server, port: number) => void +) => { + const server = http.createServer(); + const sio = createServerInstance(server); + server.listen(0, () => { + const port = (server.address() as AddressInfo).port; + callback(sio, port); + }); +}; + +export const createServerInstance = (server?: http.Server) => { + if (isV2) { + return (socketIo as any)(server, { serveClient: false }); + } + return new Server(server); +}; + +export const getSocketIoSpans = (): ReadableSpan[] => + getTestSpans().filter( + s => s.attributes[SemanticAttributes.MESSAGING_SYSTEM] === 'socket.io' + ) as ReadableSpan[]; + +export const expectSpan = ( + spanName: string, + callback?: (span: ReadableSpan) => void, + spanCount?: number +) => { + const spans = getSocketIoSpans(); + expect(spans.length).toEqual(spanCount || 1); + const span = spans.find(s => s.name === spanName); + expect(span).toBeDefined(); + if (span && callback) { + callback(span); + } +}; diff --git a/plugins/node/instrumentation-socket.io/tsconfig.json b/plugins/node/instrumentation-socket.io/tsconfig.json new file mode 100644 index 0000000000..3feff66260 --- /dev/null +++ b/plugins/node/instrumentation-socket.io/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build", + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +} diff --git a/release-please-config.json b/release-please-config.json index 76911a8653..6894b5d84d 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -23,6 +23,7 @@ "plugins/node/instrumentation-fs": {}, "plugins/node/instrumentation-lru-memoizer": {}, "plugins/node/instrumentation-mongoose": {}, + "plugins/node/instrumentation-socket.io": {}, "plugins/node/instrumentation-tedious": {}, "plugins/node/opentelemetry-instrumentation-aws-lambda": {}, "plugins/node/opentelemetry-instrumentation-aws-sdk": {}, From fef44f4f80770e422bb0123724cfda1067819999 Mon Sep 17 00:00:00 2001 From: Amir Blum Date: Thu, 24 Nov 2022 09:31:42 +0200 Subject: [PATCH 2/7] chore(deps): update dependency expect to v29 (#1233) * chore: upgrade expect to v29.2.0 * fix: commit forgotten file --- packages/opentelemetry-propagation-utils/package.json | 2 +- .../test/pubsub-propagation.test.ts | 2 +- plugins/node/instrumentation-amqplib/package.json | 2 +- .../node/instrumentation-amqplib/test/amqplib-callbacks.test.ts | 2 +- .../instrumentation-amqplib/test/amqplib-connection.test.ts | 2 +- .../node/instrumentation-amqplib/test/amqplib-promise.test.ts | 2 +- plugins/node/instrumentation-amqplib/test/utils.test.ts | 2 +- plugins/node/instrumentation-amqplib/test/utils.ts | 2 +- plugins/node/instrumentation-lru-memoizer/package.json | 2 +- plugins/node/instrumentation-lru-memoizer/test/index.test.ts | 2 +- plugins/node/instrumentation-mongoose/package.json | 2 +- plugins/node/instrumentation-mongoose/test/asserts.ts | 2 +- plugins/node/instrumentation-mongoose/test/mongoose.test.ts | 2 +- plugins/node/opentelemetry-instrumentation-aws-sdk/package.json | 2 +- .../test/MessageAttributes.test.ts | 2 +- .../test/aws-sdk-v2.test.ts | 2 +- .../test/aws-sdk-v3.test.ts | 2 +- .../opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts | 2 +- .../opentelemetry-instrumentation-aws-sdk/test/lambda.test.ts | 2 +- .../node/opentelemetry-instrumentation-aws-sdk/test/sns.test.ts | 2 +- .../node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts | 2 +- .../opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts | 2 +- 22 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/opentelemetry-propagation-utils/package.json b/packages/opentelemetry-propagation-utils/package.json index adae4993a2..44d2e89f8d 100644 --- a/packages/opentelemetry-propagation-utils/package.json +++ b/packages/opentelemetry-propagation-utils/package.json @@ -49,7 +49,7 @@ "@types/mocha": "^9.1.1", "@types/node": "18.11.7", "@types/sinon": "^10.0.11", - "expect": "27.4.2", + "expect": "29.2.0", "gts": "3.1.0", "mocha": "7.2.0", "nyc": "15.1.0", diff --git a/packages/opentelemetry-propagation-utils/test/pubsub-propagation.test.ts b/packages/opentelemetry-propagation-utils/test/pubsub-propagation.test.ts index 8c853b791f..62199d4f33 100644 --- a/packages/opentelemetry-propagation-utils/test/pubsub-propagation.test.ts +++ b/packages/opentelemetry-propagation-utils/test/pubsub-propagation.test.ts @@ -20,7 +20,7 @@ import { resetMemoryExporter, } from '@opentelemetry/contrib-test-utils'; import { ROOT_CONTEXT, trace } from '@opentelemetry/api'; -import * as expect from 'expect'; +import { expect } from 'expect'; registerInstrumentationTestingProvider(); diff --git a/plugins/node/instrumentation-amqplib/package.json b/plugins/node/instrumentation-amqplib/package.json index 0588be63b9..6a4282a7e6 100644 --- a/plugins/node/instrumentation-amqplib/package.json +++ b/plugins/node/instrumentation-amqplib/package.json @@ -59,7 +59,7 @@ "@types/sinon": "10.0.2", "@types/node": "18.11.7", "amqplib": "0.8.0", - "expect": "27.4.2", + "expect": "29.2.0", "lodash": "4.17.21", "mocha": "7.2.0", "ts-mocha": "10.0.0", diff --git a/plugins/node/instrumentation-amqplib/test/amqplib-callbacks.test.ts b/plugins/node/instrumentation-amqplib/test/amqplib-callbacks.test.ts index 424554e4dd..e9fbdf96e8 100644 --- a/plugins/node/instrumentation-amqplib/test/amqplib-callbacks.test.ts +++ b/plugins/node/instrumentation-amqplib/test/amqplib-callbacks.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import 'mocha'; -import * as expect from 'expect'; +import { expect } from 'expect'; import { AmqplibInstrumentation } from '../src'; import { getTestSpans, diff --git a/plugins/node/instrumentation-amqplib/test/amqplib-connection.test.ts b/plugins/node/instrumentation-amqplib/test/amqplib-connection.test.ts index 748f06c7ee..7ac3e686e5 100644 --- a/plugins/node/instrumentation-amqplib/test/amqplib-connection.test.ts +++ b/plugins/node/instrumentation-amqplib/test/amqplib-connection.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import 'mocha'; -import * as expect from 'expect'; +import { expect } from 'expect'; import { shouldTest } from './utils'; import { censoredUrl, diff --git a/plugins/node/instrumentation-amqplib/test/amqplib-promise.test.ts b/plugins/node/instrumentation-amqplib/test/amqplib-promise.test.ts index b343dc71ea..e130f5e942 100644 --- a/plugins/node/instrumentation-amqplib/test/amqplib-promise.test.ts +++ b/plugins/node/instrumentation-amqplib/test/amqplib-promise.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import 'mocha'; -import * as expect from 'expect'; +import { expect } from 'expect'; import * as sinon from 'sinon'; import * as lodash from 'lodash'; import { diff --git a/plugins/node/instrumentation-amqplib/test/utils.test.ts b/plugins/node/instrumentation-amqplib/test/utils.test.ts index efa69b5886..34e6fb7aa9 100644 --- a/plugins/node/instrumentation-amqplib/test/utils.test.ts +++ b/plugins/node/instrumentation-amqplib/test/utils.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import 'mocha'; -import * as expect from 'expect'; +import { expect } from 'expect'; import { getConnectionAttributesFromServer, getConnectionAttributesFromUrl, diff --git a/plugins/node/instrumentation-amqplib/test/utils.ts b/plugins/node/instrumentation-amqplib/test/utils.ts index f6297dfb66..d1e3a2e4ca 100644 --- a/plugins/node/instrumentation-amqplib/test/utils.ts +++ b/plugins/node/instrumentation-amqplib/test/utils.ts @@ -15,7 +15,7 @@ */ import type * as amqp from 'amqplib'; import type * as amqpCallback from 'amqplib/callback_api'; -import * as expect from 'expect'; +import { expect } from 'expect'; export const asyncConfirmSend = ( confirmChannel: amqp.ConfirmChannel | amqpCallback.ConfirmChannel, diff --git a/plugins/node/instrumentation-lru-memoizer/package.json b/plugins/node/instrumentation-lru-memoizer/package.json index 49c7e6e37d..caec77996e 100644 --- a/plugins/node/instrumentation-lru-memoizer/package.json +++ b/plugins/node/instrumentation-lru-memoizer/package.json @@ -47,7 +47,7 @@ "@types/lru-cache": "7.10.9", "@types/mocha": "8.2.3", "@types/node": "18.11.7", - "expect": "25.5.0", + "expect": "29.2.0", "gts": "3.1.0", "lru-memoizer": "2.1.4", "mocha": "7.2.0", diff --git a/plugins/node/instrumentation-lru-memoizer/test/index.test.ts b/plugins/node/instrumentation-lru-memoizer/test/index.test.ts index 95969e8be8..bb80c9994d 100644 --- a/plugins/node/instrumentation-lru-memoizer/test/index.test.ts +++ b/plugins/node/instrumentation-lru-memoizer/test/index.test.ts @@ -15,7 +15,7 @@ */ import LruMemoizerInstrumentation from '../src'; import { trace, context } from '@opentelemetry/api'; -import * as expect from 'expect'; +import { expect } from 'expect'; import { registerInstrumentationTesting } from '@opentelemetry/contrib-test-utils'; diff --git a/plugins/node/instrumentation-mongoose/package.json b/plugins/node/instrumentation-mongoose/package.json index 6edb5db039..99e77cf6a3 100644 --- a/plugins/node/instrumentation-mongoose/package.json +++ b/plugins/node/instrumentation-mongoose/package.json @@ -50,7 +50,7 @@ "@opentelemetry/sdk-trace-base": "^1.8.0", "@types/mocha": "8.2.3", "@types/node": "18.11.7", - "expect": "27.4.2", + "expect": "29.2.0", "gts": "3.1.0", "mocha": "7.2.0", "mongoose": "6.5.2", diff --git a/plugins/node/instrumentation-mongoose/test/asserts.ts b/plugins/node/instrumentation-mongoose/test/asserts.ts index f6c7e15ec3..ad21f9a716 100644 --- a/plugins/node/instrumentation-mongoose/test/asserts.ts +++ b/plugins/node/instrumentation-mongoose/test/asserts.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import * as expect from 'expect'; +import { expect } from 'expect'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import { SpanStatusCode } from '@opentelemetry/api'; diff --git a/plugins/node/instrumentation-mongoose/test/mongoose.test.ts b/plugins/node/instrumentation-mongoose/test/mongoose.test.ts index 6ba0297bd5..9049762e0e 100644 --- a/plugins/node/instrumentation-mongoose/test/mongoose.test.ts +++ b/plugins/node/instrumentation-mongoose/test/mongoose.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import 'mocha'; -import * as expect from 'expect'; +import { expect } from 'expect'; import { context, ROOT_CONTEXT } from '@opentelemetry/api'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import { MongooseInstrumentation } from '../src'; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/package.json b/plugins/node/opentelemetry-instrumentation-aws-sdk/package.json index 48cb5b90b5..4535cbdc8f 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/package.json +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/package.json @@ -66,7 +66,7 @@ "@types/sinon": "10.0.6", "aws-sdk": "2.1008.0", "eslint": "8.7.0", - "expect": "27.4.2", + "expect": "29.2.0", "gts": "3.1.0", "mocha": "7.2.0", "nock": "13.2.1", diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts index 77717b53c5..51d63442f0 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import * as expect from 'expect'; +import { expect } from 'expect'; import { MAX_MESSAGE_ATTRIBUTES, contextSetter, 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 index f40f80903b..0d7d8a8aea 100644 --- 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 @@ -31,7 +31,7 @@ import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import { SpanStatusCode, Span, SpanKind } from '@opentelemetry/api'; import { AttributeNames } from '../src/enums'; import { mockV2AwsSend } from './testing-utils'; -import * as expect from 'expect'; +import { expect } from 'expect'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; describe('instrumentation-aws-sdk-v2', () => { 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 index 8623b6e873..0fb17f7ffd 100644 --- 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 @@ -47,7 +47,7 @@ import { SemanticAttributes, } from '@opentelemetry/semantic-conventions'; import { AttributeNames } from '../src/enums'; -import * as expect from 'expect'; +import { expect } from 'expect'; import * as fs from 'fs'; import * as nock from 'nock'; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts index 41b715770a..5ff0c2b0ab 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/dynamodb.test.ts @@ -27,7 +27,7 @@ import { DbSystemValues, SemanticAttributes, } from '@opentelemetry/semantic-conventions'; -import * as expect from 'expect'; +import { expect } from 'expect'; import type { ConsumedCapacity as ConsumedCapacityV2 } from 'aws-sdk/clients/dynamodb'; import type { ConsumedCapacity as ConsumedCapacityV3 } from '@aws-sdk/client-dynamodb'; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/lambda.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/lambda.test.ts index 372ebc4e67..be3965a872 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/lambda.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/lambda.test.ts @@ -27,7 +27,7 @@ import { SpanKind } from '@opentelemetry/api'; import { Lambda, InvocationType } from '@aws-sdk/client-lambda'; import { ClientRequest } from 'http'; import * as nock from 'nock'; -import * as expect from 'expect'; +import { expect } from 'expect'; process.env.AWS_ACCESS_KEY_ID = 'testing'; process.env.AWS_SECRET_ACCESS_KEY = 'testing'; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sns.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sns.test.ts index a930dba4a1..ce6ce9c885 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sns.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sns.test.ts @@ -27,7 +27,7 @@ import * as fs from 'fs'; import * as nock from 'nock'; import { mockV2AwsSend } from './testing-utils'; -import * as expect from 'expect'; +import { expect } from 'expect'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as sinon from 'sinon'; import { diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts index 901b4fca1d..e162fa57e8 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts @@ -39,7 +39,7 @@ import { import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import { mockV2AwsSend } from './testing-utils'; import { Message } from 'aws-sdk/clients/sqs'; -import * as expect from 'expect'; +import { expect } from 'expect'; import * as sinon from 'sinon'; import * as messageAttributes from '../src/services/MessageAttributes'; import { AttributeNames } from '../src/enums'; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts index a08f3e1237..7ce6d373cc 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/testing-utils.ts @@ -16,7 +16,7 @@ import { context } from '@opentelemetry/api'; import { isTracingSuppressed } from '@opentelemetry/core'; import { getInstrumentation } from '@opentelemetry/contrib-test-utils'; -import * as expect from 'expect'; +import { expect } from 'expect'; import * as AWS from 'aws-sdk'; // we want to mock the request object and trigger events on it's events emitter. From a0003e76fc46afbbee2558a7d21906be7c9cb1d1 Mon Sep 17 00:00:00 2001 From: Henri Normak Date: Thu, 24 Nov 2022 17:51:42 +0200 Subject: [PATCH 3/7] feat: add sqlcommenter comment with trace context to queries in pg instrumentation (#1286) --- .../README.md | 2 + .../package.json | 3 + .../src/instrumentation.ts | 16 +++ .../src/types.ts | 6 + .../src/utils.ts | 69 ++++++++++ .../test/pg.test.ts | 123 ++++++++++++++++++ .../test/utils.test.ts | 99 +++++++++++++- 7 files changed, 317 insertions(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/README.md b/plugins/node/opentelemetry-instrumentation-pg/README.md index 578b2cc173..b14e036343 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/README.md +++ b/plugins/node/opentelemetry-instrumentation-pg/README.md @@ -48,6 +48,8 @@ PostgreSQL instrumentation has few options available to choose from. You can set | ------- | ---- | ----------- | | [`enhancedDatabaseReporting`](./src/types.ts#L30) | `boolean` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations | | `responseHook` | `PgInstrumentationExecutionResponseHook` (function) | Function for adding custom attributes from db response | +| `requireParentSpan` | `boolean` | If true, requires a parent span to create new spans (default false) | +| `addSqlCommenterCommentToQueries` | `boolean` | If true, adds [sqlcommenter](https://github.com/open-telemetry/opentelemetry-sqlcommenter) specification compliant comment to queries with tracing context (default false). _NOTE: A comment will not be added to queries that already contain `--` or `/* ... */` in them, even if these are not actually part of comments_ | ## Useful links diff --git a/plugins/node/opentelemetry-instrumentation-pg/package.json b/plugins/node/opentelemetry-instrumentation-pg/package.json index 917139df6e..fdf69fe8be 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/package.json +++ b/plugins/node/opentelemetry-instrumentation-pg/package.json @@ -60,6 +60,7 @@ "@opentelemetry/sdk-trace-node": "^1.8.0", "@types/mocha": "7.0.2", "@types/node": "18.11.7", + "@types/sinon": "10.0.2", "cross-env": "7.0.3", "gts": "3.1.0", "mocha": "7.2.0", @@ -67,11 +68,13 @@ "pg": "8.7.1", "pg-pool": "3.4.1", "rimraf": "3.0.2", + "sinon": "14.0.0", "test-all-versions": "5.0.1", "ts-mocha": "10.0.0", "typescript": "4.3.5" }, "dependencies": { + "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.34.0", "@opentelemetry/semantic-conventions": "^1.0.0", "@types/pg": "8.6.1", diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index 7c1b6d77f2..f309555951 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -178,6 +178,7 @@ export class PgInstrumentation extends InstrumentationBase { // Handle different client.query(...) signatures if (typeof args[0] === 'string') { const query = args[0]; + if (args.length > 1 && args[1] instanceof Array) { const params = args[1]; span = utils.handleParameterizedQuery.call( @@ -195,14 +196,29 @@ export class PgInstrumentation extends InstrumentationBase { query ); } + + if (plugin.getConfig().addSqlCommenterCommentToQueries) { + // Modify the query with a tracing comment + args[0] = utils.addSqlCommenterComment(span, args[0]); + } } else if (typeof args[0] === 'object') { const queryConfig = args[0] as NormalizedQueryConfig; + span = utils.handleConfigQuery.call( this, plugin.tracer, plugin.getConfig(), queryConfig ); + + if (plugin.getConfig().addSqlCommenterCommentToQueries) { + // Copy the query config instead of writing to args[0].text so that we don't modify user's + // original query config reference + args[0] = { + ...queryConfig, + text: utils.addSqlCommenterComment(span, queryConfig.text), + }; + } } else { return utils.handleInvalidQuery.call( this, diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/types.ts b/plugins/node/opentelemetry-instrumentation-pg/src/types.ts index d37ab0323f..3e0d280cad 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/types.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/types.ts @@ -46,4 +46,10 @@ export interface PgInstrumentationConfig extends InstrumentationConfig { * @default false */ requireParentSpan?: boolean; + + /** + * If true, queries are modified to also include a comment with + * the tracing context, following the {@link https://github.com/open-telemetry/opentelemetry-sqlcommenter sqlcommenter} format + */ + addSqlCommenterCommentToQueries?: boolean; } diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index 7e5359c4ec..048c0d7ad0 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -24,7 +24,10 @@ import { diag, INVALID_SPAN_CONTEXT, Attributes, + defaultTextMapSetter, + ROOT_CONTEXT, } from '@opentelemetry/api'; +import { W3CTraceContextPropagator } from '@opentelemetry/core'; import { AttributeNames } from './enums/AttributeNames'; import { SemanticAttributes, @@ -289,3 +292,69 @@ export function patchClientConnectCallback( cb.call(this, err); }; } + +// NOTE: This function currently is returning false-positives +// in cases where comment characters appear in string literals +// ("SELECT '-- not a comment';" would return true, although has no comment) +function hasValidSqlComment(query: string): boolean { + const indexOpeningDashDashComment = query.indexOf('--'); + if (indexOpeningDashDashComment >= 0) { + return true; + } + + const indexOpeningSlashComment = query.indexOf('/*'); + if (indexOpeningSlashComment < 0) { + return false; + } + + const indexClosingSlashComment = query.indexOf('*/'); + return indexOpeningDashDashComment < indexClosingSlashComment; +} + +// sqlcommenter specification (https://google.github.io/sqlcommenter/spec/#value-serialization) +// expects us to URL encode based on the RFC 3986 spec (https://en.wikipedia.org/wiki/Percent-encoding), +// but encodeURIComponent does not handle some characters correctly (! ' ( ) *), +// which means we need special handling for this +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent +function fixedEncodeURIComponent(str: string) { + return encodeURIComponent(str).replace( + /[!'()*]/g, + c => `%${c.charCodeAt(0).toString(16).toUpperCase()}` + ); +} + +export function addSqlCommenterComment(span: Span, query: string): string { + if (typeof query !== 'string' || query.length === 0) { + return query; + } + + // As per sqlcommenter spec we shall not add a comment if there already is a comment + // in the query + if (hasValidSqlComment(query)) { + return query; + } + + const propagator = new W3CTraceContextPropagator(); + const headers: { [key: string]: string } = {}; + propagator.inject( + trace.setSpan(ROOT_CONTEXT, span), + headers, + defaultTextMapSetter + ); + + // sqlcommenter spec requires keys in the comment to be sorted lexicographically + const sortedKeys = Object.keys(headers).sort(); + + if (sortedKeys.length === 0) { + return query; + } + + const commentString = sortedKeys + .map(key => { + const encodedValue = fixedEncodeURIComponent(headers[key]); + return `${key}='${encodedValue}'`; + }) + .join(','); + + return `${query} /*${commentString}*/`; +} diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts index d43eb7537f..e91b0034fb 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts @@ -32,6 +32,7 @@ import { } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import type * as pg from 'pg'; +import * as sinon from 'sinon'; import { PgInstrumentation, PgInstrumentationConfig, @@ -44,6 +45,7 @@ import { DbSystemValues, } from '@opentelemetry/semantic-conventions'; import { isSupported } from './utils'; +import { addSqlCommenterComment } from '../src/utils'; const pgVersion = require('pg/package.json').version; const nodeVersion = process.versions.node; @@ -110,6 +112,12 @@ describe('pg', () => { const testPostgresLocally = process.env.RUN_POSTGRES_TESTS_LOCAL; // For local: spins up local postgres db via docker const shouldTest = testPostgres || testPostgresLocally; // Skips these tests if false (default) + function getExecutedQueries() { + return (client as any).queryQueue.push.args.flat() as (pg.Query & { + text?: string; + })[]; + } + before(async function () { const skipForUnsupported = process.env.IN_TAV && !isSupported(nodeVersion, pgVersion); @@ -156,11 +164,16 @@ describe('pg', () => { beforeEach(() => { contextManager = new AsyncHooksContextManager().enable(); context.setGlobalContextManager(contextManager); + + // Add a spy on the underlying client's internal query queue so that + // we could assert on what the final queries are that are executed + sinon.spy((client as any).queryQueue, 'push'); }); afterEach(() => { memoryExporter.reset(); context.disable(); + sinon.restore(); }); it('should return an instrumentation', () => { @@ -649,6 +662,116 @@ describe('pg', () => { }); }); + it('should not add sqlcommenter comment when flag is not specified', async () => { + const span = tracer.startSpan('test span'); + await context.with(trace.setSpan(context.active(), span), async () => { + try { + const query = 'SELECT NOW()'; + const resPromise = await client.query(query); + assert.ok(resPromise); + + const [span] = memoryExporter.getFinishedSpans(); + assert.ok(span); + + const commentedQuery = addSqlCommenterComment( + trace.wrapSpanContext(span.spanContext()), + query + ); + + const executedQueries = getExecutedQueries(); + assert.equal(executedQueries.length, 1); + assert.equal(executedQueries[0].text, query); + assert.notEqual(query, commentedQuery); + } catch (e) { + assert.ok(false, e.message); + } + }); + }); + + it('should not add sqlcommenter comment with client.query({text, callback}) when flag is not specified', done => { + const span = tracer.startSpan('test span'); + context.with(trace.setSpan(context.active(), span), () => { + const query = 'SELECT NOW()'; + client.query({ + text: query, + callback: (err: Error, res: pg.QueryResult) => { + assert.strictEqual(err, null); + assert.ok(res); + + const [span] = memoryExporter.getFinishedSpans(); + const commentedQuery = addSqlCommenterComment( + trace.wrapSpanContext(span.spanContext()), + query + ); + + const executedQueries = getExecutedQueries(); + assert.equal(executedQueries.length, 1); + assert.equal(executedQueries[0].text, query); + assert.notEqual(query, commentedQuery); + done(); + }, + } as pg.QueryConfig); + }); + }); + + it('should add sqlcommenter comment when addSqlCommenterCommentToQueries=true is specified', async () => { + instrumentation.setConfig({ + addSqlCommenterCommentToQueries: true, + }); + + const span = tracer.startSpan('test span'); + await context.with(trace.setSpan(context.active(), span), async () => { + try { + const query = 'SELECT NOW()'; + const resPromise = await client.query(query); + assert.ok(resPromise); + + const [span] = memoryExporter.getFinishedSpans(); + const commentedQuery = addSqlCommenterComment( + trace.wrapSpanContext(span.spanContext()), + query + ); + + const executedQueries = getExecutedQueries(); + assert.equal(executedQueries.length, 1); + assert.equal(executedQueries[0].text, commentedQuery); + assert.notEqual(query, commentedQuery); + } catch (e) { + assert.ok(false, e.message); + } + }); + }); + + it('should add sqlcommenter comment when addSqlCommenterCommentToQueries=true is specified with client.query({text, callback})', done => { + instrumentation.setConfig({ + addSqlCommenterCommentToQueries: true, + }); + + const span = tracer.startSpan('test span'); + context.with(trace.setSpan(context.active(), span), () => { + const query = 'SELECT NOW()'; + client.query({ + text: query, + callback: (err: Error, res: pg.QueryResult) => { + assert.strictEqual(err, null); + assert.ok(res); + + const [span] = memoryExporter.getFinishedSpans(); + const commentedQuery = addSqlCommenterComment( + trace.wrapSpanContext(span.spanContext()), + query + ); + + const executedQueries = getExecutedQueries(); + assert.equal(executedQueries.length, 1); + assert.equal(executedQueries[0].text, commentedQuery); + assert.notEqual(query, commentedQuery); + done(); + }, + } as pg.QueryConfig); + }); + }); + it('should not generate traces for client.query() when requireParentSpan=true is specified', done => { instrumentation.setConfig({ requireParentSpan: true, diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts index 6bb03076bf..4b43e4da95 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts @@ -14,7 +14,14 @@ * limitations under the License. */ -import { context, INVALID_SPAN_CONTEXT, trace } from '@opentelemetry/api'; +import { + context, + createTraceState, + INVALID_SPAN_CONTEXT, + SpanContext, + trace, + TraceFlags, +} from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { InstrumentationConfig } from '@opentelemetry/instrumentation'; import { @@ -204,4 +211,94 @@ describe('utils.ts', () => { assert.strictEqual(pgValues, '[0]'); }); }); + + describe('addSqlCommenterComment', () => { + it('adds comment to a simple query', () => { + const spanContext: SpanContext = { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.SAMPLED, + }; + + const query = 'SELECT * from FOO;'; + assert.strictEqual( + utils.addSqlCommenterComment(trace.wrapSpanContext(spanContext), query), + "SELECT * from FOO; /*traceparent='00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-01'*/" + ); + }); + + it('does not add a comment if query already has a comment', () => { + const span = trace.wrapSpanContext({ + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.SAMPLED, + }); + + const blockComment = 'SELECT * from FOO; /* Test comment */'; + assert.strictEqual( + utils.addSqlCommenterComment(span, blockComment), + blockComment + ); + + const dashedComment = 'SELECT * from FOO; -- Test comment'; + assert.strictEqual( + utils.addSqlCommenterComment(span, dashedComment), + dashedComment + ); + }); + + it('does not add a comment to an empty query', () => { + const spanContext: SpanContext = { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.SAMPLED, + }; + + assert.strictEqual( + utils.addSqlCommenterComment(trace.wrapSpanContext(spanContext), ''), + '' + ); + }); + + it('does not add a comment if span context is invalid', () => { + const query = 'SELECT * from FOO;'; + assert.strictEqual( + utils.addSqlCommenterComment( + trace.wrapSpanContext(INVALID_SPAN_CONTEXT), + query + ), + query + ); + }); + + it('correctly also sets trace state', () => { + const spanContext: SpanContext = { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.SAMPLED, + traceState: createTraceState('foo=bar,baz=qux'), + }; + + const query = 'SELECT * from FOO;'; + assert.strictEqual( + utils.addSqlCommenterComment(trace.wrapSpanContext(spanContext), query), + "SELECT * from FOO; /*traceparent='00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-01',tracestate='foo%3Dbar%2Cbaz%3Dqux'*/" + ); + }); + + it('escapes special characters in values', () => { + const spanContext: SpanContext = { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.SAMPLED, + traceState: createTraceState("foo='bar,baz='qux!()*',hack='DROP TABLE"), + }; + + const query = 'SELECT * from FOO;'; + assert.strictEqual( + utils.addSqlCommenterComment(trace.wrapSpanContext(spanContext), query), + "SELECT * from FOO; /*traceparent='00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-01',tracestate='foo%3D%27bar%2Cbaz%3D%27qux%21%28%29%2A%27%2Chack%3D%27DROP%20TABLE'*/" + ); + }); + }); }); From 4cafe6becc19cfb76c53393cb03a5c0574a68b0d Mon Sep 17 00:00:00 2001 From: Thomas Ankcorn Date: Thu, 24 Nov 2022 17:51:00 +0000 Subject: [PATCH 4/7] Fix typo in aws-lambda plugin (#1300) --- .../node/opentelemetry-instrumentation-aws-lambda/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/README.md b/plugins/node/opentelemetry-instrumentation-aws-lambda/README.md index 98cf98025e..7251803be6 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/README.md +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/README.md @@ -60,11 +60,11 @@ const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws new AwsLambdaInstrumentation({ requestHook: (span, { event, context }) => { - span.setAttributes('faas.name', context.functionName); + span.setAttribute('faas.name', context.functionName); }, responseHook: (span, { err, res }) => { - if (err instanceof Error) span.setAttributes('faas.error', err.message); - if (res) span.setAttributes('faas.res', res); + if (err instanceof Error) span.setAttribute('faas.error', err.message); + if (res) span.setAttribute('faas.res', res); } }) ``` From 092a2509bcf884e1b997e0eaec3a6ca02cfd2058 Mon Sep 17 00:00:00 2001 From: Haddas Bronfman <85441461+haddasbronfman@users.noreply.github.com> Date: Sun, 27 Nov 2022 11:23:20 +0200 Subject: [PATCH 5/7] fix(redis): serialize non sensitive arguments into db.statement attribute (#1299) --- .release-please-manifest.json | 3 +- examples/ioredis/tracer.js | 1 + .../opentelemetry-redis-common/.eslintignore | 1 + .../opentelemetry-redis-common/.eslintrc.js | 7 + packages/opentelemetry-redis-common/LICENSE | 201 ++++++++++++++++++ packages/opentelemetry-redis-common/README.md | 7 + .../opentelemetry-redis-common/package.json | 50 +++++ .../opentelemetry-redis-common/src/index.ts | 76 +++++++ .../test/redis-common.test.ts | 54 +++++ .../opentelemetry-redis-common/tsconfig.json | 11 + .../README.md | 3 +- .../package.json | 1 + .../src/instrumentation.ts | 3 +- .../src/utils.ts | 50 ----- .../test/ioredis.test.ts | 48 +---- .../README.md | 8 +- .../package.json | 1 + .../src/instrumentation.ts | 3 +- .../src/utils.ts | 4 - .../test/redis.test.ts | 12 +- .../README.md | 8 +- .../package.json | 1 + .../src/utils.ts | 9 +- .../test/redis.test.ts | 6 +- release-please-config.json | 1 + 25 files changed, 444 insertions(+), 125 deletions(-) create mode 100644 packages/opentelemetry-redis-common/.eslintignore create mode 100644 packages/opentelemetry-redis-common/.eslintrc.js create mode 100644 packages/opentelemetry-redis-common/LICENSE create mode 100644 packages/opentelemetry-redis-common/README.md create mode 100644 packages/opentelemetry-redis-common/package.json create mode 100644 packages/opentelemetry-redis-common/src/index.ts create mode 100644 packages/opentelemetry-redis-common/test/redis-common.test.ts create mode 100644 packages/opentelemetry-redis-common/tsconfig.json diff --git a/.release-please-manifest.json b/.release-please-manifest.json index e41db53c94..9910dc9599 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,2 +1 @@ -{"detectors/node/opentelemetry-resource-detector-alibaba-cloud":"0.27.3","detectors/node/opentelemetry-resource-detector-aws":"1.2.1","detectors/node/opentelemetry-resource-detector-container":"0.2.1","detectors/node/opentelemetry-resource-detector-gcp":"0.27.4","detectors/node/opentelemetry-resource-detector-github":"0.27.0","detectors/node/opentelemetry-resource-detector-instana":"0.4.0","metapackages/auto-instrumentations-node":"0.35.0","metapackages/auto-instrumentations-web":"0.31.0","packages/opentelemetry-host-metrics":"0.31.0","packages/opentelemetry-id-generator-aws-xray":"1.1.1","packages/opentelemetry-propagation-utils":"0.29.1","packages/opentelemetry-test-utils":"0.33.0","plugins/node/instrumentation-amqplib":"0.32.0","plugins/node/instrumentation-dataloader":"0.3.0","plugins/node/instrumentation-fs":"0.6.0","plugins/node/instrumentation-lru-memoizer":"0.32.0","plugins/node/instrumentation-mongoose":"0.32.0", "plugins/node/instrumentation-socket.io": "0.32.0","plugins/node/instrumentation-tedious":"0.5.0","plugins/node/opentelemetry-instrumentation-aws-lambda":"0.34.0","plugins/node/opentelemetry-instrumentation-aws-sdk":"0.10.0","plugins/node/opentelemetry-instrumentation-bunyan":"0.31.0","plugins/node/opentelemetry-instrumentation-cassandra":"0.32.0","plugins/node/opentelemetry-instrumentation-connect":"0.31.0","plugins/node/opentelemetry-instrumentation-dns":"0.31.0","plugins/node/opentelemetry-instrumentation-express":"0.32.0","plugins/node/opentelemetry-instrumentation-fastify":"0.31.0","plugins/node/opentelemetry-instrumentation-generic-pool":"0.31.0","plugins/node/opentelemetry-instrumentation-graphql":"0.33.0","plugins/node/opentelemetry-instrumentation-hapi":"0.31.0","plugins/node/opentelemetry-instrumentation-ioredis":"0.33.0","plugins/node/opentelemetry-instrumentation-knex":"0.31.0","plugins/node/opentelemetry-instrumentation-koa":"0.34.0","plugins/node/opentelemetry-instrumentation-memcached":"0.31.0","plugins/node/opentelemetry-instrumentation-mongodb":"0.33.0","plugins/node/opentelemetry-instrumentation-mysql":"0.32.0","plugins/node/opentelemetry-instrumentation-mysql2":"0.33.0","plugins/node/opentelemetry-instrumentation-nestjs-core":"0.32.0","plugins/node/opentelemetry-instrumentation-net":"0.31.0","plugins/node/opentelemetry-instrumentation-pg":"0.33.0","plugins/node/opentelemetry-instrumentation-pino":"0.33.0","plugins/node/opentelemetry-instrumentation-redis":"0.34.0","plugins/node/opentelemetry-instrumentation-redis-4":"0.34.0","plugins/node/opentelemetry-instrumentation-restify":"0.31.0","plugins/node/opentelemetry-instrumentation-router":"0.32.0","plugins/node/opentelemetry-instrumentation-winston":"0.31.0","plugins/web/opentelemetry-instrumentation-document-load":"0.31.0","plugins/web/opentelemetry-instrumentation-long-task":"0.32.0","plugins/web/opentelemetry-instrumentation-user-interaction":"0.32.0","plugins/web/opentelemetry-plugin-react-load":"0.28.1","propagators/opentelemetry-propagator-aws-xray":"1.1.1","propagators/opentelemetry-propagator-grpc-census-binary":"0.26.0","propagators/opentelemetry-propagator-instana":"0.2.1","propagators/opentelemetry-propagator-ot-trace":"0.26.2"} - +{"detectors/node/opentelemetry-resource-detector-alibaba-cloud":"0.27.3","detectors/node/opentelemetry-resource-detector-aws":"1.2.1","detectors/node/opentelemetry-resource-detector-container":"0.2.1","detectors/node/opentelemetry-resource-detector-gcp":"0.27.4","detectors/node/opentelemetry-resource-detector-github":"0.27.0","detectors/node/opentelemetry-resource-detector-instana":"0.4.0","metapackages/auto-instrumentations-node":"0.35.0","metapackages/auto-instrumentations-web":"0.31.0","packages/opentelemetry-host-metrics":"0.31.0","packages/opentelemetry-id-generator-aws-xray":"1.1.1","packages/opentelemetry-propagation-utils":"0.29.1","packages/opentelemetry-redis-common":"0.33.0","packages/opentelemetry-test-utils":"0.33.0","plugins/node/instrumentation-amqplib":"0.32.0","plugins/node/instrumentation-dataloader":"0.3.0","plugins/node/instrumentation-fs":"0.6.0","plugins/node/instrumentation-lru-memoizer":"0.32.0","plugins/node/instrumentation-mongoose":"0.32.0","plugins/node/instrumentation-socket.io": "0.32.0","plugins/node/instrumentation-tedious":"0.5.0","plugins/node/opentelemetry-instrumentation-aws-lambda":"0.34.0","plugins/node/opentelemetry-instrumentation-aws-sdk":"0.10.0","plugins/node/opentelemetry-instrumentation-bunyan":"0.31.0","plugins/node/opentelemetry-instrumentation-cassandra":"0.32.0","plugins/node/opentelemetry-instrumentation-connect":"0.31.0","plugins/node/opentelemetry-instrumentation-dns":"0.31.0","plugins/node/opentelemetry-instrumentation-express":"0.32.0","plugins/node/opentelemetry-instrumentation-fastify":"0.31.0","plugins/node/opentelemetry-instrumentation-generic-pool":"0.31.0","plugins/node/opentelemetry-instrumentation-graphql":"0.33.0","plugins/node/opentelemetry-instrumentation-hapi":"0.31.0","plugins/node/opentelemetry-instrumentation-ioredis":"0.33.0","plugins/node/opentelemetry-instrumentation-knex":"0.31.0","plugins/node/opentelemetry-instrumentation-koa":"0.34.0","plugins/node/opentelemetry-instrumentation-memcached":"0.31.0","plugins/node/opentelemetry-instrumentation-mongodb":"0.33.0","plugins/node/opentelemetry-instrumentation-mysql":"0.32.0","plugins/node/opentelemetry-instrumentation-mysql2":"0.33.0","plugins/node/opentelemetry-instrumentation-nestjs-core":"0.32.0","plugins/node/opentelemetry-instrumentation-net":"0.31.0","plugins/node/opentelemetry-instrumentation-pg":"0.33.0","plugins/node/opentelemetry-instrumentation-pino":"0.33.0","plugins/node/opentelemetry-instrumentation-redis":"0.34.0","plugins/node/opentelemetry-instrumentation-redis-4":"0.34.0","plugins/node/opentelemetry-instrumentation-restify":"0.31.0","plugins/node/opentelemetry-instrumentation-router":"0.32.0","plugins/node/opentelemetry-instrumentation-winston":"0.31.0","plugins/web/opentelemetry-instrumentation-document-load":"0.31.0","plugins/web/opentelemetry-instrumentation-long-task":"0.32.0","plugins/web/opentelemetry-instrumentation-user-interaction":"0.32.0","plugins/web/opentelemetry-plugin-react-load":"0.28.1","propagators/opentelemetry-propagator-aws-xray":"1.1.1","propagators/opentelemetry-propagator-grpc-census-binary":"0.26.0","propagators/opentelemetry-propagator-instana":"0.2.1","propagators/opentelemetry-propagator-ot-trace":"0.26.2"} diff --git a/examples/ioredis/tracer.js b/examples/ioredis/tracer.js index dec989fec2..6d18533d9a 100644 --- a/examples/ioredis/tracer.js +++ b/examples/ioredis/tracer.js @@ -20,6 +20,7 @@ registerInstrumentations({ instrumentations: [ new IORedisInstrumentation(), ], + tracerProvider: provider, }); module.exports = opentelemetry.trace.getTracer('ioredis-example'); diff --git a/packages/opentelemetry-redis-common/.eslintignore b/packages/opentelemetry-redis-common/.eslintignore new file mode 100644 index 0000000000..378eac25d3 --- /dev/null +++ b/packages/opentelemetry-redis-common/.eslintignore @@ -0,0 +1 @@ +build diff --git a/packages/opentelemetry-redis-common/.eslintrc.js b/packages/opentelemetry-redis-common/.eslintrc.js new file mode 100644 index 0000000000..f726f3becb --- /dev/null +++ b/packages/opentelemetry-redis-common/.eslintrc.js @@ -0,0 +1,7 @@ +module.exports = { + "env": { + "mocha": true, + "node": true + }, + ...require('../../eslint.config.js') +} diff --git a/packages/opentelemetry-redis-common/LICENSE b/packages/opentelemetry-redis-common/LICENSE new file mode 100644 index 0000000000..e50e8c80f9 --- /dev/null +++ b/packages/opentelemetry-redis-common/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 [2022] 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 + + 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-redis-common/README.md b/packages/opentelemetry-redis-common/README.md new file mode 100644 index 0000000000..bb8de2d704 --- /dev/null +++ b/packages/opentelemetry-redis-common/README.md @@ -0,0 +1,7 @@ +# Redis Common Utils for OpenTelemetry redis packages + +This is an internal utils package used for the different redis instrumentations: + +1. ioredis +2. redis +3. redis-4 diff --git a/packages/opentelemetry-redis-common/package.json b/packages/opentelemetry-redis-common/package.json new file mode 100644 index 0000000000..037c492099 --- /dev/null +++ b/packages/opentelemetry-redis-common/package.json @@ -0,0 +1,50 @@ +{ + "name": "@opentelemetry/redis-common", + "version": "0.33.0", + "description": "Redis utilities for redis instrumentations", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "publishConfig": { + "access": "public" + }, + "scripts": { + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "compile": "tsc --build tsconfig.json", + "precompile": "tsc --version && lerna run version:update --scope @opentelemetry/redis-common --include-dependencies", + "prewatch": "npm run precompile", + "prepare": "npm run compile", + "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'", + "watch": "tsc -w" + }, + "repository": "open-telemetry/opentelemetry-js-contrib", + "keywords": [ + "opentelemetry", + "redis-common" + ], + "files": [ + "build/**/*.js", + "build/**/*.js.map", + "build/**/*.d.ts", + "LICENSE", + "README.md" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "engines": { + "node": ">=14" + }, + "bugs": { + "url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues" + }, + "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/redis-common#readme", + "devDependencies": { + "@types/mocha": "^9.1.1", + "@types/node": "18.11.7", + "gts": "3.1.0", + "mocha": "7.2.0", + "nyc": "15.1.0", + "ts-mocha": "10.0.0", + "typescript": "4.3.5" + } +} diff --git a/packages/opentelemetry-redis-common/src/index.ts b/packages/opentelemetry-redis-common/src/index.ts new file mode 100644 index 0000000000..03f0d2845a --- /dev/null +++ b/packages/opentelemetry-redis-common/src/index.ts @@ -0,0 +1,76 @@ +/* + * 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. + */ + +/** + * List of regexes and the number of arguments that should be serialized for matching commands. + * For example, HSET should serialize which key and field it's operating on, but not its value. + * Setting the subset to -1 will serialize all arguments. + * Commands without a match will have their first argument serialized. + * + * Refer to https://redis.io/commands/ for the full list. + */ +const serializationSubsets = [ + { + regex: /^ECHO/i, + args: 0, + }, + { + regex: /^(LPUSH|MSET|PFA|PUBLISH|RPUSH|SADD|SET|SPUBLISH|XADD|ZADD)/i, + args: 1, + }, + { + regex: /^(HSET|HMSET|LSET|LINSERT)/i, + args: 2, + }, + { + regex: + /^(ACL|BIT|B[LRZ]|CLIENT|CLUSTER|CONFIG|COMMAND|DECR|DEL|EVAL|EX|FUNCTION|GEO|GET|HINCR|HMGET|HSCAN|INCR|L[TRLM]|MEMORY|P[EFISTU]|RPOP|S[CDIMORSU]|XACK|X[CDGILPRT]|Z[CDILMPRS])/i, + args: -1, + }, +]; + +export type DbStatementSerializer = ( + cmdName: string, + cmdArgs: Array +) => string; + +/** + * Given the redis command name and arguments, return a combination of the + * command name + the allowed arguments according to `serializationSubsets`. + * @param cmdName The redis command name + * @param cmdArgs The redis command arguments + * @returns a combination of the command name + args according to `serializationSubsets`. + */ +export const defaultDbStatementSerializer: DbStatementSerializer = ( + cmdName, + cmdArgs +) => { + if (Array.isArray(cmdArgs) && cmdArgs.length) { + const nArgsToSerialize = + serializationSubsets.find(({ regex }) => { + return regex.test(cmdName); + })?.args ?? 0; + const argsToSerialize = + nArgsToSerialize >= 0 ? cmdArgs.slice(0, nArgsToSerialize) : cmdArgs; + if (cmdArgs.length > argsToSerialize.length) { + argsToSerialize.push( + `[${cmdArgs.length - nArgsToSerialize} other arguments]` + ); + } + return `${cmdName} ${argsToSerialize.join(' ')}`; + } + return cmdName; +}; diff --git a/packages/opentelemetry-redis-common/test/redis-common.test.ts b/packages/opentelemetry-redis-common/test/redis-common.test.ts new file mode 100644 index 0000000000..874903fadd --- /dev/null +++ b/packages/opentelemetry-redis-common/test/redis-common.test.ts @@ -0,0 +1,54 @@ +/* + * 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 { defaultDbStatementSerializer } from '../src/index'; +import * as assert from 'assert'; + +describe('#defaultDbStatementSerializer()', () => { + [ + { + cmdName: 'UNKNOWN', + cmdArgs: ['something'], + expected: 'UNKNOWN [1 other arguments]', + }, + { + cmdName: 'ECHO', + cmdArgs: ['echo'], + expected: 'ECHO [1 other arguments]', + }, + { + cmdName: 'LPUSH', + cmdArgs: ['list', 'value'], + expected: 'LPUSH list [1 other arguments]', + }, + { + cmdName: 'HSET', + cmdArgs: ['hash', 'field', 'value'], + expected: 'HSET hash field [1 other arguments]', + }, + { + cmdName: 'INCRBY', + cmdArgs: ['key', 5], + expected: 'INCRBY key 5', + }, + ].forEach(({ cmdName, cmdArgs, expected }) => { + it(`should serialize the correct number of arguments for ${cmdName}`, () => { + assert.strictEqual( + defaultDbStatementSerializer(cmdName, cmdArgs), + expected + ); + }); + }); +}); diff --git a/packages/opentelemetry-redis-common/tsconfig.json b/packages/opentelemetry-redis-common/tsconfig.json new file mode 100644 index 0000000000..4078877ce6 --- /dev/null +++ b/packages/opentelemetry-redis-common/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +} diff --git a/plugins/node/opentelemetry-instrumentation-ioredis/README.md b/plugins/node/opentelemetry-instrumentation-ioredis/README.md index 79159202f4..a9d72ffd3d 100644 --- a/plugins/node/opentelemetry-instrumentation-ioredis/README.md +++ b/plugins/node/opentelemetry-instrumentation-ioredis/README.md @@ -57,7 +57,8 @@ IORedis instrumentation has few options available to choose from. You can set th The instrumentation serializes the command into a Span attribute called `db.statement`. The standard serialization format attempts to be as informative as possible while avoiding the export of potentially sensitive data. The number of serialized arguments depends on the specific command, see the configuration -list in `src/utils.ts`. +list in `@opentelemetry/redis-common`. + It is also possible to define a custom serialization function. The function will receive the command name and arguments and must return a string. Here is a simple example to serialize the command name skipping arguments: diff --git a/plugins/node/opentelemetry-instrumentation-ioredis/package.json b/plugins/node/opentelemetry-instrumentation-ioredis/package.json index 6fd3593ade..32522684f7 100644 --- a/plugins/node/opentelemetry-instrumentation-ioredis/package.json +++ b/plugins/node/opentelemetry-instrumentation-ioredis/package.json @@ -71,6 +71,7 @@ }, "dependencies": { "@opentelemetry/instrumentation": "^0.34.0", + "@opentelemetry/redis-common": "^0.33.0", "@opentelemetry/semantic-conventions": "^1.0.0", "@types/ioredis": "4.26.6" }, diff --git a/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts index ce5b9d10a8..ac2d32b8d1 100644 --- a/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts @@ -27,7 +27,8 @@ import { SemanticAttributes, } from '@opentelemetry/semantic-conventions'; import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation'; -import { endSpan, defaultDbStatementSerializer } from './utils'; +import { endSpan } from './utils'; +import { defaultDbStatementSerializer } from '@opentelemetry/redis-common'; import { VERSION } from './version'; const DEFAULT_CONFIG: IORedisInstrumentationConfig = { diff --git a/plugins/node/opentelemetry-instrumentation-ioredis/src/utils.ts b/plugins/node/opentelemetry-instrumentation-ioredis/src/utils.ts index 55c93fee0f..6250284342 100644 --- a/plugins/node/opentelemetry-instrumentation-ioredis/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-ioredis/src/utils.ts @@ -15,35 +15,6 @@ */ import { Span, SpanStatusCode } from '@opentelemetry/api'; -import { DbStatementSerializer } from './types'; - -/** - * List of regexes and the number of arguments that should be serialized for matching commands. - * For example, HSET should serialize which key and field it's operating on, but not its value. - * Setting the subset to -1 will serialize all arguments. - * Commands without a match will have their first argument serialized. - * - * Refer to https://redis.io/commands/ for the full list. - */ -const serializationSubsets = [ - { - regex: /^ECHO/i, - args: 0, - }, - { - regex: /^(LPUSH|MSET|PFA|PUBLISH|RPUSH|SADD|SET|SPUBLISH|XADD|ZADD)/i, - args: 1, - }, - { - regex: /^(HSET|HMSET|LSET|LINSERT)/i, - args: 2, - }, - { - regex: - /^(ACL|BIT|B[LRZ]|CLIENT|CLUSTER|CONFIG|COMMAND|DECR|DEL|EVAL|EX|FUNCTION|GEO|GET|HINCR|HMGET|HSCAN|INCR|L[TRLM]|MEMORY|P[EFISTU]|RPOP|S[CDIMORSU]|XACK|X[CDGILPRT]|Z[CDILMPRS])/i, - args: -1, - }, -]; export const endSpan = ( span: Span, @@ -58,24 +29,3 @@ export const endSpan = ( } span.end(); }; - -export const defaultDbStatementSerializer: DbStatementSerializer = ( - cmdName, - cmdArgs -) => { - if (Array.isArray(cmdArgs) && cmdArgs.length) { - const nArgsToSerialize = - serializationSubsets.find(({ regex }) => { - return regex.test(cmdName); - })?.args ?? 0; - const argsToSerialize = - nArgsToSerialize >= 0 ? cmdArgs.slice(0, nArgsToSerialize) : cmdArgs; - if (cmdArgs.length > argsToSerialize.length) { - argsToSerialize.push( - `[${cmdArgs.length - nArgsToSerialize} other arguments]` - ); - } - return `${cmdName} ${argsToSerialize.join(' ')}`; - } - return cmdName; -}; diff --git a/plugins/node/opentelemetry-instrumentation-ioredis/test/ioredis.test.ts b/plugins/node/opentelemetry-instrumentation-ioredis/test/ioredis.test.ts index dae05f48ba..a54ea1b812 100644 --- a/plugins/node/opentelemetry-instrumentation-ioredis/test/ioredis.test.ts +++ b/plugins/node/opentelemetry-instrumentation-ioredis/test/ioredis.test.ts @@ -43,7 +43,6 @@ import { DbSystemValues, SemanticAttributes, } from '@opentelemetry/semantic-conventions'; -import { defaultDbStatementSerializer } from '../src/utils'; const memoryExporter = new InMemorySpanExporter(); @@ -188,14 +187,14 @@ describe('ioredis', () => { description: string; name: string; args: Array; - serializedArgs: Array; + expectedDbStatement: string; method: (cb: ioredisTypes.CallbackFunction) => unknown; }> = [ { description: 'insert', name: 'hset', args: [hashKeyName, 'testField', 'testValue'], - serializedArgs: [hashKeyName, 'testField', '[1 other arguments]'], + expectedDbStatement: `${hashKeyName} testField [1 other arguments]`, method: (cb: ioredisTypes.CallbackFunction) => client.hset(hashKeyName, 'testField', 'testValue', cb), }, @@ -203,7 +202,7 @@ describe('ioredis', () => { description: 'get', name: 'get', args: [testKeyName], - serializedArgs: [testKeyName], + expectedDbStatement: `${testKeyName}`, method: (cb: ioredisTypes.CallbackFunction) => client.get(testKeyName, cb), }, @@ -245,9 +244,7 @@ describe('ioredis', () => { it(`should create a child span for cb style ${command.description}`, done => { const attributes = { ...DEFAULT_ATTRIBUTES, - [SemanticAttributes.DB_STATEMENT]: `${ - command.name - } ${command.serializedArgs.join(' ')}`, + [SemanticAttributes.DB_STATEMENT]: `${command.name} ${command.expectedDbStatement}`, }; const span = provider .getTracer('ioredis-test') @@ -1002,41 +999,4 @@ describe('ioredis', () => { }); }); }); - - describe('#defaultDbStatementSerializer()', () => { - [ - { - cmdName: 'UNKNOWN', - cmdArgs: ['something'], - expected: 'UNKNOWN [1 other arguments]', - }, - { - cmdName: 'ECHO', - cmdArgs: ['echo'], - expected: 'ECHO [1 other arguments]', - }, - { - cmdName: 'LPUSH', - cmdArgs: ['list', 'value'], - expected: 'LPUSH list [1 other arguments]', - }, - { - cmdName: 'HSET', - cmdArgs: ['hash', 'field', 'value'], - expected: 'HSET hash field [1 other arguments]', - }, - { - cmdName: 'INCRBY', - cmdArgs: ['key', 5], - expected: 'INCRBY key 5', - }, - ].forEach(({ cmdName, cmdArgs, expected }) => { - it(`should serialize the correct number of arguments for ${cmdName}`, () => { - assert.strictEqual( - defaultDbStatementSerializer(cmdName, cmdArgs), - expected - ); - }); - }); - }); }); diff --git a/plugins/node/opentelemetry-instrumentation-redis-4/README.md b/plugins/node/opentelemetry-instrumentation-redis-4/README.md index 7d4870a511..dec67f105d 100644 --- a/plugins/node/opentelemetry-instrumentation-redis-4/README.md +++ b/plugins/node/opentelemetry-instrumentation-redis-4/README.md @@ -53,14 +53,14 @@ Redis instrumentation has a few options available to choose from. You can set th #### Custom `db.statement` Serializer -The instrumentation serializes the command into a Span attribute called -`db.statement`. The default serialization sets the attribute to the command -name, without the command arguments. +The instrumentation serializes the command into a Span attribute called `db.statement`. The standard serialization format attempts to be as informative as possible while avoiding the export of potentially sensitive data. The number of serialized arguments depends on the specific command, see the configuration +list in `@opentelemetry/redis-common`. It is also possible to define a custom serialization function. The function will receive the command name and arguments and must return a string. -Here is a simple example to serialize the command name and arguments: +Here is a simple example to serialize the command name and all command arguments. +Notice that it might capture sensitive data and big payloads: ```javascript const { RedisInstrumentation } = require('@opentelemetry/instrumentation-redis'); diff --git a/plugins/node/opentelemetry-instrumentation-redis-4/package.json b/plugins/node/opentelemetry-instrumentation-redis-4/package.json index 08b95f8103..d4284e0568 100644 --- a/plugins/node/opentelemetry-instrumentation-redis-4/package.json +++ b/plugins/node/opentelemetry-instrumentation-redis-4/package.json @@ -71,6 +71,7 @@ }, "dependencies": { "@opentelemetry/instrumentation": "^0.34.0", + "@opentelemetry/redis-common": "^0.33.0", "@opentelemetry/semantic-conventions": "^1.0.0" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-redis-4#readme" diff --git a/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts index b73d86c5f4..b4b229f66f 100644 --- a/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts @@ -28,7 +28,8 @@ import { InstrumentationNodeModuleDefinition, InstrumentationNodeModuleFile, } from '@opentelemetry/instrumentation'; -import { defaultDbStatementSerializer, getClientAttributes } from './utils'; +import { getClientAttributes } from './utils'; +import { defaultDbStatementSerializer } from '@opentelemetry/redis-common'; import { RedisInstrumentationConfig } from './types'; import { VERSION } from './version'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; diff --git a/plugins/node/opentelemetry-instrumentation-redis-4/src/utils.ts b/plugins/node/opentelemetry-instrumentation-redis-4/src/utils.ts index 168bb2f36c..f102606474 100644 --- a/plugins/node/opentelemetry-instrumentation-redis-4/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-redis-4/src/utils.ts @@ -13,15 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { DbStatementSerializer } from './types'; import { DbSystemValues, SemanticAttributes, } from '@opentelemetry/semantic-conventions'; -export const defaultDbStatementSerializer: DbStatementSerializer = cmdName => - cmdName; - export function getClientAttributes(options: any) { return { [SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS, diff --git a/plugins/node/opentelemetry-instrumentation-redis-4/test/redis.test.ts b/plugins/node/opentelemetry-instrumentation-redis-4/test/redis.test.ts index ffbec97a85..615572cf5a 100644 --- a/plugins/node/opentelemetry-instrumentation-redis-4/test/redis.test.ts +++ b/plugins/node/opentelemetry-instrumentation-redis-4/test/redis.test.ts @@ -99,7 +99,7 @@ describe('redis@^4.0.0', () => { ); assert.strictEqual( setSpan?.attributes[SemanticAttributes.DB_STATEMENT], - 'SET' + 'SET key [1 other arguments]' ); assert.strictEqual( setSpan?.attributes[SemanticAttributes.NET_PEER_NAME], @@ -124,7 +124,7 @@ describe('redis@^4.0.0', () => { ); assert.strictEqual( getSpan?.attributes[SemanticAttributes.DB_STATEMENT], - 'GET' + 'GET key' ); assert.strictEqual( getSpan?.attributes[SemanticAttributes.NET_PEER_NAME], @@ -149,7 +149,7 @@ describe('redis@^4.0.0', () => { assert.ok(setSpan); assert.strictEqual( setSpan?.attributes[SemanticAttributes.DB_STATEMENT], - 'SET' + 'SET key [1 other arguments]' ); assert.strictEqual( setSpan?.attributes[SemanticAttributes.NET_PEER_NAME], @@ -253,7 +253,7 @@ describe('redis@^4.0.0', () => { assert.strictEqual(multiSetSpan.name, 'redis-SET'); assert.strictEqual( multiSetSpan.attributes[SemanticAttributes.DB_STATEMENT], - 'SET' + 'SET key [1 other arguments]' ); assert.strictEqual( multiSetSpan?.attributes[SemanticAttributes.NET_PEER_NAME], @@ -272,7 +272,7 @@ describe('redis@^4.0.0', () => { assert.strictEqual(multiGetSpan.name, 'redis-GET'); assert.strictEqual( multiGetSpan.attributes[SemanticAttributes.DB_STATEMENT], - 'GET' + 'GET another-key' ); assert.strictEqual( multiGetSpan?.attributes[SemanticAttributes.NET_PEER_NAME], @@ -299,7 +299,7 @@ describe('redis@^4.0.0', () => { assert.ok(multiSetSpan); assert.strictEqual( multiSetSpan.attributes[SemanticAttributes.DB_STATEMENT], - 'SET' + 'SET key [1 other arguments]' ); assert.strictEqual( multiSetSpan?.attributes[SemanticAttributes.NET_PEER_NAME], diff --git a/plugins/node/opentelemetry-instrumentation-redis/README.md b/plugins/node/opentelemetry-instrumentation-redis/README.md index 4386a95da7..cf8aae670f 100644 --- a/plugins/node/opentelemetry-instrumentation-redis/README.md +++ b/plugins/node/opentelemetry-instrumentation-redis/README.md @@ -55,14 +55,14 @@ Redis instrumentation has a few options available to choose from. You can set th #### Custom `db.statement` Serializer -The instrumentation serializes the command into a Span attribute called -`db.statement`. The default serialization sets the attribute to the command -name, without the command arguments. +The instrumentation serializes the command into a Span attribute called `db.statement`. The standard serialization format attempts to be as informative as possible while avoiding the export of potentially sensitive data. The number of serialized arguments depends on the specific command, see the configuration +list in `@opentelemetry/redis-common`. It is also possible to define a custom serialization function. The function will receive the command name and arguments and must return a string. -Here is a simple example to serialize the command name and arguments: +Here is a simple example to serialize the command name and all command arguments. +Notice that it might capture sensitive data and big payloads: ```javascript const { RedisInstrumentation } = require('@opentelemetry/instrumentation-redis'); diff --git a/plugins/node/opentelemetry-instrumentation-redis/package.json b/plugins/node/opentelemetry-instrumentation-redis/package.json index 0d34a57604..f0a9046162 100644 --- a/plugins/node/opentelemetry-instrumentation-redis/package.json +++ b/plugins/node/opentelemetry-instrumentation-redis/package.json @@ -71,6 +71,7 @@ }, "dependencies": { "@opentelemetry/instrumentation": "^0.34.0", + "@opentelemetry/redis-common": "^0.33.0", "@opentelemetry/semantic-conventions": "^1.0.0", "@types/redis": "2.8.31" }, diff --git a/plugins/node/opentelemetry-instrumentation-redis/src/utils.ts b/plugins/node/opentelemetry-instrumentation-redis/src/utils.ts index 29b2e34f95..988c5c1a0f 100644 --- a/plugins/node/opentelemetry-instrumentation-redis/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-redis/src/utils.ts @@ -24,11 +24,7 @@ import { trace, diag, } from '@opentelemetry/api'; -import { - DbStatementSerializer, - RedisCommand, - RedisInstrumentationConfig, -} from './types'; +import { RedisCommand, RedisInstrumentationConfig } from './types'; import { EventEmitter } from 'events'; import { RedisInstrumentation } from './'; import { @@ -37,6 +33,7 @@ import { } from '@opentelemetry/semantic-conventions'; import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation'; import { RedisPluginClientTypes } from './internal-types'; +import { defaultDbStatementSerializer } from '@opentelemetry/redis-common'; const endSpan = (span: Span, err?: Error | null) => { if (err) { @@ -75,8 +72,6 @@ export const getTracedCreateStreamTrace = ( }; }; -const defaultDbStatementSerializer: DbStatementSerializer = cmdName => cmdName; - export const getTracedInternalSendCommand = ( tracer: Tracer, original: Function, diff --git a/plugins/node/opentelemetry-instrumentation-redis/test/redis.test.ts b/plugins/node/opentelemetry-instrumentation-redis/test/redis.test.ts index 09842809fc..01d449a57e 100644 --- a/plugins/node/opentelemetry-instrumentation-redis/test/redis.test.ts +++ b/plugins/node/opentelemetry-instrumentation-redis/test/redis.test.ts @@ -133,12 +133,14 @@ describe('redis@2.x', () => { description: string; command: string; args: string[]; + expectedDbStatement: string; method: (cb: redisTypes.Callback) => unknown; }> = [ { description: 'insert', command: 'hset', args: ['hash', 'random', 'random'], + expectedDbStatement: 'hash random [1 other arguments]', method: (cb: redisTypes.Callback) => client.hset('hash', 'random', 'random', cb), }, @@ -146,6 +148,7 @@ describe('redis@2.x', () => { description: 'get', command: 'get', args: ['test'], + expectedDbStatement: 'test', method: (cb: redisTypes.Callback) => client.get('test', cb), }, @@ -153,6 +156,7 @@ describe('redis@2.x', () => { description: 'delete', command: 'del', args: ['test'], + expectedDbStatement: 'test', method: (cb: redisTypes.Callback) => client.del('test', cb), }, ]; @@ -188,7 +192,7 @@ describe('redis@2.x', () => { it(`should create a child span for ${operation.description}`, done => { const attributes = { ...DEFAULT_ATTRIBUTES, - [SemanticAttributes.DB_STATEMENT]: operation.command, + [SemanticAttributes.DB_STATEMENT]: `${operation.command} ${operation.expectedDbStatement}`, }; const span = tracer.startSpan('test span'); context.with(trace.setSpan(context.active(), span), () => { diff --git a/release-please-config.json b/release-please-config.json index 6894b5d84d..424fe6824a 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -17,6 +17,7 @@ "packages/opentelemetry-host-metrics": {}, "packages/opentelemetry-id-generator-aws-xray": {}, "packages/opentelemetry-propagation-utils": {}, + "packages/opentelemetry-redis-common": {}, "packages/opentelemetry-test-utils": {}, "plugins/node/instrumentation-amqplib": {}, "plugins/node/instrumentation-dataloader": {}, From 8a375f59f3043a7d3749b1e8af5603b9ed30f08f Mon Sep 17 00:00:00 2001 From: Ethan Resnick Date: Wed, 30 Nov 2022 11:53:35 -0800 Subject: [PATCH 6/7] fix: pg span names (#1306) * refactor(pg): more defensive, DRYer implementation The prior code had three different utility functions for constructing the span to use to trace the query: handleTextQuery, handleConfigQuery, and handleParameterizedQuery. These functions contained a lot of duplicated logic, and that duplication was gonna make it harder to introduce new features (see next commit) and was already leading to bugs/inconsistencies. For example, `handleConfigQuery` would verify that the parameter `values` provided with the query were provided as an array before attaching them to the span, whereas `handleParameterizedQuery` would do so without this validation. This commit instead normalizes the arguments to `client.query` first, so that only function is needed to create the span. In the process of normalizing the arguments, the new code also performs a bit more validation on them, to make sure that the instrumentation will not throw at runtime. For example, the prior code would've thrown on `client.query(null)`, as it was checking `typeof args[0] === 'object'` without excluding null. The prior code also assumed, if an object was passed, that the `text` and `name` keys held a string; this is now verified before attaching that data to the span. Finally, the prior code had two different code paths for invoking the original `client.query` method; one that went through `handleInvalidQuery` and one directly in `_getClientQueryPatch`. The former wrapped the call in a try-catch, while the latter did not. Now, there's only one call to the original client.query, and it's always in a try-catch, which ensures that other cases of invalid args passed to client.query (which might lead it to throw synchronously) won't be an issue. * refactor(pg): dry up attribute generation * fix(pg): spec-compliant span naming BREAKING CHANGE. This improves the way that OTEL span names are generated in two different ways. First, if the query is a prepared statement with a `name`, the `name` is put into the span name as-is. Before, the `name` was transformed first, as though it were a full SQL query string (i.e., it was split on spaces and only the first 'word' was used). Second, when we do only have a full query string, and we take the first word to form the span name, the code now calls `toUpperCase()` on that string, so that a `select * from x` and a `SELECT * from x` query both end up with a span name like `pg.query:SELECT`. * test(pg): more error handling tests --- .../src/instrumentation.ts | 150 +++++++------ .../src/internal-types.ts | 6 - .../src/utils.ts | 202 +++++++++--------- .../test/pg.test.ts | 20 ++ .../test/utils.test.ts | 98 +++++---- 5 files changed, 248 insertions(+), 228 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index f309555951..f70e39f671 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -26,7 +26,6 @@ import { PgClientConnect, PgClientExtended, PgErrorCallback, - NormalizedQueryConfig, PostgresCallback, PgPoolExtended, PgPoolCallback, @@ -137,12 +136,7 @@ export class PgInstrumentation extends InstrumentationBase { `${PgInstrumentation.COMPONENT}.connect`, { [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, - [SemanticAttributes.DB_NAME]: this.database, - [SemanticAttributes.NET_PEER_NAME]: this.host, - [SemanticAttributes.DB_CONNECTION_STRING]: - utils.getConnectionString(this), - [SemanticAttributes.NET_PEER_PORT]: this.port, - [SemanticAttributes.DB_USER]: this.user, + ...utils.getSemanticAttributesFromConnection(this), } ); @@ -173,72 +167,70 @@ export class PgInstrumentation extends InstrumentationBase { `Patching ${PgInstrumentation.COMPONENT}.Client.prototype.query` ); return function query(this: PgClientExtended, ...args: unknown[]) { - let span: Span; + // client.query(text, cb?), client.query(text, values, cb?), and + // client.query(configObj, cb?) are all valid signatures. We construct + // a queryConfig obj from all (valid) signatures to build the span in a + // unified way. We verify that we at least have query text, and code + // defensively when dealing with `queryConfig` after that (to handle all + // the other invalid cases, like a non-array for values being provided). + // The type casts here reflect only what we've actually validated. + const arg0 = args[0]; + const firstArgIsString = typeof arg0 === 'string'; + const firstArgIsQueryObjectWithText = + utils.isObjectWithTextString(arg0); + + // TODO: remove the `as ...` casts below when the TS version is upgraded. + // Newer TS versions will use the result of firstArgIsQueryObjectWithText + // to properly narrow arg0, but TS 4.3.5 does not. + const queryConfig = firstArgIsString + ? { + text: arg0 as string, + values: Array.isArray(args[1]) ? args[1] : undefined, + } + : firstArgIsQueryObjectWithText + ? (arg0 as utils.ObjectWithText) + : undefined; - // Handle different client.query(...) signatures - if (typeof args[0] === 'string') { - const query = args[0]; + const instrumentationConfig = plugin.getConfig(); - if (args.length > 1 && args[1] instanceof Array) { - const params = args[1]; - span = utils.handleParameterizedQuery.call( - this, - plugin.tracer, - plugin.getConfig(), - query, - params - ); - } else { - span = utils.handleTextQuery.call( - this, - plugin.tracer, - plugin.getConfig(), - query - ); - } + const span = utils.handleConfigQuery.call( + this, + plugin.tracer, + instrumentationConfig, + queryConfig + ); - if (plugin.getConfig().addSqlCommenterCommentToQueries) { - // Modify the query with a tracing comment - args[0] = utils.addSqlCommenterComment(span, args[0]); - } - } else if (typeof args[0] === 'object') { - const queryConfig = args[0] as NormalizedQueryConfig; - - span = utils.handleConfigQuery.call( - this, - plugin.tracer, - plugin.getConfig(), - queryConfig - ); - - if (plugin.getConfig().addSqlCommenterCommentToQueries) { - // Copy the query config instead of writing to args[0].text so that we don't modify user's - // original query config reference - args[0] = { - ...queryConfig, - text: utils.addSqlCommenterComment(span, queryConfig.text), - }; - } - } else { - return utils.handleInvalidQuery.call( - this, - plugin.tracer, - plugin.getConfig(), - original, - ...args - ); + // Modify query text w/ a tracing comment before invoking original for + // tracing, but only if args[0] has one of our expected shapes. + // + // TODO: remove the `as ...` casts below when the TS version is upgraded. + // Newer TS versions will use the result of firstArgIsQueryObjectWithText + // to properly narrow arg0, but TS 4.3.5 does not. + if (instrumentationConfig.addSqlCommenterCommentToQueries) { + args[0] = firstArgIsString + ? utils.addSqlCommenterComment(span, arg0 as string) + : firstArgIsQueryObjectWithText + ? { + ...(arg0 as utils.ObjectWithText), + text: utils.addSqlCommenterComment( + span, + (arg0 as utils.ObjectWithText).text + ), + } + : args[0]; } - // Bind callback to parent span + // Bind callback (if any) to parent span (if any) if (args.length > 0) { const parentSpan = trace.getSpan(context.active()); if (typeof args[args.length - 1] === 'function') { // Patch ParameterQuery callback args[args.length - 1] = utils.patchCallback( - plugin.getConfig(), + instrumentationConfig, span, - args[args.length - 1] as PostgresCallback + args[args.length - 1] as PostgresCallback // nb: not type safe. ); + // If a parent span exists, bind the callback if (parentSpan) { args[args.length - 1] = context.bind( @@ -246,28 +238,37 @@ export class PgInstrumentation extends InstrumentationBase { args[args.length - 1] ); } - } else if ( - typeof (args[0] as NormalizedQueryConfig).callback === 'function' - ) { + } else if (typeof queryConfig?.callback === 'function') { // Patch ConfigQuery callback let callback = utils.patchCallback( plugin.getConfig(), span, - (args[0] as NormalizedQueryConfig).callback! + queryConfig.callback as PostgresCallback // nb: not type safe. ); + // If a parent span existed, bind the callback if (parentSpan) { callback = context.bind(context.active(), callback); } - // Copy the callback instead of writing to args.callback so that we don't modify user's - // original callback reference + // Copy the callback instead of writing to args.callback so that we + // don't modify user's original callback reference args[0] = { ...(args[0] as object), callback }; } } - // Perform the original query - const result: unknown = original.apply(this, args as any); + let result: unknown; + try { + result = original.apply(this, args as never); + } catch (e: unknown) { + // span.recordException(e); + span.setStatus({ + code: SpanStatusCode.ERROR, + message: utils.getErrorMessage(e), + }); + span.end(); + throw e; + } // Bind promise to parent span and end the span if (result instanceof Promise) { @@ -302,7 +303,6 @@ export class PgInstrumentation extends InstrumentationBase { const plugin = this; return (originalConnect: typeof pgPoolTypes.prototype.connect) => { return function connect(this: PgPoolExtended, callback?: PgPoolCallback) { - const connString = utils.getConnectionString(this.options); // setup span const span = startSpan( plugin.tracer, @@ -310,11 +310,7 @@ export class PgInstrumentation extends InstrumentationBase { `${PG_POOL_COMPONENT}.connect`, { [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, - [SemanticAttributes.DB_NAME]: this.options.database, // required - [SemanticAttributes.NET_PEER_NAME]: this.options.host, // required - [SemanticAttributes.DB_CONNECTION_STRING]: connString, // required - [SemanticAttributes.NET_PEER_PORT]: this.options.port, - [SemanticAttributes.DB_USER]: this.options.user, + ...utils.getSemanticAttributesFromConnection(this.options), [AttributeNames.IDLE_TIMEOUT_MILLIS]: this.options.idleTimeoutMillis, [AttributeNames.MAX_CLIENT]: this.options.maxClient, @@ -359,10 +355,10 @@ function handleConnectResult(span: Span, connectResult: unknown) { span.end(); return result; }) - .catch((error: Error) => { + .catch((error: unknown) => { span.setStatus({ code: SpanStatusCode.ERROR, - message: error.message, + message: utils.getErrorMessage(error), }); span.end(); return Promise.reject(error); diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts index dc4e54fe7a..60b2377bc9 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts @@ -32,12 +32,6 @@ export interface PgClientExtended extends pgTypes.Client { connectionParameters: PgClientConnectionParams; } -// Interface name based on original driver implementation -// https://github.com/brianc/node-postgres/blob/2ef55503738eb2cbb6326744381a92c0bc0439ab/packages/pg/lib/utils.js#L157 -export interface NormalizedQueryConfig extends pgTypes.QueryConfig { - callback?: PostgresCallback; -} - export type PgPoolCallback = ( err: Error, client: any, diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index 048c0d7ad0..d5e74b714c 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -35,7 +35,6 @@ import { } from '@opentelemetry/semantic-conventions'; import { PgClientExtended, - NormalizedQueryConfig, PostgresCallback, PgClientConnectionParams, PgErrorCallback, @@ -51,20 +50,72 @@ function arrayStringifyHelper(arr: Array): string { return '[' + arr.toString() + ']'; } -// Helper function to get a low cardinality command name from the full text query -function getCommandFromText(text?: string): string { - if (!text) return 'unknown'; - const words = text.split(' '); - return words[0].length > 0 ? words[0] : 'unknown'; +/** + * Helper function to get a low cardinality span name from whatever info we have + * about the query. + * + * This is tricky, because we don't have most of the information (table name, + * operation name, etc) the spec recommends using to build a low-cardinality + * value w/o parsing. So, we use db.name and assume that, if the query's a named + * prepared statement, those `name` values will be low cardinality. If we don't + * have a named prepared statement, we try to parse an operation (despite the + * spec's warnings). + * + * @params dbName The name of the db against which this query is being issued, + * which could be missing if no db name was given at the time that the + * connection was established. + * @params queryConfig Information we have about the query being issued, typed + * to reflect only the validation we've actually done on the args to + * `client.query()`. This will be undefined if `client.query()` was called + * with invalid arguments. + */ +export function getQuerySpanName( + dbName: string | undefined, + queryConfig?: { text: string; name?: unknown } +) { + // NB: when the query config is invalid, we omit the dbName too, so that + // someone (or some tool) reading the span name doesn't misinterpret the + // dbName as being a prepared statement or sql commit name. + if (!queryConfig) return PgInstrumentation.BASE_SPAN_NAME; + + // Either the name of a prepared statement; or an attempted parse + // of the SQL command, normalized to uppercase; or unknown. + const command = + typeof queryConfig.name === 'string' && queryConfig.name + ? queryConfig.name + : parseNormalizedOperationName(queryConfig.text); + + return `${PgInstrumentation.BASE_SPAN_NAME}:${command}${ + dbName ? ` ${dbName}` : '' + }`; +} + +function parseNormalizedOperationName(queryText: string) { + const sqlCommand = queryText.split(' ')[0].toUpperCase(); + + // Handle query text being "COMMIT;", which has an extra semicolon before the space. + return sqlCommand.endsWith(';') ? sqlCommand.slice(0, -1) : sqlCommand; } -export function getConnectionString(params: PgClientConnectionParams) { +function getConnectionString(params: PgClientConnectionParams) { const host = params.host || 'localhost'; const port = params.port || 5432; const database = params.database || ''; return `postgresql://${host}:${port}/${database}`; } +export function getSemanticAttributesFromConnection( + params: PgClientConnectionParams +) { + return { + [SemanticAttributes.DB_NAME]: params.database, // required + [SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required + [SemanticAttributes.NET_PEER_NAME]: params.host, // required + [SemanticAttributes.NET_PEER_PORT]: params.port, + [SemanticAttributes.DB_USER]: params.user, + }; +} + export function startSpan( tracer: Tracer, instrumentationConfig: PgInstrumentationConfig, @@ -85,127 +136,51 @@ export function startSpan( }); } -// Private helper function to start a span after a connection has been established -function startQuerySpan( - client: PgClientExtended, +// Create a span from our normalized queryConfig object, +// or return a basic span if no queryConfig was given/could be created. +export function handleConfigQuery( + this: PgClientExtended, tracer: Tracer, instrumentationConfig: PgInstrumentationConfig, - name: string + queryConfig?: { text: string; values?: unknown; name?: unknown } ) { - const jdbcString = getConnectionString(client.connectionParameters); - return startSpan(tracer, instrumentationConfig, name, { - [SemanticAttributes.DB_NAME]: client.connectionParameters.database, // required + // Create child span. + const { connectionParameters } = this; + const dbName = connectionParameters.database; + + const spanName = getQuerySpanName(dbName, queryConfig); + const span = startSpan(tracer, instrumentationConfig, spanName, { [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, // required - [SemanticAttributes.DB_CONNECTION_STRING]: jdbcString, // required - [SemanticAttributes.NET_PEER_NAME]: client.connectionParameters.host, // required - [SemanticAttributes.NET_PEER_PORT]: client.connectionParameters.port, - [SemanticAttributes.DB_USER]: client.connectionParameters.user, + ...getSemanticAttributesFromConnection(connectionParameters), }); -} -// Queries where args[0] is a QueryConfig -export function handleConfigQuery( - this: PgClientExtended, - tracer: Tracer, - instrumentationConfig: PgInstrumentationConfig, - queryConfig: NormalizedQueryConfig -) { - // Set child span name - const queryCommand = getCommandFromText(queryConfig.name || queryConfig.text); - const name = PgInstrumentation.BASE_SPAN_NAME + ':' + queryCommand; - const span = startQuerySpan(this, tracer, instrumentationConfig, name); + if (!queryConfig) { + return span; + } // Set attributes if (queryConfig.text) { span.setAttribute(SemanticAttributes.DB_STATEMENT, queryConfig.text); } + if ( instrumentationConfig.enhancedDatabaseReporting && - queryConfig.values instanceof Array + Array.isArray(queryConfig.values) ) { span.setAttribute( AttributeNames.PG_VALUES, arrayStringifyHelper(queryConfig.values) ); } + // Set plan name attribute, if present - if (queryConfig.name) { + if (typeof queryConfig.name === 'string') { span.setAttribute(AttributeNames.PG_PLAN, queryConfig.name); } return span; } -// Queries where args[1] is a 'values' array -export function handleParameterizedQuery( - this: PgClientExtended, - tracer: Tracer, - instrumentationConfig: PgInstrumentationConfig, - query: string, - values: unknown[] -) { - // Set child span name - const queryCommand = getCommandFromText(query); - const name = PgInstrumentation.BASE_SPAN_NAME + ':' + queryCommand; - const span = startQuerySpan(this, tracer, instrumentationConfig, name); - - // Set attributes - span.setAttribute(SemanticAttributes.DB_STATEMENT, query); - if (instrumentationConfig.enhancedDatabaseReporting) { - span.setAttribute(AttributeNames.PG_VALUES, arrayStringifyHelper(values)); - } - - return span; -} - -// Queries where args[0] is a text query and 'values' was not specified -export function handleTextQuery( - this: PgClientExtended, - tracer: Tracer, - instrumentationConfig: PgInstrumentationConfig, - query: string -) { - // Set child span name - const queryCommand = getCommandFromText(query); - const name = PgInstrumentation.BASE_SPAN_NAME + ':' + queryCommand; - const span = startQuerySpan(this, tracer, instrumentationConfig, name); - - // Set attributes - span.setAttribute(SemanticAttributes.DB_STATEMENT, query); - - return span; -} - -/** - * Invalid query handler. We should never enter this function unless invalid args were passed to the driver. - * Create and immediately end a new span - */ -export function handleInvalidQuery( - this: PgClientExtended, - tracer: Tracer, - instrumentationConfig: PgInstrumentationConfig, - originalQuery: typeof pgTypes.Client.prototype.query, - ...args: unknown[] -) { - let result; - const span = startQuerySpan( - this, - tracer, - instrumentationConfig, - PgInstrumentation.BASE_SPAN_NAME - ); - try { - result = originalQuery.apply(this, args as never); - } catch (e) { - // span.recordException(e); - span.setStatus({ code: SpanStatusCode.ERROR, message: e.message }); - throw e; - } finally { - span.end(); - } - return result; -} - export function handleExecutionResult( config: PgInstrumentationConfig, span: Span, @@ -358,3 +333,26 @@ export function addSqlCommenterComment(span: Span, query: string): string { return `${query} /*${commentString}*/`; } + +/** + * Attempt to get a message string from a thrown value, while being quite + * defensive, to recognize the fact that, in JS, any kind of value (even + * primitives) can be thrown. + */ +export function getErrorMessage(e: unknown) { + return typeof e === 'object' && e !== null && 'message' in e + ? String((e as { message?: unknown }).message) + : undefined; +} + +export function isObjectWithTextString(it: unknown): it is ObjectWithText { + return ( + typeof it === 'object' && + typeof (it as null | { text?: unknown })?.text === 'string' + ); +} + +export type ObjectWithText = { + text: string; + [k: string]: unknown; +}; diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts index e91b0034fb..be8e1491cf 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts @@ -203,6 +203,26 @@ describe('pg', () => { runCallbackTest(null, DEFAULT_ATTRIBUTES, [], errorStatus); memoryExporter.reset(); + assert.throws( + () => { + (client as any).query(null); + }, + assertPgError, + 'pg should throw when null provided as only arg' + ); + runCallbackTest(null, DEFAULT_ATTRIBUTES, [], errorStatus); + memoryExporter.reset(); + + assert.throws( + () => { + (client as any).query(undefined); + }, + assertPgError, + 'pg should throw when undefined provided as only arg' + ); + runCallbackTest(null, DEFAULT_ATTRIBUTES, [], errorStatus); + memoryExporter.reset(); + assert.doesNotThrow( () => (client as any).query({ foo: 'bar' }, undefined, () => { diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts index 4b43e4da95..099ffa3bf4 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts @@ -33,7 +33,7 @@ import * as assert from 'assert'; import * as pg from 'pg'; import { PgInstrumentationConfig } from '../src'; import { AttributeNames } from '../src/enums/AttributeNames'; -import { PgClientExtended, NormalizedQueryConfig } from '../src/internal-types'; +import { PgClientExtended } from '../src/internal-types'; import * as utils from '../src/utils'; const memoryExporter = new InMemorySpanExporter(); @@ -75,6 +75,59 @@ describe('utils.ts', () => { context.disable(); }); + describe('.getQuerySpanName()', () => { + const dummyQuery = { + text: 'SELECT $1', + values: ['hello'], + name: 'select-placeholder-val', + }; + + it('uses prepared statement name when given, over query text', () => { + assert.strictEqual( + utils.getQuerySpanName('dbName', dummyQuery), + 'pg.query:select-placeholder-val dbName' + ); + }); + + it('falls back to parsing query text when no (valid) name is available', () => { + assert.strictEqual( + utils.getQuerySpanName('dbName', { ...dummyQuery, name: undefined }), + 'pg.query:SELECT dbName' + ); + }); + + it('normalizes operation names parsed from query text', () => { + const queryUpperCase = { text: dummyQuery.text.toUpperCase() }; + const queryLowerCase = { text: dummyQuery.text.toLowerCase() }; + + assert.strictEqual( + utils.getQuerySpanName('dbName', queryUpperCase), + utils.getQuerySpanName('dbName', queryLowerCase) + ); + }); + + it('ignores trailing semicolons when parsing operation names', () => { + assert.strictEqual( + utils.getQuerySpanName('dbName', { text: 'COMMIT;' }), + 'pg.query:COMMIT dbName' + ); + }); + + it('omits db name if missing', () => { + assert.strictEqual( + utils.getQuerySpanName(undefined, dummyQuery), + 'pg.query:select-placeholder-val' + ); + }); + + it('should omit all info if the queryConfig is invalid', () => { + assert.strictEqual( + utils.getQuerySpanName('db-name-ignored', undefined), + 'pg.query' + ); + }); + }); + describe('.startSpan()', () => { it('starts real span when requireParentSpan=false', async () => { const span = utils.startSpan(tracer, instrumentationConfig, 'spanName', { @@ -131,7 +184,7 @@ describe('utils.ts', () => { }); describe('.handleConfigQuery()', () => { - const queryConfig: NormalizedQueryConfig = { + const queryConfig = { text: 'SELECT $1::text', values: ['0'], }; @@ -171,47 +224,6 @@ describe('utils.ts', () => { }); }); - describe('.handleParameterizedQuery()', () => { - const query = 'SELECT $1::text'; - const values = ['0']; - - it('does not track pg.values by default', async () => { - const querySpan = utils.handleParameterizedQuery.call( - client, - tracer, - instrumentationConfig, - query, - values - ); - querySpan.end(); - - const readableSpan = getLatestSpan(); - - const pgValues = readableSpan.attributes[AttributeNames.PG_VALUES]; - assert.strictEqual(pgValues, undefined); - }); - - it('tracks pg.values if enabled explicitly', async () => { - const extPluginConfig: PgInstrumentationConfig & InstrumentationConfig = { - ...instrumentationConfig, - enhancedDatabaseReporting: true, - }; - const querySpan = utils.handleParameterizedQuery.call( - client, - tracer, - extPluginConfig, - query, - values - ); - querySpan.end(); - - const readableSpan = getLatestSpan(); - - const pgValues = readableSpan.attributes[AttributeNames.PG_VALUES]; - assert.strictEqual(pgValues, '[0]'); - }); - }); - describe('addSqlCommenterComment', () => { it('adds comment to a simple query', () => { const spanContext: SpanContext = { From f0a93685cfb43543b7ca577dd370d56576b49e3f Mon Sep 17 00:00:00 2001 From: Ethan Resnick Date: Wed, 30 Nov 2022 21:08:54 -0800 Subject: [PATCH 7/7] feat(pg): support requestHook hook (#1307) --- .../README.md | 3 +- .../package.json | 1 + .../src/instrumentation.ts | 45 +++++++++ .../src/internal-types.ts | 16 +++- .../src/types.ts | 29 +++++- .../src/utils.ts | 6 +- .../test/pg.test.ts | 95 +++++++++++++++++++ 7 files changed, 186 insertions(+), 9 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/README.md b/plugins/node/opentelemetry-instrumentation-pg/README.md index b14e036343..3703143057 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/README.md +++ b/plugins/node/opentelemetry-instrumentation-pg/README.md @@ -47,7 +47,8 @@ PostgreSQL instrumentation has few options available to choose from. You can set | Options | Type | Description | | ------- | ---- | ----------- | | [`enhancedDatabaseReporting`](./src/types.ts#L30) | `boolean` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations | -| `responseHook` | `PgInstrumentationExecutionResponseHook` (function) | Function for adding custom attributes from db response | +| `requestHook` | `PgInstrumentationExecutionRequestHook` (function) | Function for adding custom span attributes using information about the query being issued and the db to which it's directed | +| `responseHook` | `PgInstrumentationExecutionResponseHook` (function) | Function for adding custom span attributes from db response | | `requireParentSpan` | `boolean` | If true, requires a parent span to create new spans (default false) | | `addSqlCommenterCommentToQueries` | `boolean` | If true, adds [sqlcommenter](https://github.com/open-telemetry/opentelemetry-sqlcommenter) specification compliant comment to queries with tracing context (default false). _NOTE: A comment will not be added to queries that already contain `--` or `/* ... */` in them, even if these are not actually part of comments_ | diff --git a/plugins/node/opentelemetry-instrumentation-pg/package.json b/plugins/node/opentelemetry-instrumentation-pg/package.json index fdf69fe8be..d3d4e2de1b 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/package.json +++ b/plugins/node/opentelemetry-instrumentation-pg/package.json @@ -68,6 +68,7 @@ "pg": "8.7.1", "pg-pool": "3.4.1", "rimraf": "3.0.2", + "safe-stable-stringify": "^2.4.1", "sinon": "14.0.0", "test-all-versions": "5.0.1", "ts-mocha": "10.0.0", diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index f70e39f671..e41ce010ae 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -17,6 +17,7 @@ import { isWrapped, InstrumentationBase, InstrumentationNodeModuleDefinition, + safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; import { context, diag, trace, Span, SpanStatusCode } from '@opentelemetry/api'; @@ -257,6 +258,50 @@ export class PgInstrumentation extends InstrumentationBase { } } + if ( + typeof instrumentationConfig.requestHook === 'function' && + queryConfig + ) { + safeExecuteInTheMiddle( + () => { + // pick keys to expose explicitly, so we're not leaking pg package + // internals that are subject to change + const { database, host, port, user } = this.connectionParameters; + const connection = { database, host, port, user }; + + instrumentationConfig.requestHook!(span, { + connection, + query: { + text: queryConfig.text, + // nb: if `client.query` is called with illegal arguments + // (e.g., if `queryConfig.values` is passed explicitly, but a + // non-array is given), then the type casts will be wrong. But + // we leave it up to the queryHook to handle that, and we + // catch and swallow any errors it throws. The other options + // are all worse. E.g., we could leave `queryConfig.values` + // and `queryConfig.name` as `unknown`, but then the hook body + // would be forced to validate (or cast) them before using + // them, which seems incredibly cumbersome given that these + // casts will be correct 99.9% of the time -- and pg.query + // will immediately throw during development in the other .1% + // of cases. Alternatively, we could simply skip calling the + // hook when `values` or `name` don't have the expected type, + // but that would add unnecessary validation overhead to every + // hook invocation and possibly be even more confusing/unexpected. + values: queryConfig.values as unknown[], + name: queryConfig.name as string | undefined, + }, + }); + }, + err => { + if (err) { + plugin._diag.error('Error running query hook', err); + } + }, + true + ); + } + let result: unknown; try { result = original.apply(this, args as never); diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts index 60b2377bc9..546d84b9ee 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts @@ -19,9 +19,17 @@ import type * as pgPoolTypes from 'pg-pool'; export type PostgresCallback = (err: Error, res: object) => unknown; -// These are not included in @types/pg, so manually define them. -// https://github.com/brianc/node-postgres/blob/fde5ec586e49258dfc4a2fcd861fcdecb4794fc3/lib/client.js#L25 -export interface PgClientConnectionParams { +// NB: this type describes the shape of a parsed, normalized form of the +// connection information that's stored inside each pg.Client instance. It's +// _not_ the same as the ConnectionConfig type exported from `@types/pg`. That +// type defines how data must be _passed in_ when creating a new `pg.Client`, +// which doesn't necessarily match the normalized internal form. E.g., a user +// can call `new Client({ connectionString: '...' }), but `connectionString` +// will never show up in the type below, because only the extracted host, port, +// etc. are recorded in this normalized config. The keys listed below are also +// incomplete, which is fine because the type is internal and these keys are the +// only ones our code is reading. See https://github.com/brianc/node-postgres/blob/fde5ec586e49258dfc4a2fcd861fcdecb4794fc3/lib/client.js#L25 +export interface PgParsedConnectionParams { database?: string; host?: string; port?: number; @@ -29,7 +37,7 @@ export interface PgClientConnectionParams { } export interface PgClientExtended extends pgTypes.Client { - connectionParameters: PgClientConnectionParams; + connectionParameters: PgParsedConnectionParams; } export type PgPoolCallback = ( diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/types.ts b/plugins/node/opentelemetry-instrumentation-pg/src/types.ts index 3e0d280cad..1c9a7da2c6 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/types.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/types.ts @@ -26,12 +26,39 @@ export interface PgInstrumentationExecutionResponseHook { (span: api.Span, responseInfo: PgResponseHookInformation): void; } +export interface PgRequestHookInformation { + query: { + text: string; + name?: string; + values?: unknown[]; + }; + connection: { + database?: string; + host?: string; + port?: number; + user?: string; + }; +} + +export interface PgInstrumentationExecutionRequestHook { + (span: api.Span, queryInfo: PgRequestHookInformation): void; +} + export interface PgInstrumentationConfig extends InstrumentationConfig { /** - * If true, additional information about query parameters will be attached (as `attributes`) to spans representing + * If true, an attribute containing the query's parameters will be attached + * the spans generated to represent the query. */ enhancedDatabaseReporting?: boolean; + /** + * Hook that allows adding custom span attributes or updating the + * span's name based on the data about the query to execute. + * + * @default undefined + */ + requestHook?: PgInstrumentationExecutionRequestHook; + /** * Hook that allows adding custom span attributes based on the data * returned from "query" Pg actions. diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index d5e74b714c..3cc25d45eb 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -36,10 +36,10 @@ import { import { PgClientExtended, PostgresCallback, - PgClientConnectionParams, PgErrorCallback, PgPoolCallback, PgPoolExtended, + PgParsedConnectionParams, } from './internal-types'; import { PgInstrumentationConfig } from './types'; import type * as pgTypes from 'pg'; @@ -97,7 +97,7 @@ function parseNormalizedOperationName(queryText: string) { return sqlCommand.endsWith(';') ? sqlCommand.slice(0, -1) : sqlCommand; } -function getConnectionString(params: PgClientConnectionParams) { +export function getConnectionString(params: PgParsedConnectionParams) { const host = params.host || 'localhost'; const port = params.port || 5432; const database = params.database || ''; @@ -105,7 +105,7 @@ function getConnectionString(params: PgClientConnectionParams) { } export function getSemanticAttributesFromConnection( - params: PgClientConnectionParams + params: PgParsedConnectionParams ) { return { [SemanticAttributes.DB_NAME]: params.database, // required diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts index be8e1491cf..393da4ca0e 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts @@ -33,6 +33,7 @@ import { import * as assert from 'assert'; import type * as pg from 'pg'; import * as sinon from 'sinon'; +import stringify from 'safe-stable-stringify'; import { PgInstrumentation, PgInstrumentationConfig, @@ -513,6 +514,100 @@ describe('pg', () => { }); }); + describe('when specifying a requestHook configuration', () => { + const dataAttributeName = 'pg_data'; + const query = 'SELECT 0::text'; + const events: TimedEvent[] = []; + + // these are the attributes that we'd expect would end up on the final + // span if there is no requestHook. + const attributes = { + ...DEFAULT_ATTRIBUTES, + [SemanticAttributes.DB_STATEMENT]: query, + }; + + // These are the attributes we expect on the span after the requestHook + // has run. We set up the hook to just add to the span a stringified + // version of the args it receives (which is an easy way to assert both + // that the proper args were passed and that the hook was called). + const attributesAfterHook = { + ...attributes, + [dataAttributeName]: stringify({ + connection: { + database: CONFIG.database, + port: CONFIG.port, + host: CONFIG.host, + user: CONFIG.user, + }, + query: { text: query }, + }), + }; + + describe('AND valid requestHook', () => { + beforeEach(async () => { + create({ + enhancedDatabaseReporting: true, + requestHook: (span, requestInfo) => { + span.setAttribute(dataAttributeName, stringify(requestInfo)); + }, + }); + }); + + it('should attach request hook data to resulting spans for query with callback ', done => { + const span = tracer.startSpan('test span'); + context.with(trace.setSpan(context.active(), span), () => { + const res = client.query(query, (err, res) => { + assert.strictEqual(err, null); + assert.ok(res); + runCallbackTest(span, attributesAfterHook, events); + done(); + }); + assert.strictEqual(res, undefined, 'No promise is returned'); + }); + }); + + it('should attach request hook data to resulting spans for query returning a Promise', async () => { + const span = tracer.startSpan('test span'); + await context.with( + trace.setSpan(context.active(), span), + async () => { + const resPromise = await client.query({ text: query }); + try { + assert.ok(resPromise); + runCallbackTest(span, attributesAfterHook, events); + } catch (e) { + assert.ok(false, e.message); + } + } + ); + }); + }); + + describe('AND invalid requestHook', () => { + beforeEach(async () => { + create({ + enhancedDatabaseReporting: true, + requestHook: (_span, _requestInfo) => { + throw 'some kind of failure!'; + }, + }); + }); + + it('should not do any harm when throwing an exception', done => { + const span = tracer.startSpan('test span'); + context.with(trace.setSpan(context.active(), span), () => { + const res = client.query(query, (err, res) => { + assert.strictEqual(err, null); + assert.ok(res); + runCallbackTest(span, attributes, events); + done(); + }); + assert.strictEqual(res, undefined, 'No promise is returned'); + }); + }); + }); + }); + describe('when specifying a responseHook configuration', () => { const dataAttributeName = 'pg_data'; const query = 'SELECT 0::text';