Propagate context from either created activity or Current (#1431)
* Propagate context from either created activity or Current * minor wording change
This commit is contained in:
parent
772ed953ff
commit
985c5378c5
|
|
@ -21,6 +21,7 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenTelemetry;
|
||||
using OpenTelemetry.Context.Propagation;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
|
|
@ -56,8 +57,9 @@ namespace Utils.Messaging
|
|||
|
||||
public void ReceiveMessage(BasicDeliverEventArgs ea)
|
||||
{
|
||||
// Extract the ActivityContext of the upstream parent from the message headers.
|
||||
// Extract the PropagationContext of the upstream parent from the message headers.
|
||||
var parentContext = Propagator.Extract(default, ea.BasicProperties, this.ExtractTraceContextFromBasicProperties);
|
||||
Baggage.Current = parentContext.Baggage;
|
||||
|
||||
// Start an activity with a name following the semantic convention of the OpenTelemetry messaging specification.
|
||||
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#span-name
|
||||
|
|
@ -71,13 +73,10 @@ namespace Utils.Messaging
|
|||
|
||||
this.logger.LogInformation($"Message received: [{message}]");
|
||||
|
||||
if (activity != null)
|
||||
{
|
||||
activity.SetTag("message", message);
|
||||
activity?.SetTag("message", message);
|
||||
|
||||
// The OpenTelemetry messaging specification defines a number of attributes. These attributes are added here.
|
||||
RabbitMqHelper.AddMessagingTags(activity);
|
||||
}
|
||||
// The OpenTelemetry messaging specification defines a number of attributes. These attributes are added here.
|
||||
RabbitMqHelper.AddMessagingTags(activity);
|
||||
|
||||
// Simulate some work
|
||||
Thread.Sleep(1000);
|
||||
|
|
|
|||
|
|
@ -59,15 +59,26 @@ namespace Utils.Messaging
|
|||
{
|
||||
var props = this.channel.CreateBasicProperties();
|
||||
|
||||
// Depending on Sampling (and whether a listener is registered or not), the
|
||||
// activity above may not be created.
|
||||
// If it is created, then propagate its context.
|
||||
// If it is not created, the propagate the Current context,
|
||||
// if any.
|
||||
ActivityContext contextToInject = default;
|
||||
if (activity != null)
|
||||
{
|
||||
// Inject the ActivityContext into the message headers to propagate trace context to the receiving service.
|
||||
Propagator.Inject(new PropagationContext(activity.Context, Baggage.Current), props, this.InjectTraceContextIntoBasicProperties);
|
||||
|
||||
// The OpenTelemetry messaging specification defines a number of attributes. These attributes are added here.
|
||||
RabbitMqHelper.AddMessagingTags(activity);
|
||||
contextToInject = activity.Context;
|
||||
}
|
||||
else if (Activity.Current != null)
|
||||
{
|
||||
contextToInject = Activity.Current.Context;
|
||||
}
|
||||
|
||||
// Inject the ActivityContext into the message headers to propagate trace context to the receiving service.
|
||||
Propagator.Inject(new PropagationContext(contextToInject, Baggage.Current), props, this.InjectTraceContextIntoBasicProperties);
|
||||
|
||||
// The OpenTelemetry messaging specification defines a number of attributes. These attributes are added here.
|
||||
RabbitMqHelper.AddMessagingTags(activity);
|
||||
var body = $"Published message: DateTime.Now = {DateTime.Now}.";
|
||||
|
||||
this.channel.BasicPublish(
|
||||
|
|
|
|||
Loading…
Reference in New Issue