Skip to content

Commit

Permalink
feat: port mongodb-core plugin to mongodb #622
Browse files Browse the repository at this point in the history
vmarchaud committed Jan 1, 2020
1 parent e859b8e commit 692ffc6
Showing 10 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# 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
```

## 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: <https://opentelemetry.io/>
@@ -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
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",
@@ -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",
Original file line number Diff line number Diff line change
@@ -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();
@@ -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
@@ -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(
@@ -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
@@ -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;
@@ -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
@@ -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;
};
};

Original file line number Diff line number Diff line change
@@ -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],

0 comments on commit 692ffc6

Please sign in to comment.