最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to add fields to all OpenTelemetry under a scoped lifetimeduring the processing of a request? - Stack Overflow

programmeradmin0浏览0评论

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);
  }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论