简体   繁体   English

通过 MassTransit 发布者/消费者传播 W3C 跟踪上下文

[英]Propagating W3C trace context over MassTransit publisher/consumer

'm trying to support propagating the W3C trace context traceId and spanId properties from http calls -> publisher -> consumer -> http call within MassTransit (just so they show up in the logs/seq for now, but we're using Dynatrace), but am I couldn't find anything out-of-the-box here: https://masstransit-project.com/advanced/monitoring/diagnostic-source.html我正在尝试支持从 http 调用传播 W3C 跟踪上下文 traceId 和 spanId 属性 -> 发布者 -> 消费者 -> MassTransit 中的 http 调用(只是为了让它们暂时显示在日志/序列中,但我们正在使用 Dynatrace) ,但我在这里找不到任何开箱即用的东西吗:https://masstransit-project.com/advanced/monitoring/diagnostic-source.html

If there is nothing available I'll probably try to create something myself based on these articles:如果没有可用的东西,我可能会尝试根据这些文章自己创建一些东西:

I could find this one as an example for OpenTracing: https://github.com/yesmarket/MassTransit.OpenTracing我可以找到这个作为 OpenTracing 的示例: https://github.com/yesmarket/MassTransit.OpenTracing

And this as a reference for NServiceBus: https://jimmybogard.com/building-end-to-end-diagnostics-and-tracing-a-primer-trace-context/这作为 NServiceBus 的参考: https://jimmybogard.com/building-end-to-end-diagnostics-and-tracing-a-primer-trace-context/

Unless anyone can suggest something that already exists?除非有人可以建议已经存在的东西?

EDIT2: The latest versions of MassTransit propagate the trace context by default. EDIT2:最新版本的 MassTransit 默认传播跟踪上下文。

Enable W3C Tracing in your startup:在您的启动中启用 W3C 跟踪:

Activity.DefaultIdFormat = ActivityIdFormat.W3C;

on configuring your bus call:在配置您的总线呼叫时:

bus.UseInstrumentation();

EDIT: Based on the library by Ryan Bartsch & article by Jimmy Bogard, I created this package which does what I need it to do: https://github.com/zidad/MassTransit.ActivityTracing编辑:基于 Ryan Bartsch 的图书馆和 Jimmy Bogard 的文章,我创建了这个 package,它完成了我需要它做的事情: https://github.com/zidad/MassTransit.ActivityTracing

Dynatracing claims to integrate seamlessly with OpenTracing https://www.dynatrace.com/integrations/opentracing/ and if you use the library you mentioned and if you have your HTTP part instrumented with OpenTracing, it will work out of the box. Dynatracing 声称与 OpenTracing https://www.dynatrace.com/integrations/opentracing/无缝集成,如果您使用您提到的库,并且如果您的 HTTP 部分使用 OpenTracing 进行检测,它将开箱即用。

The only potential drawback that in the service that hosts gets an HTTP call and within the context of handling it sends or publishes a message via MassTransit, everything must be instrumented with OpenTracing, because it will start the child span using the OpenTracing API.唯一的潜在缺点是,在托管服务中调用 HTTP 并在处理它的上下文中通过 MassTransit 发送或发布消息,所有内容都必须使用 OpenTracing 进行检测,因为它将使用 OpenTracing API 启动子跨度。

We do this with Datadog, so we use the Datadog OpenTracing integration library and trace the WebApi and HttpClient using OpenTracing Contrib libraries.我们使用 Datadog 执行此操作,因此我们使用 Datadog OpenTracing 集成库并使用 OpenTracing Contrib 库跟踪 WebApi 和 HttpClient。 So, the automatic instrumentation didn't work for us.因此,自动检测对我们不起作用。 But it's not hard to use those libs to instrument your app instead of using the automatic instrumentation.但是使用这些库来检测您的应用程序而不是使用自动检测并不难。

The usual flow is like:通常的流程是这样的:

  • Outside -> WebApi: start a span外部 -> WebApi:开始跨度
  • WebApi -> MassTransit: start a child span, inject the context to headers WebApi -> MassTransit:启动子跨度,将上下文注入标题
  • MassTransit -> consumer: extract the context, start a child span and so on MassTransit -> 消费者:提取上下文,启动子跨度等

Both bits with injection and extraction are handled in the MassTransit.OpenTracing library, so, there's nothing extra to do.注入和提取的两个位都在MassTransit.OpenTracing库中处理,因此,没有什么额外的事情要做。

With a library that your provider has to support OpenTracing it's usually like this:对于您的提供商必须支持 OpenTracing 的库,它通常是这样的:

  • Configure the tracer of the provider配置提供者的跟踪器
  • Set the OpenTracing global tracer to the wrapper, using the integration library from your provider使用提供商的集成库将 OpenTracing 全局跟踪器设置为包装器
  • When you create a span with OpenTracing, it will create a vendor-specific span and wrap it in OpenTracing span当您使用 OpenTracing 创建跨度时,它会创建一个特定于供应商的跨度并将其包装在 OpenTracing 跨度中

I'm the author of the MassTransit.OpenTracing library you referenced, but I wrote it prior to the W3C recommendation, which looks to be quite recent (Feb 6, 2020).我是你引用的 MassTransit.OpenTracing 库的作者,但我是在 W3C 推荐之前写的,它看起来是最近的(2020 年 2 月 6 日)。

My goal was that I wanted distributed tracing through a microservice architecture that had both synchronous/HTTP as well as asynchronous message broker communications.我的目标是希望通过具有同步/HTTP 和异步消息代理通信的微服务架构进行分布式跟踪。 For the HTTP stuff I was using OpenTracing.Contrib.NetCore , which 'uses .NET's DiagnosticSource to instrument its code'.对于 HTTP 我使用的是OpenTracing.Contrib.NetCore ,它“使用 .NET 的 DiagnosticSource 来检测其代码”。 For the asynchronous message broker communications, I was using RabbitMQ with MassTransit, but I didn't really understand the MassTransit DiagnosticSource approach suggested on the website (and nor could I find any examples), so I decided to get into the nuts and bolts a bit and roll my own.对于异步消息代理通信,我将 RabbitMQ 与 MassTransit 一起使用,但我并不真正理解网站上建议的 MassTransit DiagnosticSource 方法(我也找不到任何示例),所以我决定深入了解具体细节咬住我自己的。

Long story short, it all worked as expected using Jaeger as the tracer.长话短说,使用 Jaeger 作为示踪剂,一切都按预期进行。 Interestingly, we (as in the company I work for) decided to also use DynaTrace, which operates at a much lower level and kind of removes the need for handling a lot of this stuff in code.有趣的是,我们(和我工作的公司一样)决定也使用 DynaTrace,它在更低的级别上运行,并且在某种程度上消除了在代码中处理大量此类内容的需要。 That said, the approach is not invalid (IMO), as not everyone can afford DynaTrace (or similar APM tools).也就是说,该方法并非无效(IMO),因为并非每个人都能负担得起 DynaTrace(或类似的 APM 工具)。

I'll try to upgrade this library with the W3C recommendation in the coming week or 2. Let me know if you want to help with contribution/review (or if you want to go off in a different direction and roll your own is also fine)...我将在接下来的一两周内尝试使用 W3C 推荐升级这个库。如果你想帮助贡献/审查,请告诉我(或者如果你想在不同的方向关闭 go 并推出你自己的也很好)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM