{"version":3,"file":"instrumentation.cjs","names":["InstrumentationBase","PACKAGE_NAME","PACKAGE_VERSION","logs","ATTR_EXCEPTION_MESSAGE","ATTR_EXCEPTION_TYPE","ATTR_EXCEPTION_STACKTRACE","SeverityNumber","hrTime"],"sources":["../src/instrumentation.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {\n  InstrumentationBase,\n  type InstrumentationConfig,\n} from '@opentelemetry/instrumentation';\nimport {\n  ATTR_EXCEPTION_TYPE,\n  ATTR_EXCEPTION_MESSAGE,\n  ATTR_EXCEPTION_STACKTRACE,\n} from '@opentelemetry/semantic-conventions';\nimport {\n  SeverityNumber,\n  logs,\n  type Attributes,\n  type LogRecord,\n} from '@opentelemetry/api';\nimport { hrTime } from '@opentelemetry/core';\n\n/** @knipignore */\nimport { PACKAGE_NAME, PACKAGE_VERSION } from './version';\n\nexport interface GlobalErrorsInstrumentationConfig\n  extends InstrumentationConfig {\n  /**\n   * A callback function for adding custom attributes to the log event when an error is recorded.\n   *\n   * @param {Error} error - The error object that is being recorded.\n   * @returns {Attributes} - The attributes to add to the span.\n   */\n  applyCustomAttributes?: (error: Error | string) => Attributes;\n}\n\nexport class ExceptionInstrumentation extends InstrumentationBase<GlobalErrorsInstrumentationConfig> {\n  declare private _onErrorHandler?: (\n    event: ErrorEvent | PromiseRejectionEvent\n  ) => void;\n\n  constructor(config: GlobalErrorsInstrumentationConfig = {}) {\n    super(PACKAGE_NAME, PACKAGE_VERSION, config);\n  }\n\n  init() {}\n\n  onError(event: ErrorEvent | PromiseRejectionEvent) {\n    const EXCEPTION_EVENT_NAME = 'exception';\n    const error: Error | string | undefined =\n      'reason' in event ? event.reason : event.error;\n\n    if (error === undefined) {\n      return;\n    }\n\n    const logger = logs\n      .getLoggerProvider()\n      .getLogger(this.instrumentationName, this.instrumentationVersion);\n\n    const config = this.getConfig();\n    const customAttributes = config.applyCustomAttributes\n      ? config.applyCustomAttributes(error)\n      : {};\n\n    let errorAttributes: Attributes = {};\n\n    if (typeof error === 'string') {\n      errorAttributes = { [ATTR_EXCEPTION_MESSAGE]: error };\n    } else {\n      errorAttributes = {\n        [ATTR_EXCEPTION_TYPE]: error.name,\n        [ATTR_EXCEPTION_MESSAGE]: error.message,\n        [ATTR_EXCEPTION_STACKTRACE]: error.stack,\n      };\n    }\n\n    const errorLog: LogRecord = {\n      eventName: EXCEPTION_EVENT_NAME,\n      severityNumber: SeverityNumber.ERROR,\n      attributes: { ...errorAttributes, ...customAttributes },\n      timestamp: hrTime(),\n      observedTimestamp: hrTime(),\n    };\n\n    logger.emit(errorLog);\n  }\n\n  override disable(): void {\n    if (this._onErrorHandler) {\n      window.removeEventListener('error', this._onErrorHandler);\n      window.removeEventListener('unhandledrejection', this._onErrorHandler);\n    }\n  }\n\n  override enable(): void {\n    if (!this._onErrorHandler) {\n      this._onErrorHandler = this.onError.bind(this);\n    }\n    window.addEventListener('error', this._onErrorHandler);\n    window.addEventListener('unhandledrejection', this._onErrorHandler);\n  }\n}\n"],"mappings":";;;;;;;AAoCA,IAAa,2BAAb,cAA8CA,+BAAAA,oBAAuD;CAKnG,YAAY,SAA4C,CAAC,GAAG;EAC1D,MAAMC,gBAAAA,cAAcC,gBAAAA,iBAAiB,MAAM;CAC7C;CAEA,OAAO,CAAC;CAER,QAAQ,OAA2C;EACjD,MAAM,uBAAuB;EAC7B,MAAM,QACJ,YAAY,QAAQ,MAAM,SAAS,MAAM;EAE3C,IAAI,UAAU,KAAA,GACZ;EAGF,MAAM,SAASC,mBAAAA,KACZ,kBAAkB,CAAC,CACnB,UAAU,KAAK,qBAAqB,KAAK,sBAAsB;EAElE,MAAM,SAAS,KAAK,UAAU;EAC9B,MAAM,mBAAmB,OAAO,wBAC5B,OAAO,sBAAsB,KAAK,IAClC,CAAC;EAEL,IAAI,kBAA8B,CAAC;EAEnC,IAAI,OAAO,UAAU,UACnB,kBAAkB,GAAGC,oCAAAA,yBAAyB,MAAM;O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