From 2ab2a428303ec004a005468c31c2f8e6ae0e5d45 Mon Sep 17 00:00:00 2001 From: Rachel Yang Date: Tue, 17 Dec 2024 22:11:54 +0900 Subject: [PATCH 1/2] baggage API --- ddtrace/ddtrace.go | 14 ++++++- ddtrace/internal/globaltracer.go | 13 ++++++- ddtrace/mocktracer/mockspan.go | 22 +++++++++-- ddtrace/mocktracer/mockspan_test.go | 27 ++++++++++++- ddtrace/mocktracer/mockspancontext.go | 34 ++++++++++++++++- ddtrace/mocktracer/mockspancontext_test.go | 28 +++++++++++++- ddtrace/mocktracer/mocktracer_test.go | 8 ++-- ddtrace/tracer/civisibility_tslv.go | 26 +++++++++++-- ddtrace/tracer/span.go | 22 +++++++++-- ddtrace/tracer/span_test.go | 38 ++++++++++++++++++- ddtrace/tracer/spancontext.go | 44 +++++++++++++++++++++- ddtrace/tracer/spancontext_test.go | 8 ++++ 12 files changed, 262 insertions(+), 22 deletions(-) diff --git a/ddtrace/ddtrace.go b/ddtrace/ddtrace.go index 928816ba6c..77f42ec525 100644 --- a/ddtrace/ddtrace.go +++ b/ddtrace/ddtrace.go @@ -72,13 +72,23 @@ type Span interface { // a representative name for a group of spans (e.g. "grpc.server" or "http.request"). SetOperationName(operationName string) - // BaggageItem returns the baggage item held by the given key. - BaggageItem(key string) string + // GetBaggageItem returns the baggage item held by the given key. + GetBaggageItem(key string) string // SetBaggageItem sets a new baggage item at the given key. The baggage // item should propagate to all descendant spans, both in- and cross-process. SetBaggageItem(key, val string) + // GetAllBaggageItems returns a copy of all baggage items. + // If no items exist, returns an empty map. + GetAllBaggageItems() map[string]string + + // RemoveBaggageItem removes a single baggage item by its key. + RemoveBaggageItem(key string) + + // RemoveAllBaggageItems removes all baggage items from the span. + RemoveAllBaggageItems() + // Finish finishes the current span with the given options. Finish calls should be idempotent. Finish(opts ...FinishOption) diff --git a/ddtrace/internal/globaltracer.go b/ddtrace/internal/globaltracer.go index 363d1f9983..9a7ba43582 100644 --- a/ddtrace/internal/globaltracer.go +++ b/ddtrace/internal/globaltracer.go @@ -75,11 +75,22 @@ func (NoopSpan) SetTag(_ string, _ interface{}) {} func (NoopSpan) SetOperationName(_ string) {} // BaggageItem implements ddtrace.Span. -func (NoopSpan) BaggageItem(_ string) string { return "" } +func (NoopSpan) GetBaggageItem(_ string) string { return "" } // SetBaggageItem implements ddtrace.Span. func (NoopSpan) SetBaggageItem(_, _ string) {} +// RemoveBaggageItem implements ddtrace.Span. +func (NoopSpan) RemoveBaggageItem(_ string) {} + +// RemoveAllBaggageItems implements ddtrace.Span. +func (NoopSpan) RemoveAllBaggageItems() {} + +// GetAllBaggageItems implements ddtrace.Span. +func (NoopSpan) GetAllBaggageItems() map[string]string { + return make(map[string]string) +} + // Finish implements ddtrace.Span. func (NoopSpan) Finish(_ ...ddtrace.FinishOption) {} diff --git a/ddtrace/mocktracer/mockspan.go b/ddtrace/mocktracer/mockspan.go index 4701d96c7f..b987530fd0 100644 --- a/ddtrace/mocktracer/mockspan.go +++ b/ddtrace/mocktracer/mockspan.go @@ -176,9 +176,9 @@ func (s *mockspan) SetOperationName(operationName string) { return } -// BaggageItem returns the baggage item with the given key. -func (s *mockspan) BaggageItem(key string) string { - return s.context.baggageItem(key) +// GetBaggageItem returns the baggage item with the given key. +func (s *mockspan) GetBaggageItem(key string) string { + return s.context.getBaggageItem(key) } // SetBaggageItem sets a new baggage item at the given key. The baggage @@ -188,6 +188,22 @@ func (s *mockspan) SetBaggageItem(key, val string) { return } +// RemoveBaggageItem removes the baggage item identified by the given key. +func (s *mockspan) RemoveBaggageItem(key string) { + s.context.removeBaggageItem(key) +} + +// RemoveAllBaggageItems removes all baggage items from this span. +func (s *mockspan) RemoveAllBaggageItems() { + s.context.removeAllBaggageItems() +} + +// GetAllBaggageItems returns a copy of all baggage items. +// If there are no items, it returns an empty map. +func (s *mockspan) GetAllBaggageItems() map[string]string { + return s.context.getAllBaggageItems() +} + // Finish finishes the current span with the given options. func (s *mockspan) Finish(opts ...ddtrace.FinishOption) { var cfg ddtrace.FinishConfig diff --git a/ddtrace/mocktracer/mockspan_test.go b/ddtrace/mocktracer/mockspan_test.go index 9d849ac82e..9e2040493a 100644 --- a/ddtrace/mocktracer/mockspan_test.go +++ b/ddtrace/mocktracer/mockspan_test.go @@ -144,7 +144,32 @@ func TestSpanBaggageFunctions(t *testing.T) { t.Run("BaggageItem", func(t *testing.T) { s := basicSpan("http.request") s.SetBaggageItem("a", "b") - assert.Equal(t, "b", s.BaggageItem("a")) + assert.Equal(t, "b", s.GetBaggageItem("a")) + }) + + t.Run("GetAllBaggageItems", func(t *testing.T) { + s := basicSpan("http.request") + s.SetBaggageItem("a", "b") + s.SetBaggageItem("c", "d") + baggage := s.GetAllBaggageItems() + assert.Len(t, baggage, 2) + assert.Equal(t, "b", baggage["a"]) + assert.Equal(t, "d", baggage["c"]) + }) + + t.Run("RemoveBaggageItem", func(t *testing.T) { + s := basicSpan("http.request") + s.SetBaggageItem("a", "b") + s.RemoveBaggageItem("a") + assert.Empty(t, s.context.baggage) + }) + + t.Run("RemoveAllBaggageItems", func(t *testing.T) { + s := basicSpan("http.request") + s.SetBaggageItem("a", "b") + s.SetBaggageItem("c", "d") + s.RemoveAllBaggageItems() + assert.Empty(t, s.context.baggage) }) } diff --git a/ddtrace/mocktracer/mockspancontext.go b/ddtrace/mocktracer/mockspancontext.go index 738a24cfff..9cca97dd46 100644 --- a/ddtrace/mocktracer/mockspancontext.go +++ b/ddtrace/mocktracer/mockspancontext.go @@ -48,12 +48,44 @@ func (sc *spanContext) setBaggageItem(k, v string) { sc.baggage[k] = v } -func (sc *spanContext) baggageItem(k string) string { +func (sc *spanContext) getBaggageItem(k string) string { sc.RLock() defer sc.RUnlock() return sc.baggage[k] } +func (sc *spanContext) removeBaggageItem(k string) { + sc.Lock() + defer sc.Unlock() + if sc.baggage == nil { + return + } + delete(sc.baggage, k) + if len(sc.baggage) == 0 { + sc.baggage = nil + } +} + +func (sc *spanContext) removeAllBaggageItems() { + sc.Lock() + defer sc.Unlock() + sc.baggage = nil +} + +func (sc *spanContext) getAllBaggageItems() map[string]string { + sc.RLock() + defer sc.RUnlock() + if sc.baggage == nil { + return make(map[string]string) + } + // Return a copy to avoid callers mutating the internal map + copyMap := make(map[string]string, len(sc.baggage)) + for key, val := range sc.baggage { + copyMap[key] = val + } + return copyMap +} + func (sc *spanContext) setSamplingPriority(p int) { sc.Lock() defer sc.Unlock() diff --git a/ddtrace/mocktracer/mockspancontext_test.go b/ddtrace/mocktracer/mockspancontext_test.go index 453e979e2b..b475e2c31c 100644 --- a/ddtrace/mocktracer/mockspancontext_test.go +++ b/ddtrace/mocktracer/mockspancontext_test.go @@ -35,8 +35,32 @@ func TestSpanContextGetBaggage(t *testing.T) { var sc spanContext sc.setBaggageItem("a", "b") sc.setBaggageItem("c", "d") - assert.Equal(t, sc.baggageItem("a"), "b") - assert.Equal(t, sc.baggageItem("c"), "d") + assert.Equal(t, sc.getBaggageItem("a"), "b") + assert.Equal(t, sc.getBaggageItem("c"), "d") +} + +func TestSpanContextGetAllBaggage(t *testing.T) { + var sc spanContext + sc.setBaggageItem("a", "b") + sc.setBaggageItem("c", "d") + assert.Equal(t, map[string]string{"a": "b", "c": "d"}, sc.getAllBaggageItems()) +} + +func TestSpanContextRemoveBaggage(t *testing.T) { + var sc spanContext + sc.setBaggageItem("a", "b") + sc.setBaggageItem("c", "d") + sc.removeBaggageItem("a") + assert.Equal(t, sc.baggage["c"], "d") + assert.Empty(t, sc.baggage["a"]) +} + +func TestSpanContextRemoveAllBaggage(t *testing.T) { + var sc spanContext + sc.setBaggageItem("a", "b") + sc.setBaggageItem("c", "d") + sc.removeAllBaggageItems() + assert.Empty(t, sc.baggage) } func TestSpanContextIterator(t *testing.T) { diff --git a/ddtrace/mocktracer/mocktracer_test.go b/ddtrace/mocktracer/mocktracer_test.go index fe2a2c229a..20a3fb39d2 100644 --- a/ddtrace/mocktracer/mocktracer_test.go +++ b/ddtrace/mocktracer/mocktracer_test.go @@ -278,8 +278,8 @@ func TestTracerExtract(t *testing.T) { assert.Nil(err) sc, ok = ctx.(*spanContext) assert.True(ok) - assert.Equal("B", sc.baggageItem("a")) - assert.Equal("D", sc.baggageItem("c")) + assert.Equal("B", sc.getBaggageItem("a")) + assert.Equal("D", sc.getBaggageItem("c")) ctx, err = mt.Extract(carry(traceHeader, "1", spanHeader, "2", priorityHeader, "-1")) assert.Nil(err) @@ -302,7 +302,7 @@ func TestTracerExtract(t *testing.T) { assert.Equal(uint64(1), got.traceID) assert.Equal(uint64(2), got.spanID) - assert.Equal("D", got.baggageItem("c")) - assert.Equal("B", got.baggageItem("a")) + assert.Equal("D", got.getBaggageItem("c")) + assert.Equal("B", got.getBaggageItem("a")) }) } diff --git a/ddtrace/tracer/civisibility_tslv.go b/ddtrace/tracer/civisibility_tslv.go index ef6614d48f..0f866110f0 100644 --- a/ddtrace/tracer/civisibility_tslv.go +++ b/ddtrace/tracer/civisibility_tslv.go @@ -177,7 +177,7 @@ func (e *ciVisibilityEvent) SetOperationName(operationName string) { e.Content.Name = e.span.Name } -// BaggageItem retrieves the baggage item associated with the given key from the event's span. +// GetBaggageItem retrieves the baggage item associated with the given key from the event's span. // // Parameters: // @@ -186,8 +186,8 @@ func (e *ciVisibilityEvent) SetOperationName(operationName string) { // Returns: // // The baggage item value. -func (e *ciVisibilityEvent) BaggageItem(key string) string { - return e.span.BaggageItem(key) +func (e *ciVisibilityEvent) GetBaggageItem(key string) string { + return e.span.GetBaggageItem(key) } // SetBaggageItem sets a baggage item on the event's span. @@ -200,6 +200,26 @@ func (e *ciVisibilityEvent) SetBaggageItem(key, val string) { e.span.SetBaggageItem(key, val) } +// GetAllBaggageItems returns a copy of all baggage items from the event's span. +// If there are no items, it returns an empty map. +func (e *ciVisibilityEvent) GetAllBaggageItems() map[string]string { + return e.span.GetAllBaggageItems() +} + +// RemoveBaggageItem removes a baggage item from the event's span. +// +// Parameters: +// +// key - The baggage item key. +func (e *ciVisibilityEvent) RemoveBaggageItem(key string) { + e.span.RemoveBaggageItem(key) +} + +// RemoveAllBaggageItems removes all baggage items from the event's span. +func (e *ciVisibilityEvent) RemoveAllBaggageItems() { + e.span.RemoveAllBaggageItems() +} + // Finish completes the event's span with optional finish options. // // Parameters: diff --git a/ddtrace/tracer/span.go b/ddtrace/tracer/span.go index 27437ff5c4..c14e96ff3e 100644 --- a/ddtrace/tracer/span.go +++ b/ddtrace/tracer/span.go @@ -103,10 +103,26 @@ func (s *span) SetBaggageItem(key, val string) { s.context.setBaggageItem(key, val) } -// BaggageItem gets the value for a baggage item given its key. Returns the +// GetBaggageItem gets the value for a baggage item given its key. Returns the // empty string if the value isn't found in this Span. -func (s *span) BaggageItem(key string) string { - return s.context.baggageItem(key) +func (s *span) GetBaggageItem(key string) string { // change this to GetBaggageItem + return s.context.getBaggageItem(key) +} + +// GetAllBaggageItems returns a copy of all baggage items. +// If no items exist, returns an empty map. +func (s *span) GetAllBaggageItems() map[string]string { + return s.context.getAllBaggageItems() +} + +// RemoveBaggageItem removes a single baggage item by its key. +func (s *span) RemoveBaggageItem(key string) { + s.context.removeBaggageItem(key) +} + +// RemoveAllBaggageItems removes all baggage items from the span. +func (s *span) RemoveAllBaggageItems() { + s.context.removeAllBaggageItems() } // SetTag adds a set of key/value metadata to the span. diff --git a/ddtrace/tracer/span_test.go b/ddtrace/tracer/span_test.go index 51ff73f3fb..4ffc614067 100644 --- a/ddtrace/tracer/span_test.go +++ b/ddtrace/tracer/span_test.go @@ -54,7 +54,43 @@ func TestSpanBaggage(t *testing.T) { span := newBasicSpan("web.request") span.SetBaggageItem("key", "value") - assert.Equal("value", span.BaggageItem("key")) + assert.Equal("value", span.GetBaggageItem("key")) +} + +func TestSpanBaggageRemove(t *testing.T) { + assert := assert.New(t) + + span := newBasicSpan("web.request") + span.SetBaggageItem("key", "value") + assert.Equal("value", span.GetBaggageItem("key")) + // test remove baggage + span.RemoveBaggageItem("key") + assert.Equal("", span.GetBaggageItem("key")) +} + +func TestSpanBaggageRemoveAll(t *testing.T) { + assert := assert.New(t) + + span := newBasicSpan("web.request") + span.SetBaggageItem("key", "value") + span.SetBaggageItem("key2", "value2") + assert.Equal("value", span.GetBaggageItem("key")) + assert.Equal("value2", span.GetBaggageItem("key2")) + // test remove all baggage + span.RemoveAllBaggageItems() + assert.Equal("", span.GetBaggageItem("key")) + assert.Equal("", span.GetBaggageItem("key2")) +} + +func TestSpanBaggageGetAll(t *testing.T) { + assert := assert.New(t) + + span := newBasicSpan("web.request") + span.SetBaggageItem("key", "value") + span.SetBaggageItem("key2", "value2") + baggage := span.GetAllBaggageItems() + assert.Equal("value", baggage["key"]) + assert.Equal("value2", baggage["key2"]) } func TestSpanContext(t *testing.T) { diff --git a/ddtrace/tracer/spancontext.go b/ddtrace/tracer/spancontext.go index 30a791ee8e..a6052e0115 100644 --- a/ddtrace/tracer/spancontext.go +++ b/ddtrace/tracer/spancontext.go @@ -232,7 +232,7 @@ func (c *spanContext) setBaggageItem(key, val string) { c.baggage[key] = val } -func (c *spanContext) baggageItem(key string) string { +func (c *spanContext) getBaggageItem(key string) string { // change this function name to getBaggageItem if atomic.LoadUint32(&c.hasBaggage) == 0 { return "" } @@ -241,6 +241,48 @@ func (c *spanContext) baggageItem(key string) string { return c.baggage[key] } +func (c *spanContext) getAllBaggageItems() map[string]string { + if atomic.LoadUint32(&c.hasBaggage) == 0 { + // Return an empty map to avoid nil issues + return map[string]string{} + } + c.mu.RLock() + defer c.mu.RUnlock() + + copy := make(map[string]string, len(c.baggage)) + for k, v := range c.baggage { + copy[k] = v + } + return copy +} + +func (c *spanContext) removeBaggageItem(key string) { + if atomic.LoadUint32(&c.hasBaggage) == 0 { + return + } + c.mu.Lock() + defer c.mu.Unlock() + if _, ok := c.baggage[key]; ok { + delete(c.baggage, key) + // If baggage is now empty, reset hasBaggage + if len(c.baggage) == 0 { + atomic.StoreUint32(&c.hasBaggage, 0) + c.baggage = nil + } + } +} + +func (c *spanContext) removeAllBaggageItems() { + if atomic.LoadUint32(&c.hasBaggage) == 0 { + return + } + c.mu.Lock() + defer c.mu.Unlock() + // Clear baggage + c.baggage = nil + atomic.StoreUint32(&c.hasBaggage, 0) +} + func (c *spanContext) meta(key string) (val string, ok bool) { c.span.RLock() defer c.span.RUnlock() diff --git a/ddtrace/tracer/spancontext_test.go b/ddtrace/tracer/spancontext_test.go index 7e86d9fdb1..4a247c9336 100644 --- a/ddtrace/tracer/spancontext_test.go +++ b/ddtrace/tracer/spancontext_test.go @@ -783,6 +783,14 @@ func TestSpanContextBaggage(t *testing.T) { var ctx spanContext ctx.setBaggageItem("key", "value") assert.Equal("value", ctx.baggage["key"]) + ctx.setBaggageItem("foo", "bar") + assert.Equal("bar", ctx.getBaggageItem("foo")) + ctx.removeBaggageItem("key") + assert.NotContains(ctx.baggage, "key") + ctx.setBaggageItem("key1", "value1") + assert.Equal(map[string]string{"foo": "bar", "key1": "value1"}, ctx.getAllBaggageItems()) + ctx.removeAllBaggageItems() + assert.Empty(ctx.baggage) } func TestSpanContextIterator(t *testing.T) { From c653cfd20955a584a8cd3f67b05857dc8c25ad0a Mon Sep 17 00:00:00 2001 From: Rachel Yang Date: Wed, 18 Dec 2024 20:34:57 +0900 Subject: [PATCH 2/2] reverting baggageItem name change --- ddtrace/ddtrace.go | 4 ++-- ddtrace/internal/globaltracer.go | 2 +- ddtrace/mocktracer/mockspan.go | 6 +++--- ddtrace/mocktracer/mockspan_test.go | 2 +- ddtrace/mocktracer/mockspancontext.go | 2 +- ddtrace/mocktracer/mockspancontext_test.go | 6 +++--- ddtrace/mocktracer/mocktracer_test.go | 8 ++++---- ddtrace/tracer/civisibility_tslv.go | 6 +++--- ddtrace/tracer/span.go | 6 +++--- ddtrace/tracer/span_test.go | 14 +++++++------- ddtrace/tracer/spancontext.go | 2 +- ddtrace/tracer/spancontext_test.go | 2 +- 12 files changed, 30 insertions(+), 30 deletions(-) diff --git a/ddtrace/ddtrace.go b/ddtrace/ddtrace.go index 77f42ec525..d1a2d076a2 100644 --- a/ddtrace/ddtrace.go +++ b/ddtrace/ddtrace.go @@ -72,8 +72,8 @@ type Span interface { // a representative name for a group of spans (e.g. "grpc.server" or "http.request"). SetOperationName(operationName string) - // GetBaggageItem returns the baggage item held by the given key. - GetBaggageItem(key string) string + // BaggageItem returns the baggage item held by the given key. + BaggageItem(key string) string // SetBaggageItem sets a new baggage item at the given key. The baggage // item should propagate to all descendant spans, both in- and cross-process. diff --git a/ddtrace/internal/globaltracer.go b/ddtrace/internal/globaltracer.go index 9a7ba43582..e160477fb0 100644 --- a/ddtrace/internal/globaltracer.go +++ b/ddtrace/internal/globaltracer.go @@ -75,7 +75,7 @@ func (NoopSpan) SetTag(_ string, _ interface{}) {} func (NoopSpan) SetOperationName(_ string) {} // BaggageItem implements ddtrace.Span. -func (NoopSpan) GetBaggageItem(_ string) string { return "" } +func (NoopSpan) BaggageItem(_ string) string { return "" } // SetBaggageItem implements ddtrace.Span. func (NoopSpan) SetBaggageItem(_, _ string) {} diff --git a/ddtrace/mocktracer/mockspan.go b/ddtrace/mocktracer/mockspan.go index b987530fd0..9ad33cf01c 100644 --- a/ddtrace/mocktracer/mockspan.go +++ b/ddtrace/mocktracer/mockspan.go @@ -176,9 +176,9 @@ func (s *mockspan) SetOperationName(operationName string) { return } -// GetBaggageItem returns the baggage item with the given key. -func (s *mockspan) GetBaggageItem(key string) string { - return s.context.getBaggageItem(key) +// BaggageItem returns the baggage item with the given key. +func (s *mockspan) BaggageItem(key string) string { + return s.context.baggageItem(key) } // SetBaggageItem sets a new baggage item at the given key. The baggage diff --git a/ddtrace/mocktracer/mockspan_test.go b/ddtrace/mocktracer/mockspan_test.go index 9e2040493a..18fdde12c7 100644 --- a/ddtrace/mocktracer/mockspan_test.go +++ b/ddtrace/mocktracer/mockspan_test.go @@ -144,7 +144,7 @@ func TestSpanBaggageFunctions(t *testing.T) { t.Run("BaggageItem", func(t *testing.T) { s := basicSpan("http.request") s.SetBaggageItem("a", "b") - assert.Equal(t, "b", s.GetBaggageItem("a")) + assert.Equal(t, "b", s.BaggageItem("a")) }) t.Run("GetAllBaggageItems", func(t *testing.T) { diff --git a/ddtrace/mocktracer/mockspancontext.go b/ddtrace/mocktracer/mockspancontext.go index 9cca97dd46..df8d4218c7 100644 --- a/ddtrace/mocktracer/mockspancontext.go +++ b/ddtrace/mocktracer/mockspancontext.go @@ -48,7 +48,7 @@ func (sc *spanContext) setBaggageItem(k, v string) { sc.baggage[k] = v } -func (sc *spanContext) getBaggageItem(k string) string { +func (sc *spanContext) baggageItem(k string) string { sc.RLock() defer sc.RUnlock() return sc.baggage[k] diff --git a/ddtrace/mocktracer/mockspancontext_test.go b/ddtrace/mocktracer/mockspancontext_test.go index b475e2c31c..d87319e0b3 100644 --- a/ddtrace/mocktracer/mockspancontext_test.go +++ b/ddtrace/mocktracer/mockspancontext_test.go @@ -31,12 +31,12 @@ func TestSpanContextSetBaggage(t *testing.T) { assert.Equal(t, sc.baggage["c"], "d") } -func TestSpanContextGetBaggage(t *testing.T) { +func TestSpanContextBaggage(t *testing.T) { var sc spanContext sc.setBaggageItem("a", "b") sc.setBaggageItem("c", "d") - assert.Equal(t, sc.getBaggageItem("a"), "b") - assert.Equal(t, sc.getBaggageItem("c"), "d") + assert.Equal(t, sc.baggageItem("a"), "b") + assert.Equal(t, sc.baggageItem("c"), "d") } func TestSpanContextGetAllBaggage(t *testing.T) { diff --git a/ddtrace/mocktracer/mocktracer_test.go b/ddtrace/mocktracer/mocktracer_test.go index 20a3fb39d2..fe2a2c229a 100644 --- a/ddtrace/mocktracer/mocktracer_test.go +++ b/ddtrace/mocktracer/mocktracer_test.go @@ -278,8 +278,8 @@ func TestTracerExtract(t *testing.T) { assert.Nil(err) sc, ok = ctx.(*spanContext) assert.True(ok) - assert.Equal("B", sc.getBaggageItem("a")) - assert.Equal("D", sc.getBaggageItem("c")) + assert.Equal("B", sc.baggageItem("a")) + assert.Equal("D", sc.baggageItem("c")) ctx, err = mt.Extract(carry(traceHeader, "1", spanHeader, "2", priorityHeader, "-1")) assert.Nil(err) @@ -302,7 +302,7 @@ func TestTracerExtract(t *testing.T) { assert.Equal(uint64(1), got.traceID) assert.Equal(uint64(2), got.spanID) - assert.Equal("D", got.getBaggageItem("c")) - assert.Equal("B", got.getBaggageItem("a")) + assert.Equal("D", got.baggageItem("c")) + assert.Equal("B", got.baggageItem("a")) }) } diff --git a/ddtrace/tracer/civisibility_tslv.go b/ddtrace/tracer/civisibility_tslv.go index 0f866110f0..70bb83306f 100644 --- a/ddtrace/tracer/civisibility_tslv.go +++ b/ddtrace/tracer/civisibility_tslv.go @@ -177,7 +177,7 @@ func (e *ciVisibilityEvent) SetOperationName(operationName string) { e.Content.Name = e.span.Name } -// GetBaggageItem retrieves the baggage item associated with the given key from the event's span. +// BaggageItem retrieves the baggage item associated with the given key from the event's span. // // Parameters: // @@ -186,8 +186,8 @@ func (e *ciVisibilityEvent) SetOperationName(operationName string) { // Returns: // // The baggage item value. -func (e *ciVisibilityEvent) GetBaggageItem(key string) string { - return e.span.GetBaggageItem(key) +func (e *ciVisibilityEvent) BaggageItem(key string) string { + return e.span.BaggageItem(key) } // SetBaggageItem sets a baggage item on the event's span. diff --git a/ddtrace/tracer/span.go b/ddtrace/tracer/span.go index c14e96ff3e..765d703155 100644 --- a/ddtrace/tracer/span.go +++ b/ddtrace/tracer/span.go @@ -103,10 +103,10 @@ func (s *span) SetBaggageItem(key, val string) { s.context.setBaggageItem(key, val) } -// GetBaggageItem gets the value for a baggage item given its key. Returns the +// BaggageItem gets the value for a baggage item given its key. Returns the // empty string if the value isn't found in this Span. -func (s *span) GetBaggageItem(key string) string { // change this to GetBaggageItem - return s.context.getBaggageItem(key) +func (s *span) BaggageItem(key string) string { + return s.context.baggageItem(key) } // GetAllBaggageItems returns a copy of all baggage items. diff --git a/ddtrace/tracer/span_test.go b/ddtrace/tracer/span_test.go index 4ffc614067..a6b6798f18 100644 --- a/ddtrace/tracer/span_test.go +++ b/ddtrace/tracer/span_test.go @@ -54,7 +54,7 @@ func TestSpanBaggage(t *testing.T) { span := newBasicSpan("web.request") span.SetBaggageItem("key", "value") - assert.Equal("value", span.GetBaggageItem("key")) + assert.Equal("value", span.BaggageItem("key")) } func TestSpanBaggageRemove(t *testing.T) { @@ -62,10 +62,10 @@ func TestSpanBaggageRemove(t *testing.T) { span := newBasicSpan("web.request") span.SetBaggageItem("key", "value") - assert.Equal("value", span.GetBaggageItem("key")) + assert.Equal("value", span.BaggageItem("key")) // test remove baggage span.RemoveBaggageItem("key") - assert.Equal("", span.GetBaggageItem("key")) + assert.Equal("", span.BaggageItem("key")) } func TestSpanBaggageRemoveAll(t *testing.T) { @@ -74,12 +74,12 @@ func TestSpanBaggageRemoveAll(t *testing.T) { span := newBasicSpan("web.request") span.SetBaggageItem("key", "value") span.SetBaggageItem("key2", "value2") - assert.Equal("value", span.GetBaggageItem("key")) - assert.Equal("value2", span.GetBaggageItem("key2")) + assert.Equal("value", span.BaggageItem("key")) + assert.Equal("value2", span.BaggageItem("key2")) // test remove all baggage span.RemoveAllBaggageItems() - assert.Equal("", span.GetBaggageItem("key")) - assert.Equal("", span.GetBaggageItem("key2")) + assert.Equal("", span.BaggageItem("key")) + assert.Equal("", span.BaggageItem("key2")) } func TestSpanBaggageGetAll(t *testing.T) { diff --git a/ddtrace/tracer/spancontext.go b/ddtrace/tracer/spancontext.go index a6052e0115..000e0b2c52 100644 --- a/ddtrace/tracer/spancontext.go +++ b/ddtrace/tracer/spancontext.go @@ -232,7 +232,7 @@ func (c *spanContext) setBaggageItem(key, val string) { c.baggage[key] = val } -func (c *spanContext) getBaggageItem(key string) string { // change this function name to getBaggageItem +func (c *spanContext) baggageItem(key string) string { if atomic.LoadUint32(&c.hasBaggage) == 0 { return "" } diff --git a/ddtrace/tracer/spancontext_test.go b/ddtrace/tracer/spancontext_test.go index 4a247c9336..374dc3cc0b 100644 --- a/ddtrace/tracer/spancontext_test.go +++ b/ddtrace/tracer/spancontext_test.go @@ -784,7 +784,7 @@ func TestSpanContextBaggage(t *testing.T) { ctx.setBaggageItem("key", "value") assert.Equal("value", ctx.baggage["key"]) ctx.setBaggageItem("foo", "bar") - assert.Equal("bar", ctx.getBaggageItem("foo")) + assert.Equal("bar", ctx.baggageItem("foo")) ctx.removeBaggageItem("key") assert.NotContains(ctx.baggage, "key") ctx.setBaggageItem("key1", "value1")