Instrumentation.prototype.start = function()

in lib/instrumentation/index.js [406:477]


Instrumentation.prototype.start = function (runContextClass) {
  if (this._started) return;
  this._started = true;

  // Could have changed in Agent.start().
  this._log = this._agent.logger;

  // Select the appropriate run-context manager.
  const confContextManager = this._agent._conf.contextManager;
  if (confContextManager === CONTEXT_MANAGER_ASYNCHOOKS) {
    this._runCtxMgr = new AsyncHooksRunContextManager(
      this._log,
      runContextClass,
    );
  } else if (nodeSupportsAsyncLocalStorage) {
    this._runCtxMgr = new AsyncLocalStorageRunContextManager(
      this._log,
      runContextClass,
    );
  } else {
    if (confContextManager === CONTEXT_MANAGER_ASYNCLOCALSTORAGE) {
      this._log.warn(
        `config includes 'contextManager="${confContextManager}"', but node ${process.version} does not support AsyncLocalStorage for run-context management: falling back to using async_hooks`,
      );
    }
    this._runCtxMgr = new AsyncHooksRunContextManager(
      this._log,
      runContextClass,
    );
  }

  // Load module patchers: from MODULE_PATCHERS, for Lambda, and from
  // config.addPatch.
  for (let info of MODULE_PATCHERS) {
    let patcher;
    if (info.patcher) {
      patcher = path.resolve(__dirname, info.patcher);
    } else {
      // Typically the patcher module for the APM agent's included
      // instrumentations is "./modules/${modPath}[.js]".
      patcher = path.resolve(
        __dirname,
        'modules',
        info.modPath + (info.modPath.endsWith('.js') ? '' : '.js'),
      );
    }

    this._patcherReg.add(info.modPath, patcher, info.diKey);
  }

  this._maybeLoadLambdaPatcher();

  const patches = this._agent._conf.addPatch;
  if (Array.isArray(patches)) {
    for (const [modPath, patcher] of patches) {
      this._patcherReg.add(modPath, patcher);
    }
  }

  this._runCtxMgr.enable();
  this._restartHooks();

  if (nodeHasInstrumentableFetch && this._isModuleEnabled('undici')) {
    this._log.debug('instrumenting fetch');
    undiciInstr.instrumentUndici(this._agent);
  }

  if (azureFunctionsInstr.isAzureFunctionsEnvironment) {
    this._log.debug('instrumenting azure-functions');
    azureFunctionsInstr.instrument(this._agent);
  }
};