Search code examples
gojaegeropentracing

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


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?


Solution

  • 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