Skip to content

Commit

Permalink
chore(instrumentation-grpc): cleanup remnants of grpc-native support (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
llc1123 authored Jul 4, 2023
1 parent c51c9eb commit dccd906
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 205 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ All notable changes to experimental packages in this project will be documented

### :house: (Internal)

* chore(instrumentation-grpc): Cleanup remnants of grpc-native support. [#3886](https://github.com/open-telemetry/opentelemetry-js/pull/3886) @llc1123

## 0.40.0

### :boom: Breaking Change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"cross-var": "1.1.0",
"lerna": "7.1.1",
"mocha": "10.2.0",
"node-pre-gyp": "0.17.0",
"nyc": "15.1.0",
"semver": "7.5.3",
"sinon": "15.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,32 @@
* limitations under the License.
*/

import { GrpcJsInstrumentation } from './';
import type { GrpcClientFunc, SendUnaryDataCallback } from './types';
import {
Span,
SpanStatusCode,
SpanStatus,
propagation,
context,
} from '@opentelemetry/api';
import type { EventEmitter } from 'events';
import type { Span, SpanStatus } from '@opentelemetry/api';
import type { Client, Metadata, ServiceError } from '@grpc/grpc-js';
import type * as grpcJs from '@grpc/grpc-js';
import type { GrpcJsInstrumentation } from './';
import type { GrpcClientFunc, SendUnaryDataCallback } from './types';
import type { metadataCaptureType } from '../internal-types';

import { SpanStatusCode, propagation, context } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { CALL_SPAN_ENDED } from './serverUtils';
import { AttributeNames } from '../enums/AttributeNames';
import { GRPC_STATUS_CODE_OK } from '../status-code';
import {
_grpcStatusCodeToSpanStatus,
_grpcStatusCodeToOpenTelemetryStatusCode,
_methodIsIgnored,
} from '../utils';
import { CALL_SPAN_ENDED } from './serverUtils';
import { EventEmitter } from 'events';
import { AttributeNames } from '../enums/AttributeNames';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { metadataCaptureType } from '../internal-types';
import { GRPC_STATUS_CODE_OK } from '../status-code';

/**
* Parse a package method list and return a list of methods to patch
* with both possible casings e.g. "TestMethod" & "testMethod"
*/
export function getMethodsToWrap(
this: GrpcJsInstrumentation,
client: typeof grpcJs.Client,
client: typeof Client,
methods: { [key: string]: { originalName?: string } }
): string[] {
const methodList: string[] = [];
Expand Down Expand Up @@ -74,8 +71,8 @@ export function makeGrpcClientRemoteCall(
metadataCapture: metadataCaptureType,
original: GrpcClientFunc,
args: unknown[],
metadata: grpcJs.Metadata,
self: grpcJs.Client
metadata: Metadata,
self: Client
): (span: Span) => EventEmitter {
/**
* Patches a callback so that the current span for this trace is also ended
Expand All @@ -86,7 +83,7 @@ export function makeGrpcClientRemoteCall(
callback: SendUnaryDataCallback<ResponseType>
) {
const wrappedFn: SendUnaryDataCallback<ResponseType> = (
err: grpcJs.ServiceError | null,
err: ServiceError | null,
res?: ResponseType
) => {
if (err) {
Expand Down Expand Up @@ -145,7 +142,7 @@ export function makeGrpcClientRemoteCall(
}
};
context.bind(context.active(), call);
call.on('error', (err: grpcJs.ServiceError) => {
call.on('error', (err: ServiceError) => {
if (call[CALL_SPAN_ENDED]) {
return;
}
Expand Down Expand Up @@ -185,22 +182,22 @@ export function makeGrpcClientRemoteCall(
*/
export function getMetadata(
this: GrpcJsInstrumentation,
grpcClient: typeof grpcJs,
original: GrpcClientFunc,
args: Array<unknown | grpcJs.Metadata>
): grpcJs.Metadata {
let metadata: grpcJs.Metadata;
grpcClient: typeof grpcJs,
args: Array<unknown | Metadata>
): Metadata {
let metadata: Metadata;

// This finds an instance of Metadata among the arguments.
// A possible issue that could occur is if the 'options' parameter from
// the user contains an '_internal_repr' as well as a 'getMap' function,
// but this is an extremely rare case.
let metadataIndex = args.findIndex((arg: unknown | grpcJs.Metadata) => {
let metadataIndex = args.findIndex((arg: unknown | Metadata) => {
return (
arg &&
typeof arg === 'object' &&
(arg as grpcJs.Metadata)['internalRepr'] && // changed from _internal_repr in grpc --> @grpc/grpc-js https://github.com/grpc/grpc-node/blob/95289edcaf36979cccf12797cc27335da8d01f03/packages/grpc-js/src/metadata.ts#L88
typeof (arg as grpcJs.Metadata).getMap === 'function'
(arg as Metadata)['internalRepr'] && // changed from _internal_repr in grpc --> @grpc/grpc-js https://github.com/grpc/grpc-node/blob/95289edcaf36979cccf12797cc27335da8d01f03/packages/grpc-js/src/metadata.ts#L88
typeof (arg as Metadata).getMap === 'function'
);
});
if (metadataIndex === -1) {
Expand All @@ -214,7 +211,7 @@ export function getMetadata(
}
args.splice(metadataIndex, 0, metadata);
} else {
metadata = args[metadataIndex] as grpcJs.Metadata;
metadata = args[metadataIndex] as Metadata;
}
return metadata;
}
Expand All @@ -224,7 +221,7 @@ export function getMetadata(
* grpc receiver
* @param metadata
*/
export function setSpanContext(metadata: grpcJs.Metadata): void {
export function setSpanContext(metadata: Metadata): void {
propagation.inject(context.active(), metadata, {
set: (meta, k, v) => meta.set(k, v),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
* limitations under the License.
*/

import type { EventEmitter } from 'events';

import type {
Server,
serialize as Serialize,
deserialize as Deserialize,
Metadata,
Client,
ServiceDefinition,
loadPackageDefinition,
GrpcObject,
} from '@grpc/grpc-js';
import type * as grpcJs from '@grpc/grpc-js';
import {
InstrumentationNodeModuleDefinition,
isWrapped,
} from '@opentelemetry/instrumentation';
import { InstrumentationBase } from '@opentelemetry/instrumentation';
import { GrpcInstrumentationConfig } from '../types';
import { metadataCaptureType } from '../internal-types';
import {

import type {
ServerCallWithMeta,
SendUnaryDataCallback,
ServerRegisterFunction,
Expand All @@ -31,6 +37,9 @@ import {
PackageDefinition,
GrpcClientFunc,
} from './types';
import type { GrpcInstrumentationConfig } from '../types';
import type { metadataCaptureType } from '../internal-types';

import {
context,
propagation,
Expand All @@ -39,6 +48,13 @@ import {
SpanKind,
trace,
} from '@opentelemetry/api';
import {
InstrumentationNodeModuleDefinition,
isWrapped,
InstrumentationBase,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

import {
shouldNotTraceServerCall,
handleServerFunction,
Expand All @@ -49,10 +65,8 @@ import {
makeGrpcClientRemoteCall,
getMetadata,
} from './clientUtils';
import { EventEmitter } from 'events';
import { _extractMethodAndService, metadataCapture, URI_REGEX } from '../utils';
import { AttributeValues } from '../enums/AttributeValues';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

export class GrpcJsInstrumentation extends InstrumentationBase {
private _metadataCapture: metadataCaptureType;
Expand Down Expand Up @@ -143,11 +157,11 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
const config = this.getConfig();
instrumentation._diag.debug('patched gRPC server');
return function register<RequestType, ResponseType>(
this: grpcJs.Server,
this: Server,
name: string,
handler: HandleCall<unknown, unknown>,
serialize: grpcJs.serialize<unknown>,
deserialize: grpcJs.deserialize<unknown>,
serialize: Serialize<unknown>,
deserialize: Deserialize<unknown>,
type: string
): boolean {
const originalRegisterResult = originalRegister.call(
Expand All @@ -171,13 +185,7 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
) {
const self = this;

if (
shouldNotTraceServerCall(
call.metadata,
name,
config.ignoreGrpcMethods
)
) {
if (shouldNotTraceServerCall(name, config.ignoreGrpcMethods)) {
return handleUntracedServerFunction(
type,
originalFunc,
Expand Down Expand Up @@ -220,14 +228,13 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
instrumentation._wrap(
call,
'sendMetadata',
originalSendMetadata =>
(responseMetadata: grpcJs.Metadata) => {
instrumentation._metadataCapture.server.captureResponseMetadata(
span,
responseMetadata
);
originalSendMetadata.call(call, responseMetadata);
}
originalSendMetadata => (responseMetadata: Metadata) => {
instrumentation._metadataCapture.server.captureResponseMetadata(
span,
responseMetadata
);
originalSendMetadata.call(call, responseMetadata);
}
);

context.with(trace.setSpan(context.active(), span), () => {
Expand All @@ -246,7 +253,7 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
}
);
return originalRegisterResult;
} as typeof grpcJs.Server.prototype.register;
} as typeof Server.prototype.register;
};
}

Expand All @@ -263,8 +270,8 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
return (original: MakeClientConstructorFunction) => {
instrumentation._diag.debug('patching client');
return function makeClientConstructor(
this: typeof grpcJs.Client,
methods: grpcJs.ServiceDefinition,
this: typeof Client,
methods: ServiceDefinition,
serviceName: string,
options?: object
) {
Expand All @@ -286,18 +293,18 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
private _patchLoadPackageDefinition(grpcClient: typeof grpcJs) {
const instrumentation = this;
instrumentation._diag.debug('patching loadPackageDefinition');
return (original: typeof grpcJs.loadPackageDefinition) => {
return (original: typeof loadPackageDefinition) => {
return function patchedLoadPackageDefinition(
this: null,
packageDef: PackageDefinition
) {
const result: grpcJs.GrpcObject = original.call(
const result: GrpcObject = original.call(
this,
packageDef
) as grpcJs.GrpcObject;
) as GrpcObject;
instrumentation._patchLoadedPackage(grpcClient, result);
return result;
} as typeof grpcJs.loadPackageDefinition;
} as typeof loadPackageDefinition;
};
}

Expand All @@ -310,13 +317,13 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
const instrumentation = this;
return (original: GrpcClientFunc) => {
instrumentation._diag.debug('patch all client methods');
function clientMethodTrace(this: grpcJs.Client) {
function clientMethodTrace(this: Client) {
const name = `grpc.${original.path.replace('/', '')}`;
const args = [...arguments];
const metadata = getMetadata.call(
instrumentation,
grpcClient,
original,
grpcClient,
args
);
const { service, method } = _extractMethodAndService(original.path);
Expand Down Expand Up @@ -369,7 +376,7 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
*/
private _patchLoadedPackage(
grpcClient: typeof grpcJs,
result: grpcJs.GrpcObject
result: GrpcObject
): void {
Object.values(result).forEach(service => {
if (typeof service === 'function') {
Expand All @@ -380,11 +387,7 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
);
} else if (typeof service.format !== 'string') {
// GrpcObject
this._patchLoadedPackage.call(
this,
grpcClient,
service as grpcJs.GrpcObject
);
this._patchLoadedPackage.call(this, grpcClient, service as GrpcObject);
}
});
}
Expand Down
Loading

0 comments on commit dccd906

Please sign in to comment.