Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const wireCtx =
tracer.extract(opentracing.FORMAT_HTTP_HEADERS, req.headers);
const pathname = url.parse(req.url).pathname;
const span = tracer.startSpan(pathname, { childOf: wireCtx });
span.logEvent('request_received');
// include some useful tags on the trace
span.setTag('http.method', req.method);
span.setTag('span.kind', 'server');
span.setTag('http.url', req.url);
span.setTag('component', 'express');
// include trace ID in headers so that we can debug slow requests we see in
// the browser by looking up the trace ID found in response headers
const responseHeaders = {};
tracer.inject(span, opentracing.FORMAT_TEXT_MAP, responseHeaders);
Object.keys(responseHeaders)
.forEach(key => res.setHeader(key, responseHeaders[key]));
// add the span to the request object for handlers to use
Object.assign(req, { span });
// finalize the span when the response is completed
const endOld = res.end;
res.end = function() {
span.logEvent('request_finished');
// Route matching often happens after the middleware is run. Try changing
// the operation name
// to the route matcher.
const opName = (req.route && req.route.path) || pathname;
span.setOperationName(opName);
span.setTag('http.status_code', res.statusCode);
async exec (context, name, func) {
let err, result
let startTime, latency
// https://opentracing-javascript.surge.sh/interfaces/spanoptions.html
// https://github.com/opentracing/specification/blob/master/semantic_conventions.md
const span = this.tracer.startSpan(name, { childOf: context.span }) // { childOf: context.span.context() }
span.setTag(opentracing.Tags.SPAN_KIND, 'requester')
span.setTag(opentracing.Tags.PEER_SERVICE, 'asset-service')
span.setTag(opentracing.Tags.PEER_ADDRESS, this.conn.currentServer.url.host)
const carrier = {}
this.tracer.inject(span, opentracing.FORMAT_TEXT_MAP, carrier)
const spanContext = JSON.stringify(carrier)
// Core functionality
try {
startTime = Date.now()
result = await func(spanContext)
} catch (e) {
err = e
this.logger.error(err)
} finally {
latency = (Date.now() - startTime) / 1000
}
// Metrics
const labelValues = { op: name, success: err ? 'false' : 'true' }
this.histogram.observe(labelValues, latency)
return (req, res, next) => {
const wireCtx = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, req.headers);
const pathname = url.parse(req.url).pathname;
const span = tracer.startSpan(pathname, {childOf: wireCtx});
span.logEvent("request_received");
// include some useful tags on the trace
span.setTag("http.method", req.method);
span.setTag("span.kind", "server");
span.setTag("http.url", req.url);
// include trace ID in headers so that we can debug slow requests we see in
// the browser by looking up the trace ID found in response headers
const responseHeaders = {};
tracer.inject(span, opentracing.FORMAT_TEXT_MAP, responseHeaders);
Object.keys(responseHeaders).forEach(key => res.setHeader(key, responseHeaders[key]));
// add the span to the request object for handlers to use
Object.assign(req, {span});
// finalize the span when the response is completed
const finishSpan = () => {
span.logEvent("request_finished");
// Route matching often happens after the middleware is run. Try changing the operation name
// to the route matcher.
const opName = (req.route && req.route.path) || pathname;
span.setOperationName(opName);
span.setTag("http.status_code", res.statusCode);
if (res.statusCode >= 500) {
span.setTag("error", true);
span.setTag("sampling.priority", 1);
it('should not sample or finalize span after serializing context', () => {
let span = tracer.startSpan('opName');
let carrier = {};
tracer.inject(span.context(), opentracing.FORMAT_TEXT_MAP, carrier);
assert.isDefined(carrier['uber-trace-id']);
assert.isFalse(span._spanContext.isSampled(), 'sampled');
assert.isFalse(span._spanContext.samplingFinalized, 'finalized');
});
function extractSpanContext(tracer, httpHeaders) {
return tracer.extract(opentracing.FORMAT_TEXT_MAP, httpHeaders);
}
import * as opentracing from "opentracing";
const tracingConfig: jaegerClient.TracingConfig = {};
const tracingOptions: jaegerClient.TracingOptions = {};
const tracer = jaegerClient.initTracer(tracingConfig, tracingOptions);
jaegerClient.initTracerFromEnv(tracingConfig, tracingOptions);
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);
expect(function () {
tracer.extract(ot.FORMAT_TEXT_MAP, textCarrier)
}).not.toThrow(Error)
expect(function () {
function createCarrier(tracer, span) {
const carrier = {};
tracer.inject(span.context(), opentracing.FORMAT_TEXT_MAP, carrier);
return carrier;
}
this._metrics
);
this._debugThrottler = options.debugThrottler || new DefaultThrottler(false);
this._injectors = {};
this._extractors = {};
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),
'use strict';
var opentracing = require('opentracing');
var constants = require('../constants');
var Span = require('./Span');
var baggageKeyPrefix = 'x-instana-b-';
var valueEncoders = {};
valueEncoders[opentracing.FORMAT_TEXT_MAP] = identity;
valueEncoders[opentracing.FORMAT_HTTP_HEADERS] = encodeURIComponent;
var valueDecoders = {};
valueDecoders[opentracing.FORMAT_TEXT_MAP] = identity;
valueDecoders[opentracing.FORMAT_HTTP_HEADERS] = decodeURIComponent;
function Tracer(isActive) {
opentracing.Tracer.apply(this, arguments);
this._isActive = isActive;
}
module.exports = Tracer;
Tracer.prototype = Object.create(opentracing.Tracer.prototype);
Tracer.prototype._startSpan = function _startSpan(name, fields) {
return new Span(this, name, fields);