@embrace-io/web-sdk
Version:
1 lines • 7.54 kB
Source Map (JSON)
{"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;CAKoB;CACA;CALlB,UAAgC,CAAC;CACjC;CAEA,YACE,qBACA,wBACA,QACA;EAHgB,KAAA,sBAAA;EACA,KAAA,yBAAA;EAGhB,KAAK,UAAU,MAAM;EAErB,KAAK,QAAQA,mBAAAA,KAAK,sBAAsB,EACtC,WAAW,oBACb,CAAC;EAED,KAAK,UAAUC,mBAAAA,MAAM,UAAU,qBAAqB,sBAAsB;EAC1E,KAAK,SAASC,mBAAAA,QAAQ,SAAS,qBAAqB,sBAAsB;EAC1E,KAAK,UAAUC,wBAAAA,KAAK,UAAU,qBAAqB,sBAAsB;EACzE,KAAK,yBAAyB;CAChC;CAEA;CAGA,IAAc,SAAiB;EAC7B,OAAO,KAAK;CACd;CAEA;CAGA,IAAc,QAAe;EAC3B,OAAO,KAAK;CACd;CAEA;CAGA,IAAc,SAAiB;EAC7B,OAAO,KAAK;CACd;;;;;;;;;CAUA,uBAAiE;EAC/D,MAAM,aAAa,KAAK,KAAK,KAAK,CAA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