Skip to content

Commit

Permalink
feat: port mongodb-core plugin to mongodb #622 (#652)
Browse files Browse the repository at this point in the history
* feat: port mongodb-core plugin to mongodb #622

* chore: address PR comments
  • Loading branch information
vmarchaud authored and mayurkale22 committed Jan 3, 2020
1 parent 6c98ab9 commit 53c40c1
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ cache_2: &cache_2
- packages/opentelemetry-plugin-grpc/node_modules
- packages/opentelemetry-plugin-http/node_modules
- packages/opentelemetry-plugin-http2/node_modules
- packages/opentelemetry-plugin-mongodb-core/node_modules
- packages/opentelemetry-plugin-mongodb/node_modules
- packages/opentelemetry-plugin-redis/node_modules
- packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/node_modules
- packages/opentelemetry-plugin-document-load/node_modules
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ OpenTelemetry can collect tracing data automatically using plugins. Vendors/User
- [@opentelemetry/plugin-grpc](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-grpc)
- [@opentelemetry/plugin-http](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-http)
- [@opentelemetry/plugin-https](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-https)
- [@opentelemetry/plugin-mongodb-core](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-mongodb-core)
- [@opentelemetry/plugin-mongodb](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-mongodb)
- [@opentelemetry/plugin-mysql](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-mysql)
- [@opentelemetry/plugin-pg](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg)
- [@opentelemetry/plugin-pg-pool](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool)
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-node/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface NodeTracerConfig extends BasicTracerConfig {

/** List of all default supported plugins */
export const DEFAULT_INSTRUMENTATION_PLUGINS: Plugins = {
'mongodb-core': { enabled: true, path: '@opentelemetry/plugin-mongodb-core' },
mongodb: { enabled: true, path: '@opentelemetry/plugin-mongodb' },
grpc: { enabled: true, path: '@opentelemetry/plugin-grpc' },
http: { enabled: true, path: '@opentelemetry/plugin-http' },
https: { enabled: true, path: '@opentelemetry/plugin-https' },
Expand Down
18 changes: 0 additions & 18 deletions packages/opentelemetry-plugin-mongodb-core/src/version.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# 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.

## Installation

```bash
npm install --save @opentelemetry/plugin-mongodb-core
npm install --save @opentelemetry/plugin-mongodb
```
### Supported Versions
- `'>=2.0.0 <4`

## Usage

Expand All @@ -25,10 +27,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',
}
}
});
Expand All @@ -41,7 +43,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: <https://opentelemetry.io/>
Expand All @@ -56,7 +58,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
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -19,7 +19,7 @@
},
"keywords": [
"opentelemetry",
"mongodb-core",
"mongodb",
"nodejs",
"tracing",
"profiling",
Expand Down Expand Up @@ -50,7 +50,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ import * as mongodb from 'mongodb';
import * as shimmer from 'shimmer';

/** MongoDBCore instrumentation plugin for OpenTelemetry */
export class MongoDBCorePlugin extends BasePlugin<typeof mongodb> {
export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
private readonly _SERVER_METHODS = ['insert', 'update', 'remove', 'command'];
private readonly _CURSOR_METHODS = ['_next', 'next'];

private readonly _COMPONENT = 'mongodb-core';
private readonly _COMPONENT = 'mongodb';
private readonly _DB_TYPE = 'mongodb';

readonly supportedVersions = ['>=2 <3'];
readonly supportedVersions = ['>=2 <4'];

constructor(readonly moduleName: string) {
super();
Expand All @@ -51,7 +51,7 @@ export class MongoDBCorePlugin extends BasePlugin<typeof mongodb> {

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
Expand All @@ -65,7 +65,7 @@ export class MongoDBCorePlugin extends BasePlugin<typeof mongodb> {

if (this._moduleExports.Cursor) {
this._logger.debug(
'patching mongodb-core.Cursor.prototype functions:',
'patching mongodb.Cursor.prototype functions:',
this._CURSOR_METHODS
);
shimmer.massWrap(
Expand Down Expand Up @@ -170,10 +170,12 @@ export class MongoDBCorePlugin extends BasePlugin<typeof mongodb> {
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
Expand All @@ -184,7 +186,7 @@ export class MongoDBCorePlugin extends BasePlugin<typeof mongodb> {
});

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;
Expand Down Expand Up @@ -246,4 +248,4 @@ export class MongoDBCorePlugin extends BasePlugin<typeof mongodb> {
}
}

export const plugin = new MongoDBCorePlugin('mongodb-core');
export const plugin = new MongoDBPlugin('mongodb');
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function assertSpans(
assert.strictEqual(mongoSpan.kind, expectedKind);
assert.strictEqual(
mongoSpan.attributes[AttributeNames.COMPONENT],
'mongodb-core'
'mongodb'
);
assert.strictEqual(
mongoSpan.attributes[AttributeNames.PEER_HOSTNAME],
Expand Down

0 comments on commit 53c40c1

Please sign in to comment.