Skip to content

Commit

Permalink
fix(instr-http): respect requireParent flag when INVALID_SPAN_CONTEXT…
Browse files Browse the repository at this point in the history
… is used
  • Loading branch information
reberhardt7 committed Jun 20, 2024
1 parent 0608f40 commit 658e198
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

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

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,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,
SpanAttributes,
DiagConsoleLogger,
INVALID_SPAN_CONTEXT,
} from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
Expand Down Expand Up @@ -1043,6 +1044,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

0 comments on commit 658e198

Please sign in to comment.