Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Add comments explaining copy-on-write behavior of baggage
Browse files Browse the repository at this point in the history
Per issue #526.

Signed-off-by: Yuri Shkuro <ys@uber.com>
  • Loading branch information
Yuri Shkuro committed Aug 16, 2020
1 parent 7b8d143 commit cf8927b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion span.go
Expand Up @@ -303,7 +303,14 @@ func (s *Span) fixLogsIfDropped() {
s.numDroppedLogs = 0
}

// SetBaggageItem implements SetBaggageItem() of opentracing.SpanContext
// SetBaggageItem implements SetBaggageItem() of opentracing.SpanContext.
// The call is proxied via tracer.baggageSetter to allow policies to be applied
// before allowing to set/replace baggage keys.
// The setter eventually stores a new SpanContext with extended baggage:
//
// span.context = span.context.WithBaggageItem(key, value)
//
// See SpanContext.WithBaggageItem() for explanation why it's done this way.
func (s *Span) SetBaggageItem(key, value string) opentracing.Span {
s.Lock()
defer s.Unlock()
Expand Down
8 changes: 8 additions & 0 deletions span_context.go
Expand Up @@ -304,6 +304,14 @@ func (c *SpanContext) CopyFrom(ctx *SpanContext) {
}

// WithBaggageItem creates a new context with an extra baggage item.
//
// The SpanContext is designed to be immutable and passed by value. As such,
// it cannot contain any locks, and should only hold immutable data, including baggage.
// Another reason for why baggage is immutable is when the span context is passed
// as a parent when starting a new span. The new span's baggage cannot affect the parent
// span's baggage, so the child span either needs to take a copy of the parent baggage
// (which is expensive and unnecessary since baggage rarely changes in the life span of
// a trace), or it needs to do a copy-on-write, which is the approach taken here.
func (c SpanContext) WithBaggageItem(key, value string) SpanContext {
var newBaggage map[string]string
if c.baggage == nil {
Expand Down

0 comments on commit cf8927b

Please sign in to comment.