diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 4c19664c2ab..fdbe99a56d8 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to experimental packages in this project will be documented * (internal) OTLPExporterBrowserBase: `RequestType` has been replaced by a `ResponseType` type-argument * (internal) OTLPExporterNodeBase: `ServiceRequest` has been replaced by a `ServiceResponse` type-argument * (internal) the `@opentelemetry/otlp-exporter-proto-base` package has been removed, and will from now on be deprecated in `npm` +* feat(instrumentation): remove default value for config in base instrumentation constructor [#4695](https://github.com/open-telemetry/opentelemetry-js/pull/4695): @blumamir * fix(instrumentation)!: remove unused supportedVersions from Instrumentation interface [#4694](https://github.com/open-telemetry/opentelemetry-js/pull/4694) @blumamir ### :rocket: (Enhancement) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index d459d3884f4..85aca36bf83 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -79,7 +79,7 @@ export class FetchInstrumentation extends InstrumentationBase(); private _tasksCount = 0; - constructor(config?: FetchInstrumentationConfig) { + constructor(config: FetchInstrumentationConfig = {}) { super('@opentelemetry/instrumentation-fetch', VERSION, config); } diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts index 6216de0765f..3daec0b63c5 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts @@ -90,7 +90,7 @@ import { VERSION } from './version'; export class GrpcInstrumentation extends InstrumentationBase { private _metadataCapture: metadataCaptureType; - constructor(config?: GrpcInstrumentationConfig) { + constructor(config: GrpcInstrumentationConfig = {}) { super('@opentelemetry/instrumentation-grpc', VERSION, config); this._metadataCapture = this._createMetadataCapture(); } diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 85ed9c897e1..a42927fff5e 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -69,7 +69,7 @@ export class HttpInstrumentation extends InstrumentationBase(); private _usedResources = new WeakSet(); - constructor(config?: XMLHttpRequestInstrumentationConfig) { + constructor(config: XMLHttpRequestInstrumentationConfig = {}) { super('@opentelemetry/instrumentation-xml-http-request', VERSION, config); } diff --git a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts index 551b1df11e9..113c2133a15 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts @@ -51,8 +51,10 @@ export abstract class InstrumentationAbstract< constructor( public readonly instrumentationName: string, public readonly instrumentationVersion: string, - config: ConfigType = {} as ConfigType // assuming ConfigType is an object with optional fields only + config: ConfigType ) { + // copy config first level properties to ensure they are immutable. + // nested properties are not copied, thus are mutable from the outside. this._config = { enabled: true, ...config, @@ -144,10 +146,10 @@ export abstract class InstrumentationAbstract< * Sets InstrumentationConfig to this plugin * @param InstrumentationConfig */ - public setConfig(config: ConfigType = {} as ConfigType): void { - // the assertion that {} is compatible with ConfigType may not be correct, - // ConfigType should contain only optional fields, but there is no enforcement in place for that - this._config = Object.assign({}, config); + public setConfig(config: ConfigType): void { + // copy config first level properties to ensure they are immutable. + // nested properties are not copied, thus are mutable from the outside. + this._config = { ...config }; } /** diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/browser/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/browser/instrumentation.ts index 46f3b2cc725..e5b13b80bb3 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/browser/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/browser/instrumentation.ts @@ -30,7 +30,7 @@ export abstract class InstrumentationBase< constructor( instrumentationName: string, instrumentationVersion: string, - config: ConfigType = {} as ConfigType // The cast here may be wrong as ConfigType may contain required fields + config: ConfigType ) { super(instrumentationName, instrumentationVersion, config); diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index 24426c05a6a..6c2aa08281e 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -54,7 +54,7 @@ export abstract class InstrumentationBase< constructor( instrumentationName: string, instrumentationVersion: string, - config: ConfigType = {} as ConfigType // The cast here may be wrong as ConfigType may contain required fields + config: ConfigType ) { super(instrumentationName, instrumentationVersion, config);