Skip to content

Commit

Permalink
feat: Mongoose tracing support added to MongoDB (#3252)
Browse files Browse the repository at this point in the history
* Add flag to switch to mongoose as the wrapped mongo library
  • Loading branch information
underscorebrody committed Feb 17, 2021
1 parent 7c03a77 commit 61572bc
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/tracing/src/integrations/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ interface MongoCollection {
interface MongoOptions {
operations?: Operation[];
describeOperations?: boolean | Operation[];
useMongoose?: boolean;
}

/** Tracing integration for mongo package */
Expand All @@ -101,6 +102,7 @@ export class Mongo implements Integration {

private _operations: Operation[];
private _describeOperations?: boolean | Operation[];
private _useMongoose: boolean;

/**
* @inheritDoc
Expand All @@ -110,19 +112,20 @@ export class Mongo implements Integration {
? options.operations
: ((OPERATIONS as unknown) as Operation[]);
this._describeOperations = 'describeOperations' in options ? options.describeOperations : true;
this._useMongoose = !!options.useMongoose;
}

/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
let collection: MongoCollection;

const moduleName = this._useMongoose ? 'mongoose' : 'mongodb';
try {
const mongodbModule = dynamicRequire(module, 'mongodb') as { Collection: MongoCollection };
const mongodbModule = dynamicRequire(module, moduleName) as { Collection: MongoCollection };
collection = mongodbModule.Collection;
} catch (e) {
logger.error('Mongo Integration was unable to require `mongodb` package.');
logger.error(`Mongo Integration was unable to require \`${moduleName}\` package.`);
return;
}

Expand Down

0 comments on commit 61572bc

Please sign in to comment.