From 8facb6e30bbdab1fae606f1c89a9467c41fe8ed4 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sun, 29 Dec 2019 20:15:20 +0100 Subject: [PATCH] feat: port mongodb-core plugin to mongodb #622 --- .../.npmignore | 0 .../LICENSE | 0 .../README.md | 20 ++++++++--------- .../package.json | 8 +++---- .../src/index.ts | 0 .../src/mongodb.ts | 22 ++++++++++--------- .../src/types.ts | 9 ++++++-- .../test/mongodb.test.ts | 2 +- .../tsconfig.json | 0 .../tslint.json | 0 10 files changed, 34 insertions(+), 27 deletions(-) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/.npmignore (100%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/LICENSE (100%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/README.md (82%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/package.json (89%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/src/index.ts (100%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/src/mongodb.ts (91%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/src/types.ts (88%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/test/mongodb.test.ts (99%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/tsconfig.json (100%) rename packages/{opentelemetry-plugin-mongodb-core => opentelemetry-plugin-mongodb}/tslint.json (100%) diff --git a/packages/opentelemetry-plugin-mongodb-core/.npmignore b/packages/opentelemetry-plugin-mongodb/.npmignore similarity index 100% rename from packages/opentelemetry-plugin-mongodb-core/.npmignore rename to packages/opentelemetry-plugin-mongodb/.npmignore diff --git a/packages/opentelemetry-plugin-mongodb-core/LICENSE b/packages/opentelemetry-plugin-mongodb/LICENSE similarity index 100% rename from packages/opentelemetry-plugin-mongodb-core/LICENSE rename to packages/opentelemetry-plugin-mongodb/LICENSE diff --git a/packages/opentelemetry-plugin-mongodb-core/README.md b/packages/opentelemetry-plugin-mongodb/README.md similarity index 82% rename from packages/opentelemetry-plugin-mongodb-core/README.md rename to packages/opentelemetry-plugin-mongodb/README.md index d4da15c5d4e..a2a1316c49b 100644 --- a/packages/opentelemetry-plugin-mongodb-core/README.md +++ b/packages/opentelemetry-plugin-mongodb/README.md @@ -1,10 +1,10 @@ -# OpenTelemetry mongodb-core Instrumentation for Node.js +# 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-core`](https://github.com/mongodb-js/mongodb-core). +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/master/packages/opentelemetry-node) package. @@ -12,7 +12,7 @@ For automatic instrumentation see the ## Installation ```bash -npm install --save @opentelemetry/plugin-mongodb-core +npm install --save @opentelemetry/plugin-mongodb ``` ## Usage @@ -25,10 +25,10 @@ const { NodeTracer } = require('@opentelemetry/node'); const tracer = new NodeTracer({ plugins: { - 'mongodb-core': { + mongodb: { enabled: true, // You may use a package name or absolute path to the file. - path: '@opentelemetry/plugin-mongodb-core', + path: '@opentelemetry/plugin-mongodb', } } }); @@ -41,7 +41,7 @@ const { NodeTracer } = require('@opentelemetry/node'); const tracer = new NodeTracer(); ``` -See [examples/mongodb](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/mongodb-core) for a short example. +See [examples/mongodb](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/mongodb) for a short example. ## Useful links - For more information on OpenTelemetry, visit: @@ -56,7 +56,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information. [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/blob/master/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/status.svg?path=packages/opentelemetry-plugin-mongodb-core -[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-plugin-mongodb-core -[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-plugin-mongodb-core -[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-plugin-mongodb-core&type=dev +[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-plugin-mongodb +[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-plugin-mongodb +[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-plugin-mongodb +[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-plugin-mongodb&type=dev diff --git a/packages/opentelemetry-plugin-mongodb-core/package.json b/packages/opentelemetry-plugin-mongodb/package.json similarity index 89% rename from packages/opentelemetry-plugin-mongodb-core/package.json rename to packages/opentelemetry-plugin-mongodb/package.json index 1bd5421efca..3482283de8e 100644 --- a/packages/opentelemetry-plugin-mongodb-core/package.json +++ b/packages/opentelemetry-plugin-mongodb/package.json @@ -1,7 +1,7 @@ { - "name": "@opentelemetry/plugin-mongodb-core", + "name": "@opentelemetry/plugin-mongodb", "version": "0.3.1", - "description": "OpenTelemetry mongodb-core automatic instrumentation package.", + "description": "OpenTelemetry mongodb automatic instrumentation package.", "main": "build/src/index.js", "types": "build/src/index.d.ts", "repository": "open-telemetry/opentelemetry-js", @@ -18,7 +18,7 @@ }, "keywords": [ "opentelemetry", - "mongodb-core", + "mongodb", "nodejs", "tracing", "profiling", @@ -49,7 +49,7 @@ "codecov": "^3.6.1", "gts": "^1.1.0", "mocha": "^6.2.0", - "mongodb": "^3.3.0", + "mongodb": "^3.4.1", "nyc": "^14.1.1", "rimraf": "^3.0.0", "ts-mocha": "^6.0.0", diff --git a/packages/opentelemetry-plugin-mongodb-core/src/index.ts b/packages/opentelemetry-plugin-mongodb/src/index.ts similarity index 100% rename from packages/opentelemetry-plugin-mongodb-core/src/index.ts rename to packages/opentelemetry-plugin-mongodb/src/index.ts diff --git a/packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts b/packages/opentelemetry-plugin-mongodb/src/mongodb.ts similarity index 91% rename from packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts rename to packages/opentelemetry-plugin-mongodb/src/mongodb.ts index 0c2afbbf3fa..dc434d1ac6e 100644 --- a/packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts +++ b/packages/opentelemetry-plugin-mongodb/src/mongodb.ts @@ -30,14 +30,14 @@ import * as mongodb from 'mongodb'; import * as shimmer from 'shimmer'; /** MongoDBCore instrumentation plugin for OpenTelemetry */ -export class MongoDBCorePlugin extends BasePlugin { +export class MongoDBPlugin extends BasePlugin { private readonly _SERVER_METHODS = ['insert', 'update', 'remove', 'command']; private readonly _CURSOR_METHODS = ['_next', 'next']; - private readonly _COMPONENT = 'mongodb-core'; + private readonly _COMPONENT = 'mongodb-nodejs'; private readonly _DB_TYPE = 'mongodb'; - readonly supportedVersions = ['>=2 <3']; + readonly supportedVersions = ['>=2 <4']; constructor(readonly moduleName: string) { super(); @@ -51,7 +51,7 @@ export class MongoDBCorePlugin extends BasePlugin { if (this._moduleExports.Server) { for (const fn of this._SERVER_METHODS) { - this._logger.debug(`patching mongodb-core.Server.prototype.${fn}`); + this._logger.debug(`patching mongodb.Server.prototype.${fn}`); shimmer.wrap( this._moduleExports.Server.prototype, // Forced to ignore due to incomplete typings @@ -65,7 +65,7 @@ export class MongoDBCorePlugin extends BasePlugin { if (this._moduleExports.Cursor) { this._logger.debug( - 'patching mongodb-core.Cursor.prototype functions:', + 'patching mongodb.Cursor.prototype functions:', this._CURSOR_METHODS ); shimmer.massWrap( @@ -170,10 +170,12 @@ export class MongoDBCorePlugin extends BasePlugin { topology: MongoInternalTopology ) { // add network attributes to determine the remote server - if (topology && topology.s && topology.s.options) { + if (topology && topology.s) { span.setAttributes({ - [AttributeNames.PEER_HOSTNAME]: `${topology.s.options.host}`, - [AttributeNames.PEER_PORT]: `${topology.s.options.port}`, + [AttributeNames.PEER_HOSTNAME]: `${topology.s.options?.host ?? + topology.s.host}`, + [AttributeNames.PEER_PORT]: `${topology.s.options?.port ?? + topology.s.port}`, }); } // add database related attributes @@ -184,7 +186,7 @@ export class MongoDBCorePlugin extends BasePlugin { }); if (command === undefined) return; - const query = Object.keys(command.query || command.q || {}).reduce( + const query = Object.keys(command.query ?? command.q ?? command).reduce( (obj, key) => { obj[key] = '?'; return obj; @@ -246,4 +248,4 @@ export class MongoDBCorePlugin extends BasePlugin { } } -export const plugin = new MongoDBCorePlugin('mongodb-core'); +export const plugin = new MongoDBPlugin('mongodb'); diff --git a/packages/opentelemetry-plugin-mongodb-core/src/types.ts b/packages/opentelemetry-plugin-mongodb/src/types.ts similarity index 88% rename from packages/opentelemetry-plugin-mongodb-core/src/types.ts rename to packages/opentelemetry-plugin-mongodb/src/types.ts index 3c8b33b542b..396517471c6 100644 --- a/packages/opentelemetry-plugin-mongodb-core/src/types.ts +++ b/packages/opentelemetry-plugin-mongodb/src/types.ts @@ -20,18 +20,23 @@ export type MongoInternalCommand = { createIndexes: boolean; count: boolean; ismaster: boolean; + indexes?: unknown[]; query?: { [key: string]: unknown }; q?: { [key: string]: unknown }; }; -// -// https://github.com/mongodb-js/mongodb-core/blob/master/lib/topologies/server.js#L117 + +// https://github.com/mongodb/node-mongodb-native/blob/master/lib/topologies/server.js#L179 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; }; }; diff --git a/packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts b/packages/opentelemetry-plugin-mongodb/test/mongodb.test.ts similarity index 99% rename from packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts rename to packages/opentelemetry-plugin-mongodb/test/mongodb.test.ts index b0f4ee15fcd..815ef5b5420 100644 --- a/packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts +++ b/packages/opentelemetry-plugin-mongodb/test/mongodb.test.ts @@ -77,7 +77,7 @@ function assertSpans( assert.strictEqual(mongoSpan.kind, expectedKind); assert.strictEqual( mongoSpan.attributes[AttributeNames.COMPONENT], - 'mongodb-core' + 'mongodb-nodejs' ); assert.strictEqual( mongoSpan.attributes[AttributeNames.PEER_HOSTNAME], diff --git a/packages/opentelemetry-plugin-mongodb-core/tsconfig.json b/packages/opentelemetry-plugin-mongodb/tsconfig.json similarity index 100% rename from packages/opentelemetry-plugin-mongodb-core/tsconfig.json rename to packages/opentelemetry-plugin-mongodb/tsconfig.json diff --git a/packages/opentelemetry-plugin-mongodb-core/tslint.json b/packages/opentelemetry-plugin-mongodb/tslint.json similarity index 100% rename from packages/opentelemetry-plugin-mongodb-core/tslint.json rename to packages/opentelemetry-plugin-mongodb/tslint.json