Skip to content

Commit 0768f20

Browse files
committed
Rebasing on latest master and removing the need for OpenTelemetry.API dependency.
1 parent c39dfd3 commit 0768f20

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

projects/RabbitMQ.Client/RabbitMQ.Client.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
6262
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
6363
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="all" />
64-
<PackageReference Include="OpenTelemetry.Api" Version="1.3.1" />
6564
<PackageReference Include="System.Memory" Version="4.5.5" />
6665
<PackageReference Include="System.Threading.Channels" Version="7.0.0" />
6766
<PackageReference Include="System.IO.Pipelines" Version="7.0.0" />

projects/RabbitMQ.Client/client/impl/ModelBase.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
using System.Threading;
4040
using System.Threading.Tasks;
4141

42-
using OpenTelemetry;
43-
using OpenTelemetry.Context.Propagation;
44-
4542
using RabbitMQ.Client.client.framing;
4643
using RabbitMQ.Client.client.impl;
4744
using RabbitMQ.Client.ConsumerDispatching;
@@ -57,7 +54,6 @@ internal abstract class ModelBase : IModel, IRecoverable
5754
///<summary>Only used to kick-start a connection open
5855
///sequence. See <see cref="Connection.Open"/> </summary>
5956
internal BlockingCell<ConnectionStartDetails> m_connectionStartCell;
60-
private static readonly TextMapPropagator Propagator = new CompositeTextMapPropagator(new TextMapPropagator[] { new TraceContextPropagator(), new BaggagePropagator() });
6157

