diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-js.test.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-js.test.ts index 624d267a214..b7e22102983 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-js.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-js.test.ts @@ -21,8 +21,8 @@ const instrumentation = new GrpcInstrumentation(); instrumentation.enable(); instrumentation.disable(); -import * as grpcJs from '@grpc/grpc-js'; +import '@grpc/grpc-js'; describe('#grpc-js', () => { - runTests(instrumentation, 'grpc', grpcJs, 12346); + runTests(instrumentation, 'grpc', 12346); }); diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts index 661b309e909..c8703270f06 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts @@ -32,7 +32,21 @@ import { } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import * as protoLoader from '@grpc/proto-loader'; -import type * as grpcJs from '@grpc/grpc-js'; +import { + status as GrpcStatus, + requestCallback, + ServerUnaryCall, + ServerReadableStream, + ServerWritableStream, + ServerDuplexStream, + Client, + Metadata, + ServiceError, + Server, + ServerCredentials, + credentials, + loadPackageDefinition, +} from '@grpc/grpc-js'; import { assertPropagation, assertSpan } from './utils/assertionUtils'; import { promisify } from 'util'; import type { GrpcInstrumentation } from '../src'; @@ -53,17 +67,7 @@ interface TestRequestResponse { num: number; } -type ServiceError = grpcJs.ServiceError; -type Client = grpcJs.Client; -type Server = grpcJs.Server; -type ServerUnaryCall = grpcJs.ServerUnaryCall; -type RequestCallback = grpcJs.requestCallback; -type ServerReadableStream = grpcJs.ServerReadableStream; -type ServerWriteableStream = grpcJs.ServerWritableStream; -type ServerDuplexStream = grpcJs.ServerDuplexStream; -type Metadata = grpcJs.Metadata; - -type TestGrpcClient = typeof grpcJs['Client'] & { +type TestGrpcClient = Client & { unaryMethodWithMetadata: any; unaryMethod: any; UnaryMethod: any; @@ -108,10 +112,9 @@ const checkEqual = export const runTests = ( plugin: GrpcInstrumentation, moduleName: string, - grpc: typeof grpcJs, grpcPort: number ) => { - const MAX_ERROR_STATUS = grpc.status.UNAUTHENTICATED; + const MAX_ERROR_STATUS = GrpcStatus.UNAUTHENTICATED; const grpcClient = { unaryMethodWithMetadata: ( @@ -137,7 +140,7 @@ export const runTests = ( unaryMethod: ( client: TestGrpcClient, request: TestRequestResponse, - metadata: Metadata = new grpc.Metadata() + metadata = new Metadata() ): Promise => { return new Promise((resolve, reject) => { return client.unaryMethod( @@ -157,7 +160,7 @@ export const runTests = ( UnaryMethod: ( client: TestGrpcClient, request: TestRequestResponse, - metadata: Metadata = new grpc.Metadata() + metadata = new Metadata() ): Promise => { return new Promise((resolve, reject) => { return client.UnaryMethod( @@ -177,7 +180,7 @@ export const runTests = ( camelCaseMethod: ( client: TestGrpcClient, request: TestRequestResponse, - metadata: Metadata = new grpc.Metadata() + metadata = new Metadata() ): Promise => { return new Promise((resolve, reject) => { return client.camelCaseMethod( @@ -197,7 +200,7 @@ export const runTests = ( clientStreamMethod: ( client: TestGrpcClient, request: TestRequestResponse[], - metadata: Metadata = new grpc.Metadata() + metadata = new Metadata() ): Promise => { return new Promise((resolve, reject) => { const writeStream = client.clientStreamMethod( @@ -221,7 +224,7 @@ export const runTests = ( serverStreamMethod: ( client: TestGrpcClient, request: TestRequestResponse, - metadata: Metadata = new grpc.Metadata() + metadata = new Metadata() ): Promise => { return new Promise((resolve, reject) => { const result: TestRequestResponse[] = []; @@ -242,7 +245,7 @@ export const runTests = ( bidiStreamMethod: ( client: TestGrpcClient, request: TestRequestResponse[], - metadata: Metadata = new grpc.Metadata() + metadata = new Metadata() ): Promise => { return new Promise((resolve, reject) => { const result: TestRequestResponse[] = []; @@ -280,8 +283,8 @@ export const runTests = ( return result; }; - async function startServer(grpc: typeof grpcJs, proto: any) { - const server = new grpc.Server(); + async function startServer(proto: any) { + const server = new Server(); function getError(msg: string, code: number): ServiceError | null { const err: ServiceError = { @@ -290,7 +293,7 @@ export const runTests = ( message: msg, code, details: msg, - metadata: new grpc.Metadata(), + metadata: new Metadata(), }; return err; } @@ -302,56 +305,49 @@ export const runTests = ( // This method returns the request unaryMethodWithMetadata( - call: ServerUnaryCall, - callback: RequestCallback + call: ServerUnaryCall, + callback: requestCallback ) { - const serverMetadata: any = new grpc.Metadata(); + const serverMetadata = new Metadata(); serverMetadata.add('server_metadata_key', 'server_metadata_value'); call.sendMetadata(serverMetadata); call.request.num <= MAX_ERROR_STATUS ? callback( - getError( - 'Unary Method with Metadata Error', - call.request.num - ) as grpcJs.ServiceError + getError('Unary Method with Metadata Error', call.request.num) ) : callback(null, { num: call.request.num }); }, // This method returns the request - unaryMethod(call: ServerUnaryCall, callback: RequestCallback) { + unaryMethod( + call: ServerUnaryCall, + callback: requestCallback + ) { call.request.num <= MAX_ERROR_STATUS - ? callback( - getError( - 'Unary Method Error', - call.request.num - ) as grpcJs.ServiceError - ) + ? callback(getError('Unary Method Error', call.request.num)) : callback(null, { num: call.request.num }); }, // This method returns the request - camelCaseMethod(call: ServerUnaryCall, callback: RequestCallback) { + camelCaseMethod( + call: ServerUnaryCall, + callback: requestCallback + ) { call.request.num <= MAX_ERROR_STATUS - ? callback( - getError( - 'Unary Method Error', - call.request.num - ) as grpcJs.ServiceError - ) + ? callback(getError('Unary Method Error', call.request.num)) : callback(null, { num: call.request.num }); }, // This method sums the requests clientStreamMethod( - call: ServerReadableStream, - callback: RequestCallback + call: ServerReadableStream, + callback: requestCallback ) { let sum = 0; let hasError = false; - let code = grpc.status.OK; + let code = GrpcStatus.OK; call.on('data', (data: TestRequestResponse) => { sum += data.num; if (data.num <= MAX_ERROR_STATUS) { @@ -368,7 +364,7 @@ export const runTests = ( // This method returns an array that replicates the request, request.num of // times - serverStreamMethod: (call: ServerWriteableStream) => { + serverStreamMethod: (call: ServerWritableStream) => { const result = replicate(call.request); if (call.request.num <= MAX_ERROR_STATUS) { @@ -385,7 +381,7 @@ export const runTests = ( }, // This method returns the request - bidiStreamMethod: (call: ServerDuplexStream) => { + bidiStreamMethod: (call: ServerDuplexStream) => { call.on('data', (data: TestRequestResponse) => { if (data.num <= MAX_ERROR_STATUS) { call.emit( @@ -405,16 +401,16 @@ export const runTests = ( await bindAwait.call( server, 'localhost:' + grpcPort, - grpc.ServerCredentials.createInsecure() as grpcJs.ServerCredentials + ServerCredentials.createInsecure() ); server.start(); return server; } - function createClient(grpc: typeof grpcJs, proto: any) { + function createClient(proto: any) { return new proto.GrpcTester( 'localhost:' + grpcPort, - grpc.credentials.createInsecure() + credentials.createInsecure() ); } @@ -503,7 +499,7 @@ export const runTests = ( ) => { const validations = { name: `grpc.pkg_test.GrpcTester/${methodName}`, - status: grpc.status.OK, + status: GrpcStatus.OK, netPeerName: 'localhost', netPeerPort: grpcPort, }; @@ -770,10 +766,10 @@ export const runTests = ( plugin.enable(); const packageDefinition = await protoLoader.load(PROTO_PATH, options); - const proto = grpc.loadPackageDefinition(packageDefinition).pkg_test; + const proto = loadPackageDefinition(packageDefinition).pkg_test; - server = await startServer(grpc, proto); - client = createClient(grpc, proto); + server = await startServer(proto); + client = createClient(proto); }); after(done => { @@ -792,9 +788,9 @@ export const runTests = ( methodList.forEach(method => { describe(`Test error raising for grpc remote ${method.description}`, () => { - Object.keys(grpc.status).forEach((statusKey: string) => { - const errorCode = Number(grpc.status[statusKey as any]); - if (errorCode > grpc.status.OK) { + Object.keys(GrpcStatus).forEach((statusKey: string) => { + const errorCode = Number(GrpcStatus[statusKey as any]); + if (errorCode > GrpcStatus.OK) { runErrorTest(method, statusKey, errorCode, provider); } }); @@ -813,10 +809,10 @@ export const runTests = ( plugin.disable(); const packageDefinition = await protoLoader.load(PROTO_PATH, options); - const proto = grpc.loadPackageDefinition(packageDefinition).pkg_test; + const proto = loadPackageDefinition(packageDefinition).pkg_test; - server = await startServer(grpc, proto); - client = createClient(grpc, proto); + server = await startServer(proto); + client = createClient(proto); }); after(done => { @@ -847,10 +843,10 @@ export const runTests = ( plugin.enable(); const packageDefinition = await protoLoader.load(PROTO_PATH, options); - const proto = grpc.loadPackageDefinition(packageDefinition).pkg_test; + const proto = loadPackageDefinition(packageDefinition).pkg_test; - server = await startServer(grpc, proto); - client = createClient(grpc, proto); + server = await startServer(proto); + client = createClient(proto); }); after(done => { @@ -891,10 +887,10 @@ export const runTests = ( plugin.enable(); const packageDefinition = await protoLoader.load(PROTO_PATH, options); - const proto = grpc.loadPackageDefinition(packageDefinition).pkg_test; + const proto = loadPackageDefinition(packageDefinition).pkg_test; - server = await startServer(grpc, proto); - client = createClient(grpc, proto); + server = await startServer(proto); + client = createClient(proto); }); after(done => { @@ -921,9 +917,9 @@ export const runTests = ( plugin.enable(); const packageDefinition = await protoLoader.load(PROTO_PATH, options); - const proto = grpc.loadPackageDefinition(packageDefinition).pkg_test; + const proto = loadPackageDefinition(packageDefinition).pkg_test; - client = createClient(grpc, proto); + client = createClient(proto); }); after(done => { @@ -943,7 +939,7 @@ export const runTests = ( const provider = new NodeTracerProvider(); provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); - const clientMetadata: Metadata = new grpc.Metadata(); + const clientMetadata = new Metadata(); clientMetadata.add('client_metadata_key', 'client_metadata_value'); const customMetadataMethod: TestGrpcCall = { @@ -978,10 +974,10 @@ export const runTests = ( plugin.enable(); const packageDefinition = await protoLoader.load(PROTO_PATH, options); - const proto = grpc.loadPackageDefinition(packageDefinition).pkg_test; + const proto = loadPackageDefinition(packageDefinition).pkg_test; - server = await startServer(grpc, proto); - client = createClient(grpc, proto); + server = await startServer(proto); + client = createClient(proto); }); after(done => { diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts index fcdd546b329..1b7966639e0 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts @@ -16,7 +16,7 @@ import { SpanKind, SpanStatusCode } from '@opentelemetry/api'; import * as assert from 'assert'; -import type * as grpcJs from '@grpc/grpc-js'; +import type { status as GrpcStatus } from '@grpc/grpc-js'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import { hrTimeToMilliseconds, @@ -25,7 +25,7 @@ import { import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; export const grpcStatusCodeToOpenTelemetryStatusCode = ( - status: grpcJs.status + status: GrpcStatus ): SpanStatusCode => { if (status !== undefined && status === 0) { return SpanStatusCode.UNSET; @@ -39,7 +39,7 @@ export const assertSpan = ( kind: SpanKind, validations: { name: string; - status: grpcJs.status; + status: GrpcStatus; netPeerName?: string; netPeerPort?: number; }