diff --git a/plugins/node/instrumentation-amqplib/src/amqplib.ts b/plugins/node/instrumentation-amqplib/src/amqplib.ts index 54fcf43afa..9b27d3d29c 100644 --- a/plugins/node/instrumentation-amqplib/src/amqplib.ts +++ b/plugins/node/instrumentation-amqplib/src/amqplib.ts @@ -78,19 +78,13 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; const supportedVersions = ['>=0.5.5 <1']; -export class AmqplibInstrumentation extends InstrumentationBase { - protected override _config!: AmqplibInstrumentationConfig; - +export class AmqplibInstrumentation extends InstrumentationBase { constructor(config: AmqplibInstrumentationConfig = {}) { - super( - PACKAGE_NAME, - PACKAGE_VERSION, - Object.assign({}, DEFAULT_CONFIG, config) - ); + super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config }); } override setConfig(config: AmqplibInstrumentationConfig = {}) { - this._config = Object.assign({}, DEFAULT_CONFIG, config); + super.setConfig({ ...DEFAULT_CONFIG, ...config }); } protected init() { @@ -394,10 +388,11 @@ export class AmqplibInstrumentation extends InstrumentationBase { if ( !Object.prototype.hasOwnProperty.call(channel, CHANNEL_SPANS_NOT_ENDED) ) { - if (self._config.consumeTimeoutMs) { + const { consumeTimeoutMs } = self.getConfig(); + if (consumeTimeoutMs) { const timer = setInterval(() => { self.checkConsumeTimeoutOnChannel(channel); - }, self._config.consumeTimeoutMs); + }, consumeTimeoutMs); timer.unref(); channel[CHANNEL_CONSUME_TIMEOUT_TIMER] = timer; } @@ -455,9 +450,10 @@ export class AmqplibInstrumentation extends InstrumentationBase { parentContext ); - if (self._config.consumeHook) { + const { consumeHook } = self.getConfig(); + if (consumeHook) { safeExecuteInTheMiddle( - () => self._config.consumeHook!(span, { moduleVersion, msg }), + () => consumeHook(span, { moduleVersion, msg }), e => { if (e) { diag.error('amqplib instrumentation: consumerHook error', e); @@ -516,10 +512,11 @@ export class AmqplibInstrumentation extends InstrumentationBase { options ); - if (self._config.publishHook) { + const { publishHook } = self.getConfig(); + if (publishHook) { safeExecuteInTheMiddle( () => - self._config.publishHook!(span, { + publishHook(span, { moduleVersion, exchange, routingKey, @@ -544,10 +541,11 @@ export class AmqplibInstrumentation extends InstrumentationBase { try { callback?.call(this, err, ok); } finally { - if (self._config.publishConfirmHook) { + const { publishConfirmHook } = self.getConfig(); + if (publishConfirmHook) { safeExecuteInTheMiddle( () => - self._config.publishConfirmHook!(span, { + publishConfirmHook(span, { moduleVersion, exchange, routingKey, @@ -616,10 +614,11 @@ export class AmqplibInstrumentation extends InstrumentationBase { options ); - if (self._config.publishHook) { + const { publishHook } = self.getConfig(); + if (publishHook) { safeExecuteInTheMiddle( () => - self._config.publishHook!(span, { + publishHook(span, { moduleVersion, exchange, routingKey, @@ -731,10 +730,11 @@ export class AmqplibInstrumentation extends InstrumentationBase { rejected: boolean | null, endOperation: EndOperation ) { - if (!this._config.consumeEndHook) return; + const { consumeEndHook } = this.getConfig(); + if (!consumeEndHook) return; safeExecuteInTheMiddle( - () => this._config.consumeEndHook!(span, { msg, rejected, endOperation }), + () => consumeEndHook(span, { msg, rejected, endOperation }), e => { if (e) { diag.error('amqplib instrumentation: consumerEndHook error', e); @@ -748,15 +748,14 @@ export class AmqplibInstrumentation extends InstrumentationBase { const currentTime = hrTime(); const spansNotEnded = channel[CHANNEL_SPANS_NOT_ENDED] ?? []; let i: number; + const { consumeTimeoutMs } = this.getConfig(); for (i = 0; i < spansNotEnded.length; i++) { const currMessage = spansNotEnded[i]; const timeFromConsume = hrTimeDuration( currMessage.timeOfConsume, currentTime ); - if ( - hrTimeToMilliseconds(timeFromConsume) < this._config.consumeTimeoutMs! - ) { + if (hrTimeToMilliseconds(timeFromConsume) < consumeTimeoutMs!) { break; } this.endConsumerSpan( diff --git a/plugins/node/instrumentation-cucumber/src/instrumentation.ts b/plugins/node/instrumentation-cucumber/src/instrumentation.ts index 564f4e9604..0f4bfd5518 100644 --- a/plugins/node/instrumentation-cucumber/src/instrumentation.ts +++ b/plugins/node/instrumentation-cucumber/src/instrumentation.ts @@ -48,7 +48,7 @@ type Step = (typeof steps)[number]; const supportedVersions = ['>=8.0.0 <11']; -export class CucumberInstrumentation extends InstrumentationBase { +export class CucumberInstrumentation extends InstrumentationBase { private module: Cucumber | undefined; constructor(config: CucumberInstrumentationConfig = {}) { diff --git a/plugins/node/instrumentation-dataloader/src/instrumentation.ts b/plugins/node/instrumentation-dataloader/src/instrumentation.ts index 617104ee7d..53bc18a6b2 100644 --- a/plugins/node/instrumentation-dataloader/src/instrumentation.ts +++ b/plugins/node/instrumentation-dataloader/src/instrumentation.ts @@ -43,7 +43,7 @@ type DataloaderInternal = typeof Dataloader.prototype & { type LoadFn = (typeof Dataloader.prototype)['load']; type LoadManyFn = (typeof Dataloader.prototype)['loadMany']; -export class DataloaderInstrumentation extends InstrumentationBase { +export class DataloaderInstrumentation extends InstrumentationBase { constructor(config: DataloaderInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -72,14 +72,6 @@ export class DataloaderInstrumentation extends InstrumentationBase { ]; } - override getConfig(): DataloaderInstrumentationConfig { - return this._config; - } - - override setConfig(config: DataloaderInstrumentationConfig = {}) { - this._config = config; - } - private shouldCreateSpans(): boolean { const config = this.getConfig(); const hasParentSpan = trace.getSpan(context.active()) !== undefined; diff --git a/plugins/node/instrumentation-fs/src/instrumentation.ts b/plugins/node/instrumentation-fs/src/instrumentation.ts index 281e43fc10..cd2f8306dc 100644 --- a/plugins/node/instrumentation-fs/src/instrumentation.ts +++ b/plugins/node/instrumentation-fs/src/instrumentation.ts @@ -51,7 +51,7 @@ function patchedFunctionWithOriginalProperties< return Object.assign(patchedFunction, original); } -export class FsInstrumentation extends InstrumentationBase { +export class FsInstrumentation extends InstrumentationBase { constructor(config: FsInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -438,7 +438,7 @@ export class FsInstrumentation extends InstrumentationBase { protected _runCreateHook( ...args: Parameters ): ReturnType { - const { createHook } = this.getConfig() as FsInstrumentationConfig; + const { createHook } = this.getConfig(); if (typeof createHook === 'function') { try { return createHook(...args); @@ -450,7 +450,7 @@ export class FsInstrumentation extends InstrumentationBase { } protected _runEndHook(...args: Parameters): ReturnType { - const { endHook } = this.getConfig() as FsInstrumentationConfig; + const { endHook } = this.getConfig(); if (typeof endHook === 'function') { try { endHook(...args); @@ -467,7 +467,7 @@ export class FsInstrumentation extends InstrumentationBase { return false; } - const { requireParentSpan } = this.getConfig() as FsInstrumentationConfig; + const { requireParentSpan } = this.getConfig(); if (requireParentSpan) { const parentSpan = api.trace.getSpan(context); if (parentSpan == null) { diff --git a/plugins/node/instrumentation-kafkajs/src/instrumentation.ts b/plugins/node/instrumentation-kafkajs/src/instrumentation.ts index 69a158e815..25bc9aad70 100644 --- a/plugins/node/instrumentation-kafkajs/src/instrumentation.ts +++ b/plugins/node/instrumentation-kafkajs/src/instrumentation.ts @@ -53,9 +53,7 @@ import { isWrapped, } from '@opentelemetry/instrumentation'; -export class KafkaJsInstrumentation extends InstrumentationBase { - protected override _config!: KafkaJsInstrumentationConfig; - +export class KafkaJsInstrumentation extends InstrumentationBase { constructor(config: KafkaJsInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -367,9 +365,10 @@ export class KafkaJsInstrumentation extends InstrumentationBase { context ); - if (this._config?.consumerHook && message) { + const { consumerHook } = this.getConfig(); + if (consumerHook && message) { safeExecuteInTheMiddle( - () => this._config.consumerHook!(span, { topic, message }), + () => consumerHook(span, { topic, message }), e => { if (e) this._diag.error('consumerHook error', e); }, @@ -392,9 +391,10 @@ export class KafkaJsInstrumentation extends InstrumentationBase { message.headers = message.headers ?? {}; propagation.inject(trace.setSpan(context.active(), span), message.headers); - if (this._config?.producerHook) { + const { producerHook } = this.getConfig(); + if (producerHook) { safeExecuteInTheMiddle( - () => this._config.producerHook!(span, { topic, message }), + () => producerHook(span, { topic, message }), e => { if (e) this._diag.error('producerHook error', e); }, diff --git a/plugins/node/instrumentation-mongoose/src/mongoose.ts b/plugins/node/instrumentation-mongoose/src/mongoose.ts index 827b42e7cd..18f6a41c87 100644 --- a/plugins/node/instrumentation-mongoose/src/mongoose.ts +++ b/plugins/node/instrumentation-mongoose/src/mongoose.ts @@ -57,17 +57,11 @@ const contextCaptureFunctions = [ // calls. this bypass the unlinked spans issue on thenables await operations. export const _STORED_PARENT_SPAN: unique symbol = Symbol('stored-parent-span'); -export class MongooseInstrumentation extends InstrumentationBase { - protected override _config!: MongooseInstrumentationConfig; - +export class MongooseInstrumentation extends InstrumentationBase { constructor(config: MongooseInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } - override setConfig(config: MongooseInstrumentationConfig = {}) { - this._config = Object.assign({}, config); - } - protected init(): InstrumentationModuleDefinition { const module = new InstrumentationNodeModuleDefinition( 'mongoose', @@ -140,7 +134,7 @@ export class MongooseInstrumentation extends InstrumentationBase { return (originalAggregate: Function) => { return function exec(this: any, callback?: Function) { if ( - self._config.requireParentSpan && + self.getConfig().requireParentSpan && trace.getSpan(context.active()) === undefined ) { return originalAggregate.apply(this, arguments); @@ -148,12 +142,15 @@ export class MongooseInstrumentation extends InstrumentationBase { const parentSpan = this[_STORED_PARENT_SPAN]; const attributes: Attributes = {}; - if (self._config.dbStatementSerializer) { - attributes[SEMATTRS_DB_STATEMENT] = - self._config.dbStatementSerializer('aggregate', { + const { dbStatementSerializer } = self.getConfig(); + if (dbStatementSerializer) { + attributes[SEMATTRS_DB_STATEMENT] = dbStatementSerializer( + 'aggregate', + { options: this.options, aggregatePipeline: this._pipeline, - }); + } + ); } const span = self._startSpan( @@ -181,7 +178,7 @@ export class MongooseInstrumentation extends InstrumentationBase { return (originalExec: Function) => { return function exec(this: any, callback?: Function) { if ( - self._config.requireParentSpan && + self.getConfig().requireParentSpan && trace.getSpan(context.active()) === undefined ) { return originalExec.apply(this, arguments); @@ -189,14 +186,14 @@ export class MongooseInstrumentation extends InstrumentationBase { const parentSpan = this[_STORED_PARENT_SPAN]; const attributes: Attributes = {}; - if (self._config.dbStatementSerializer) { - attributes[SEMATTRS_DB_STATEMENT] = - self._config.dbStatementSerializer(this.op, { - condition: this._conditions, - updates: this._update, - options: this.options, - fields: this._fields, - }); + const { dbStatementSerializer } = self.getConfig(); + if (dbStatementSerializer) { + attributes[SEMATTRS_DB_STATEMENT] = dbStatementSerializer(this.op, { + condition: this._conditions, + updates: this._update, + options: this.options, + fields: this._fields, + }); } const span = self._startSpan( this.mongooseCollection, @@ -223,7 +220,7 @@ export class MongooseInstrumentation extends InstrumentationBase { return (originalOnModelFunction: Function) => { return function method(this: any, options?: any, callback?: Function) { if ( - self._config.requireParentSpan && + self.getConfig().requireParentSpan && trace.getSpan(context.active()) === undefined ) { return originalOnModelFunction.apply(this, arguments); @@ -234,9 +231,12 @@ export class MongooseInstrumentation extends InstrumentationBase { serializePayload.options = options; } const attributes: Attributes = {}; - if (self._config.dbStatementSerializer) { - attributes[SEMATTRS_DB_STATEMENT] = - self._config.dbStatementSerializer(op, serializePayload); + const { dbStatementSerializer } = self.getConfig(); + if (dbStatementSerializer) { + attributes[SEMATTRS_DB_STATEMENT] = dbStatementSerializer( + op, + serializePayload + ); } const span = self._startSpan( this.constructor.collection, @@ -331,7 +331,7 @@ export class MongooseInstrumentation extends InstrumentationBase { originalThis, span, args, - self._config.responseHook, + self.getConfig().responseHook, moduleVersion ) ); @@ -342,14 +342,14 @@ export class MongooseInstrumentation extends InstrumentationBase { return handlePromiseResponse( response, span, - self._config.responseHook, + self.getConfig().responseHook, moduleVersion ); } } private _callOriginalFunction(originalFunction: (...args: any[]) => T): T { - if (this._config?.suppressInternalInstrumentation) { + if (this.getConfig().suppressInternalInstrumentation) { return context.with(suppressTracing(context.active()), originalFunction); } else { return originalFunction(); diff --git a/plugins/node/instrumentation-runtime-node/src/instrumentation.ts b/plugins/node/instrumentation-runtime-node/src/instrumentation.ts index ba26129590..3e80ae4280 100644 --- a/plugins/node/instrumentation-runtime-node/src/instrumentation.ts +++ b/plugins/node/instrumentation-runtime-node/src/instrumentation.ts @@ -26,7 +26,7 @@ const DEFAULT_CONFIG: RuntimeNodeInstrumentationConfig = { eventLoopUtilizationMeasurementInterval: 5000, }; -export class RuntimeNodeInstrumentation extends InstrumentationBase { +export class RuntimeNodeInstrumentation extends InstrumentationBase { private _ELUs: EventLoopUtilization[] = []; private _interval: NodeJS.Timeout | undefined; @@ -79,8 +79,7 @@ export class RuntimeNodeInstrumentation extends InstrumentationBase { clearInterval(this._interval); this._interval = setInterval( () => this._addELU(), - (this._config as RuntimeNodeInstrumentationConfig) - .eventLoopUtilizationMeasurementInterval + this.getConfig().eventLoopUtilizationMeasurementInterval ); // unref so that it does not keep the process running if disable() is never called diff --git a/plugins/node/instrumentation-socket.io/src/socket.io.ts b/plugins/node/instrumentation-socket.io/src/socket.io.ts index 9f8de7b534..4fd9d3e4a3 100644 --- a/plugins/node/instrumentation-socket.io/src/socket.io.ts +++ b/plugins/node/instrumentation-socket.io/src/socket.io.ts @@ -53,9 +53,7 @@ const reservedEvents = [ 'removeListener', ]; -export class SocketIoInstrumentation extends InstrumentationBase { - protected override _config!: SocketIoInstrumentationConfig; - +export class SocketIoInstrumentation extends InstrumentationBase { constructor(config: SocketIoInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, normalizeConfig(config)); } @@ -286,10 +284,10 @@ export class SocketIoInstrumentation extends InstrumentationBase { const self = this; return (original: Function) => { return function (this: any, ev: any, originalListener: Function) { - if (!self._config.traceReserved && reservedEvents.includes(ev)) { + if (!self.getConfig().traceReserved && reservedEvents.includes(ev)) { return original.apply(this, arguments); } - if (self._config.onIgnoreEventList?.includes(ev)) { + if (self.getConfig().onIgnoreEventList?.includes(ev)) { return original.apply(this, arguments); } const wrappedListener = function (this: any, ...args: any[]) { @@ -315,10 +313,10 @@ export class SocketIoInstrumentation extends InstrumentationBase { } ); - if (self._config.onHook) { + const { onHook } = self.getConfig(); + if (onHook) { safeExecuteInTheMiddle( - () => - self._config?.onHook?.(span, { moduleVersion, payload: args }), + () => onHook(span, { moduleVersion, payload: args }), e => { if (e) self._diag.error('onHook error', e); }, @@ -369,10 +367,10 @@ export class SocketIoInstrumentation extends InstrumentationBase { const self = this; return (original: Function) => { return function (this: any, ev: any, ...args: any[]) { - if (!self._config.traceReserved && reservedEvents.includes(ev)) { + if (!self.getConfig().traceReserved && reservedEvents.includes(ev)) { return original.apply(this, arguments); } - if (self._config?.emitIgnoreEventList?.includes(ev)) { + if (self.getConfig().emitIgnoreEventList?.includes(ev)) { return original.apply(this, arguments); } const messagingSystem = 'socket.io'; @@ -401,10 +399,10 @@ export class SocketIoInstrumentation extends InstrumentationBase { attributes, }); - if (self._config.emitHook) { + const { emitHook } = self.getConfig(); + if (emitHook) { safeExecuteInTheMiddle( - () => - self._config.emitHook?.(span, { moduleVersion, payload: args }), + () => emitHook(span, { moduleVersion, payload: args }), e => { if (e) self._diag.error('emitHook error', e); }, diff --git a/plugins/node/instrumentation-tedious/src/instrumentation.ts b/plugins/node/instrumentation-tedious/src/instrumentation.ts index f701451421..e5fd7e4113 100644 --- a/plugins/node/instrumentation-tedious/src/instrumentation.ts +++ b/plugins/node/instrumentation-tedious/src/instrumentation.ts @@ -67,7 +67,7 @@ function setDatabase(this: ApproxConnection, databaseName: string) { }); } -export class TediousInstrumentation extends InstrumentationBase { +export class TediousInstrumentation extends InstrumentationBase { static readonly COMPONENT = 'tedious'; constructor(config: TediousInstrumentationConfig = {}) { diff --git a/plugins/node/instrumentation-undici/src/undici.ts b/plugins/node/instrumentation-undici/src/undici.ts index e436c8aff4..59f0dfc5ca 100644 --- a/plugins/node/instrumentation-undici/src/undici.ts +++ b/plugins/node/instrumentation-undici/src/undici.ts @@ -59,7 +59,7 @@ interface InstrumentationRecord { // A combination of https://github.com/elastic/apm-agent-nodejs and // https://github.com/gadget-inc/opentelemetry-instrumentations/blob/main/packages/opentelemetry-instrumentation-undici/src/index.ts -export class UndiciInstrumentation extends InstrumentationBase { +export class UndiciInstrumentation extends InstrumentationBase { // Keep ref to avoid https://github.com/nodejs/node/issues/42170 bug and for // unsubscribing. private _channelSubs!: Array; @@ -77,20 +77,22 @@ export class UndiciInstrumentation extends InstrumentationBase { } override disable(): void { - if (!this._config.enabled) { + if (!this.getConfig().enabled) { return; } this._channelSubs.forEach(sub => sub.channel.unsubscribe(sub.onMessage)); this._channelSubs.length = 0; - this._config.enabled = false; + super.disable(); + this.setConfig({ ...this.getConfig(), enabled: false }); } override enable(): void { - if (this._config.enabled) { + if (this.getConfig().enabled) { return; } - this._config.enabled = true; + super.enable(); + this.setConfig({ ...this.getConfig(), enabled: true }); // This method is called by the `InstrumentationAbstract` constructor before // ours is called. So we need to ensure the property is initalized @@ -138,10 +140,6 @@ export class UndiciInstrumentation extends InstrumentationBase { ); } - private _getConfig(): UndiciInstrumentationConfig { - return this._config as UndiciInstrumentationConfig; - } - private subscribeToChannel( diagnosticChannel: string, onMessage: ListenerRecord['onMessage'] @@ -163,7 +161,7 @@ export class UndiciInstrumentation extends InstrumentationBase { // - instrumentation is disabled // - ignored by config // - method is 'CONNECT' - const config = this._getConfig(); + const config = this.getConfig(); const shouldIgnoreReq = safeExecuteInTheMiddle( () => !config.enabled || @@ -297,7 +295,7 @@ export class UndiciInstrumentation extends InstrumentationBase { return; } - const config = this._getConfig(); + const config = this.getConfig(); const { span } = record; const { remoteAddress, remotePort } = socket; const spanAttributes: Attributes = { @@ -355,7 +353,7 @@ export class UndiciInstrumentation extends InstrumentationBase { [SemanticAttributes.HTTP_RESPONSE_STATUS_CODE]: response.statusCode, }; - const config = this._getConfig(); + const config = this.getConfig(); const headersToAttribs = new Set(); if (config.headersToSpanAttributes?.responseHeaders) { diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 0d8eb8a5e2..4aef1509ce 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -73,15 +73,12 @@ const headerGetter: TextMapGetter = { export const traceContextEnvironmentKey = '_X_AMZN_TRACE_ID'; -export class AwsLambdaInstrumentation extends InstrumentationBase { +export class AwsLambdaInstrumentation extends InstrumentationBase { private _traceForceFlusher?: () => Promise; private _metricForceFlusher?: () => Promise; - protected override _config!: AwsLambdaInstrumentationConfig; - constructor(config: AwsLambdaInstrumentationConfig = {}) { - super(PACKAGE_NAME, PACKAGE_VERSION, config); - if (this._config.disableAwsContextPropagation == null) { + if (config.disableAwsContextPropagation == null) { if ( typeof env['OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION'] === 'string' && @@ -89,18 +86,16 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { 'OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION' ].toLocaleLowerCase() === 'true' ) { - this._config.disableAwsContextPropagation = true; + config = { ...config, disableAwsContextPropagation: true }; } } - } - override setConfig(config: AwsLambdaInstrumentationConfig = {}) { - this._config = config; + super(PACKAGE_NAME, PACKAGE_VERSION, config); } init() { const taskRoot = process.env.LAMBDA_TASK_ROOT; - const handlerDef = this._config.lambdaHandler ?? process.env._HANDLER; + const handlerDef = this.getConfig().lambdaHandler ?? process.env._HANDLER; // _HANDLER and LAMBDA_TASK_ROOT are always defined in Lambda but guard bail out if in the future this changes. if (!taskRoot || !handlerDef) { @@ -187,7 +182,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { context: Context, callback: Callback ) { - const config = plugin._config; + const config = plugin.getConfig(); const parent = AwsLambdaInstrumentation._determineParent( event, context, @@ -213,9 +208,10 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { parent ); - if (config.requestHook) { + const { requestHook } = config; + if (requestHook) { safeExecuteInTheMiddle( - () => config.requestHook!(span, { event, context }), + () => requestHook(span, { event, context }), e => { if (e) diag.error('aws-lambda instrumentation: requestHook error', e); @@ -362,9 +358,10 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { err?: Error | string | null, res?: any ) { - if (this._config?.responseHook) { + const { responseHook } = this.getConfig(); + if (responseHook) { safeExecuteInTheMiddle( - () => this._config.responseHook!(span, { err, res }), + () => responseHook(span, { err, res }), e => { if (e) diag.error('aws-lambda instrumentation: responseHook error', e); diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts index 1427ae0386..c8180d3def 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts @@ -72,19 +72,14 @@ type V2PluginRequest = AWS.Request & { [REQUEST_SPAN_KEY]?: Span; }; -export class AwsInstrumentation extends InstrumentationBase { +export class AwsInstrumentation extends InstrumentationBase { static readonly component = 'aws-sdk'; - protected override _config!: AwsSdkInstrumentationConfig; private servicesExtensions: ServicesExtensions = new ServicesExtensions(); constructor(config: AwsSdkInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } - override setConfig(config: AwsSdkInstrumentationConfig = {}) { - this._config = Object.assign({}, config); - } - protected init(): InstrumentationModuleDefinition[] { const v3MiddlewareStackFileOldVersions = new InstrumentationNodeModuleFile( '@aws-sdk/middleware-stack/dist/cjs/MiddlewareStack.js', @@ -274,13 +269,14 @@ export class AwsInstrumentation extends InstrumentationBase { request: NormalizedRequest, moduleVersion: string | undefined ) { - if (this._config?.preRequestHook) { + const { preRequestHook } = this.getConfig(); + if (preRequestHook) { const requestInfo: AwsSdkRequestHookInformation = { moduleVersion, request, }; safeExecuteInTheMiddle( - () => this._config.preRequestHook!(span, requestInfo), + () => preRequestHook(span, requestInfo), (e: Error | undefined) => { if (e) diag.error( @@ -294,7 +290,7 @@ export class AwsInstrumentation extends InstrumentationBase { } private _callUserResponseHook(span: Span, response: NormalizedResponse) { - const responseHook = this._config?.responseHook; + const { responseHook } = this.getConfig(); if (!responseHook) return; const responseInfo: AwsSdkResponseHookInformation = { @@ -343,7 +339,7 @@ export class AwsInstrumentation extends InstrumentationBase { normalizedResponse, span, self.tracer, - self._config + self.getConfig() ); } @@ -462,7 +458,7 @@ export class AwsInstrumentation extends InstrumentationBase { ); const requestMetadata = self.servicesExtensions.requestPreSpanHook( normalizedRequest, - self._config, + self.getConfig(), self._diag ); const span = self._startAwsV3Span(normalizedRequest, requestMetadata); @@ -528,7 +524,7 @@ export class AwsInstrumentation extends InstrumentationBase { normalizedResponse, span, self.tracer, - self._config + self.getConfig() ); self._callUserResponseHook(span, normalizedResponse); return response; @@ -601,7 +597,7 @@ export class AwsInstrumentation extends InstrumentationBase { const normalizedRequest = normalizeV2Request(this); const requestMetadata = self.servicesExtensions.requestPreSpanHook( normalizedRequest, - self._config, + self.getConfig(), self._diag ); const span = self._startAwsV2Span( @@ -644,7 +640,7 @@ export class AwsInstrumentation extends InstrumentationBase { const normalizedRequest = normalizeV2Request(this); const requestMetadata = self.servicesExtensions.requestPreSpanHook( normalizedRequest, - self._config, + self.getConfig(), self._diag ); const span = self._startAwsV2Span( @@ -680,7 +676,7 @@ export class AwsInstrumentation extends InstrumentationBase { } private _callOriginalFunction(originalFunction: (...args: any[]) => T): T { - if (this._config?.suppressInternalInstrumentation) { + if (this.getConfig().suppressInternalInstrumentation) { return context.with(suppressTracing(context.active()), originalFunction); } else { return originalFunction(); diff --git a/plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts index e70fab1a00..ed32f31142 100644 --- a/plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts @@ -32,13 +32,9 @@ const DEFAULT_CONFIG: BunyanInstrumentationConfig = { disableLogCorrelation: false, }; -export class BunyanInstrumentation extends InstrumentationBase { +export class BunyanInstrumentation extends InstrumentationBase { constructor(config: BunyanInstrumentationConfig = {}) { - super( - PACKAGE_NAME, - PACKAGE_VERSION, - Object.assign({}, DEFAULT_CONFIG, config) - ); + super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config }); } protected init() { @@ -99,12 +95,8 @@ export class BunyanInstrumentation extends InstrumentationBase { ]; } - override getConfig(): BunyanInstrumentationConfig { - return this._config; - } - override setConfig(config: BunyanInstrumentationConfig = {}) { - this._config = Object.assign({}, DEFAULT_CONFIG, config); + this._config = { ...DEFAULT_CONFIG, ...config }; } private _getPatchedEmit() { @@ -150,7 +142,7 @@ export class BunyanInstrumentation extends InstrumentationBase { } private _addStream(logger: any) { - const config: BunyanInstrumentationConfig = this.getConfig(); + const config = this.getConfig(); if (!this.isEnabled() || config.disableLogSending) { return; } @@ -168,14 +160,14 @@ export class BunyanInstrumentation extends InstrumentationBase { } private _callHook(span: Span, record: Record) { - const hook = this.getConfig().logHook; + const { logHook } = this.getConfig(); - if (typeof hook !== 'function') { + if (typeof logHook !== 'function') { return; } safeExecuteInTheMiddle( - () => hook(span, record), + () => logHook(span, record), err => { if (err) { this._diag.error('error calling logHook', err); diff --git a/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts index 46949a7380..d73e3b0253 100644 --- a/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts @@ -45,9 +45,7 @@ import type * as CassandraDriver from 'cassandra-driver'; const supportedVersions = ['>=4.4.0 <5']; -export class CassandraDriverInstrumentation extends InstrumentationBase { - protected override _config!: CassandraDriverInstrumentationConfig; - +export class CassandraDriverInstrumentation extends InstrumentationBase { constructor(config: CassandraDriverInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -120,13 +118,11 @@ export class CassandraDriverInstrumentation extends InstrumentationBase { } private _getMaxQueryLength(): number { - const config = this.getConfig() as CassandraDriverInstrumentationConfig; - return config.maxQueryLength ?? 65536; + return this.getConfig().maxQueryLength ?? 65536; } private _shouldIncludeDbStatement(): boolean { - const config = this.getConfig() as CassandraDriverInstrumentationConfig; - return config.enhancedDatabaseReporting ?? false; + return this.getConfig().enhancedDatabaseReporting ?? false; } private _getPatchedExecute() { @@ -334,12 +330,13 @@ export class CassandraDriverInstrumentation extends InstrumentationBase { } private _callResponseHook(span: Span, response: ResultSet) { - if (!this._config.responseHook) { + const { responseHook } = this.getConfig(); + if (!responseHook) { return; } safeExecuteInTheMiddle( - () => this._config.responseHook!(span, { response: response }), + () => responseHook(span, { response: response }), e => { if (e) { this._diag.error('responseHook error', e); diff --git a/plugins/node/opentelemetry-instrumentation-dns/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-dns/src/instrumentation.ts index ae97438ddb..a99051a502 100644 --- a/plugins/node/opentelemetry-instrumentation-dns/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-dns/src/instrumentation.ts @@ -36,9 +36,7 @@ import { /** * Dns instrumentation for Opentelemetry */ -export class DnsInstrumentation extends InstrumentationBase { - protected override _config!: DnsInstrumentationConfig; - +export class DnsInstrumentation extends InstrumentationBase { constructor(config: DnsInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -112,8 +110,10 @@ export class DnsInstrumentation extends InstrumentationBase { ...args: unknown[] ) { if ( - utils.isIgnored(hostname, plugin._config.ignoreHostnames, (e: Error) => - diag.error('caught ignoreHostname error: ', e) + utils.isIgnored( + hostname, + plugin.getConfig().ignoreHostnames, + (e: Error) => diag.error('caught ignoreHostname error: ', e) ) ) { return original.apply(this, [hostname, ...args]); diff --git a/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts index 7aeb400ab2..bcff7a52e2 100644 --- a/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts @@ -50,19 +50,11 @@ import { } from './internal-types'; /** Express instrumentation for OpenTelemetry */ -export class ExpressInstrumentation extends InstrumentationBase { +export class ExpressInstrumentation extends InstrumentationBase { constructor(config: ExpressInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } - override setConfig(config: ExpressInstrumentationConfig = {}) { - this._config = Object.assign({}, config); - } - - override getConfig(): ExpressInstrumentationConfig { - return this._config as ExpressInstrumentationConfig; - } - init() { return [ new InstrumentationNodeModuleDefinition( @@ -199,7 +191,7 @@ export class ExpressInstrumentation extends InstrumentationBase { } // verify against the config if the layer should be ignored - if (isLayerIgnored(metadata.name, type, instrumentation._config)) { + if (isLayerIgnored(metadata.name, type, instrumentation.getConfig())) { if (type === ExpressLayerType.MIDDLEWARE) { (req[_LAYERS_STORE_PROPERTY] as string[]).pop(); } @@ -222,10 +214,11 @@ export class ExpressInstrumentation extends InstrumentationBase { attributes: Object.assign(attributes, metadata.attributes), }); - if (instrumentation.getConfig().requestHook) { + const { requestHook } = instrumentation.getConfig(); + if (requestHook) { safeExecuteInTheMiddle( () => - instrumentation.getConfig().requestHook!(span, { + requestHook(span, { request: req, layerType: type, route, @@ -334,14 +327,14 @@ export class ExpressInstrumentation extends InstrumentationBase { } _getSpanName(info: ExpressRequestInfo, defaultName: string) { - const hook = this.getConfig().spanNameHook; + const { spanNameHook } = this.getConfig(); - if (!(hook instanceof Function)) { + if (!(spanNameHook instanceof Function)) { return defaultName; } try { - return hook(info, defaultName) ?? defaultName; + return spanNameHook(info, defaultName) ?? defaultName; } catch (err) { diag.error( 'express instrumentation: error calling span name rewrite hook', diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index 641bfc86e4..e7014c5365 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -46,19 +46,11 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; export const ANONYMOUS_NAME = 'anonymous'; /** Fastify instrumentation for OpenTelemetry */ -export class FastifyInstrumentation extends InstrumentationBase { +export class FastifyInstrumentation extends InstrumentationBase { constructor(config: FastifyInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } - override setConfig(config: FastifyInstrumentationConfig = {}) { - this._config = Object.assign({}, config); - } - - override getConfig(): FastifyInstrumentationConfig { - return this._config as FastifyInstrumentationConfig; - } - init() { return [ new InstrumentationNodeModuleDefinition( @@ -281,9 +273,10 @@ export class FastifyInstrumentation extends InstrumentationBase { spanAttributes ); - if (instrumentation.getConfig().requestHook) { + const { requestHook } = instrumentation.getConfig(); + if (requestHook) { safeExecuteInTheMiddle( - () => instrumentation.getConfig().requestHook!(span, { request }), + () => requestHook(span, { request }), e => { if (e) { instrumentation._diag.error('request hook failed', e); diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts index 88cb3afec9..1fc1bdf429 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts @@ -33,7 +33,6 @@ import { executeType, parseType, validateType, - GraphQLInstrumentationParsedConfig, OtelExecutionArgs, ObjectWithGraphQLData, OPERATION_NOT_SUPPORTED, @@ -52,9 +51,12 @@ import { import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; import * as api from '@opentelemetry/api'; import type { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue'; -import { GraphQLInstrumentationConfig } from './types'; +import { + GraphQLInstrumentationConfig, + GraphQLInstrumentationParsedConfig, +} from './types'; -const DEFAULT_CONFIG: GraphQLInstrumentationConfig = { +const DEFAULT_CONFIG: GraphQLInstrumentationParsedConfig = { mergeItems: false, depth: -1, allowValues: false, @@ -63,21 +65,13 @@ const DEFAULT_CONFIG: GraphQLInstrumentationConfig = { const supportedVersions = ['>=14.0.0 <17']; -export class GraphQLInstrumentation extends InstrumentationBase { +export class GraphQLInstrumentation extends InstrumentationBase { constructor(config: GraphQLInstrumentationConfig = {}) { - super( - PACKAGE_NAME, - PACKAGE_VERSION, - Object.assign({}, DEFAULT_CONFIG, config) - ); - } - - private _getConfig(): GraphQLInstrumentationParsedConfig { - return this._config as GraphQLInstrumentationParsedConfig; + super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config }); } override setConfig(config: GraphQLInstrumentationConfig = {}) { - this._config = Object.assign({}, DEFAULT_CONFIG, config); + super.setConfig({ ...DEFAULT_CONFIG, ...config }); } protected init() { @@ -238,7 +232,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { err?: Error, result?: PromiseOrValue ) { - const config = this._getConfig(); + const config = this.getConfig(); if (result === undefined || err) { endSpan(span, err); return; @@ -270,10 +264,14 @@ export class GraphQLInstrumentation extends InstrumentationBase { span: api.Span, result: graphqlTypes.ExecutionResult ) { - const config = this._getConfig(); + const { responseHook } = this.getConfig(); + if (!responseHook) { + return; + } + safeExecuteInTheMiddle( () => { - config.responseHook(span, result); + responseHook(span, result); }, err => { if (err) { @@ -329,7 +327,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { source: string | graphqlTypes.Source, options?: graphqlTypes.ParseOptions ): graphqlTypes.DocumentNode { - const config = this._getConfig(); + const config = this.getConfig(); const span = this.tracer.startSpan(SpanNames.PARSE); return context.with(trace.setSpan(context.active(), span), () => { @@ -397,7 +395,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { operation: graphqlTypes.DefinitionNode | undefined, processedArgs: graphqlTypes.ExecutionArgs ): api.Span { - const config = this._getConfig(); + const config = this.getConfig(); const span = this.tracer.startSpan(SpanNames.EXECUTE, {}); if (operation) { @@ -457,7 +455,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { if ( contextValue[OTEL_GRAPHQL_DATA_SYMBOL] || - this._getConfig().ignoreResolveSpans + this.getConfig().ignoreResolveSpans ) { return { schema, @@ -477,22 +475,14 @@ export class GraphQLInstrumentation extends InstrumentationBase { const fieldResolverForExecute = fieldResolver ?? defaultFieldResolved; fieldResolver = wrapFieldResolver( this.tracer, - this._getConfig.bind(this), + () => this.getConfig(), fieldResolverForExecute, isUsingDefaultResolver ); if (schema) { - wrapFields( - schema.getQueryType(), - this.tracer, - this._getConfig.bind(this) - ); - wrapFields( - schema.getMutationType(), - this.tracer, - this._getConfig.bind(this) - ); + wrapFields(schema.getQueryType(), this.tracer, () => this.getConfig()); + wrapFields(schema.getMutationType(), this.tracer, () => this.getConfig()); } return { diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/internal-types.ts index 3242ea94e4..59cd1b5b31 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/internal-types.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { InstrumentationConfig } from '@opentelemetry/instrumentation'; import type * as graphqlTypes from 'graphql'; import type * as api from '@opentelemetry/api'; import type { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue'; @@ -24,17 +23,10 @@ import type { GraphQLTypeResolver, } from 'graphql/type/definition'; import { OTEL_GRAPHQL_DATA_SYMBOL, OTEL_PATCHED_SYMBOL } from './symbols'; -import { GraphQLInstrumentationConfig } from './types'; export const OPERATION_NOT_SUPPORTED = 'Operation$operationName$not' + ' supported'; -/** - * Merged and parsed config of default instrumentation config and GraphQL - */ -export type GraphQLInstrumentationParsedConfig = - Required & InstrumentationConfig; - export type executeFunctionWithObj = ( args: graphqlTypes.ExecutionArgs ) => PromiseOrValue; diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/types.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/types.ts index 7cedff14a8..0ec82003aa 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/types.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/types.ts @@ -80,3 +80,12 @@ export interface GraphQLInstrumentationConfig extends InstrumentationConfig { */ responseHook?: GraphQLInstrumentationExecutionResponseHook; } + +// Utility type to make specific properties required +type RequireSpecificKeys = T & { [P in K]-?: T[P] }; + +// Merged and parsed config of default instrumentation config and GraphQL +export type GraphQLInstrumentationParsedConfig = RequireSpecificKeys< + GraphQLInstrumentationConfig, + 'mergeItems' | 'depth' | 'allowValues' | 'ignoreResolveSpans' +>; diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts index de7a555975..9bb98ed975 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts @@ -22,12 +22,11 @@ import { OTEL_GRAPHQL_DATA_SYMBOL, OTEL_PATCHED_SYMBOL } from './symbols'; import { GraphQLField, GraphQLPath, - GraphQLInstrumentationParsedConfig, ObjectWithGraphQLData, OtelPatched, Maybe, } from './internal-types'; -import { GraphQLInstrumentationConfig } from './types'; +import { GraphQLInstrumentationParsedConfig } from './types'; const OPERATION_VALUES = Object.values(AllowedOperationTypes); @@ -367,12 +366,17 @@ const handleResolveSpanSuccess = ( export function wrapFieldResolver( tracer: api.Tracer, - getConfig: () => Required, + getConfig: () => GraphQLInstrumentationParsedConfig, fieldResolver: Maybe< graphqlTypes.GraphQLFieldResolver & OtelPatched >, isDefaultResolver = false -): graphqlTypes.GraphQLFieldResolver & OtelPatched { +): graphqlTypes.GraphQLFieldResolver< + TSource, + TContext & ObjectWithGraphQLData, + TArgs +> & + OtelPatched { if ( (wrappedFieldResolver as OtelPatched)[OTEL_PATCHED_SYMBOL] || typeof fieldResolver !== 'function' diff --git a/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts index 346eed7165..232d5d9952 100644 --- a/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts @@ -39,15 +39,13 @@ const DEFAULT_CONFIG: IORedisInstrumentationConfig = { requireParentSpan: true, }; -export class IORedisInstrumentation extends InstrumentationBase { - protected override _config!: IORedisInstrumentationConfig; - +export class IORedisInstrumentation extends InstrumentationBase { constructor(config: IORedisInstrumentationConfig = {}) { - super( - PACKAGE_NAME, - PACKAGE_VERSION, - Object.assign({}, DEFAULT_CONFIG, config) - ); + super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config }); + } + + override setConfig(config: IORedisInstrumentationConfig = {}) { + super.setConfig({ ...DEFAULT_CONFIG, ...config }); } init(): InstrumentationNodeModuleDefinition[] { @@ -112,13 +110,12 @@ export class IORedisInstrumentation extends InstrumentationBase { if (arguments.length < 1 || typeof cmd !== 'object') { return original.apply(this, arguments); } - const config = - instrumentation.getConfig() as IORedisInstrumentationConfig; + const config = instrumentation.getConfig(); const dbStatementSerializer = - config?.dbStatementSerializer || defaultDbStatementSerializer; + config.dbStatementSerializer || defaultDbStatementSerializer; const hasNoParentSpan = trace.getSpan(context.active()) === undefined; - if (config?.requireParentSpan === true && hasNoParentSpan) { + if (config.requireParentSpan === true && hasNoParentSpan) { return original.apply(this, arguments); } @@ -130,10 +127,11 @@ export class IORedisInstrumentation extends InstrumentationBase { }, }); - if (config?.requestHook) { + const { requestHook } = config; + if (requestHook) { safeExecuteInTheMiddle( () => - config?.requestHook!(span, { + requestHook(span, { moduleVersion, cmdName: cmd.name, cmdArgs: cmd.args, @@ -162,7 +160,7 @@ export class IORedisInstrumentation extends InstrumentationBase { /* eslint-disable @typescript-eslint/no-explicit-any */ cmd.resolve = function (result: any) { safeExecuteInTheMiddle( - () => config?.responseHook?.(span, cmd.name, cmd.args, result), + () => config.responseHook?.(span, cmd.name, cmd.args, result), e => { if (e) { diag.error('ioredis instrumentation: response hook failed', e); @@ -192,10 +190,11 @@ export class IORedisInstrumentation extends InstrumentationBase { private _traceConnection(original: Function) { const instrumentation = this; return function (this: RedisInterface) { - const config = - instrumentation.getConfig() as IORedisInstrumentationConfig; const hasNoParentSpan = trace.getSpan(context.active()) === undefined; - if (config?.requireParentSpan === true && hasNoParentSpan) { + if ( + instrumentation.getConfig().requireParentSpan === true && + hasNoParentSpan + ) { return original.apply(this, arguments); } diff --git a/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts index c80ebbe55c..5dbe4d3035 100644 --- a/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts @@ -35,20 +35,20 @@ import { SEMATTRS_NET_TRANSPORT, } from '@opentelemetry/semantic-conventions'; import * as utils from './utils'; -import * as types from './types'; +import { KnexInstrumentationConfig } from './types'; const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context'); -const DEFAULT_CONFIG: types.KnexInstrumentationConfig = { +const DEFAULT_CONFIG: KnexInstrumentationConfig = { maxQueryLength: 1022, }; -export class KnexInstrumentation extends InstrumentationBase { - constructor(config: types.KnexInstrumentationConfig = {}) { - super( - PACKAGE_NAME, - PACKAGE_VERSION, - Object.assign({}, DEFAULT_CONFIG, config) - ); +export class KnexInstrumentation extends InstrumentationBase { + constructor(config: KnexInstrumentationConfig = {}) { + super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config }); + } + + override setConfig(config: KnexInstrumentationConfig = {}) { + super.setConfig({ ...DEFAULT_CONFIG, ...config }); } init() { @@ -130,9 +130,7 @@ export class KnexInstrumentation extends InstrumentationBase { const operation = query?.method; const name = config?.connection?.filename || config?.connection?.database; - const maxLen = ( - instrumentation._config as types.KnexInstrumentationConfig - ).maxQueryLength!; + const { maxQueryLength } = instrumentation.getConfig(); const attributes: api.SpanAttributes = { 'knex.version': moduleVersion, @@ -146,10 +144,11 @@ export class KnexInstrumentation extends InstrumentationBase { [SEMATTRS_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined, }; - if (maxLen !== 0) { + if (maxQueryLength) { + // filters both undefined and 0 attributes[SEMATTRS_DB_STATEMENT] = utils.limitLength( query?.sql, - maxLen + maxQueryLength ); } diff --git a/plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts index 538916b922..6dd6c86021 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts @@ -35,19 +35,11 @@ import { } from './internal-types'; /** Koa instrumentation for OpenTelemetry */ -export class KoaInstrumentation extends InstrumentationBase { +export class KoaInstrumentation extends InstrumentationBase { constructor(config: KoaInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } - override setConfig(config: KoaInstrumentationConfig = {}) { - this._config = Object.assign({}, config); - } - - override getConfig(): KoaInstrumentationConfig { - return this._config as KoaInstrumentationConfig; - } - protected init() { return new InstrumentationNodeModuleDefinition( 'koa', @@ -179,10 +171,11 @@ export class KoaInstrumentation extends InstrumentationBase { rpcMetadata.route = context._matchedRoute.toString(); } - if (this.getConfig().requestHook) { + const { requestHook } = this.getConfig(); + if (requestHook) { safeExecuteInTheMiddle( () => - this.getConfig().requestHook!(span, { + requestHook(span, { context, middlewareLayer, layerType, diff --git a/plugins/node/opentelemetry-instrumentation-memcached/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-memcached/src/instrumentation.ts index 53676b7468..bb54a99626 100644 --- a/plugins/node/opentelemetry-instrumentation-memcached/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-memcached/src/instrumentation.ts @@ -31,7 +31,7 @@ import * as utils from './utils'; import { InstrumentationConfig } from './types'; import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; -export class MemcachedInstrumentation extends InstrumentationBase { +export class MemcachedInstrumentation extends InstrumentationBase { static readonly COMPONENT = 'memcached'; static readonly COMMON_ATTRIBUTES = { [SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MEMCACHED, @@ -41,19 +41,14 @@ export class MemcachedInstrumentation extends InstrumentationBase { }; constructor(config: InstrumentationConfig = {}) { - super( - PACKAGE_NAME, - PACKAGE_VERSION, - Object.assign({}, MemcachedInstrumentation.DEFAULT_CONFIG, config) - ); + super(PACKAGE_NAME, PACKAGE_VERSION, { + ...MemcachedInstrumentation.DEFAULT_CONFIG, + ...config, + }); } override setConfig(config: InstrumentationConfig = {}) { - this._config = Object.assign( - {}, - MemcachedInstrumentation.DEFAULT_CONFIG, - config - ); + super.setConfig({ ...MemcachedInstrumentation.DEFAULT_CONFIG, ...config }); } init() { @@ -142,9 +137,8 @@ export class MemcachedInstrumentation extends InstrumentationBase { 'db.memcached.key': query.key, 'db.memcached.lifetime': query.lifetime, [SEMATTRS_DB_OPERATION]: query.type, - [SEMATTRS_DB_STATEMENT]: ( - instrumentation._config as InstrumentationConfig - ).enhancedDatabaseReporting + [SEMATTRS_DB_STATEMENT]: instrumentation.getConfig() + .enhancedDatabaseReporting ? query.command : undefined, ...utils.getPeerAttributes(client, server, query), diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index 3408f77bb9..b257b24621 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -55,12 +55,10 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; import { UpDownCounter } from '@opentelemetry/api'; /** mongodb instrumentation plugin for OpenTelemetry */ -export class MongoDBInstrumentation extends InstrumentationBase { +export class MongoDBInstrumentation extends InstrumentationBase { private _connectionsUsage!: UpDownCounter; private _poolName!: string; - protected override _config!: MongoDBInstrumentationConfig; - constructor(config: MongoDBInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -903,9 +901,12 @@ export class MongoDBInstrumentation extends InstrumentationBase { } } if (!commandObj) return; + + const { dbStatementSerializer: configDbStatementSerializer } = + this.getConfig(); const dbStatementSerializer = - typeof this._config.dbStatementSerializer === 'function' - ? this._config.dbStatementSerializer + typeof configDbStatementSerializer === 'function' + ? configDbStatementSerializer : this._defaultDbStatementSerializer.bind(this); safeExecuteInTheMiddle( @@ -923,8 +924,8 @@ export class MongoDBInstrumentation extends InstrumentationBase { } private _defaultDbStatementSerializer(commandObj: Record) { - const enhancedDbReporting = !!this._config?.enhancedDatabaseReporting; - const resultObj = enhancedDbReporting + const { enhancedDatabaseReporting } = this.getConfig(); + const resultObj = enhancedDatabaseReporting ? commandObj : this._scrubStatement(commandObj); return JSON.stringify(resultObj); @@ -954,11 +955,11 @@ export class MongoDBInstrumentation extends InstrumentationBase { * @param result The command result */ private _handleExecutionResult(span: Span, result: CommandResult) { - const config: MongoDBInstrumentationConfig = this.getConfig(); - if (typeof config.responseHook === 'function') { + const { responseHook } = this.getConfig(); + if (typeof responseHook === 'function') { safeExecuteInTheMiddle( () => { - config.responseHook!(span, { data: result }); + responseHook(span, { data: result }); }, err => { if (err) { diff --git a/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts index 5ce35c594f..bb7139b57e 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts @@ -50,7 +50,7 @@ type getConnectionCallbackType = ( connection: mysqlTypes.PoolConnection ) => void; -export class MySQLInstrumentation extends InstrumentationBase { +export class MySQLInstrumentation extends InstrumentationBase { static readonly COMMON_ATTRIBUTES = { [SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL, }; @@ -317,10 +317,7 @@ export class MySQLInstrumentation extends InstrumentationBase { span.setAttribute(SEMATTRS_DB_STATEMENT, getDbStatement(query)); - const instrumentationConfig: MySQLInstrumentationConfig = - thisPlugin.getConfig(); - - if (instrumentationConfig.enhancedDatabaseReporting) { + if (thisPlugin.getConfig().enhancedDatabaseReporting) { let values; if (Array.isArray(_valuesOrCallback)) { diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts index ee8ebfc6f5..0c60c0f452 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts @@ -39,7 +39,7 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; type formatType = typeof mysqlTypes.format; -export class MySQL2Instrumentation extends InstrumentationBase { +export class MySQL2Instrumentation extends InstrumentationBase { static readonly COMMON_ATTRIBUTES = { [SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL, }; @@ -96,9 +96,6 @@ export class MySQL2Instrumentation extends InstrumentationBase { _valuesOrCallback?: unknown[] | Function, _callback?: Function ) { - const thisPluginConfig: MySQL2InstrumentationConfig = - thisPlugin._config; - let values; if (Array.isArray(_valuesOrCallback)) { values = _valuesOrCallback; @@ -115,7 +112,10 @@ export class MySQL2Instrumentation extends InstrumentationBase { }, }); - if (!isPrepared && thisPluginConfig.addSqlCommenterCommentToQueries) { + if ( + !isPrepared && + thisPlugin.getConfig().addSqlCommenterCommentToQueries + ) { arguments[0] = query = typeof query === 'string' ? addSqlCommenterComment(span, query) @@ -131,10 +131,11 @@ export class MySQL2Instrumentation extends InstrumentationBase { message: err.message, }); } else { - if (typeof thisPluginConfig.responseHook === 'function') { + const { responseHook } = thisPlugin.getConfig(); + if (typeof responseHook === 'function') { safeExecuteInTheMiddle( () => { - thisPluginConfig.responseHook!(span, { + responseHook(span, { queryResults: results, }); }, diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index 5d472a94b7..2df376d29f 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -42,7 +42,7 @@ import { addSqlCommenterComment } from '@opentelemetry/sql-common'; import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; import { SpanNames } from './enums/SpanNames'; -export class PgInstrumentation extends InstrumentationBase { +export class PgInstrumentation extends InstrumentationBase { constructor(config: PgInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -113,14 +113,6 @@ export class PgInstrumentation extends InstrumentationBase { return [modulePG, modulePGPool]; } - override setConfig(config: PgInstrumentationConfig = {}) { - this._config = Object.assign({}, config); - } - - override getConfig(): PgInstrumentationConfig { - return this._config as PgInstrumentationConfig; - } - private _getClientConnectPatch() { const plugin = this; return (original: PgClientConnect) => { @@ -244,10 +236,8 @@ export class PgInstrumentation extends InstrumentationBase { } } - if ( - typeof instrumentationConfig.requestHook === 'function' && - queryConfig - ) { + const { requestHook } = instrumentationConfig; + if (typeof requestHook === 'function' && queryConfig) { safeExecuteInTheMiddle( () => { // pick keys to expose explicitly, so we're not leaking pg package @@ -255,7 +245,7 @@ export class PgInstrumentation extends InstrumentationBase { const { database, host, port, user } = this.connectionParameters; const connection = { database, host, port, user }; - instrumentationConfig.requestHook!(span, { + requestHook(span, { connection, query: { text: queryConfig.text, diff --git a/plugins/node/opentelemetry-instrumentation-pino/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pino/src/instrumentation.ts index 7a6edf907d..8a33b69df3 100644 --- a/plugins/node/opentelemetry-instrumentation-pino/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pino/src/instrumentation.ts @@ -38,7 +38,7 @@ const DEFAULT_LOG_KEYS = { traceFlags: 'trace_flags', }; -export class PinoInstrumentation extends InstrumentationBase { +export class PinoInstrumentation extends InstrumentationBase { constructor(config: PinoInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -138,23 +138,15 @@ export class PinoInstrumentation extends InstrumentationBase { ]; } - override getConfig(): PinoInstrumentationConfig { - return this._config; - } - - override setConfig(config: PinoInstrumentationConfig = {}) { - this._config = config; - } - private _callHook(span: Span, record: Record, level: number) { - const hook = this.getConfig().logHook; + const { logHook } = this.getConfig(); - if (!hook) { + if (!logHook) { return; } safeExecuteInTheMiddle( - () => hook(span, record, level), + () => logHook(span, record, level), err => { if (err) { diag.error('pino instrumentation: error calling logHook', err); diff --git a/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts index d62eceaa2c..d1459da8ff 100644 --- a/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-redis-4/src/instrumentation.ts @@ -51,17 +51,15 @@ const DEFAULT_CONFIG: RedisInstrumentationConfig = { requireParentSpan: false, }; -export class RedisInstrumentation extends InstrumentationBase { +export class RedisInstrumentation extends InstrumentationBase { static readonly COMPONENT = 'redis'; - protected override _config!: RedisInstrumentationConfig; - constructor(config: RedisInstrumentationConfig = {}) { - super(PACKAGE_NAME, PACKAGE_VERSION, config); + super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config }); } override setConfig(config: RedisInstrumentationConfig = {}) { - this._config = Object.assign({}, DEFAULT_CONFIG, config); + super.setConfig({ ...DEFAULT_CONFIG, ...config }); } protected init() { @@ -376,7 +374,7 @@ export class RedisInstrumentation extends InstrumentationBase { redisCommandArguments: Array ) { const hasNoParentSpan = trace.getSpan(context.active()) === undefined; - if (hasNoParentSpan && this._config?.requireParentSpan) { + if (hasNoParentSpan && this.getConfig().requireParentSpan) { return origFunction.apply(origThis, origArguments); } @@ -386,7 +384,7 @@ export class RedisInstrumentation extends InstrumentationBase { const commandArgs = redisCommandArguments.slice(1); const dbStatementSerializer = - this._config?.dbStatementSerializer || defaultDbStatementSerializer; + this.getConfig().dbStatementSerializer || defaultDbStatementSerializer; const attributes = getClientAttributes(this._diag, clientOptions); @@ -474,9 +472,10 @@ export class RedisInstrumentation extends InstrumentationBase { response: unknown, error: Error | undefined ) { - if (!error && this._config.responseHook) { + const { responseHook } = this.getConfig(); + if (!error && responseHook) { try { - this._config.responseHook(span, commandName, commandArgs, response); + responseHook(span, commandName, commandArgs, response); } catch (err) { this._diag.error('responseHook throw an exception', err); } diff --git a/plugins/node/opentelemetry-instrumentation-redis/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-redis/src/instrumentation.ts index 93928cc29f..1911af1cdd 100644 --- a/plugins/node/opentelemetry-instrumentation-redis/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-redis/src/instrumentation.ts @@ -31,17 +31,15 @@ const DEFAULT_CONFIG: RedisInstrumentationConfig = { requireParentSpan: false, }; -export class RedisInstrumentation extends InstrumentationBase { +export class RedisInstrumentation extends InstrumentationBase { static readonly COMPONENT = 'redis'; - protected override _config!: RedisInstrumentationConfig; - constructor(config: RedisInstrumentationConfig = {}) { - super(PACKAGE_NAME, PACKAGE_VERSION, config); + super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config }); } override setConfig(config: RedisInstrumentationConfig = {}) { - this._config = Object.assign({}, DEFAULT_CONFIG, config); + super.setConfig({ ...DEFAULT_CONFIG, ...config }); } protected init() { @@ -102,7 +100,7 @@ export class RedisInstrumentation extends InstrumentationBase { */ private _getPatchInternalSendCommand() { const tracer = this.tracer; - const config = this._config; + const config = this.getConfig(); return function internal_send_command(original: Function) { return getTracedInternalSendCommand(tracer, original, config); }; diff --git a/plugins/node/opentelemetry-instrumentation-restify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-restify/src/instrumentation.ts index ea682f74de..372b90d3ae 100644 --- a/plugins/node/opentelemetry-instrumentation-restify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-restify/src/instrumentation.ts @@ -37,7 +37,7 @@ import type { RestifyInstrumentationConfig } from './types'; const supportedVersions = ['>=4.0.0 <12']; -export class RestifyInstrumentation extends InstrumentationBase { +export class RestifyInstrumentation extends InstrumentationBase { constructor(config: RestifyInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -45,14 +45,6 @@ export class RestifyInstrumentation extends InstrumentationBase { private _moduleVersion?: string; private _isDisabled = false; - override setConfig(config: RestifyInstrumentationConfig = {}) { - this._config = Object.assign({}, config); - } - - override getConfig(): RestifyInstrumentationConfig { - return this._config as RestifyInstrumentationConfig; - } - init() { const module = new InstrumentationNodeModuleDefinition( constants.MODULE_NAME, @@ -190,11 +182,11 @@ export class RestifyInstrumentation extends InstrumentationBase { ); const instrumentation = this; - const requestHook = instrumentation.getConfig().requestHook; + const { requestHook } = instrumentation.getConfig(); if (requestHook) { safeExecuteInTheMiddle( () => { - return requestHook!(span, { + return requestHook(span, { request: req, layerType: metadata.type, }); diff --git a/plugins/node/opentelemetry-instrumentation-winston/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-winston/src/instrumentation.ts index b7164e2395..96c83e245a 100644 --- a/plugins/node/opentelemetry-instrumentation-winston/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-winston/src/instrumentation.ts @@ -36,7 +36,7 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; const winston3Versions = ['>=3 <4']; const winstonPre3Versions = ['>=1 <3']; -export class WinstonInstrumentation extends InstrumentationBase { +export class WinstonInstrumentation extends InstrumentationBase { constructor(config: WinstonInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -112,23 +112,15 @@ export class WinstonInstrumentation extends InstrumentationBase { ]; } - override getConfig(): WinstonInstrumentationConfig { - return this._config; - } - - override setConfig(config: WinstonInstrumentationConfig = {}) { - this._config = config; - } - private _callHook(span: Span, record: Record) { - const hook = this.getConfig().logHook; + const { logHook } = this.getConfig(); - if (!hook) { + if (!logHook) { return; } safeExecuteInTheMiddle( - () => hook(span, record), + () => logHook(span, record), err => { if (err) { this._diag.error('error calling logHook', err); diff --git a/plugins/web/opentelemetry-instrumentation-document-load/src/instrumentation.ts b/plugins/web/opentelemetry-instrumentation-document-load/src/instrumentation.ts index 5849be561d..7bdd78df0c 100644 --- a/plugins/web/opentelemetry-instrumentation-document-load/src/instrumentation.ts +++ b/plugins/web/opentelemetry-instrumentation-document-load/src/instrumentation.ts @@ -52,15 +52,11 @@ import { /** * This class represents a document load plugin */ -export class DocumentLoadInstrumentation extends InstrumentationBase { +export class DocumentLoadInstrumentation extends InstrumentationBase { readonly component: string = 'document-load'; readonly version: string = '1'; moduleName = this.component; - /** - * - * @param config - */ constructor(config: DocumentLoadInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -120,12 +116,12 @@ export class DocumentLoadInstrumentation extends InstrumentationBase { if (fetchSpan) { fetchSpan.setAttribute(SEMATTRS_HTTP_URL, location.href); context.with(trace.setSpan(context.active(), fetchSpan), () => { - if (!this._getConfig().ignoreNetworkEvents) { + if (!this.getConfig().ignoreNetworkEvents) { addSpanNetworkEvents(fetchSpan, entries); } this._addCustomAttributesOnSpan( fetchSpan, - this._getConfig().applyCustomAttributesOnSpan?.documentFetch + this.getConfig().applyCustomAttributesOnSpan?.documentFetch ); this._endSpan(fetchSpan, PTN.RESPONSE_END, entries); }); @@ -137,7 +133,7 @@ export class DocumentLoadInstrumentation extends InstrumentationBase { this._addResourcesSpans(rootSpan); - if (!this._getConfig().ignoreNetworkEvents) { + if (!this.getConfig().ignoreNetworkEvents) { addSpanNetworkEvent(rootSpan, PTN.FETCH_START, entries); addSpanNetworkEvent(rootSpan, PTN.UNLOAD_EVENT_START, entries); addSpanNetworkEvent(rootSpan, PTN.UNLOAD_EVENT_END, entries); @@ -157,13 +153,13 @@ export class DocumentLoadInstrumentation extends InstrumentationBase { addSpanNetworkEvent(rootSpan, PTN.LOAD_EVENT_END, entries); } - if (!this._getConfig().ignorePerformancePaintEvents) { + if (!this.getConfig().ignorePerformancePaintEvents) { addSpanPerformancePaintEvents(rootSpan); } this._addCustomAttributesOnSpan( rootSpan, - this._getConfig().applyCustomAttributesOnSpan?.documentLoad + this.getConfig().applyCustomAttributesOnSpan?.documentLoad ); this._endSpan(rootSpan, PTN.LOAD_EVENT_END, entries); }); @@ -208,13 +204,13 @@ export class DocumentLoadInstrumentation extends InstrumentationBase { ); if (span) { span.setAttribute(SEMATTRS_HTTP_URL, resource.name); - if (!this._getConfig().ignoreNetworkEvents) { + if (!this.getConfig().ignoreNetworkEvents) { addSpanNetworkEvents(span, resource); } this._addCustomAttributesOnResourceSpan( span, resource, - this._getConfig().applyCustomAttributesOnSpan?.resourceFetch + this.getConfig().applyCustomAttributesOnSpan?.resourceFetch ); this._endSpan(span, PTN.RESPONSE_END, resource); } @@ -261,9 +257,6 @@ export class DocumentLoadInstrumentation extends InstrumentationBase { } } - private _getConfig(): DocumentLoadInstrumentationConfig { - return this._config; - } /** * adds custom attributes to root span if configured */ diff --git a/plugins/web/opentelemetry-instrumentation-long-task/src/instrumentation.ts b/plugins/web/opentelemetry-instrumentation-long-task/src/instrumentation.ts index b77bae7a65..1dc990eb5e 100644 --- a/plugins/web/opentelemetry-instrumentation-long-task/src/instrumentation.ts +++ b/plugins/web/opentelemetry-instrumentation-long-task/src/instrumentation.ts @@ -24,16 +24,11 @@ import type { const LONGTASK_PERFORMANCE_TYPE = 'longtask'; -export class LongTaskInstrumentation extends InstrumentationBase { +export class LongTaskInstrumentation extends InstrumentationBase { readonly version: string = PACKAGE_VERSION; private _observer?: PerformanceObserver; - override _config!: LongtaskInstrumentationConfig; - /** - * - * @param config - */ constructor(config: LongtaskInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, config); } @@ -57,9 +52,10 @@ export class LongTaskInstrumentation extends InstrumentationBase { const span = this.tracer.startSpan(LONGTASK_PERFORMANCE_TYPE, { startTime: hrTime(entry.startTime), }); - if (this._config.observerCallback) { + const { observerCallback } = this.getConfig(); + if (observerCallback) { try { - this._config.observerCallback(span, { longtaskEntry: entry }); + observerCallback(span, { longtaskEntry: entry }); } catch (err) { diag.error('longtask instrumentation: observer callback failed', err); } diff --git a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts index 4a502b7051..4bd38efbe7 100644 --- a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts +++ b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts @@ -49,7 +49,7 @@ function defaultShouldPreventSpanCreation() { * If zone.js is available then it patches the zone otherwise it patches * addEventListener of HTMLElement */ -export class UserInteractionInstrumentation extends InstrumentationBase { +export class UserInteractionInstrumentation extends InstrumentationBase { readonly version = PACKAGE_VERSION; readonly moduleName: string = 'user-interaction'; private _spansData = new WeakMap();