Skip to content

Commit 658e198

Browse files
committed
fix(instr-http): respect requireParent flag when INVALID_SPAN_CONTEXT is used
1 parent 0608f40 commit 658e198

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ All notable changes to experimental packages in this project will be documented
1111

1212
### :bug: (Bug Fix)
1313

14+
* fix(instrumentation-http): respect requireParent flag when INVALID_SPAN_CONTEXT is used [#4788](https://github.com/open-telemetry/opentelemetry-js/pull/4788) @reberhardt7
15+
1416
### :books: (Refine Doc)
1517

1618
### :house: (Internal)

experimental/packages/opentelemetry-instrumentation-http/src/http.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,10 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
765765
let span: Span;
766766
const currentSpan = trace.getSpan(ctx);
767767

768-
if (requireParent === true && currentSpan === undefined) {
768+
if (
769+
requireParent === true &&
770+
(!currentSpan || !trace.isSpanContextValid(currentSpan.spanContext()))
771+
) {
769772
span = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
770773
} else if (requireParent === true && currentSpan?.spanContext().isRemote) {
771774
span = currentSpan;

experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
trace,
2424
SpanAttributes,
2525
DiagConsoleLogger,
26+
INVALID_SPAN_CONTEXT,
2627
} from '@opentelemetry/api';
2728
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2829
import {
@@ -1043,6 +1044,24 @@ describe('HttpInstrumentation', () => {
10431044
);
10441045
});
10451046

1047+
it('should not trace with INVALID_SPAN_CONTEXT parent with requireParent options enabled', async () => {
1048+
instrumentation.disable();
1049+
instrumentation.setConfig({
1050+
requireParentforIncomingSpans: true,
1051+
requireParentforOutgoingSpans: true,
1052+
});
1053+
instrumentation.enable();
1054+
const root = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
1055+
await context.with(trace.setSpan(context.active(), root), async () => {
1056+
const testPath = '/test/test';
1057+
await httpRequest.get(
1058+
`${protocol}://${hostname}:${serverPort}${testPath}`
1059+
);
1060+
});
1061+
const spans = memoryExporter.getFinishedSpans();
1062+
assert.strictEqual(spans.length, 0);
1063+
});
1064+
10461065
it('should trace with parent with both requireParent options enabled', done => {
10471066
instrumentation.disable();
10481067
instrumentation.setConfig({

0 commit comments

Comments
 (0)