diff --git a/src/telemetry-tracing.ts b/src/telemetry-tracing.ts index e6f44b0d2..cb422382d 100644 --- a/src/telemetry-tracing.ts +++ b/src/telemetry-tracing.ts @@ -504,7 +504,11 @@ export class PubsubSpans { if (span) { // Also attempt to link from message spans back to the publish RPC span. messages.forEach(m => { - if (m.parentSpan && isSampled(m.parentSpan)) { + if ( + m.parentSpan && + isSampled(m.parentSpan) && + typeof m.parentSpan?.addLink === 'function' + ) { m.parentSpan.addLink({context: span.spanContext()}); } }); @@ -549,7 +553,7 @@ export class PubsubSpans { if (span) { // Also attempt to link from the subscribe span(s) back to the publish RPC span. messageSpans.forEach(m => { - if (m && isSampled(m)) { + if (m && isSampled(m) && typeof m?.addLink === 'function') { m.addLink({context: span.spanContext()}); } }); @@ -597,7 +601,7 @@ export class PubsubSpans { if (span) { // Also attempt to link from the subscribe span(s) back to the publish RPC span. messageSpans.forEach(m => { - if (m && isSampled(m)) { + if (m && isSampled(m) && typeof m?.addLink === 'function') { m.addLink({context: span.spanContext()}); } }); diff --git a/test/telemetry-tracing.ts b/test/telemetry-tracing.ts index 02715a304..8354833ca 100644 --- a/test/telemetry-tracing.ts +++ b/test/telemetry-tracing.ts @@ -366,6 +366,7 @@ describe('OpenTelemetryTracer', () => { topicName, 'test', ) as trace.Span; + const parentSpanLink = sinon.spy(span, 'addLink'); message.parentSpan = span; span.end(); @@ -374,7 +375,6 @@ describe('OpenTelemetryTracer', () => { topicName, 'test', ); - publishSpan?.end(); const spans = exporter.getFinishedSpans(); const publishReadSpan = spans.pop(); @@ -387,6 +387,7 @@ describe('OpenTelemetryTracer', () => { ); assert.strictEqual(publishReadSpan.links.length, 1); assert.strictEqual(childReadSpan.links.length, 1); + assert.strictEqual(parentSpanLink.callCount, 1); }); }); });