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 14, 2024
1 parent 812c774 commit a89e6b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to experimental packages in this project will be documented
### :bug: (Bug Fix)

* fix(sdk-node): register context manager if no tracer options are provided [#4781](https://github.com/open-telemetry/opentelemetry-js/pull/4781) @pichlermarc
* 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)

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

if (requireParent === true && currentSpan === undefined) {
if (requireParent === true && !currentSpan?.isRecording()) {
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
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a89e6b3

Please sign in to comment.