I have a C# (.NET 8) program that consumes messages off a queue via MassTransit. I would like to annotate all OpenTelemetry events that happens during the processing of this request to be annotated with a value from the request object.
Currently I have an ugly hack using an AsyncLocal
inside an ITelemetryInitializer
but this seems to have recently become prone to race conditions somehow.
The project is using Microsoft.ApplicationInsights
.
As I see it there is a few options:
- Use a logger scope
ILogger.BeginScope()
- Use an operation
ITelemetryClient.StartOperation<TOperation>(string name)
- Use
System.Diagnostics.Activity.Current
somehow
But I am not sure I fully understand them.
But basically the code is
class MyConsumer : IConsumer<MyMessage> {
public Task Consume(ConsumeContext<MyMessage> context) {
AnnotateAllEventsInThisScope(context.Message.MyCoolId);
ProcessRequst(context.Message, context.Headers, context.CancellationToke);
}
}