Skip to content

Commit 4b5b023

Browse files
authored
fix(http): do not set outgoing http span as active in the context #1479 (#1546)
1 parent bdd88f5 commit 4b5b023

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

packages/opentelemetry-plugin-http/src/http.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
SpanContext,
2525
TraceFlags,
2626
getExtractedSpanContext,
27+
setActiveSpan,
2728
} from '@opentelemetry/api';
2829
import { BasePlugin, NoRecordingSpan } from '@opentelemetry/core';
2930
import type {
@@ -410,21 +411,24 @@ export class HttpPlugin extends BasePlugin<Http> {
410411
kind: SpanKind.CLIENT,
411412
};
412413
const span = plugin._startHttpSpan(operationName, spanOptions);
414+
if (!optionsParsed.headers) {
415+
optionsParsed.headers = {};
416+
}
417+
propagation.inject(
418+
optionsParsed.headers,
419+
undefined,
420+
setActiveSpan(context.active(), span)
421+
);
413422

414-
return plugin._tracer.withSpan(span, () => {
415-
if (!optionsParsed.headers) optionsParsed.headers = {};
416-
propagation.inject(optionsParsed.headers);
417-
418-
const request: ClientRequest = plugin._safeExecute(
419-
span,
420-
() => original.apply(this, [optionsParsed, ...args]),
421-
true
422-
);
423+
const request: ClientRequest = plugin._safeExecute(
424+
span,
425+
() => original.apply(this, [optionsParsed, ...args]),
426+
true
427+
);
423428

424-
plugin._logger.debug('%s plugin outgoingRequest', plugin.moduleName);
425-
plugin._tracer.bind(request);
426-
return plugin._traceClientRequest(request, optionsParsed, span);
427-
});
429+
plugin._logger.debug('%s plugin outgoingRequest', plugin.moduleName);
430+
plugin._tracer.bind(request);
431+
return plugin._traceClientRequest(request, optionsParsed, span);
428432
};
429433
}
430434

packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
propagation,
2020
Span as ISpan,
2121
SpanKind,
22+
getActiveSpan,
2223
} from '@opentelemetry/api';
2324
import { NoopLogger } from '@opentelemetry/core';
2425
import { NodeTracerProvider } from '@opentelemetry/node';
@@ -709,6 +710,14 @@ describe('HttpPlugin', () => {
709710
SpanKind.CLIENT
710711
);
711712
});
713+
714+
it('should not set span as active in context for outgoing request', done => {
715+
assert.deepStrictEqual(getActiveSpan(context.active()), undefined);
716+
http.get(`${protocol}://${hostname}:${serverPort}/test`, res => {
717+
assert.deepStrictEqual(getActiveSpan(context.active()), undefined);
718+
done();
719+
});
720+
});
712721
});
713722

714723
describe('with require parent span', () => {

0 commit comments

Comments
 (0)