{"version":3,"file":"InstrumentationAbstract.cjs","names":["diag","trace","metrics","logs"],"sources":["../../../src/instrumentations/InstrumentationAbstract/InstrumentationAbstract.ts"],"sourcesContent":["import type {\n  DiagLogger,\n  Meter,\n  MeterProvider,\n  Span,\n  Tracer,\n  TracerProvider,\n} from '@opentelemetry/api';\nimport { diag, metrics, trace } from '@opentelemetry/api';\nimport type { Logger } from '@opentelemetry/api-logs';\nimport { logs } from '@opentelemetry/api-logs';\nimport type {\n  Instrumentation,\n  InstrumentationConfig,\n  InstrumentationModuleDefinition,\n  SpanCustomizationHook,\n} from '@opentelemetry/instrumentation';\nimport type { LoggerProvider } from '@opentelemetry/sdk-logs';\n\n// copied directly from https://github.com/open-telemetry/opentelemetry-js/blob/90afa2850c0690f7a18ecc511c04927a3183490b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts\n// to avoid importing internal and experimental code. Some unused blocks removed.\n/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nexport abstract class InstrumentationAbstract<\n  ConfigType extends InstrumentationConfig = InstrumentationConfig,\n> implements Instrumentation<ConfigType>\n{\n  protected _config: ConfigType = {} as ConfigType;\n  protected _diag: DiagLogger;\n\n  public constructor(\n    public readonly instrumentationName: string,\n    public readonly instrumentationVersion: string,\n    config: ConfigType,\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  private _tracer: Tracer;\n\n  /* Returns tracer */\n  protected get tracer(): Tracer {\n    return this._tracer;\n  }\n\n  private _meter: Meter;\n\n  /* Returns meter */\n  protected get meter(): Meter {\n    return this._meter;\n  }\n\n  private _logger: Logger;\n\n  /* Returns logger */\n  protected get logger(): Logger {\n    return this._logger;\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  /* Disable plugin */\n  public abstract disable(): void;\n\n  /* Enable plugin */\n  public abstract enable(): void;\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 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   * 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  /**\n   * Sets TraceProvider 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  /**\n   * Init method in which plugin should define _modules and patches for\n   * methods.\n   *\n   * Note: OTel uses `| void` but we use `| undefined` for semantic clarity.\n   * `void` in union types is confusing because it mixes \"returns nothing\" with \"returns data or nothing\".\n   * `undefined` better expresses the intent: this method may or may not return a value.\n   */\n  protected abstract init():\n    | InstrumentationModuleDefinition\n    | InstrumentationModuleDefinition[]\n    | undefined;\n\n  protected _updateMetricInstruments(): void {\n    return;\n  }\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":";;;;;AAoCA,IAAsB,0BAAtB,MAGA;CACE,UAAgC,EAAE;CAClC;CAEA,YACE,qBACA,wBACA,QACA;AAHgB,OAAA,sBAAA;AACA,OAAA,yBAAA;AAGhB,OAAK,UAAU,OAAO;AAEtB,OAAK,QAAQA,mBAAAA,KAAK,sBAAsB,EACtC,WAAW,qBACZ,CAAC;AAEF,OAAK,UAAUC,mBAAAA,MAAM,UAAU,qBAAqB,uBAAuB;AAC3E,OAAK,SAASC,mBAAAA,QAAQ,SAAS,qBAAqB,uBAAuB;AAC3E,OAAK,UAAUC,wBAAAA,KAAK,UAAU,qBAAqB,uBAAuB;AAC1E,OAAK,0BAA0B;;CAGjC;CAGA,IAAc,SAAiB;AAC7B,SAAO,KAAK;;CAGd;CAGA,IAAc,QAAe;AAC3B,SAAO,KAAK;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