Skip to content

Commit 2ab2a42

Browse files
committed
baggage API
1 parent 5a8a82c commit 2ab2a42

12 files changed

Lines changed: 262 additions & 22 deletions

‎ddtrace/ddtrace.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,23 @@ type Span interface {
7272
// a representative name for a group of spans (e.g. "grpc.server" or "http.request").
7373
SetOperationName(operationName string)
7474

75-
// BaggageItem returns the baggage item held by the given key.
76-
BaggageItem(key string) string
75+
// GetBaggageItem returns the baggage item held by the given key.
76+
GetBaggageItem(key string) string
7777

7878
// SetBaggageItem sets a new baggage item at the given key. The baggage
7979
// item should propagate to all descendant spans, both in- and cross-process.
8080
SetBaggageItem(key, val string)
8181

82+
// GetAllBaggageItems returns a copy of all baggage items.
83+
// If no items exist, returns an empty map.
84+
GetAllBaggageItems() map[string]string
85+
86+
// RemoveBaggageItem removes a single baggage item by its key.
87+
RemoveBaggageItem(key string)
88+
89+
// RemoveAllBaggageItems removes all baggage items from the span.
90+
RemoveAllBaggageItems()
91+
8292
// Finish finishes the current span with the given options. Finish calls should be idempotent.
8393
Finish(opts ...FinishOption)
8494

‎ddtrace/internal/globaltracer.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,22 @@ func (NoopSpan) SetTag(_ string, _ interface{}) {}
7575
func (NoopSpan) SetOperationName(_ string) {}
7676

7777
// BaggageItem implements ddtrace.Span.
78-
func (NoopSpan) BaggageItem(_ string) string { return "" }
78+
func (NoopSpan) GetBaggageItem(_ string) string { return "" }
7979

8080
// SetBaggageItem implements ddtrace.Span.
8181
func (NoopSpan) SetBaggageItem(_, _ string) {}
8282

83+
// RemoveBaggageItem implements ddtrace.Span.
84+
func (NoopSpan) RemoveBaggageItem(_ string) {}
85+
86+
// RemoveAllBaggageItems implements ddtrace.Span.
87+
func (NoopSpan) RemoveAllBaggageItems() {}
88+
89+
// GetAllBaggageItems implements ddtrace.Span.
90+
func (NoopSpan) GetAllBaggageItems() map[string]string {
91+
return make(map[string]string)
92+
}
93+
8394
// Finish implements ddtrace.Span.
8495
func (NoopSpan) Finish(_ ...ddtrace.FinishOption) {}
8596

‎ddtrace/mocktracer/mockspan.go‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ func (s *mockspan) SetOperationName(operationName string) {
176176
return
177177
}
178178

179-
// BaggageItem returns the baggage item with the given key.
180-
func (s *mockspan) BaggageItem(key string) string {
181-
return s.context.baggageItem(key)
179+
// GetBaggageItem returns the baggage item with the given key.
180+
func (s *mockspan) GetBaggageItem(key string) string {
181+
return s.context.getBaggageItem(key)
182182
}
183183

184184
// SetBaggageItem sets a new baggage item at the given key. The baggage
@@ -188,6 +188,22 @@ func (s *mockspan) SetBaggageItem(key, val string) {
188188
return
189189
}
190190

191+
// RemoveBaggageItem removes the baggage item identified by the given key.
192+
func (s *mockspan) RemoveBaggageItem(key string) {
193+
s.context.removeBaggageItem(key)
194+
}
195+
196+
// RemoveAllBaggageItems removes all baggage items from this span.
197+
func (s *mockspan) RemoveAllBaggageItems() {
198+
s.context.removeAllBaggageItems()
199+
}
200+
201+
// GetAllBaggageItems returns a copy of all baggage items.
202+
// If there are no items, it returns an empty map.
203+
func (s *mockspan) GetAllBaggageItems() map[string]string {
204+
return s.context.getAllBaggageItems()
205+
}
206+
191207
// Finish finishes the current span with the given options.
192208
func (s *mockspan) Finish(opts ...ddtrace.FinishOption) {
193209
var cfg ddtrace.FinishConfig

‎ddtrace/mocktracer/mockspan_test.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,32 @@ func TestSpanBaggageFunctions(t *testing.T) {
144144
t.Run("BaggageItem", func(t *testing.T) {
145145
s := basicSpan("http.request")
146146
s.SetBaggageItem("a", "b")
147-
assert.Equal(t, "b", s.BaggageItem("a"))
147+
assert.Equal(t, "b", s.GetBaggageItem("a"))
148+
})
149+
150+
t.Run("GetAllBaggageItems", func(t *testing.T) {
151+
s := basicSpan("http.request")
152+
s.SetBaggageItem("a", "b")
153+
s.SetBaggageItem("c", "d")
154+
baggage := s.GetAllBaggageItems()
155+
assert.Len(t, baggage, 2)
156+
assert.Equal(t, "b", baggage["a"])
157+
assert.Equal(t, "d", baggage["c"])
158+
})
159+
160+
t.Run("RemoveBaggageItem", func(t *testing.T) {
161+
s := basicSpan("http.request")
162+
s.SetBaggageItem("a", "b")
163+
s.RemoveBaggageItem("a")
164+
assert.Empty(t, s.context.baggage)
165+
})
166+
167+
t.Run("RemoveAllBaggageItems", func(t *testing.T) {
168+
s := basicSpan("http.request")
169+
s.SetBaggageItem("a", "b")
170+
s.SetBaggageItem("c", "d")
171+
s.RemoveAllBaggageItems()
172+
assert.Empty(t, s.context.baggage)
148173
})
149174
}
150175

‎ddtrace/mocktracer/mockspancontext.go‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,44 @@ func (sc *spanContext) setBaggageItem(k, v string) {
4848
sc.baggage[k] = v
4949
}
5050

51-
func (sc *spanContext) baggageItem(k string) string {
51+
func (sc *spanContext) getBaggageItem(k string) string {
5252
sc.RLock()
5353
defer sc.RUnlock()
5454
return sc.baggage[k]
5555
}
5656

57+
func (sc *spanContext) removeBaggageItem(k string) {
58+
sc.Lock()
59+
defer sc.Unlock()
60+
if sc.baggage == nil {
61+
return
62+
}
63+
delete(sc.baggage, k)
64+
if len(sc.baggage) == 0 {
65+
sc.baggage = nil
66+
}
67+
}
68+
69+
func (sc *spanContext) removeAllBaggageItems() {
70+
sc.Lock()
71+
defer sc.Unlock()
72+
sc.baggage = nil
73+
}
74+
75+
func (sc *spanContext) getAllBaggageItems() map[string]string {
76+
sc.RLock()
77+
defer sc.RUnlock()
78+
if sc.baggage == nil {
79+
return make(map[string]string)
80+
}
81+
// Return a copy to avoid callers mutating the internal map
82+
copyMap := make(map[string]string, len(sc.baggage))
83+
for key, val := range sc.baggage {
84+
copyMap[key] = val
85+
}
86+
return copyMap
87+
}
88+
5789
func (sc *spanContext) setSamplingPriority(p int) {
5890
sc.Lock()
5991
defer sc.Unlock()

‎ddtrace/mocktracer/mockspancontext_test.go‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,32 @@ func TestSpanContextGetBaggage(t *testing.T) {
3535
var sc spanContext
3636
sc.setBaggageItem("a", "b")
3737
sc.setBaggageItem("c", "d")
38-
assert.Equal(t, sc.baggageItem("a"), "b")
39-
assert.Equal(t, sc.baggageItem("c"), "d")
38+
assert.Equal(t, sc.getBaggageItem("a"), "b")
39+
assert.Equal(t, sc.getBaggageItem("c"), "d")
40+
}
41+
42+
func TestSpanContextGetAllBaggage(t *testing.T) {
43+
var sc spanContext
44+
sc.setBaggageItem("a", "b")
45+
sc.setBaggageItem("c", "d")
46+
assert.Equal(t, map[string]string{"a": "b", "c": "d"}, sc.getAllBaggageItems())
47+
}
48+
49+
func TestSpanContextRemoveBaggage(t *testing.T) {
50+
var sc spanContext
51+
sc.setBaggageItem("a", "b")
52+
sc.setBaggageItem("c", "d")
53+
sc.removeBaggageItem("a")
54+
assert.Equal(t, sc.baggage["c"], "d")
55+
assert.Empty(t, sc.baggage["a"])
56+
}
57+
58+
func TestSpanContextRemoveAllBaggage(t *testing.T) {
59+
var sc spanContext
60+
sc.setBaggageItem("a", "b")
61+
sc.setBaggageItem("c", "d")
62+
sc.removeAllBaggageItems()
63+
assert.Empty(t, sc.baggage)
4064
}
4165

4266
func TestSpanContextIterator(t *testing.T) {

‎ddtrace/mocktracer/mocktracer_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ func TestTracerExtract(t *testing.T) {
278278
assert.Nil(err)
279279
sc, ok = ctx.(*spanContext)
280280
assert.True(ok)
281-
assert.Equal("B", sc.baggageItem("a"))
282-
assert.Equal("D", sc.baggageItem("c"))
281+
assert.Equal("B", sc.getBaggageItem("a"))
282+
assert.Equal("D", sc.getBaggageItem("c"))
283283

284284
ctx, err = mt.Extract(carry(traceHeader, "1", spanHeader, "2", priorityHeader, "-1"))
285285
assert.Nil(err)
@@ -302,7 +302,7 @@ func TestTracerExtract(t *testing.T) {
302302

303303
assert.Equal(uint64(1), got.traceID)
304304
assert.Equal(uint64(2), got.spanID)
305-
assert.Equal("D", got.baggageItem("c"))
306-
assert.Equal("B", got.baggageItem("a"))
305+
assert.Equal("D", got.getBaggageItem("c"))
306+
assert.Equal("B", got.getBaggageItem("a"))
307307
})
308308
}

‎ddtrace/tracer/civisibility_tslv.go‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (e *ciVisibilityEvent) SetOperationName(operationName string) {
177177
e.Content.Name = e.span.Name
178178
}
179179

180-
// BaggageItem retrieves the baggage item associated with the given key from the event's span.
180+
// GetBaggageItem retrieves the baggage item associated with the given key from the event's span.
181181
//
182182
// Parameters:
183183
//
@@ -186,8 +186,8 @@ func (e *ciVisibilityEvent) SetOperationName(operationName string) {
186186
// Returns:
187187
//
188188
// The baggage item value.
189-
func (e *ciVisibilityEvent) BaggageItem(key string) string {
190-
return e.span.BaggageItem(key)
189+
func (e *ciVisibilityEvent) GetBaggageItem(key string) string {
190+
return e.span.GetBaggageItem(key)
191191
}
192192

193193
// SetBaggageItem sets a baggage item on the event's span.
@@ -200,6 +200,26 @@ func (e *ciVisibilityEvent) SetBaggageItem(key, val string) {
200200
e.span.SetBaggageItem(key, val)
201201
}
202202

203+
// GetAllBaggageItems returns a copy of all baggage items from the event's span.
204+
// If there are no items, it returns an empty map.
205+
func (e *ciVisibilityEvent) GetAllBaggageItems() map[string]string {
206+
return e.span.GetAllBaggageItems()
207+
}
208+
209+
// RemoveBaggageItem removes a baggage item from the event's span.
210+
//
211+
// Parameters:
212+
//
213+
// key - The baggage item key.
214+
func (e *ciVisibilityEvent) RemoveBaggageItem(key string) {
215+
e.span.RemoveBaggageItem(key)
216+
}
217+
218+
// RemoveAllBaggageItems removes all baggage items from the event's span.
219+
func (e *ciVisibilityEvent) RemoveAllBaggageItems() {
220+
e.span.RemoveAllBaggageItems()
221+
}
222+
203223
// Finish completes the event's span with optional finish options.
204224
//
205225
// Parameters:

‎ddtrace/tracer/span.go‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,26 @@ func (s *span) SetBaggageItem(key, val string) {
103103
s.context.setBaggageItem(key, val)
104104
}
105105

106-
// BaggageItem gets the value for a baggage item given its key. Returns the
106+
// GetBaggageItem gets the value for a baggage item given its key. Returns the
107107
// empty string if the value isn't found in this Span.
108-
func (s *span) BaggageItem(key string) string {
109-
return s.context.baggageItem(key)
108+
func (s *span) GetBaggageItem(key string) string { // change this to GetBaggageItem
109+
return s.context.getBaggageItem(key)
110+
}
111+
112+
// GetAllBaggageItems returns a copy of all baggage items.
113+
// If no items exist, returns an empty map.
114+
func (s *span) GetAllBaggageItems() map[string]string {
115+
return s.context.getAllBaggageItems()
116+
}
117+
118+
// RemoveBaggageItem removes a single baggage item by its key.
119+
func (s *span) RemoveBaggageItem(key string) {
120+
s.context.removeBaggageItem(key)
121+
}
122+
123+
// RemoveAllBaggageItems removes all baggage items from the span.
124+
func (s *span) RemoveAllBaggageItems() {
125+
s.context.removeAllBaggageItems()
110126
}
111127

112128
// SetTag adds a set of key/value metadata to the span.

‎ddtrace/tracer/span_test.go‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,43 @@ func TestSpanBaggage(t *testing.T) {
5454

5555
span := newBasicSpan("web.request")
5656
span.SetBaggageItem("key", "value")
57-
assert.Equal("value", span.BaggageItem("key"))
57+
assert.Equal("value", span.GetBaggageItem("key"))
58+
}
59+
60+
func TestSpanBaggageRemove(t *testing.T) {
61+
assert := assert.New(t)
62+
63+
span := newBasicSpan("web.request")
64+
span.SetBaggageItem("key", "value")
65+
assert.Equal("value", span.GetBaggageItem("key"))
66+
// test remove baggage
67+
span.RemoveBaggageItem("key")
68+
assert.Equal("", span.GetBaggageItem("key"))
69+
}
70+
71+
func TestSpanBaggageRemoveAll(t *testing.T) {
72+
assert := assert.New(t)
73+
74+
span := newBasicSpan("web.request")
75+
span.SetBaggageItem("key", "value")
76+
span.SetBaggageItem("key2", "value2")
77+
assert.Equal("value", span.GetBaggageItem("key"))
78+
assert.Equal("value2", span.GetBaggageItem("key2"))
79+
// test remove all baggage
80+
span.RemoveAllBaggageItems()
81+
assert.Equal("", span.GetBaggageItem("key"))
82+
assert.Equal("", span.GetBaggageItem("key2"))
83+
}
84+
85+
func TestSpanBaggageGetAll(t *testing.T) {
86+
assert := assert.New(t)
87+
88+
span := newBasicSpan("web.request")
89+
span.SetBaggageItem("key", "value")
90+
span.SetBaggageItem("key2", "value2")
91+
baggage := span.GetAllBaggageItems()
92+
assert.Equal("value", baggage["key"])
93+
assert.Equal("value2", baggage["key2"])
5894
}
5995

6096
func TestSpanContext(t *testing.T) {

0 commit comments

Comments
 (0)