Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function hemeraOpentracing(hemera, opts, done) {
if (!opts.serviceName) {
return done(new Error('serviceName is required'))
}
const sampler = new Jaeger.RateLimitingSampler(opts.jaeger.maxTracesPerSecond)
const reporter = new Jaeger.RemoteReporter(new UDPSender())
const tracer = new Jaeger.Tracer(opts.serviceName, reporter, sampler, opts)
hemera.on('serverPreRequest', function(ctx) {
const span = tracer.startSpan('serverRequest')
span.log({ event: 'serverPreRequest' })
span.setTag(Opentracing.Tags.PEER_SERVICE, ctx._topic)
// span.setTag(Opentracing.Tags.SPAN_KIND_RPC_SERVER)
// span.setTag(Opentracing.Tags.SPAN_KIND_MESSAGING_CONSUMER)
ctx._jaegerServerTrace = span
})
hemera.on('serverPreResponse', function(ctx) {
const childSpan = tracer.startSpan('serverRequest', {
childOf: ctx._jaegerServerTrace
})
const express = require('express')
const opentracing = require('opentracing')
const jaeger = require('jaeger-client')
const request = require('request-promise-native')
// Using UDP
const UDPSender = require('jaeger-client/dist/src/reporters/udp_sender').default
const app = express()
const port = process.env.PORT || 3003
// Tracer
const udpSender = new UDPSender()
const reporter = new jaeger.RemoteReporter(udpSender)
const sampler = new jaeger.RateLimitingSampler(1)
const tracer = new jaeger.Tracer('service 2', reporter, sampler)
app.get('/site/:site', (req, res) => {
const spanContext = jaeger.SpanContext.fromString(req.headers['trace-span-context'])
const span = tracer.startSpan('http_server', {
childOf: spanContext
})
span.setTag(opentracing.Tags.HTTP_URL, `${req.protocol}://${req.hostname}${req.originalUrl}`)
span.setTag(opentracing.Tags.HTTP_METHOD, req.method)
span.setTag('request_path', req.route.path)
span.setTag('request_id', req.headers['x-request-id'])
const requestOptions = {
headers: { 'trace-span-context': span.context().toString() },
json: true
}
const express = require('express')
const request = require('request-promise-native')
const opentracing = require('opentracing')
const jaeger = require('jaeger-client')
// Using UDP
const UDPSender = require('jaeger-client/dist/src/reporters/udp_sender').default
const app = express()
const port = process.env.PORT || 3002
// Tracer
const udpSender = new UDPSender()
const reporter = new jaeger.RemoteReporter(udpSender)
const sampler = new jaeger.RateLimitingSampler(1)
const tracer = new jaeger.Tracer('service 1', reporter, sampler)
app.get('/', (req, res) => {
const span = tracer.startSpan('http_server')
span.setTag(opentracing.Tags.HTTP_URL, `${req.protocol}://${req.hostname}${req.originalUrl}`)
span.setTag(opentracing.Tags.HTTP_METHOD, req.method)
span.setTag('request_path', req.route.path)
span.setTag('request_id', req.headers['x-request-id'])
const requestOptions = {
headers: { 'trace-span-context': span.context().toString() },
json: true
}
Promise.all([
request(Object.assign({ uri: 'http://localhost:3003/site/somkiat' }, requestOptions)),
static create() {
let jaegerAddress = DynamicConfiguration.getPropertyValue("jaeger");
if (jaegerAddress) {
if (!jaegerAddress.startsWith("http://")) {
jaegerAddress = "http://" + jaegerAddress;
}
if (!/:[0-9]+/.test(jaegerAddress)) {
jaegerAddress = jaegerAddress + ':9411';
}
const sender = new UDPSender();
const tracer = new jaeger.Tracer(Service.fullServiceName,
new jaeger.RemoteReporter(sender),
new jaeger.RateLimitingSampler(1));
return new JaegerInstrumentation(tracer);
}
return null;
}
getSampler(serviceName) {
if (isFunction(this.settings.sampler))
return this.settings.sampler;
if (this.settings.sampler.type == "RateLimiting")
return new Jaeger.RateLimitingSampler(this.settings.sampler.options.maxTracesPerSecond, this.settings.sampler.options.initBalance);
if (this.settings.sampler.type == "Probabilistic")
return new Jaeger.ProbabilisticSampler(this.settings.sampler.options.samplingRate);
if (this.settings.sampler.type == "GuaranteedThroughput")
return new GuaranteedThroughputSampler(this.settings.sampler.options.lowerBound, this.settings.sampler.options.samplingRate);
if (this.settings.sampler.type == "RemoteControlled")
return new RemoteControlledSampler(serviceName, this.settings.sampler.options);
return new Jaeger.ConstSampler(this.settings.sampler.options && this.settings.sampler.options.decision != null ? this.settings.sampler.options.decision : 1);
},