Skip to content

Commit

Permalink
fix(instr-undici): respect requireParent flag when INVALID_SPAN_CONTE…
Browse files Browse the repository at this point in the history
…XT is used (#2273)
  • Loading branch information
reberhardt7 authored Jun 19, 2024
1 parent 2dc2f72 commit b08f01f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

0 comments on commit b08f01f

Please sign in to comment.