Skip to content

Commit

Permalink
feat: implement interceptor opentracing
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen committed Aug 9, 2019
1 parent 90823c1 commit a6300ea
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 75 deletions.
67 changes: 67 additions & 0 deletions __tests__/helpers/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
UntypedServiceImplementation,
Client,
ServerCredentials,
credentials,
} from 'grpc';
import { ExperimentalServer } from '../../src';
import {
RouteGuideService,
RouteGuideClient,
} from '../fixtures/static_codegen/route_guide_grpc_pb';

// method hubs
import getFeature from '../fixtures/RouteGuide/getFeature';
import recordRoute from '../fixtures/RouteGuide/recordRoute';
import routeChat from '../fixtures/RouteGuide/routeChat';
import listFeatures from '../fixtures/RouteGuide/listFeatures';

export default function runTest({
implementations,
testcase,
}: {
implementations?: UntypedServiceImplementation;
testcase: (
getServer: () => ExperimentalServer,
getClient: () => Client
) => void;
}): void {
let server: ExperimentalServer;
let client: Client;
beforeAll(done => {
const port = Math.floor(Math.random() * 1e4);
server = new ExperimentalServer();

server.addService(RouteGuideService, {
getFeature,
recordRoute,
listFeatures,
routeChat,
...implementations,
});

server.bindAsync(
`0.0.0.0:${port}`,
ServerCredentials.createInsecure(),
error => {
if (error) {
return done(error);
}
server.start();

client = new RouteGuideClient(
`0.0.0.0:${port}`,
credentials.createInsecure()
);

done();
}
);
});

afterAll(done => {
server.tryShutdown(done);
});

testcase(() => server, () => client);
}
70 changes: 70 additions & 0 deletions __tests__/opentracing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { initTracer } from 'jaeger-client';
import runTest from './helpers/runTest';
import { Metadata } from 'grpc';
import { opentracing } from '../src';
import { Span, FORMAT_HTTP_HEADERS } from 'opentracing';
import { Point } from './fixtures/static_codegen/route_guide_pb';

describe('opentracing', () => {
const tracer = initTracer({ serviceName: 'grpc-exp-server' }, {});

describe('start span', () => {
runTest({
testcase(getServer, client) {
it('without parent', done => {
const server = getServer();
server.use(opentracing());

server.use(async ({ call: { span } }, next) => {
expect(span).toBeInstanceOf(Span);
await next();
done();
});

// @ts-ignore
client().getFeature(new Point(), () => {});
});
},
});
});

describe('start span ', () => {
runTest({
testcase(getServer, client) {
it('with parent', done => {
const metadata = new Metadata();
const prx = new Proxy(metadata, {
set(target, key: string, value) {
target.set(key, value);
return true;
},
});
const clientSpan = tracer.startSpan('test');
tracer.inject(clientSpan, FORMAT_HTTP_HEADERS, prx);

const server = getServer();
server.use(opentracing({ tracer }));

server.use(async ({ call: { span } }, next) => {
expect(span).not.toBeUndefined();

const context = (span as Span).context();
// @ts-ignore
const { parentIdStr } = context;
// @ts-ignore
const { spanIdStr } = clientSpan.context();

// not empty
expect(parentIdStr).toEqual(spanIdStr);

await next();
done();
});

// @ts-ignore
client().getFeature(new Point(), metadata, () => {});
});
},
});
});
});
71 changes: 3 additions & 68 deletions __tests__/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,9 @@
import ExperimentalServer, { Interceptor } from '../src/server';
import {
RouteGuideService,
RouteGuideClient,
} from './fixtures/static_codegen/route_guide_grpc_pb';
import { Interceptor } from '../src/server';

import {
ServerCredentials,
Client,
credentials,
UntypedServiceImplementation,
handleUnaryCall,
ServiceError,
status,
} from 'grpc';
import { handleUnaryCall, ServiceError, status } from 'grpc';
import { Point, Feature } from './fixtures/static_codegen/route_guide_pb';
import getFeature from './fixtures/RouteGuide/getFeature';
import recordRoute from './fixtures/RouteGuide/recordRoute';
import routeChat from './fixtures/RouteGuide/routeChat';
import listFeatures from './fixtures/RouteGuide/listFeatures';

function runTest({
implementations,
testcase,
}: {
implementations: UntypedServiceImplementation;
testcase: (
getServer: () => ExperimentalServer,
getClient: () => Client
) => void;
}): void {
let server: ExperimentalServer;
let client: Client;
beforeAll(done => {
const port = Math.floor(Math.random() * 1e4);
console.log(port);
server = new ExperimentalServer();

server.addService(RouteGuideService, {
getFeature,
recordRoute,
listFeatures,
routeChat,
...implementations,
});

server.bindAsync(
`0.0.0.0:${port}`,
ServerCredentials.createInsecure(),
error => {
if (error) {
return done(error);
}
server.start();

client = new RouteGuideClient(
`0.0.0.0:${port}`,
credentials.createInsecure()
);

done();
}
);
});

afterAll(done => {
server.tryShutdown(done);
});

testcase(() => server, () => client);
}
import runTest from './helpers/runTest';

describe('server', () => {
describe('pass through', () => {
Expand Down
Loading

0 comments on commit a6300ea

Please sign in to comment.