Go 上下文值和 OpenTracing 行李项有什么区别?

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)。还有什么我应该知道的吗?

什么情况下应该选择哪个携带一些键值对?

两者都有不同的用例。

GoContext 用于定义包含截止日期、取消信号和跨 API 边界的其他 请求范围 值的上下文类型。

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

然而,OpenTracing 框架用于分布式跟踪。我们使用分布式跟踪来分析监视应用程序。行李是元数据小猪跨服务支持请求

SERVICE A -> SERVICE B -> SERVICE C