Go Context Value和OpenTracing Baggage Items之间有什么区别?

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

What is the difference between Go Context Value and OpenTracing Baggage Items?

问题

我想了解Go语言中的上下文(Context)和OpenTracing中的Baggage Items之间的区别,特别是它们在传递数据方面的区别。

根据我所了解的,两者都可以传递键值对给它们的后代(子上下文/子跨度)。

在Go的标准库中,我可以使用以下方法:

func context.WithValue(parent Context, key, val interface{}) Context

在OpenTracing中,我可以使用以下方法:

func SetBaggageItem(restrictedKey, value string) Span

显然,这两种方法都有一些类型限制(interface{}和string)。还有其他需要我了解的吗?

在什么情况下我应该选择使用哪种方法来传递一些键值对呢?

英文:

I want to understand the difference between Context in Go and Baggage Items in OpenTracing. Specifically, their difference to carry data.

From what I have learned, both can carry key-value pairs to their descendants(child contexts / child spans).

In Go's standard library, I can use:

func context.WithValue(parent Context, key, val interface{}) Context

In OpenTracing, I can use:

func SetBaggageItem(restrictedKey, value string) Span

Clearly, there are some type restrictions (interface{} and string). Is there anything else should I know?

Under what circumstances should I choose which to carry some key-value pairs?

答案1

得分: 1

这两者有不同的用途。

GoContext 用于定义 Context 类型,它在 API 边界之间传递截止时间、取消信号和其他请求范围的值。

> 在 Go 服务器中,每个传入的请求都在自己的 goroutine 中处理。
> 请求处理程序通常会启动额外的 goroutine 来访问后端
> 例如数据库和 RPC 服务。在处理请求的一组 goroutine 通常需要访问请求特定的值,例如
> 最终用户的身份、授权令牌和请求的
> 截止时间

而 OpenTracing 框架用于分布式跟踪。我们使用分布式跟踪来对应用程序进行性能分析和监控。行李是在服务之间携带请求的元数据

SERVICE A -> SERVICE B -> SERVICE C
英文:

Both of these have different use-cases.

GoContext is used to define Context type which carries deadlines, cancellation signals, and other request-scoped values across API boundaries.

> In Go servers, each incoming request is handled in its own goroutine.
> Request handlers often start additional goroutines to access backends
> such as databases and RPC services. The set of goroutines working on a
> request typically needs access to request-specific values such as the
> identity of the end user, authorization tokens, and the request’s
> deadline

Whereas, OpenTracing framework is used for Distributed tracing. We use distributed tracing to profile monitor applications. The baggage is metadata piggy backing the request across services.

SERVICE A -> SERVICE B -> SERVICE C

huangapple
  • 本文由 发表于 2021年9月1日 09:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/69006951.html
匿名

发表评论

匿名网友

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

确定