@@ -21,6 +21,12 @@ import * as hook from 'require-in-the-middle';
2121
2222import { Constants } from '../constants' ;
2323
24+ enum HookState {
25+ UNINITIALIZED ,
26+ ENABLED ,
27+ DISABLED
28+ }
29+
2430/**
2531 * The PluginLoader class can load instrumentation plugins that
2632 * use a patch mechanism to enable automatic tracing for
@@ -33,6 +39,11 @@ export class PluginLoader {
3339 private logger : Logger ;
3440 /** A list of loaded plugins. */
3541 plugins : Plugin [ ] = [ ] ;
42+ /**
43+ * A field that tracks whether the r-i-t-m hook has been loaded for the
44+ * first time, as well as whether the hook body is enabled or not.
45+ */
46+ private hookState = HookState . UNINITIALIZED ;
3647
3748 /**
3849 * Constructs a new PluginLoader instance.
@@ -102,30 +113,33 @@ export class PluginLoader {
102113 * @param pluginList A list of plugins.
103114 */
104115 loadPlugins ( pluginList : PluginNames ) {
105- // tslint:disable:no-any
106- hook ( Object . keys ( pluginList ) , ( exports , name , basedir ) => {
107- const version = this . getPackageVersion ( name , basedir as string ) ;
108- this . logger . info ( 'trying loading %s.%s' , name , version ) ;
109- let moduleExports = exports ;
110- if ( ! version ) {
111- return moduleExports ;
112- } else {
116+ if ( this . hookState === HookState . UNINITIALIZED ) {
117+ hook ( Object . keys ( pluginList ) , ( exports , name , basedir ) => {
118+ if ( this . hookState !== HookState . ENABLED ) {
119+ return exports ;
120+ }
121+ const version = this . getPackageVersion ( name , basedir as string ) ;
122+ this . logger . info ( 'trying loading %s.%s' , name , version ) ;
123+ if ( ! version ) {
124+ return exports ;
125+ }
113126 this . logger . debug ( 'applying patch to %s@%s module' , name , version ) ;
114127 this . logger . debug (
115128 'using package %s to patch %s' , pluginList [ name ] , name ) ;
116129 // Expecting a plugin from module;
117130 try {
118131 const plugin : Plugin = require ( pluginList [ name ] ) . plugin ;
119132 this . plugins . push ( plugin ) ;
120- moduleExports = plugin . enable ( exports , this . tracer , version , basedir ) ;
133+ return plugin . enable ( exports , this . tracer , version , basedir ) ;
121134 } catch ( e ) {
122135 this . logger . error (
123136 'could not load plugin %s of module %s. Error: %s' ,
124137 pluginList [ name ] , name , e . message ) ;
138+ return exports ;
125139 }
126- return moduleExports ;
127- }
128- } ) ;
140+ } ) ;
141+ }
142+ this . hookState = HookState . ENABLED ;
129143 }
130144
131145
@@ -135,6 +149,7 @@ export class PluginLoader {
135149 plugin . disable ( ) ;
136150 }
137151 this . plugins = [ ] ;
152+ this . hookState = HookState . DISABLED ;
138153 }
139154
140155 /**
0 commit comments