-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement interceptor opentracing
- Loading branch information
1 parent
90823c1
commit a6300ea
Showing
9 changed files
with
326 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, () => {}); | ||
}); | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.