11import { debuglog } from 'util' ;
22import shimmer from 'shimmer' ;
3- import { MongoClient , Server , Cursor , Collection } from 'mongodb' ;
43import Perf from 'performance-node' ;
54import uuid from 'uuid/v4' ;
65import get from 'lodash/get' ;
6+ import loadModuleForTracing from '../loadHelper' ;
7+
8+ const debug = debuglog ( '@iopipe:trace:mongodb' ) ;
9+
10+ let MongoClient ,
11+ Server ,
12+ Cursor ,
13+ Collection ,
14+ clientTarget ,
15+ collectionTarget ,
16+ serverTarget ,
17+ cursorTarget ;
18+
19+ const loadModule = async ( ) => {
20+ const mod = await loadModuleForTracing ( 'mongodb' )
21+ . then ( module => {
22+ MongoClient = module . MongoClient ;
23+ Server = module . Server ;
24+ Cursor = module . Cursor ;
25+ Collection = module . Collection ;
26+
27+ clientTarget = MongoClient && MongoClient . prototype ;
28+ collectionTarget = Collection && Collection . prototype ;
29+ serverTarget = Server && Server . prototype ;
30+ cursorTarget = Cursor && Cursor . prototype ;
31+
32+ return module ;
33+ } )
34+ . catch ( e => {
35+ debug ( 'Not loading mongodb' , e ) ;
36+ return null ;
37+ } ) ;
38+ return mod ;
39+ } ;
740
841const dbType = 'mongodb' ;
942const serverOps = [ 'command' , 'insert' , 'update' , 'remove' ] ;
@@ -23,13 +56,6 @@ const collectionOps = [
2356const cursorOps = [ 'next' , 'filter' , 'sort' , 'hint' , 'toArray' ] ;
2457const clientOps = [ 'connect' , 'close' , 'db' ] ;
2558
26- const clientTarget = MongoClient && MongoClient . prototype ;
27- const collectionTarget = Collection && Collection . prototype ;
28- const serverTarget = Server && Server . prototype ;
29- const cursorTarget = Cursor && Cursor . prototype ;
30-
31- const debug = debuglog ( '@iopipe/trace' ) ;
32-
3359/*eslint-disable babel/no-invalid-this*/
3460/*eslint-disable func-name-matching */
3561/*eslint-disable prefer-rest-params */
@@ -146,7 +172,13 @@ const filterRequest = (params, context) => {
146172 } ;
147173} ;
148174
149- function wrap ( { timeline, data = { } } = { } ) {
175+ async function wrap ( { timeline, data = { } } = { } ) {
176+ await loadModule ( ) ;
177+
178+ if ( ! clientTarget ) {
179+ debug ( 'mongodb plugin not accessible from trace plugin. Skipping.' ) ;
180+ return false ;
181+ }
150182 if ( ! ( timeline instanceof Perf ) ) {
151183 debug (
152184 'Timeline passed to plugins/mongodb.wrap not an instance of performance-node. Skipping.'
@@ -217,6 +249,13 @@ function wrap({ timeline, data = {} } = {}) {
217249}
218250
219251function unwrap ( ) {
252+ if ( ! clientTarget ) {
253+ debug (
254+ 'mongodb plugin not accessible from trace plugin. Nothing to unwrap.'
255+ ) ;
256+ return false ;
257+ }
258+
220259 if ( serverTarget . __iopipeShimmer ) {
221260 shimmer . massUnwrap ( serverTarget , serverOps ) ;
222261 delete serverTarget . __iopipeShimmer ;
@@ -233,6 +272,7 @@ function unwrap() {
233272 shimmer . massUnwrap ( clientTarget , clientOps ) ; // mass just seems to hang and not complete
234273 delete clientTarget . __iopipeShimmer ;
235274 }
275+ return true ;
236276}
237277
238278export { unwrap , wrap } ;
0 commit comments