Skip to content

Commit

Permalink
chore: address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Nov 15, 2019
1 parent a95c2cc commit 292bff1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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/node_modules
- packages/opentelemetry-plugin-mongodb-core/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
48 changes: 20 additions & 28 deletions packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import {
import * as mongodb from 'mongodb';
import * as shimmer from 'shimmer';

/** MongoDB instrumentation plugin for OpenTelemetry */
export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
/** MongoDBCore instrumentation plugin for OpenTelemetry */
export class MongoDBCorePlugin 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 _DB_TYPE = 'mongodb';

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

constructor(readonly moduleName: string) {
Expand Down Expand Up @@ -96,10 +99,9 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
ns: string,
commands: MongoInternalCommand[] | MongoInternalCommand,
options: {} | Function,
callback: Func<unknown>
callback: Function
): mongodb.Server {
const currentSpan = plugin._tracer.getCurrentSpan();
// @ts-ignore
const resultHandler =
typeof options === 'function' ? options : callback;
if (
Expand All @@ -121,22 +123,12 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
command,
this as MongoInternalTopology
);
if (typeof options === 'function') {
return original.call(
this,
ns,
commands,
plugin._patchEnd(span, options as Func<unknown>)
);
} else {
return original.call(
this,
ns,
commands,
options,
plugin._patchEnd(span, callback)
);
}
return original.call(
this,
ns,
commands,
plugin._patchEnd(span, resultHandler)
);
};
};
}
Expand Down Expand Up @@ -184,8 +176,8 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
// add database related attributes
span.setAttributes({
[AttributeNames.DB_INSTANCE]: `${ns}`,
[AttributeNames.DB_TYPE]: `mongodb`,
[AttributeNames.COMPONENT]: 'mongodb-core',
[AttributeNames.DB_TYPE]: this._DB_TYPE,
[AttributeNames.COMPONENT]: this._COMPONENT,
});

if (command === undefined) return;
Expand All @@ -212,9 +204,9 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
...args: unknown[]
): mongodb.Cursor {
const currentSpan = plugin._tracer.getCurrentSpan();
const resultHandler = args[0] as Func<unknown> | undefined;
if (currentSpan === null || resultHandler === undefined) {
return original.apply(this, (arguments as unknown) as unknown[]);
const resultHandler = args[0];
if (currentSpan === null || typeof resultHandler !== 'function') {
return original.apply(this, args);
}
const span = plugin._tracer.startSpan(`mongodb.query`, {
parent: currentSpan,
Expand All @@ -232,7 +224,7 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
* @param span The created span to end.
* @param resultHandler A callback function.
*/
private _patchEnd(span: Span, resultHandler: Func<unknown>): Function {
private _patchEnd(span: Span, resultHandler: Function): Function {
return function patchedEnd(this: {}, ...args: unknown[]) {
const error = args[0];
if (error instanceof Error) {
Expand All @@ -246,9 +238,9 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
});
}
span.end();
return resultHandler.apply(this, (arguments as unknown) as unknown[]);
return resultHandler.apply(this, args);
};
}
}

export const plugin = new MongoDBPlugin('mongodb-core');
export const plugin = new MongoDBCorePlugin('mongodb-core');

0 comments on commit 292bff1

Please sign in to comment.