From 4b5b023bac7efbe408d43f41816ff9431d5698e5 Mon Sep 17 00:00:00 2001 From: Valentin Marchaud Date: Tue, 13 Oct 2020 18:31:42 +0200 Subject: [PATCH] fix(http): do not set outgoing http span as active in the context #1479 (#1546) --- .../opentelemetry-plugin-http/src/http.ts | 30 +++++++++++-------- .../test/functionals/http-enable.test.ts | 9 ++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 96c87b1375..cceda23e45 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -24,6 +24,7 @@ import { SpanContext, TraceFlags, getExtractedSpanContext, + setActiveSpan, } from '@opentelemetry/api'; import { BasePlugin, NoRecordingSpan } from '@opentelemetry/core'; import type { @@ -410,21 +411,24 @@ export class HttpPlugin extends BasePlugin { kind: SpanKind.CLIENT, }; const span = plugin._startHttpSpan(operationName, spanOptions); + if (!optionsParsed.headers) { + optionsParsed.headers = {}; + } + propagation.inject( + optionsParsed.headers, + undefined, + setActiveSpan(context.active(), span) + ); - return plugin._tracer.withSpan(span, () => { - if (!optionsParsed.headers) optionsParsed.headers = {}; - propagation.inject(optionsParsed.headers); - - const request: ClientRequest = plugin._safeExecute( - span, - () => original.apply(this, [optionsParsed, ...args]), - true - ); + const request: ClientRequest = plugin._safeExecute( + span, + () => original.apply(this, [optionsParsed, ...args]), + true + ); - plugin._logger.debug('%s plugin outgoingRequest', plugin.moduleName); - plugin._tracer.bind(request); - return plugin._traceClientRequest(request, optionsParsed, span); - }); + plugin._logger.debug('%s plugin outgoingRequest', plugin.moduleName); + plugin._tracer.bind(request); + return plugin._traceClientRequest(request, optionsParsed, span); }; } diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts index b78f504304..b08f868e8e 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts @@ -19,6 +19,7 @@ import { propagation, Span as ISpan, SpanKind, + getActiveSpan, } from '@opentelemetry/api'; import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; @@ -709,6 +710,14 @@ describe('HttpPlugin', () => { SpanKind.CLIENT ); }); + + it('should not set span as active in context for outgoing request', done => { + assert.deepStrictEqual(getActiveSpan(context.active()), undefined); + http.get(`${protocol}://${hostname}:${serverPort}/test`, res => { + assert.deepStrictEqual(getActiveSpan(context.active()), undefined); + done(); + }); + }); }); describe('with require parent span', () => {