Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instr-http): respect requireParent flag when INVALID_SPAN_CONTEXT is used #4788

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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
Loading