Path: redb-route/redb.Route.AzureServiceBus/AzureServiceBusTrace.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.AzureServiceBus/AzureServiceBusTrace.cs
----
using System.Diagnostics;
using Azure.Messaging.ServiceBus;
using redb.Route.Core;
using redb.Route.Telemetry;
namespace redb.Route.AzureServiceBus;
///
/// The trace carrier of a Service Bus message: its application properties. Azure names the W3C
/// traceparent field Diagnostic-Id (same value format), so that one name is mapped; tracestate
/// and baggage go under their own names. One explicit mapping, read and written the same way.
///
internal static class AzureServiceBusTrace
{
/// The application property Azure keeps the traceparent value in.
internal const string DiagnosticId = "Diagnostic-Id";
private const string TraceParent = "traceparent";
private static string PropertyName(string field)
=> string.Equals(field, TraceParent, StringComparison.Ordinal) ? DiagnosticId : field;
/// Reads one trace field from the application properties of a received message.
internal static string? Read(IReadOnlyDictionary properties, string field)
=> properties.TryGetValue(PropertyName(field), out var value) ? value?.ToString() : null;
/// Writes one trace field to the application properties of an outgoing message, replacing a copied value.
internal static void Write(IDictionary properties, string field, string value)
=> properties[PropertyName(field)] = value;
///
/// Marks failed by , unless it is our own stop: a cancellation
/// by (the processor stopping, the caller's token). Any other cancellation, a timeout inside
/// the route for one, is a failure.
///
internal static void RecordFailure(Activity? span, Exception exception, CancellationToken ours)
{
if (exception is OperationCanceledException && ours.IsCancellationRequested)
return;
span.RecordFailure(exception);
}
///
/// Opens the receive span of : a child of the sender's context, or a root when the
/// message carries none.
///
internal static TransportSpan StartReceiveSpan(AzureServiceBusEndpoint endpoint, ServiceBusReceivedMessage message)
{
var span = RouteTelemetryExtensions.StartConsumerSpan(
(endpoint.Component as ComponentBase)?.Context,
$"{endpoint.EntityName} receive", ActivityKind.Consumer,
"messaging.system", "azureservicebus", endpoint.Uri.NormalizedKey,
message.ApplicationProperties, Read,
destination: endpoint.EntityName, operation: "receive");
if (span.Activity is { IsAllDataRequested: true } activity && !string.IsNullOrEmpty(message.MessageId))
activity.SetTag("messaging.message.id", message.MessageId);
return span;
}
}