Skip to content

Commit

Permalink
chore: cleanup setting config in instrumentations (#2522)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Marchaud <[email protected]>
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2021
1 parent c0ab952 commit 5de52ee
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ export class FetchInstrumentation extends InstrumentationBase<
private _usedResources = new WeakSet<PerformanceResourceTiming>();
private _tasksCount = 0;

constructor(config: FetchInstrumentationConfig = {}) {
constructor(config?: FetchInstrumentationConfig) {
super(
'@opentelemetry/instrumentation-fetch',
VERSION,
Object.assign({}, config)
config
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function getMethodsToWrap(

// For a method defined in .proto as "UnaryMethod"
Object.entries(methods).forEach(([name, { originalName }]) => {
if (!_methodIsIgnored(name, this._config.ignoreGrpcMethods)) {
if (!_methodIsIgnored(name, this.getConfig().ignoreGrpcMethods)) {
methodList.push(name); // adds camel case method name: "unaryMethod"
if (
originalName &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import {
InstrumentationNodeModuleDefinition,
isWrapped,
} from '@opentelemetry/instrumentation';
import {
InstrumentationBase,
InstrumentationConfig,
} from '@opentelemetry/instrumentation';
import { InstrumentationBase } from '@opentelemetry/instrumentation';
import { GrpcInstrumentationConfig } from '../types';
import {
ServerCallWithMeta,
Expand Down Expand Up @@ -56,17 +53,11 @@ import { AttributeNames } from '../enums/AttributeNames';

export class GrpcJsInstrumentation extends InstrumentationBase {
constructor(
protected override _config: GrpcInstrumentationConfig & InstrumentationConfig = {},
name: string,
version: string
version: string,
config?: GrpcInstrumentationConfig,
) {
super(name, version, _config);
}

public override setConfig(
config: GrpcInstrumentationConfig & InstrumentationConfig = {}
) {
this._config = Object.assign({}, config);
super(name, version, config);
}

init() {
Expand Down Expand Up @@ -125,6 +116,10 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
];
}

override getConfig(): GrpcInstrumentationConfig {
return super.getConfig();
}

/**
* Patch for grpc.Server.prototype.register(...) function. Provides auto-instrumentation for
* client_stream, server_stream, bidi, unary server handler calls.
Expand All @@ -134,7 +129,7 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
) => ServerRegisterFunction {
const instrumentation = this;
return (originalRegister: ServerRegisterFunction) => {
const config = this._config;
const config = this.getConfig();
instrumentation._diag.debug('patched gRPC server');
return function register<RequestType, ResponseType>(
this: grpcJs.Server,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
InstrumentationBase,
InstrumentationConfig,
isWrapped,
} from '@opentelemetry/instrumentation';
import {
Expand Down Expand Up @@ -55,17 +54,11 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
typeof grpcTypes
> {
constructor(
protected override _config: GrpcInstrumentationConfig & InstrumentationConfig = {},
name: string,
version: string
version: string,
config?: GrpcInstrumentationConfig
) {
super(name, version, _config);
}

public override setConfig(
config: GrpcInstrumentationConfig & InstrumentationConfig = {}
) {
this._config = Object.assign({}, config);
super(name, version, config);
}

init() {
Expand Down Expand Up @@ -107,6 +100,10 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
];
}

override getConfig(): GrpcInstrumentationConfig {
return super.getConfig();
}

private _getInternalPatchs() {
const onPatch = (
moduleExports: GrpcInternalClientTypes,
Expand Down Expand Up @@ -268,7 +265,7 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<

// For a method defined in .proto as "UnaryMethod"
Object.entries(methods).forEach(([name, { originalName }]) => {
if (!_methodIsIgnored(name, this._config.ignoreGrpcMethods)) {
if (!_methodIsIgnored(name, this.getConfig().ignoreGrpcMethods)) {
methodList.push(name); // adds camel case method name: "unaryMethod"
if (
originalName &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ export const shouldNotTraceServerCall = function (
const parsedName = name.split('/');
return _methodIsIgnored(
parsedName[parsedName.length - 1] || name,
this._config.ignoreGrpcMethods
this.getConfig().ignoreGrpcMethods
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { GrpcInstrumentationConfig } from './types';
import { VERSION } from './version';
import { GrpcNativeInstrumentation } from './grpc';
Expand All @@ -34,35 +33,33 @@ export class GrpcInstrumentation {
public readonly instrumentationVersion: string = VERSION;

constructor(
protected _config: GrpcInstrumentationConfig & InstrumentationConfig = {}
config?: GrpcInstrumentationConfig
) {
this._grpcJsInstrumentation = new GrpcJsInstrumentation(
_config,
this.instrumentationName,
this.instrumentationVersion
this.instrumentationVersion,
config
);
this._grpcNativeInstrumentation = new GrpcNativeInstrumentation(
_config,
this.instrumentationName,
this.instrumentationVersion
this.instrumentationVersion,
config
);
}

public setConfig(
config: GrpcInstrumentationConfig & InstrumentationConfig = {}
) {
this._config = Object.assign({}, config);
this._grpcJsInstrumentation.setConfig(this._config);
this._grpcNativeInstrumentation.setConfig(this._config);
public setConfig(config?: GrpcInstrumentationConfig) {
this._grpcJsInstrumentation.setConfig(config);
this._grpcNativeInstrumentation.setConfig(config);
}

/**
* @internal
* Public reference to the protected BaseInstrumentation `_config` instance to be used by this
* plugin's external helper functions
*/
public getConfig() {
return this._config;
public getConfig(): GrpcInstrumentationConfig {
// grpcNative and grpcJs have their own config copy which should be identical so just pick one
return this._grpcJsInstrumentation.getConfig();
}

init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import * as utils from './utils';
import { VERSION } from './version';
import {
InstrumentationBase,
InstrumentationConfig,
InstrumentationNodeModuleDefinition,
isWrapped,
safeExecuteInTheMiddle,
Expand All @@ -60,11 +59,11 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
private readonly _version = process.versions.node;
private _headerCapture;

constructor(config: HttpInstrumentationConfig & InstrumentationConfig = {}) {
constructor(config?: HttpInstrumentationConfig) {
super(
'@opentelemetry/instrumentation-http',
VERSION,
Object.assign({}, config)
config
);

this._headerCapture = this._createHeaderCapture();
Expand All @@ -74,8 +73,8 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
return this._config;
}

override setConfig(config: HttpInstrumentationConfig & InstrumentationConfig = {}): void {
this._config = Object.assign({}, config);
override setConfig(config?: HttpInstrumentationConfig): void {
super.setConfig(config);
this._headerCapture = this._createHeaderCapture();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
private _xhrMem = new WeakMap<XMLHttpRequest, XhrMem>();
private _usedResources = new WeakSet<PerformanceResourceTiming>();

constructor(
config: XMLHttpRequestInstrumentationConfig & InstrumentationConfig = {}
) {
constructor(config?: XMLHttpRequestInstrumentationConfig) {
super(
'@opentelemetry/instrumentation-xml-http-request',
VERSION,
Object.assign({}, config)
config
);
}

Expand Down

0 comments on commit 5de52ee

Please sign in to comment.