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-undici): respect requireParent flag when INVALID_SPAN_CONTEXT is used #2273

Merged
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
5 changes: 4 additions & 1 deletion plugins/node/instrumentation-undici/src/undici.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ export class UndiciInstrumentation extends InstrumentationBase {
const currentSpan = trace.getSpan(activeCtx);
let span: Span;

if (config.requireParentforSpans && !currentSpan) {
if (
config.requireParentforSpans &&
(!currentSpan || !trace.isSpanContextValid(currentSpan.spanContext()))
) {
span = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
} else {
span = this.tracer.startSpan(
Expand Down
25 changes: 25 additions & 0 deletions plugins/node/instrumentation-undici/test/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as assert from 'assert';
import { Writable } from 'stream';

import {
INVALID_SPAN_CONTEXT,
SpanKind,
SpanStatusCode,
context,
Expand Down Expand Up @@ -626,6 +627,30 @@ describe('UndiciInstrumentation `undici` tests', function () {
assert.strictEqual(spans.length, 0, 'no spans are created');
});

it('should not create spans with INVALID_SPAN_CONTEXT parent if required in configuration', async function () {
let spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);

instrumentation.setConfig({
enabled: true,
requireParentforSpans: true,
});

const root = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
await context.with(trace.setSpan(context.active(), root), async () => {
const requestUrl = `${protocol}://${hostname}:${mockServer.port}/?query=test`;
const response = await undici.request(requestUrl);
await consumeResponseBody(response.body);
assert.ok(
response.headers['propagation-error'] == null,
'propagation is set for instrumented requests'
);
});

spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0, 'no spans are created');
});

it('should create spans with parent if required in configuration', function (done) {
let spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);
Expand Down