Skip to content

Instantly share code, notes, and snippets.

@danielkhan
Created October 19, 2018 22:29
Show Gist options
  • Save danielkhan/fcb9bc539ead946b2f6f60c12d2e9123 to your computer and use it in GitHub Desktop.
Save danielkhan/fcb9bc539ead946b2f6f60c12d2e9123 to your computer and use it in GitHub Desktop.
const {
Tracer,
BatchRecorder,
ExplicitContext,
jsonEncoder: { JSON_V2 }
} = require('zipkin');
const zipkinMiddleware = require('zipkin-instrumentation-express').expressMiddleware;
const wrapRequest = require('zipkin-instrumentation-request');
const request = require('request');
const CLSContext = require('zipkin-context-cls');
const { HttpLogger } = require('zipkin-transport-http');
module.exports = (localServiceName) => {
const tracer = new Tracer({
ctxImpl: new CLSContext('zipkin'),
recorder: new BatchRecorder({
logger: new HttpLogger({
endpoint: 'http://localhost:9411/api/v2/spans',
jsonEncoder: JSON_V2
})
}),
localServiceName,
});
return {
middleware: () => zipkinMiddleware({ tracer }),
request: (remoteServiceName) => wrapRequest(request, { tracer, remoteServiceName }),
}
}
/*
// Usage:
// Express middleware:
const zipkin = require('./agent/zipkin')('<Local Service Name>');
const app = express();
app.use(zipkin.middleware());
// To get a wrapped request object:
const request = zipkin.request('<Remote Service Name>');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment