Opentelemetry:在一个进程中打开 span,在另一个进程中关闭它。

huangapple go评论101阅读模式
英文:

Opentelemetry: Open span in one process, and close it in another

问题

我正在研究使用OpenTelemetry的Golang库来记录一个由分布式消息传递(NATS)连接在一起的函数。我想要开始一个span,在函数的第二部分发送一条消息,并在完成时结束span。

我只能看到在函数结束时关闭span的示例。

如果我能创建一个span并传入一个现有的ID来使用,并获得一个记录的span返回,那就太好了,但看起来这是不可能的。

有什么想法吗?

英文:

I'm looking into using OpenTelementry golang library to record a function which is connected together by distributed messaging (NATS). I want to start a span, post a message to the second part of the function, and end the span when that completes.

I can only see examples of spans that close when a function ends.

It would be great if I could create a span and pass in an existing id to use, and get a recording span back, but it doesn't look like this is possible.

Any ideas?

答案1

得分: 1

在一个进程中打开 span,然后在另一个进程中关闭它并不是一个很好的主意。使用单个 span,你可以看到总的进程执行时间,但无法看到其各个部分的时间和细节。

如果你能为每个重要的步骤创建一个 span,会更有帮助。它可以是一个本地函数或在外部进程中执行的函数。

我在互联网上找到了一张很好的幻灯片,演示了我们可以实现的内容。

https://speakerdeck.com/simonz130/distributed-tracing-and-monitoring-with-opentelemetry?slide=26

如评论中提到的 @pati-ram-yadav,你可以在应用程序中使用 span := trace.SpanFromContext(ctx) 创建子 span。

对于外部进程来说,稍微复杂一些。

父函数需要通过网络与子函数共享上下文(accountID 和 traceID)。

之后,子进程将这些字段注入到其上下文中,使用该上下文创建的所有 span 都与父 span 相连接。

每个传输方式都以不同的方式实现发送附加数据(accountID 和 traceID)。例如,使用 HTTP 时,它是通过自定义的 HTTP 头实现的。

opentelemetry 默认支持许多传输方式,包括 HTTP。如果某个传输方式不受支持,你需要找到一个社区模块或编写自己的代码。

这是一个关于 HTTP 的示例。你需要在父请求中 Inject 上下文,并在客户端的请求中 Extract 上下文。然后,客户端将 TraceID 和 AccountID 添加到 context.Context 中,使用该上下文创建的所有 span 都与父 span 相连接。

Opentelemetry:在一个进程中打开 span,在另一个进程中关闭它。

英文:

> Open span in one process, and close it in another

That is not a very good idea. With a single span, you can see total process execution time, but cannot see time and details of its parts.

It is more helpful if you can create a span for every important step. It could be a local function or function executed in an external process.

I found that nice slide in internet that demonstrates what we can achive.

https://speakerdeck.com/simonz130/distributed-tracing-and-monitoring-with-opentelemetry?slide=26

Opentelemetry:在一个进程中打开 span,在另一个进程中关闭它。

As @pati-ram-yadav mentioned in comment, you can create child spans using span := trace.SpanFromContext(ctx) inside application.

For external process it is a bit more complicated.

Parent function needs to share context (accountID and traceID) with child function through the wire.

After that, the child process injects these fields into its context and all spans created with that context are connected to the parent.

Every transport implements sending additional data (accountID and traceID) differently. With HTTP, for example, it is implemented as custom HTTP headers.

Many transports including HTTP are supported by opentelemetry out-of-the-box. If some transport is not supported, you have to find a community module or write your own code.

This is an example for HTTP. You need to Inject context into HTTP request in parent and Extract that context from the request in client. Client then adds TraceID and AccountID to context.Context and all spans created using that context are connected to parent span.

huangapple
  • 本文由 发表于 2022年7月5日 17:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/72867313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定