6258
private readonly RpcContinuationQueue _continuationQueue = new RpcContinuationQueue();
6359
private readonly ManualResetEventSlim _flowControlBlock = new ManualResetEventSlim(true);
@@ -912,8 +908,13 @@ public BasicGetResult BasicGet(string queue, bool autoAck)
912908
public void BasicPublish<TProperties>(string exchange, string routingKey, in TProperties basicProperties, ReadOnlyMemory<byte> body, bool mandatory)
913909
where TProperties : IReadOnlyBasicProperties, IAmqpHeader
914910
{
915-
void InjectTraceContextIntoBasicProperties(IBasicProperties props, string key, string value)
911+
void InjectTraceContextIntoBasicProperties(object propsObj, string key, string value)
916912
{
913+
if (propsObj is not IBasicProperties props)
914+
{
915+
return;
916+
}
917+
917918
if (props.Headers == null)
918919
{
919920
props.Headers = new Dictionary<string, object>();
@@ -961,7 +962,7 @@ void InjectTraceContextIntoBasicProperties(IBasicProperties props, string key, s
961962
}
962963

963964
// Inject the ActivityContext into the message headers to propagate trace context to the receiving service.
964-
Propagator.Inject(new PropagationContext(sendActivity.Context, Baggage.Current), props, InjectTraceContextIntoBasicProperties);
965+
DistributedContextPropagator.Current.Inject(sendActivity, props, InjectTraceContextIntoBasicProperties);
965966

966967
ModelSend(in cmd, (BasicProperties)props, body);
967968
return;

projects/RabbitMQ.Client/client/impl/RabbitMQActivitySource.cs

+34-15
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99

10-
using OpenTelemetry;
11-
using OpenTelemetry.Context.Propagation;
12-
1310
using RabbitMQ.Client.Events;
1411
using RabbitMQ.Client.Framing.Impl;
1512

@@ -18,7 +15,6 @@ namespace RabbitMQ.Client
1815
internal class RabbitMQActivitySource
1916
{
2017
internal static ActivitySource source = new ActivitySource("RabbitMQ.Client", typeof(RabbitMQActivitySource).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion);
21-
private static readonly TextMapPropagator Propagator = new CompositeTextMapPropagator(new TextMapPropagator[] { new TraceContextPropagator(), new BaggagePropagator() });
2218

2319
static RabbitMQActivitySource()
2420
{
@@ -46,11 +42,20 @@ internal static Activity Receive(string routingKey, string exchange, ulong deliv
4642
if (source.HasListeners())
4743
{
4844
// Extract the PropagationContext of the upstream parent from the message headers.
49-
PropagationContext parentContext = Propagator.Extract(default, readOnlyBasicProperties, ExtractTraceContextFromBasicProperties);
50-
Baggage.Current = parentContext.Baggage;
51-
Activity activity = StartRabbitMQActivity($"{routingKey} receive", ActivityKind.Consumer, parentContext.ActivityContext);
45+
DistributedContextPropagator.Current.ExtractTraceIdAndState(readOnlyBasicProperties, ExtractTraceIdAndState, out string traceId, out string traceState);
46+
IEnumerable<KeyValuePair<string, string>> baggage = DistributedContextPropagator.Current.ExtractBaggage(readOnlyBasicProperties, ExtractTraceIdAndState);
47+
ActivityContext.TryParse(traceId, traceState, out ActivityContext parentContext);
48+
Activity activity = StartRabbitMQActivity($"{routingKey} receive", ActivityKind.Consumer, parentContext);
5249
if (activity != null && activity.IsAllDataRequested)
5350
{
51+
if (baggage != null)
52+
{
53+
foreach (var item in baggage)
54+
{
55+
Activity.Current?.SetBaggage(item.Key, item.Value);
56+
}
57+
}
58+
5459
PopulateMessagingTags("receive", routingKey, exchange, deliveryTag, readOnlyBasicProperties, bodySize, activity);
5560
}
5661

@@ -65,12 +70,20 @@ internal static Activity Process(BasicDeliverEventArgs deliverEventArgs)
6570
if (source.HasListeners())
6671
{
6772
// Extract the PropagationContext of the upstream parent from the message headers.
68-
PropagationContext parentContext = Propagator.Extract(default, deliverEventArgs.BasicProperties, ExtractTraceContextFromBasicProperties);
69-
Baggage.Current = parentContext.Baggage;
70-
71-
Activity activity = StartRabbitMQActivity($"{deliverEventArgs.RoutingKey} process", ActivityKind.Consumer, parentContext.ActivityContext);
73+
DistributedContextPropagator.Current.ExtractTraceIdAndState(deliverEventArgs.BasicProperties, ExtractTraceIdAndState, out string traceId, out string traceState);
74+
IEnumerable<KeyValuePair<string, string>> baggage = DistributedContextPropagator.Current.ExtractBaggage(deliverEventArgs.BasicProperties, ExtractTraceIdAndState);
75+
ActivityContext.TryParse(traceId, traceState, out ActivityContext parentContext);
76+
Activity activity = StartRabbitMQActivity($"{deliverEventArgs.RoutingKey} process", ActivityKind.Consumer, parentContext);
7277
if (activity != null && activity.IsAllDataRequested)
7378
{
79+
if (baggage != null)
80+
{
81+
foreach (var item in baggage)
82+
{
83+
Activity.Current?.SetBaggage(item.Key, item.Value);
84+
}
85+
}
86+
7487
PopulateMessagingTags("process", deliverEventArgs.RoutingKey, deliverEventArgs.Exchange, deliverEventArgs.DeliveryTag, deliverEventArgs.BasicProperties, deliverEventArgs.Body.Length, activity);
7588
}
7689

@@ -123,12 +136,18 @@ private static void PopulateMessagingTags(string operation, string routingKey, s
123136
}
124137
}
125138
}
126-
127-
static IEnumerable<string> ExtractTraceContextFromBasicProperties<T>(T props, string key) where T : IReadOnlyBasicProperties
139+
140+
private static void ExtractTraceIdAndState(object carrier, string name, out string value, out IEnumerable<string> values)
128141
{
129-
if (props.Headers.TryGetValue(key, out var value) && value is byte[] bytes)
142+
if (carrier is IReadOnlyBasicProperties props && props.Headers is not null && props.Headers.TryGetValue(name, out object propsVal) && propsVal is byte[] bytes)
143+
{
144+
value = Encoding.UTF8.GetString(bytes);
145+
values = default;
146+
}
147+
else
130148
{
131-
yield return Encoding.UTF8.GetString(bytes);
149+
value = default;
150+
values = default;
132151
}
133152
}
134153
}

0 commit comments

Comments
 (0)