From eb0ed47f2e3a86ead4009d68eeabdbe13a566ca4 Mon Sep 17 00:00:00 2001 From: Valentin Marchaud Date: Fri, 5 Mar 2021 17:19:34 +0100 Subject: [PATCH] refactor: migrate mongodb to instrumentation #250 (#354) * refactor: migrate mongodb to instrumentation * chore: address PR comments * chore: pin dev deps * chore: use external const for supported versions * chore: bump core to 0.18 --- .../.eslintignore | 1 + .../.eslintrc.js | 7 + .../.npmignore | 4 + .../LICENSE | 201 +++++++++ .../README.md | 64 +++ .../package.json | 67 +++ .../src/index.ts | 18 + .../src/mongodb.ts | 412 ++++++++++++++++++ .../src/types.ts | 122 ++++++ .../src/version.ts | 18 + .../test/mongodb.test.ts | 271 ++++++++++++ .../test/utils.ts | 99 +++++ .../tsconfig.json | 11 + 13 files changed, 1295 insertions(+) create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/.eslintignore create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/.eslintrc.js create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/.npmignore create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/LICENSE create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/README.md create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/package.json create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/src/index.ts create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/src/mongodb.ts create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/src/version.ts create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts create mode 100644 plugins/node/opentelemetry-instrumentation-mongodb/tsconfig.json diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/.eslintignore b/plugins/node/opentelemetry-instrumentation-mongodb/.eslintignore new file mode 100644 index 0000000000..378eac25d3 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/.eslintignore @@ -0,0 +1 @@ +build diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/.eslintrc.js b/plugins/node/opentelemetry-instrumentation-mongodb/.eslintrc.js new file mode 100644 index 0000000000..f756f4488b --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/.eslintrc.js @@ -0,0 +1,7 @@ +module.exports = { + "env": { + "mocha": true, + "node": true + }, + ...require('../../../eslint.config.js') +} diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/.npmignore b/plugins/node/opentelemetry-instrumentation-mongodb/.npmignore new file mode 100644 index 0000000000..9505ba9450 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/.npmignore @@ -0,0 +1,4 @@ +/bin +/coverage +/doc +/test diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/LICENSE b/plugins/node/opentelemetry-instrumentation-mongodb/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/README.md b/plugins/node/opentelemetry-instrumentation-mongodb/README.md new file mode 100644 index 0000000000..bca7a5ac3e --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/README.md @@ -0,0 +1,64 @@ +# OpenTelemetry mongodb Instrumentation for Node.js +[![Gitter chat][gitter-image]][gitter-url] +[![dependencies][dependencies-image]][dependencies-url] +[![devDependencies][devDependencies-image]][devDependencies-url] +[![Apache License][license-image]][license-image] + +This module provides automatic instrumentation for [`mongodb`](https://github.com/mongodb/node-mongodb-native). + +For automatic instrumentation see the +[@opentelemetry/node](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-node) package. + +## Installation + +```bash +npm install --save @opentelemetry/instrumentation-mongodb +``` +### Supported Versions + - `'>=3.3 <4` + +## Usage + +OpenTelemetry Mongodb Instrumentation allows the user to automatically collect trace data and export them to their backend of choice, to give observability to distributed systems. + +To load a specific instrumentation (**mongodb** in this case), specify it in the Node Tracer's configuration. + +```javascript +const { NodeTracerProvider } = require('@opentelemetry/node'); +const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb'); + +const provider = new NodeTracerProvider(); +provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +provider.register(); + +const mongodbInstrumentation = new MongoDBInstrumentation({ + // see under for available configuration +}); +``` + +### Mongo instrumentation Options + +Mongodb instrumentation has few options available to choose from. You can set the following: + +| Options | Type | Description | +| ------- | ---- | ----------- | +| [`enhancedDatabaseReporting`](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-api/src/trace/instrumentation/instrumentation.ts#L91) | `string` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations | + + +## Useful links +- For more information on OpenTelemetry, visit: +- For more about OpenTelemetry JavaScript: +- For help or feedback on this project, join us on [gitter][gitter-url] + +## License + +Apache 2.0 - See [LICENSE][license-url] for more information. + +[gitter-image]: https://badges.gitter.im/open-telemetry/opentelemetry-js.svg +[gitter-url]: https://gitter.im/open-telemetry/opentelemetry-node?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge +[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/LICENSE +[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat +[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib/status.svg?path=packages/opentelemetry-instrumentation-mongodb +[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=packages%2Fopentelemetry-instrumentation-mongodb +[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib/dev-status.svg?path=packages/opentelemetry-instrumentation-mongodb +[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=packages%2Fopentelemetry-instrumentation-mongodb&type=dev diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/package.json b/plugins/node/opentelemetry-instrumentation-mongodb/package.json new file mode 100644 index 0000000000..2b59bec445 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/package.json @@ -0,0 +1,67 @@ +{ + "name": "@opentelemetry/instrumentation-mongodb", + "version": "0.13.1", + "description": "OpenTelemetry mongodb automatic instrumentation package.", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "repository": "open-telemetry/opentelemetry-js-contrib", + "scripts": { + "test": "nyc ts-mocha --parallel -p tsconfig.json 'test/**/*.test.ts'", + "codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", + "tdd": "npm run test -- --watch-extensions ts --watch", + "clean": "rimraf build/*", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "precompile": "tsc --version", + "version:update": "node ../../../scripts/version-update.js", + "compile": "npm run version:update && tsc -p .", + "prepare": "npm run compile" + }, + "keywords": [ + "opentelemetry", + "mongodb", + "nodejs", + "tracing", + "profiling", + "plugin" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "engines": { + "node": ">=8.5.0" + }, + "files": [ + "build/src/**/*.js", + "build/src/**/*.d.ts", + "doc", + "LICENSE", + "README.md" + ], + "publishConfig": { + "access": "public" + }, + "devDependencies": { + "@opentelemetry/context-async-hooks": "0.18.0", + "@opentelemetry/node": "0.18.0", + "@opentelemetry/tracing": "0.18.0", + "@types/mocha": "7.0.2", + "@types/mongodb": "3.5.25", + "@types/node": "14.0.27", + "codecov": "3.7.2", + "gts": "3.1.0", + "mocha": "7.2.0", + "mongodb": "3.5.9", + "nyc": "15.1.0", + "rimraf": "3.0.2", + "ts-mocha": "8.0.0", + "ts-node": "9.0.0", + "tslint-consistent-codestyle": "1.16.0", + "tslint-microsoft-contrib": "6.2.0", + "typescript": "4.1.3" + }, + "dependencies": { + "@opentelemetry/api": "^0.18.0", + "@opentelemetry/instrumentation": "^0.18.0", + "@opentelemetry/semantic-conventions": "^0.18.0" + } +} diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/index.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/index.ts new file mode 100644 index 0000000000..1ff498a776 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/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 './mongodb'; +export { MongoDbInstrumentationConfig } from './types'; diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/mongodb.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/mongodb.ts new file mode 100644 index 0000000000..c72a406442 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/mongodb.ts @@ -0,0 +1,412 @@ +/* + * 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 { + getSpan, + SpanStatusCode, + Span, + context, + diag, + SpanKind, +} from '@opentelemetry/api'; +import type * as mongodb from 'mongodb'; +import { + MongodbCommandType, + MongoInternalCommand, + MongoInternalTopology, + WireProtocolInternal, + MongoDbInstrumentationConfig, + CursorState, +} from './types'; +import { VERSION } from './version'; +import { + DatabaseAttribute, + GeneralAttribute, +} from '@opentelemetry/semantic-conventions'; +import { + InstrumentationBase, + InstrumentationNodeModuleDefinition, + InstrumentationNodeModuleFile, + isWrapped, +} from '@opentelemetry/instrumentation'; + +const supportedVersions = ['>=3.3 <4']; + +/** mongodb instrumentation plugin for OpenTelemetry */ +export class MongoDBInstrumentation extends InstrumentationBase< + typeof mongodb +> { + constructor(protected _config: MongoDbInstrumentationConfig = {}) { + super('@opentelemetry/instrumentation-mongodb', VERSION, _config); + } + + init() { + const { patch, unpatch } = this._getPatches(); + return [ + new InstrumentationNodeModuleDefinition( + 'mongodb', + supportedVersions, + undefined, + undefined, + [ + new InstrumentationNodeModuleFile( + 'mongodb/lib/core/wireprotocol/index.js', + supportedVersions, + patch, + unpatch + ), + ] + ), + ]; + } + + private _getPatches() { + return { + patch: (moduleExports: T, moduleVersion?: string) => { + diag.debug(`Applying patch for mongodb@${moduleVersion}`); + // patch insert operation + if (isWrapped(moduleExports.insert)) { + this._unwrap(moduleExports, 'insert'); + } + this._wrap(moduleExports, 'insert', this._getPatchOperation('insert')); + // patch remove operation + if (isWrapped(moduleExports.remove)) { + this._unwrap(moduleExports, 'remove'); + } + this._wrap(moduleExports, 'remove', this._getPatchOperation('remove')); + // patch update operation + if (isWrapped(moduleExports.update)) { + this._unwrap(moduleExports, 'update'); + } + this._wrap(moduleExports, 'update', this._getPatchOperation('update')); + // patch other command + if (isWrapped(moduleExports.command)) { + this._unwrap(moduleExports, 'command'); + } + this._wrap(moduleExports, 'command', this._getPatchCommand()); + // patch query + if (isWrapped(moduleExports.query)) { + this._unwrap(moduleExports, 'query'); + } + this._wrap(moduleExports, 'query', this._getPatchFind()); + // patch get more operation on cursor + if (isWrapped(moduleExports.getMore)) { + this._unwrap(moduleExports, 'getMore'); + } + this._wrap(moduleExports, 'getMore', this._getPatchCursor()); + return moduleExports; + }, + unpatch: (moduleExports?: T, moduleVersion?: string) => { + if (moduleExports === undefined) return; + diag.debug(`Removing internal patch for mongodb@${moduleVersion}`); + this._unwrap(moduleExports, 'insert'); + this._unwrap(moduleExports, 'remove'); + this._unwrap(moduleExports, 'update'); + this._unwrap(moduleExports, 'command'); + this._unwrap(moduleExports, 'query'); + this._unwrap(moduleExports, 'getMore'); + }, + }; + } + + /** Creates spans for common operations */ + private _getPatchOperation(operationName: 'insert' | 'update' | 'remove') { + const instrumentation = this; + return (original: WireProtocolInternal[typeof operationName]) => { + return function patchedServerCommand( + this: unknown, + server: MongoInternalTopology, + ns: string, + ops: unknown[], + options: unknown | Function, + callback?: Function + ) { + const currentSpan = getSpan(context.active()); + const resultHandler = + typeof options === 'function' ? options : callback; + if ( + !currentSpan || + typeof resultHandler !== 'function' || + typeof ops !== 'object' + ) { + return original.call( + this, + server, + ns, + ops, + typeof options === 'function' ? callback : options, + callback + ); + } + const span = instrumentation.tracer.startSpan( + `mongodb.${operationName}`, + { + kind: SpanKind.CLIENT, + } + ); + instrumentation._populateAttributes( + span, + ns, + server, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + operationName !== 'insert' ? (ops[0] as any) : undefined + ); + const patchedCallback = instrumentation._patchEnd(span, resultHandler); + return original.call( + this, + server, + ns, + ops, + typeof options === 'function' ? patchedCallback : options, + patchedCallback + ); + }; + }; + } + + /** Creates spans for command operation */ + private _getPatchCommand() { + const instrumentation = this; + return (original: WireProtocolInternal['command']) => { + return function patchedServerCommand( + this: unknown, + server: MongoInternalTopology, + ns: string, + cmd: MongoInternalCommand, + options: unknown | Function, + callback?: Function + ) { + const currentSpan = getSpan(context.active()); + const resultHandler = + typeof options === 'function' ? options : callback; + if ( + !currentSpan || + typeof resultHandler !== 'function' || + typeof cmd !== 'object' + ) { + return original.call( + this, + server, + ns, + cmd, + typeof options === 'function' ? callback : options, + callback + ); + } + const commandType = instrumentation._getCommandType(cmd); + const type = + commandType === MongodbCommandType.UNKNOWN ? 'command' : commandType; + const span = instrumentation.tracer.startSpan(`mongodb.${type}`, { + kind: SpanKind.CLIENT, + }); + instrumentation._populateAttributes(span, ns, server, cmd); + const patchedCallback = instrumentation._patchEnd(span, resultHandler); + return original.call( + this, + server, + ns, + cmd, + typeof options === 'function' ? patchedCallback : options, + patchedCallback + ); + }; + }; + } + + /** Creates spans for find operation */ + private _getPatchFind() { + const instrumentation = this; + return (original: WireProtocolInternal['query']) => { + return function patchedServerCommand( + this: unknown, + server: MongoInternalTopology, + ns: string, + cmd: MongoInternalCommand, + cursorState: CursorState, + options: unknown | Function, + callback?: Function + ) { + const currentSpan = getSpan(context.active()); + const resultHandler = + typeof options === 'function' ? options : callback; + if ( + !currentSpan || + typeof resultHandler !== 'function' || + typeof cmd !== 'object' + ) { + return original.call( + this, + server, + ns, + cmd, + cursorState, + typeof options === 'function' ? callback : options, + callback + ); + } + const span = instrumentation.tracer.startSpan('mongodb.find', { + kind: SpanKind.CLIENT, + }); + instrumentation._populateAttributes(span, ns, server, cmd); + const patchedCallback = instrumentation._patchEnd(span, resultHandler); + return original.call( + this, + server, + ns, + cmd, + cursorState, + typeof options === 'function' ? patchedCallback : options, + patchedCallback + ); + }; + }; + } + + /** Creates spans for find operation */ + private _getPatchCursor() { + const instrumentation = this; + return (original: WireProtocolInternal['getMore']) => { + return function patchedServerCommand( + this: unknown, + server: MongoInternalTopology, + ns: string, + cursorState: CursorState, + batchSize: number, + options: unknown | Function, + callback?: Function + ) { + const currentSpan = getSpan(context.active()); + const resultHandler = + typeof options === 'function' ? options : callback; + if (!currentSpan || typeof resultHandler !== 'function') { + return original.call( + this, + server, + ns, + cursorState, + batchSize, + typeof options === 'function' ? callback : options, + callback + ); + } + const span = instrumentation.tracer.startSpan('mongodb.getMore', { + kind: SpanKind.CLIENT, + }); + instrumentation._populateAttributes(span, ns, server, cursorState.cmd); + const patchedCallback = instrumentation._patchEnd(span, resultHandler); + return original.call( + this, + server, + ns, + cursorState, + batchSize, + typeof options === 'function' ? patchedCallback : options, + patchedCallback + ); + }; + }; + } + + /** + * Get the mongodb command type from the object. + * @param command Internal mongodb command object + */ + private _getCommandType(command: MongoInternalCommand): MongodbCommandType { + if (command.createIndexes !== undefined) { + return MongodbCommandType.CREATE_INDEXES; + } else if (command.findandmodify !== undefined) { + return MongodbCommandType.FIND_AND_MODIFY; + } else if (command.ismaster !== undefined) { + return MongodbCommandType.IS_MASTER; + } else if (command.count !== undefined) { + return MongodbCommandType.COUNT; + } else { + return MongodbCommandType.UNKNOWN; + } + } + + /** + * Populate span's attributes by fetching related metadata from the context + * @param span span to add attributes to + * @param ns mongodb namespace + * @param command mongodb internal representation of a command + * @param topology mongodb internal representation of the network topology + */ + private _populateAttributes( + span: Span, + ns: string, + topology: MongoInternalTopology, + command?: MongoInternalCommand + ) { + // add network attributes to determine the remote server + if (topology && topology.s) { + span.setAttributes({ + [GeneralAttribute.NET_HOST_NAME]: `${ + topology.s.options?.host ?? topology.s.host + }`, + [GeneralAttribute.NET_HOST_PORT]: `${ + topology.s.options?.port ?? topology.s.port + }`, + }); + } + + // The namespace is a combination of the database name and the name of the + // collection or index, like so: [database-name].[collection-or-index-name]. + // It could be a string or an instance of MongoDBNamespace, as such we + // always coerce to a string to extract db and collection. + const [dbName, dbCollection] = ns.toString().split('.'); + + // add database related attributes + span.setAttributes({ + [DatabaseAttribute.DB_SYSTEM]: 'mongodb', + [DatabaseAttribute.DB_NAME]: dbName, + [DatabaseAttribute.DB_MONGODB_COLLECTION]: dbCollection, + }); + + if (command === undefined) return; + + // capture parameters within the query as well if enhancedDatabaseReporting is enabled. + const commandObj = command.query ?? command.q ?? command; + const query = + this._config?.enhancedDatabaseReporting === true + ? commandObj + : Object.keys(commandObj).reduce((obj, key) => { + obj[key] = '?'; + return obj; + }, {} as { [key: string]: unknown }); + + span.setAttribute(DatabaseAttribute.DB_STATEMENT, JSON.stringify(query)); + } + + /** + * Ends a created span. + * @param span The created span to end. + * @param resultHandler A callback function. + */ + private _patchEnd(span: Span, resultHandler: Function): Function { + return function patchedEnd(this: {}, ...args: unknown[]) { + const error = args[0]; + if (error instanceof Error) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error.message, + }); + } + span.end(); + return resultHandler.apply(this, args); + }; + } +} diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts new file mode 100644 index 0000000000..ef418e2a33 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts @@ -0,0 +1,122 @@ +/* + * 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 { InstrumentationConfig } from '@opentelemetry/instrumentation'; + +export interface MongoDbInstrumentationConfig extends InstrumentationConfig { + /** + * If true, additional information about query parameters and + * results will be attached (as `attributes`) to spans representing + * database operations. + */ + enhancedDatabaseReporting?: boolean; +} + +export type Func = (...args: unknown[]) => T; +export type MongoInternalCommand = { + findandmodify: boolean; + createIndexes: boolean; + count: boolean; + ismaster: boolean; + indexes?: unknown[]; + query?: Record; + limit?: number; + q?: Record; + u?: Record; +}; + +export type CursorState = { cmd: MongoInternalCommand } & Record< + string, + unknown +>; + +// https://github.com/mongodb/node-mongodb-native/blob/3.6/lib/core/wireprotocol/index.js +export type WireProtocolInternal = { + insert: ( + server: MongoInternalTopology, + ns: string, + ops: unknown[], + options: unknown | Function, + callback?: Function + ) => unknown; + update: ( + server: MongoInternalTopology, + ns: string, + ops: unknown[], + options: unknown | Function, + callback?: Function + ) => unknown; + remove: ( + server: MongoInternalTopology, + ns: string, + ops: unknown[], + options: unknown | Function, + callback?: Function + ) => unknown; + killCursors: ( + server: MongoInternalTopology, + ns: string, + cursorState: CursorState, + callback: Function + ) => unknown; + getMore: ( + server: MongoInternalTopology, + ns: string, + cursorState: CursorState, + batchSize: number, + options: unknown | Function, + callback?: Function + ) => unknown; + query: ( + server: MongoInternalTopology, + ns: string, + cmd: MongoInternalCommand, + cursorState: CursorState, + options: unknown | Function, + callback?: Function + ) => unknown; + command: ( + server: MongoInternalTopology, + ns: string, + cmd: MongoInternalCommand, + options: unknown | Function, + callback?: Function + ) => unknown; +}; + +// https://github.com/mongodb/node-mongodb-native/blob/3.6/lib/topologies/server.js#L172 +// https://github.com/mongodb/node-mongodb-native/blob/2.2/lib/server.js#L174 +export type MongoInternalTopology = { + s?: { + // those are for mongodb@3 + options?: { + host?: string; + port?: number; + servername?: string; + }; + // those are for mongodb@2 + host?: string; + port?: number; + }; +}; + +export enum MongodbCommandType { + CREATE_INDEXES = 'createIndexes', + FIND_AND_MODIFY = 'findAndModify', + IS_MASTER = 'isMaster', + COUNT = 'count', + UNKNOWN = 'unknown', +} diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/version.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/version.ts new file mode 100644 index 0000000000..720dc76f55 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/version.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. + */ + +// this is autogenerated file, see scripts/version-update.js +export const VERSION = '0.13.1'; diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts new file mode 100644 index 0000000000..a2fb6d4c31 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts @@ -0,0 +1,271 @@ +/* + * 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. + */ + +// for testing locally use this command to run docker +// docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=localhost -p 27017:27017 --name otmongo mongo + +import { context, setSpan, SpanKind } from '@opentelemetry/api'; +import { BasicTracerProvider } from '@opentelemetry/tracing'; +import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; +import { + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/tracing'; +import * as assert from 'assert'; +import { MongoDBInstrumentation } from '../src'; + +const instrumentation = new MongoDBInstrumentation(); +instrumentation.enable(); +instrumentation.disable(); + +import * as mongodb from 'mongodb'; +import { assertSpans, accessCollection } from './utils'; + +describe('MongoDBInstrumentation', () => { + // For these tests, mongo must be running. Add RUN_MONGODB_TESTS to run + // these tests. + const RUN_MONGODB_TESTS = process.env.RUN_MONGODB_TESTS as string; + let shouldTest = true; + if (!RUN_MONGODB_TESTS) { + console.log('Skipping test-mongodb. Run MongoDB to test'); + shouldTest = false; + } + // shouldTest = true + + const URL = `mongodb://${process.env.MONGODB_HOST || 'localhost'}:${ + process.env.MONGODB_PORT || '27017' + }`; + const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests'; + const COLLECTION_NAME = 'test'; + + let client: mongodb.MongoClient; + let collection: mongodb.Collection; + const provider = new BasicTracerProvider(); + const contextManager = new AsyncHooksContextManager().enable(); + const memoryExporter = new InMemorySpanExporter(); + const spanProcessor = new SimpleSpanProcessor(memoryExporter); + + before(done => { + instrumentation.enable(); + instrumentation.setTracerProvider(provider); + provider.addSpanProcessor(spanProcessor); + context.setGlobalContextManager(contextManager); + accessCollection(URL, DB_NAME, COLLECTION_NAME) + .then(result => { + client = result.client; + collection = result.collection; + done(); + }) + .catch((err: Error) => { + console.log( + 'Skipping test-mongodb. Could not connect. Run MongoDB to test' + ); + shouldTest = false; + done(); + }); + }); + after(() => { + contextManager.disable(); + instrumentation.disable(); + }); + + beforeEach(function mongoBeforeEach(done) { + // Skipping all tests in beforeEach() is a workaround. Mocha does not work + // properly when skipping tests in before() on nested describe() calls. + // https://github.com/mochajs/mocha/issues/2819 + if (!shouldTest) { + this.skip(); + } + memoryExporter.reset(); + // Non traced insertion of basic data to perform tests + const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; + collection.insertMany(insertData, (err, result) => { + done(); + }); + }); + + afterEach(done => { + collection.deleteMany({}, done); + }); + + after(() => { + if (client) { + client.close(); + } + }); + + /** Should intercept query */ + describe('Instrumenting query operations', () => { + it('should create a child span for insert', done => { + const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; + const span = provider.getTracer('default').startSpan('insertRootSpan'); + context.with(setSpan(context.active(), span), () => { + collection.insertMany(insertData, (err, result) => { + span.end(); + assert.ifError(err); + assertSpans( + memoryExporter.getFinishedSpans(), + 'mongodb.insert', + SpanKind.CLIENT + ); + done(); + }); + }); + }); + + it('should create a child span for update', done => { + const span = provider.getTracer('default').startSpan('updateRootSpan'); + context.with(setSpan(context.active(), span), () => { + collection.updateOne({ a: 2 }, { $set: { b: 1 } }, (err, result) => { + span.end(); + assert.ifError(err); + assertSpans( + memoryExporter.getFinishedSpans(), + 'mongodb.update', + SpanKind.CLIENT + ); + done(); + }); + }); + }); + + it('should create a child span for remove', done => { + const span = provider.getTracer('default').startSpan('removeRootSpan'); + context.with(setSpan(context.active(), span), () => { + collection.deleteOne({ a: 3 }, (err, result) => { + span.end(); + assert.ifError(err); + assertSpans( + memoryExporter.getFinishedSpans(), + 'mongodb.remove', + SpanKind.CLIENT + ); + done(); + }); + }); + }); + }); + + /** Should intercept cursor */ + describe('Instrumenting cursor operations', () => { + it('should create a child span for find', done => { + const span = provider.getTracer('default').startSpan('findRootSpan'); + context.with(setSpan(context.active(), span), () => { + collection.find({ a: 1 }).toArray((err, result) => { + span.end(); + assert.ifError(err); + assertSpans( + memoryExporter.getFinishedSpans(), + 'mongodb.find', + SpanKind.CLIENT + ); + done(); + }); + }); + }); + it('should create a child span for cursor operations', done => { + const span = provider.getTracer('default').startSpan('findRootSpan'); + context.with(setSpan(context.active(), span), () => { + const cursor = collection.find().batchSize(1); + cursor.next().then(firstElement => { + assert(firstElement !== null); + cursor.next().then(secondElement => { + span.end(); + assert(secondElement !== null); + // assert that we correctly got the first as a find + assertSpans( + memoryExporter + .getFinishedSpans() + .filter( + span => span.name.includes('mongodb.getMore') === false + ), + 'mongodb.find', + SpanKind.CLIENT + ); + // assert that we correctly got the first as a find + assertSpans( + memoryExporter + .getFinishedSpans() + .filter(span => span.name.includes('mongodb.find') === false), + 'mongodb.getMore', + SpanKind.CLIENT + ); + done(); + }); + }); + }); + }); + }); + + /** Should intercept command */ + describe('Instrumenting command operations', () => { + it('should create a child span for create index', done => { + const span = provider.getTracer('default').startSpan('indexRootSpan'); + context.with(setSpan(context.active(), span), () => { + collection.createIndex({ a: 1 }, (err, result) => { + span.end(); + assert.ifError(err); + assertSpans( + memoryExporter.getFinishedSpans(), + 'mongodb.createIndexes', + SpanKind.CLIENT + ); + done(); + }); + }); + }); + }); + + /** Should intercept command */ + describe('Removing Instrumentation', () => { + it('should unpatch plugin', () => { + assert.doesNotThrow(() => { + instrumentation.disable(); + }); + }); + + it('should not create a child span for query', done => { + const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; + const span = provider.getTracer('default').startSpan('insertRootSpan'); + collection.insertMany(insertData, (err, result) => { + span.end(); + assert.ifError(err); + assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); + done(); + }); + }); + + it('should not create a child span for cursor', done => { + const span = provider.getTracer('default').startSpan('findRootSpan'); + collection.find({}).toArray((err, result) => { + span.end(); + assert.ifError(err); + assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); + done(); + }); + }); + + it('should not create a child span for command', done => { + const span = provider.getTracer('default').startSpan('indexRootSpan'); + collection.createIndex({ a: 1 }, (err, result) => { + span.end(); + assert.ifError(err); + assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); + done(); + }); + }); + }); +}); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts new file mode 100644 index 0000000000..36e161aa65 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts @@ -0,0 +1,99 @@ +/* + * 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 { SpanStatusCode, SpanKind } from '@opentelemetry/api'; +import { ReadableSpan } from '@opentelemetry/tracing'; +import * as assert from 'assert'; +import * as mongodb from 'mongodb'; +import { + DatabaseAttribute, + GeneralAttribute, +} from '@opentelemetry/semantic-conventions'; + +export interface MongoDBAccess { + client: mongodb.MongoClient; + collection: mongodb.Collection; +} + +/** + * Access the mongodb collection. + * @param url The mongodb URL to access. + * @param dbName The mongodb database name. + * @param collectionName The mongodb collection name. + */ +export function accessCollection( + url: string, + dbName: string, + collectionName: string +): Promise { + return new Promise((resolve, reject) => { + mongodb.MongoClient.connect(url, (err, client) => { + if (err) { + reject(err); + return; + } + const db = client.db(dbName); + const collection = db.collection(collectionName); + resolve({ client, collection }); + }); + }); +} + +/** + * Asserts root spans attributes. + * @param spans Readable spans that we need to assert. + * @param expectedName The expected name of the first root span. + * @param expectedKind The expected kind of the first root span. + * @param log + * @param isEnhancedDatabaseReportingEnabled Is enhanced database reporting enabled: boolean + */ +export function assertSpans( + spans: ReadableSpan[], + expectedName: string, + expectedKind: SpanKind, + log = false, + isEnhancedDatabaseReportingEnabled = false +) { + if (log) { + console.log(spans); + } + assert.strictEqual(spans.length, 2); + spans.forEach(span => { + assert(span.endTime instanceof Array); + assert(span.endTime.length === 2); + }); + const [mongoSpan] = spans; + assert.strictEqual(mongoSpan.name, expectedName); + assert.strictEqual(mongoSpan.kind, expectedKind); + assert.strictEqual( + mongoSpan.attributes[DatabaseAttribute.DB_SYSTEM], + 'mongodb' + ); + assert.strictEqual( + mongoSpan.attributes[GeneralAttribute.NET_HOST_NAME], + process.env.MONGODB_HOST || 'localhost' + ); + assert.strictEqual(mongoSpan.status.code, SpanStatusCode.UNSET); + + if (isEnhancedDatabaseReportingEnabled) { + const dbStatement = mongoSpan.attributes[ + DatabaseAttribute.DB_STATEMENT + ] as any; + for (const key in dbStatement) { + assert.notStrictEqual(dbStatement[key], '?'); + } + } +} diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/tsconfig.json b/plugins/node/opentelemetry-instrumentation-mongodb/tsconfig.json new file mode 100644 index 0000000000..28be80d266 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mongodb/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +}