Last active
January 19, 2022 20:27
-
-
Save cheoalfredo/759429aee43d6961a86417b8c8fdf8e0 to your computer and use it in GitHub Desktop.
Activity Creation and context propagation thru RabbitMQ headers in IBasicProperties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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