Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
startSpan(name: string, options: types.SpanOptions = {}): types.Span {
const parentContext = this._getParentSpanContext(options.parent);
// make sampling decision
const samplingDecision = this._sampler.shouldSample(parentContext);
const spanId = randomSpanId();
let traceId;
let traceState;
if (!parentContext || !isValid(parentContext)) {
// New root span.
traceId = randomTraceId();
} else {
// New child span.
traceId = parentContext.traceId;
traceState = parentContext.traceState;
}
const traceFlags = samplingDecision
? TraceFlags.SAMPLED
: TraceFlags.UNSAMPLED;
const spanContext = { traceId, spanId, traceFlags, traceState };
const recordEvents = options.isRecording || false;
if (!recordEvents && !samplingDecision) {
this.logger.debug('Sampling is off, starting no recording span');
return new NoRecordingSpan(spanContext);
}
const method = request.method || 'GET';
plugin._logger.debug('%s plugin incomingRequest', plugin.moduleName);
if (Utils.isIgnored(pathname, plugin._config.ignoreIncomingPaths)) {
return original.apply(this, [event, ...args]);
}
const propagation = plugin._tracer.getHttpTextFormat();
const headers = request.headers;
const spanOptions: SpanOptions = {
kind: SpanKind.SERVER,
};
const spanContext = propagation.extract(Format.HTTP, headers);
if (spanContext && isValid(spanContext)) {
spanOptions.parent = spanContext;
}
const span = plugin._startHttpSpan(`${method} ${pathname}`, spanOptions);
return plugin._tracer.withSpan(span, () => {
plugin._tracer.bind(request);
plugin._tracer.bind(response);
// Wraps end (inspired by:
// https://github.com/GoogleCloudPlatform/cloud-trace-nodejs/blob/master/src/plugins/plugin-connect.ts#L75)
const originalEnd = response.end;
response.end = function(
this: ServerResponse,
...args: ResponseEndArgs
) {