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] 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": {},