diff --git a/google/cloud/pubsub/internal/subscriber_tracing_connection.cc b/google/cloud/pubsub/internal/subscriber_tracing_connection.cc index 2c06012177118..0639380085c45 100644 --- a/google/cloud/pubsub/internal/subscriber_tracing_connection.cc +++ b/google/cloud/pubsub/internal/subscriber_tracing_connection.cc @@ -68,10 +68,13 @@ StatusOr EndPullSpan( /*sc::kMessagingMessageEnvelopeSize=*/"messaging.message.envelope.size", static_cast(MessageSize(message))); + auto current = opentelemetry::context::RuntimeContext::GetCurrent(); auto context = ExtractTraceContext(message, *propagator); auto producer_span_context = opentelemetry::trace::GetSpan(context)->GetContext(); - if (producer_span_context.IsSampled() && producer_span_context.IsValid()) { + // If the contexts are equal, the message span was invalid. + if (!(current == context) && producer_span_context.IsSampled() && + producer_span_context.IsValid()) { #if OPENTELEMETRY_ABI_VERSION_NO >= 2 span->AddLink(producer_span_context, {}); #else