Skip to content

Commit

Permalink
chore: rewrite GrpcInstrumentation to be a proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Jan 30, 2021
1 parent c5b950f commit 0e3a42e
Showing 1 changed file with 50 additions and 37 deletions.
87 changes: 50 additions & 37 deletions packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,86 @@
* limitations under the License.
*/

import {
InstrumentationBase,
InstrumentationConfig,
} from '@opentelemetry/instrumentation';
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { GrpcInstrumentationConfig } from './types';
import { VERSION } from './version';
import { getGrpcPatches } from './grpc';
import { getGrpcJsPatches } from './grpc-js';
import { GrpcNativeInstrumentation } from './grpc';
import { GrpcJsInstrumentation } from './grpc-js';
import * as api from '@opentelemetry/api';

/** The metadata key under which span context is stored as a binary value. */
export const GRPC_TRACE_KEY = 'grpc-trace-bin';

export class GrpcInstrumentation extends InstrumentationBase {
export class GrpcInstrumentation {
private _grpcNativeInstrumentation: GrpcNativeInstrumentation;
private _grpcJsInstrumentation: GrpcJsInstrumentation;

public readonly instrumentationName: string =
'@opentelemetry/instrumentation-grpc';
public readonly instrumentationVersion: string = VERSION;

constructor(
protected _config: GrpcInstrumentationConfig & InstrumentationConfig = {}
) {
super('@opentelemetry/instrumentation-grpc', VERSION, _config);
this._grpcJsInstrumentation = new GrpcJsInstrumentation(
_config,
this.instrumentationName,
this.instrumentationVersion
);
this._grpcNativeInstrumentation = new GrpcNativeInstrumentation(
_config,
this.instrumentationName,
this.instrumentationVersion
);
}

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

/**
* @internal
* Public reference to the protected BaseInstrumentation shimmer utils to be used by this
* Public reference to the protected BaseInstrumentation `_config` instance to be used by this
* plugin's external helper functions
*/
public getShimmer() {
return {
wrap: this._wrap,
unwrap: this._unwrap,
massWrap: this._massWrap,
massUnwrap: this._massUnwrap,
};
public getConfig() {
return this._config;
}

/**
* @internal
* Public reference to the protected BaseInstrumentation `_logger` instance to be used by this
* plugin's external helper functions
*/
public getLogger() {
return this._logger;
init() {
// sub instrumentations will already be init when constructing them
return;
}

/**
* @internal
* Public reference to the protected BaseInstrumentation `tracer` instance to be used by this
* plugin's external helper functions
*/
public getTracer() {
return this.tracer;
enable() {
this._grpcJsInstrumentation.enable();
this._grpcNativeInstrumentation.enable();
}

disable() {
this._grpcJsInstrumentation.disable();
this._grpcNativeInstrumentation.disable();
}

/**
* @internal
* Public reference to the protected BaseInstrumentation `_config` instance to be used by this
* plugin's external helper functions
* Sets MeterProvider to this plugin
* @param meterProvider
*/
public getConfig() {
return this._config;
public setMeterProvider(meterProvider: api.MeterProvider) {
this._grpcJsInstrumentation.setMeterProvider(meterProvider);
this._grpcNativeInstrumentation.setMeterProvider(meterProvider);
}

init() {
return [...getGrpcJsPatches.call(this), ...getGrpcPatches.call(this)];
/**
* Sets TraceProvider to this plugin
* @param tracerProvider
*/
public setTracerProvider(tracerProvider: api.TracerProvider) {
this._grpcJsInstrumentation.setTracerProvider(tracerProvider);
this._grpcNativeInstrumentation.setTracerProvider(tracerProvider);
}
}

0 comments on commit 0e3a42e

Please sign in to comment.