{"version":3,"file":"instrumentation.mjs","names":["shimmer.wrap","shimmer.unwrap","shimmer.massWrap","shimmer.massUnwrap"],"sources":["../src/instrumentation.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type {\n  DiagLogger,\n  Meter,\n  MeterProvider,\n  Tracer,\n  TracerProvider,\n  Span,\n} from '@opentelemetry/api';\nimport { diag, metrics, trace } from '@opentelemetry/api';\nimport type { Logger, LoggerProvider } from '@opentelemetry/api';\nimport { logs } from '@opentelemetry/api';\nimport * as shimmer from './shimmer';\nimport type {\n  InstrumentationModuleDefinition,\n  Instrumentation,\n  InstrumentationConfig,\n  SpanCustomizationHook,\n} from './types';\n\n/**\n * Base abstract internal class for instrumenting node and web plugins\n */\nexport abstract class InstrumentationAbstract<\n  ConfigType extends InstrumentationConfig = InstrumentationConfig,\n> implements Instrumentation<ConfigType>\n{\n  protected _config: ConfigType = {} as ConfigType;\n\n  private _tracer: Tracer;\n  private _meter: Meter;\n  private _logger: Logger;\n  protected _diag: DiagLogger;\n  public readonly instrumentationName: string;\n  public readonly instrumentationVersion: string;\n\n  constructor(\n    instrumentationName: string,\n    instrumentationVersion: string,\n    config: ConfigType\n  ) {\n    this.instrumentationName = instrumentationName;\n    this.instrumentationVersion = instrumentationVersion;\n\n    this.setConfig(config);\n\n    this._diag = diag.createComponentLogger({\n      namespace: instrumentationName,\n    });\n\n    this._tracer = trace.getTracer(instrumentationName, instrumentationVersion);\n    this._meter = metrics.getMeter(instrumentationName, instrumentationVersion);\n    this._logger = logs.getLogger(instrumentationName, instrumentationVersion);\n    this._updateMetricInstruments();\n  }\n\n  /* Api to wrap instrumented method */\n  protected _wrap = shimmer.wrap;\n  /* Api to unwrap instrumented methods */\n  protected _unwrap = shimmer.unwrap;\n  /* Api to mass wrap instrumented method */\n  protected _massWrap = shimmer.massWrap;\n  /* Api to mass unwrap instrumented methods */\n  protected _massUnwrap = shimmer.massUnwrap;\n\n  /* Returns meter */\n  protected get meter(): Meter {\n    return this._meter;\n  }\n\n  /**\n   * Sets MeterProvider to this plugin\n   * @param meterProvider\n   */\n  public setMeterProvider(meterProvider: MeterProvider): void {\n    this._meter = meterProvider.getMeter(\n      this.instrumentationName,\n      this.instrumentationVersion\n    );\n\n    this._updateMetricInstruments();\n  }\n\n  /* Returns logger */\n  protected get logger(): Logger {\n    return this._logger;\n  }\n\n  /**\n   * Sets LoggerProvider to this plugin\n   * @param loggerProvider\n   */\n  public setLoggerProvider(loggerProvider: LoggerProvider): void {\n    this._logger = loggerProvider.getLogger(\n      this.instrumentationName,\n      this.instrumentationVersion\n    );\n  }\n\n  /**\n   * @experimental\n   *\n   * Get module definitions defined by {@link init}.\n   * This can be used for experimental compile-time instrumentation.\n   *\n   * @returns an array of {@link InstrumentationModuleDefinition}\n   */\n  public getModuleDefinitions(): InstrumentationModuleDefinition[] {\n    const initResult = this.init() ?? [];\n    if (!Array.isArray(initResult)) {\n      return [initResult];\n    }\n\n    return initResult;\n  }\n\n  /**\n   * Sets the new metric instruments with the current Meter.\n   */\n  protected _updateMetricInstruments(): void {\n    return;\n  }\n\n  /* Returns InstrumentationConfig */\n  public getConfig(): ConfigType {\n    return this._config;\n  }\n\n  /**\n   * Sets InstrumentationConfig to this plugin\n   * @param config\n   */\n  public setConfig(config: ConfigType): void {\n    // copy config first level properties to ensure they are immutable.\n    // nested properties are not copied, thus are mutable from the outside.\n    this._config = {\n      enabled: true,\n      ...config,\n    };\n  }\n\n  /**\n   * Sets TracerProvider to this plugin\n   * @param tracerProvider\n   */\n  public setTracerProvider(tracerProvider: TracerProvider): void {\n    this._tracer = tracerProvider.getTracer(\n      this.instrumentationName,\n      this.instrumentationVersion\n    );\n  }\n\n  /* Returns tracer */\n  protected get tracer(): Tracer {\n    return this._tracer;\n  }\n\n  /* Enable plugin */\n  public abstract enable(): void;\n\n  /* Disable plugin */\n  public abstract disable(): void;\n\n  /**\n   * Init method in which plugin should define _modules and patches for\n   * methods.\n   */\n  protected abstract init():\n    | InstrumentationModuleDefinition\n    | InstrumentationModuleDefinition[]\n    | void;\n\n  /**\n   * Execute span customization hook, if configured, and log any errors.\n   * Any semantics of the trigger and info are defined by the specific instrumentation.\n   * @param hookHandler The optional hook handler which the user has configured via instrumentation config\n   * @param triggerName The name of the trigger for executing the hook for logging purposes\n   * @param span The span to which the hook should be applied\n   * @param info The info object to be passed to the hook, with useful data the hook may use\n   */\n  protected _runSpanCustomizationHook<SpanCustomizationInfoType>(\n    hookHandler: SpanCustomizationHook<SpanCustomizationInfoType> | undefined,\n    triggerName: string,\n    span: Span,\n    info: SpanCustomizationInfoType\n  ) {\n    if (!hookHandler) {\n      return;\n    }\n\n    try {\n      hookHandler(span, info);\n    } catch (e) {\n      this._diag.error(\n        'Error running span customization hook due to exception in handler',\n        { triggerName },\n        e\n      );\n    }\n  }\n}\n"],"mappings":";;;;;;AA2BA,IAAsB,0BAAtB,MAGA;CACE,UAAgC,CAAC;CAEjC;CACA;CACA;CACA;CACA;CACA;CAEA,YACE,qBACA,wBACA,QACA;EACA,KAAK,sBAAsB;EAC3B,KAAK,yBAAyB;EAE9B,KAAK,UAAU,MAAM;EAErB,KAAK,QAAQ,KAAK,sBAAsB,EACtC,WAAW,oBACb,CAAC;EAED,KAAK,UAAU,MAAM,UAAU,qBAAqB,sBAAsB;EAC1E,KAAK,SAAS,QAAQ,SAAS,qBAAqB,sBAAsB;EAC1E,KAAK,UAAU,KAAK,UAAU,qBAAqB,sBAAsB;EACzE,KAAK,yBAAyB;CAChC;CAGA,QAAkBA;CAElB,UAAoBC;CAEpB,YAAsBC;CAEtB,cAAwBC;CAGxB,IAAc,QAAe;EAC3B,OAAO,KAAK;CACd;;;;;CAMA,iBAAwB,eAAoC;EAC1D,KAAK,SAAS,cAAc,SAC1B,KAAK,qBACL,KAAK,sBACP;EAEA,KAAK,yBAAyB;CAChC;CAGA,IAAc,SAAiB;EAC7B,OAAO,KAAK;CACd;;;;;CAMA,kBAAyB,gBAA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