Skip to content

Commit

Permalink
Fix span linking for Azure ServiceBus (#2474)
Browse files Browse the repository at this point in the history
Align span ID with activity's span ID in ServiceBus listener

The code changes in the `AzureMessagingServiceBusDiagnosticListener.cs`
file within the `Elastic.Apm.Azure.ServiceBus` namespace involve
modifications to how spans are started for message actions.
Specifically, the `StartSpanInternal` method now includes an explicit
`id` parameter set to `activity.SpanId.ToString()`. This change ensures
that the span ID matches the activity's span ID, which is crucial for
correctly linking the consuming span to the producer. This adjustment is
necessary because the Azure SDK automatically attaches the
`diagnostic-id` and `traceparent` to the message, and proper span
linking on the receiver end depends on this alignment.
  • Loading branch information
stevejgordon authored Nov 8, 2024
1 parent df7825d commit 0f177ce
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ private void OnMessageStart(KeyValuePair<string, object> kv, string action)

_onMessageCurrent = currentSegment switch
{
Span span => span.StartSpanInternal(name, ApiConstants.TypeMessaging, ServiceBus.SubType, action.ToLowerInvariant()),
// NOTE: We explicity set the SpanId here to match the Activity (value of the payload) to ensure that span linking on the
// receiver works as expected. The Azure SDK attaches the diagnostic-id and traceparent to the message automatically.
// On the receiving end, we need to be able to correctly link the consuming span to the producer.

Span span => span.StartSpanInternal(name, ApiConstants.TypeMessaging, ServiceBus.SubType, action.ToLowerInvariant(), id: activity.SpanId.ToString()),
Transaction transaction => transaction.StartSpanInternal(name, ApiConstants.TypeMessaging, ServiceBus.SubType,
action.ToLowerInvariant()),
action.ToLowerInvariant(), id: activity.SpanId.ToString()),
_ => _onMessageCurrent
};
}
Expand Down

0 comments on commit 0f177ce

Please sign in to comment.