Skip to content

Instantly share code, notes, and snippets.

@cheoalfredo
Last active January 19, 2022 20:27
Show Gist options
  • Save cheoalfredo/759429aee43d6961a86417b8c8fdf8e0 to your computer and use it in GitHub Desktop.
Save cheoalfredo/759429aee43d6961a86417b8c8fdf8e0 to your computer and use it in GitHub Desktop.
Activity Creation and context propagation thru RabbitMQ headers in IBasicProperties
public async Task SendMessage(string Message, string Queue)
{
await Task.Run(() =>
{
using (var activity = _activitySource.StartActivity("Queue user creation request", ActivityKind.Producer))
{
using (var channel = _conn.CreateModel())
{
channel.QueueDeclare(Queue, false, false, false, null);
ActivityContext contextToInject = default;
if (activity is not null)
{
contextToInject = activity.Context;
}
else if (Activity.Current is not null)
{
contextToInject = Activity.Current.Context;
}
var props = channel.CreateBasicProperties();
var propagationContext = new PropagationContext(contextToInject, Baggage.Current);
Propagator.Inject(propagationContext, props, InjectHeaders);
channel.BasicPublish("", Queue, props, System.Text.Encoding.UTF8.GetBytes(Message));
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment