Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2

### :bug: Bug Fixes

* fix(instrumentation-http): respect requireParent flag when INVALID_SPAN_CONTEXT is used [#4788](https://github.com/open-telemetry/opentelemetry-js/pull/4788) @reberhardt7

### :books: Documentation

### :house: Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,10 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
let span: Span;
const currentSpan = trace.getSpan(ctx);

if (requireParent === true && currentSpan === undefined) {
if (
requireParent === true &&
(!currentSpan || !trace.isSpanContextValid(currentSpan.spanContext()))
) {
span = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
} else if (requireParent === true && currentSpan?.spanContext().isRemote) {
span = currentSpan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
trace,
Attributes,
DiagConsoleLogger,
INVALID_SPAN_CONTEXT,
} from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
Expand Down Expand Up @@ -1440,6 +1441,24 @@ describe('HttpInstrumentation', () => {
);
});

it('should not trace with INVALID_SPAN_CONTEXT parent with requireParent options enabled', async () => {
instrumentation.disable();
instrumentation.setConfig({
requireParentforIncomingSpans: true,
requireParentforOutgoingSpans: true,
});
instrumentation.enable();
const root = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
await context.with(trace.setSpan(context.active(), root), async () => {
const testPath = '/test/test';
await httpRequest.get(
`${protocol}://${hostname}:${serverPort}${testPath}`
);
});
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);
});

it('should trace with parent with both requireParent options enabled', done => {
instrumentation.disable();
instrumentation.setConfig({
Expand Down
Loading