Skip to content

Commit

Permalink
chore(instrumentation-grpc): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
llc1123 committed Jun 7, 2023
1 parent 88252d9 commit 0d4ae85
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -53,17 +67,7 @@ interface TestRequestResponse {
num: number;
}

type ServiceError = grpcJs.ServiceError;
type Client = grpcJs.Client;
type Server = grpcJs.Server;
type ServerUnaryCall = grpcJs.ServerUnaryCall<any, any>;
type RequestCallback = grpcJs.requestCallback<any>;
type ServerReadableStream = grpcJs.ServerReadableStream<any, any>;
type ServerWriteableStream = grpcJs.ServerWritableStream<any, any>;
type ServerDuplexStream = grpcJs.ServerDuplexStream<any, any>;
type Metadata = grpcJs.Metadata;

type TestGrpcClient = typeof grpcJs['Client'] & {
type TestGrpcClient = Client & {
unaryMethodWithMetadata: any;
unaryMethod: any;
UnaryMethod: any;
Expand Down Expand Up @@ -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: (
Expand All @@ -137,7 +140,7 @@ export const runTests = (
unaryMethod: (
client: TestGrpcClient,
request: TestRequestResponse,
metadata: Metadata = new grpc.Metadata()
metadata = new Metadata()
): Promise<TestRequestResponse> => {
return new Promise((resolve, reject) => {
return client.unaryMethod(
Expand All @@ -157,7 +160,7 @@ export const runTests = (
UnaryMethod: (
client: TestGrpcClient,
request: TestRequestResponse,
metadata: Metadata = new grpc.Metadata()
metadata = new Metadata()
): Promise<TestRequestResponse> => {
return new Promise((resolve, reject) => {
return client.UnaryMethod(
Expand All @@ -177,7 +180,7 @@ export const runTests = (
camelCaseMethod: (
client: TestGrpcClient,
request: TestRequestResponse,
metadata: Metadata = new grpc.Metadata()
metadata = new Metadata()
): Promise<TestRequestResponse> => {
return new Promise((resolve, reject) => {
return client.camelCaseMethod(
Expand All @@ -197,7 +200,7 @@ export const runTests = (
clientStreamMethod: (
client: TestGrpcClient,
request: TestRequestResponse[],
metadata: Metadata = new grpc.Metadata()
metadata = new Metadata()
): Promise<TestRequestResponse> => {
return new Promise((resolve, reject) => {
const writeStream = client.clientStreamMethod(
Expand All @@ -221,7 +224,7 @@ export const runTests = (
serverStreamMethod: (
client: TestGrpcClient,
request: TestRequestResponse,
metadata: Metadata = new grpc.Metadata()
metadata = new Metadata()
): Promise<TestRequestResponse[]> => {
return new Promise((resolve, reject) => {
const result: TestRequestResponse[] = [];
Expand All @@ -242,7 +245,7 @@ export const runTests = (
bidiStreamMethod: (
client: TestGrpcClient,
request: TestRequestResponse[],
metadata: Metadata = new grpc.Metadata()
metadata = new Metadata()
): Promise<TestRequestResponse[]> => {
return new Promise((resolve, reject) => {
const result: TestRequestResponse[] = [];
Expand Down Expand Up @@ -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 = {
Expand All @@ -290,7 +293,7 @@ export const runTests = (
message: msg,
code,
details: msg,
metadata: new grpc.Metadata(),
metadata: new Metadata(),
};
return err;
}
Expand All @@ -302,56 +305,49 @@ export const runTests = (

// This method returns the request
unaryMethodWithMetadata(
call: ServerUnaryCall,
callback: RequestCallback
call: ServerUnaryCall<any, any>,
callback: requestCallback<any>
) {
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<any, any>,
callback: requestCallback<any>
) {
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<any, any>,
callback: requestCallback<any>
) {
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<any, any>,
callback: requestCallback<any>
) {
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) {
Expand All @@ -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<any, any>) => {
const result = replicate(call.request);

if (call.request.num <= MAX_ERROR_STATUS) {
Expand All @@ -385,7 +381,7 @@ export const runTests = (
},

// This method returns the request
bidiStreamMethod: (call: ServerDuplexStream) => {
bidiStreamMethod: (call: ServerDuplexStream<any, any>) => {
call.on('data', (data: TestRequestResponse) => {
if (data.num <= MAX_ERROR_STATUS) {
call.emit(
Expand All @@ -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()
);
}

Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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 => {
Expand All @@ -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);
}
});
Expand All @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -39,7 +39,7 @@ export const assertSpan = (
kind: SpanKind,
validations: {
name: string;
status: grpcJs.status;
status: GrpcStatus;
netPeerName?: string;
netPeerPort?: number;
}
Expand Down

0 comments on commit 0d4ae85

Please sign in to comment.