Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (req, res, next) => {
// https://opentracing-javascript.surge.sh
const parentSpanContext = options.tracer.extract(opentracing.FORMAT_HTTP_HEADERS, req.headers)
const span = options.tracer.startSpan('http-request', { childOf: parentSpanContext })
req.context = { span }
onFinished(res, (err, res) => {
if (err) {
span.log({ message: err.message })
}
// https://github.com/opentracing/specification/blob/master/semantic_conventions.md
span.setTag('http.version', req.httpVersion)
span.setTag(opentracing.Tags.HTTP_METHOD, req.method)
span.setTag(opentracing.Tags.HTTP_URL, req.originalUrl)
span.setTag(opentracing.Tags.HTTP_STATUS_CODE, res.statusCode)
span.finish()
})
Tracer.prototype._inject = function _inject(spanContext, format, carrier) {
if (format !== opentracing.FORMAT_TEXT_MAP && format !== opentracing.FORMAT_HTTP_HEADERS) {
// Only text formats supported right now
return;
}
if (carrier == null || spanContext == null) {
// For some reason this case is not handled by the OpenTracing abstraction.
return;
}
carrier[constants.spanIdHeaderNameLowerCase] = spanContext.s;
carrier[constants.traceIdHeaderNameLowerCase] = spanContext.t;
carrier[constants.traceLevelHeaderNameLowerCase] = String(spanContext.samplingPriority);
var valueEncoder = valueEncoders[format];
Object.keys(spanContext.baggage).forEach(function(baggageKey) {
carrier[baggageKeyPrefix + baggageKey] = valueEncoder(spanContext.baggage[baggageKey]);
let codecOptions = {
contextKey: options.contextKey || null,
baggagePrefix: options.baggagePrefix || null,
urlEncoding: false,
metrics: this._metrics,
};
let textCodec = new TextMapCodec(codecOptions);
this.registerInjector(opentracing.FORMAT_TEXT_MAP, textCodec);
this.registerExtractor(opentracing.FORMAT_TEXT_MAP, textCodec);
codecOptions.urlEncoding = true;
let httpCodec = new TextMapCodec(codecOptions);
this.registerInjector(opentracing.FORMAT_HTTP_HEADERS, httpCodec);
this.registerExtractor(opentracing.FORMAT_HTTP_HEADERS, httpCodec);
let binaryCodec = new BinaryCodec();
this.registerInjector(opentracing.FORMAT_BINARY, binaryCodec);
this.registerExtractor(opentracing.FORMAT_BINARY, binaryCodec);
const uuid = uuidv4();
this._tags[constants.TRACER_CLIENT_ID_TAG_KEY] = uuid;
this._process = {
serviceName: serviceName,
tags: Utils.convertObjectToTags(this._tags),
uuid: uuid,
};
this._debugThrottler.setProcess(this._process);
// TODO update reporter to implement ProcessSetter
this._reporter.setProcess(this._process.serviceName, this._process.tags);
it('should support inject of http headers format', () => {
TextMapPropagator.returns(propagator)
tracer = new Tracer({ service: 'service' })
tracer.inject(spanContext, opentracing.FORMAT_HTTP_HEADERS, carrier)
expect(propagator.inject).to.have.been.calledWith(spanContext, carrier)
})
it('set debug flag when debug-id-header is received', () => {
let metrics = new Metrics(new LocalMetricFactory());
let tracer = new Tracer('test-tracer', new InMemoryReporter(), new ConstSampler(false), {
metrics: metrics,
});
let headers = {};
headers[constants.JAEGER_DEBUG_HEADER] = encodeURIComponent('value1');
let context = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, headers);
assert.isOk(context.isDebugIDContainerOnly());
assert.equal(context.debugId, 'value1');
let span = tracer.startSpan('root', { childOf: context });
assert.isNotOk(span.context().parentId);
assert.isOk(span.context().traceId !== 0);
assert.isOk(span.context().isSampled());
assert.isOk(span.context().isDebug());
let tagFound = false;
for (let i = 0; i < span._tags.length; i++) {
let tag = span._tags[i];
if (tag.key === constants.JAEGER_DEBUG_HEADER && span._tags[i].value === 'value1') {
tagFound = true;
}
const metrics = new jaegerClient.PrometheusMetricsFactory(promClient, "foo");
const textCodec = new jaegerClient.TextMapCodec({
contextKey: 'trace-id',
baggagePrefix: 'baggage-',
urlEncoding: false,
});
tracer.registerInjector(opentracing.FORMAT_TEXT_MAP, textCodec);
tracer.registerExtractor(opentracing.FORMAT_TEXT_MAP, textCodec);
const zipkinB3TextMapCodec = new jaegerClient.ZipkinB3TextMapCodec({
baggagePrefix: 'baggage-',
urlEncoding: false,
});
tracer.registerInjector(opentracing.FORMAT_HTTP_HEADERS, zipkinB3TextMapCodec);
tracer.registerExtractor(opentracing.FORMAT_HTTP_HEADERS, zipkinB3TextMapCodec);
let assertByFormat = format => {
let carrier = {};
tracer.inject(savedContext, format, carrier);
let extractedContext = tracer.extract(format, carrier);
assert.deepEqual(savedContext.traceId, extractedContext.traceId);
assert.deepEqual(savedContext.spanId, extractedContext.spanId);
assert.deepEqual(savedContext.parentId, extractedContext.parentId);
assert.equal(savedContext.flags, extractedContext.flags);
assert.equal(savedContext.baggage[keyOne], extractedContext.baggage[keyOne]);
assert.equal(savedContext.baggage[keyTwo], extractedContext.baggage[keyTwo]);
};
assertByFormat(opentracing.FORMAT_TEXT_MAP);
assertByFormat(opentracing.FORMAT_HTTP_HEADERS);
});
_inject(spanContext, format, carrier = {}) {
switch (format) {
case opentracing.FORMAT_HTTP_HEADERS:
carrier.nodetracing = encodeURI(JSON.stringify(spanContext))
break
case 'FORMAT_GRPC_METADATA':
carrier.add('nodetracing', encodeURI(JSON.stringify(spanContext)))
break
default:
break
}
return carrier
}
_extract(format, carrier) {
external_interface.on('proxyReq', function (proxyReq, req, res, options) {
const rootSpan = tracer.startSpan('proxyReq');
rootSpan.setTag("event", "proxyReq");
rootSpan.setTag("http.method", req.method);
rootSpan.setTag("http.url", req.url);
let headersCarrier = {};
tracer.inject(rootSpan, opentracing.FORMAT_HTTP_HEADERS, headersCarrier);
const headersCarrierKeys = Object.keys(headersCarrier);
for (const key of headersCarrierKeys) {
proxyReq.setHeader(key, headersCarrier[key]);
}
logger.log('debug', 'Host headers in request', {
host: req.headers.host,
x_fowarded_host: req.headers['x-forwarded-host']
});
if (req.headers.host) {
logger.log('debug', 'Host headers in request after setHeader', {
host: req.headers.host,
x_fowarded_host: req.headers['x-forwarded-host']
});
} else {
let ip = req.headers['x-forwarded-for'] ||
var context = constTracer.startSpan('sampled-context').context();
var httpCarrier = {};
constTracer.inject(span.context(), opentracing.FORMAT_HTTP_HEADERS, httpCarrier);
var textCarrier = {};
constTracer.inject(span.context(), opentracing.FORMAT_TEXT_MAP, textCarrier);
var probabilisticTracer = new Tracer(
'probabilistic-tracer',
new InMemoryReporter(),
new ProbabilisticSampler(0.01)
);
var params = [
{
format: opentracing.FORMAT_HTTP_HEADERS,
tracer: constTracer,
carrier: httpCarrier,
description: ' with const tracer and http carrier',
},
{
format: opentracing.FORMAT_TEXT_MAP,
tracer: constTracer,
carrier: textCarrier,
description: ' with const tracer and text carrier',
},
{
format: opentracing.FORMAT_HTTP_HEADERS,
tracer: probabilisticTracer,
carrier: httpCarrier,
description: ' with probabilistic tracer and http carrier',
},