Skip to content

Commit

Permalink
ddtrace/tracer: remove Span.Tag function (reverts #2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Apr 8, 2024
1 parent 8d1ea11 commit c8ff8d7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 53 deletions.
2 changes: 1 addition & 1 deletion ddtrace/mocktracer/mockspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *Span) Tag(k string) interface{} {
}
// It's possible that a tag wasn't set through mocktracer.Span.SetTag,
// in which case we need to retrieve it from the underlying tracer.Span.
v := s.sp.Tag(k)
v := s.sp.AsMap()[k]
if v != nil {
return v
}
Expand Down
34 changes: 0 additions & 34 deletions ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,40 +261,6 @@ func (s *Span) AddLink(spanContext *SpanContext, attributes map[string]string) {
})
}

// Tag returns the value for a given tag key.
// If the key does not exist, Tag returns nil.
// All values may be of type string, float64, or int32 (only for ext.Error).
// If the original tag value was not one of these types, Tag will return a string
// representation of the value.
// Use ext.Error to check if the span has an error, and ext.ErrorMsg, ext.ErrorType,
// and ext.ErrorStack to get the error details.
func (s *Span) Tag(k string) interface{} {
if s == nil {
return nil
}
s.RLock()
defer s.RUnlock()
switch k {
case ext.Error:
return s.error
case ext.SpanName:
return s.name
case ext.ServiceName:
return s.service
case ext.ResourceName:
return s.resource
case ext.SpanType:
return s.spanType
}
if v, ok := s.meta[k]; ok {
return v
}
if v, ok := s.metrics[k]; ok {
return v
}
return nil
}

// setSamplingPriority locks then span, then updates the sampling priority.
// It also updates the trace's sampling priority.
func (s *Span) setSamplingPriority(priority int, sampler samplernames.SamplerName) {
Expand Down
23 changes: 5 additions & 18 deletions ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestNilSpan(t *testing.T) {
t.Errorf("expected empty string, got %s", v)
}
span.SetTag("key", "value")
if v := span.Tag("key"); v != nil {
if v := span.AsMap()["key"]; v != nil {
t.Errorf("expected nil, got %s", v)
}
span.SetUser("user")
Expand Down Expand Up @@ -367,37 +367,27 @@ func TestSpanSetTag(t *testing.T) {
assert := assert.New(t)
span := newBasicSpan("web.request")
assert.Equal("web.request", span.name)
assert.Equal("web.request", span.Tag(ext.SpanName))

span.SetTag("component", "tracer")
assert.Equal("tracer", span.meta["component"])
assert.Equal("tracer", span.Tag("component"))

span.SetTag("tagInt", 1234)
assert.Equal(float64(1234), span.metrics["tagInt"])
assert.Equal(float64(1234), span.Tag("tagInt"))

span.SetTag("tagStruct", struct{ A, B int }{1, 2})
assert.Equal("{1 2}", span.meta["tagStruct"])
assert.Equal("{1 2}", span.Tag("tagStruct"))

span.SetTag(ext.Error, true)
assert.Equal(int32(1), span.error)
assert.Equal(int32(1), span.Tag(ext.Error))

span.SetTag(ext.Error, nil)
assert.Equal(int32(0), span.error)
assert.Equal(int32(0), span.Tag(ext.Error))

span.SetTag(ext.Error, errors.New("abc"))
assert.Equal(int32(1), span.error)
assert.Equal(int32(1), span.Tag(ext.Error))
assert.Equal("abc", span.meta[ext.ErrorMsg])
assert.Equal("abc", span.Tag(ext.ErrorMsg))
assert.Equal("*errors.errorString", span.meta[ext.ErrorType])
assert.Equal("*errors.errorString", span.Tag(ext.ErrorType))
assert.NotEmpty(span.meta[ext.ErrorStack])
assert.Equal(span.meta[ext.ErrorStack], span.Tag(ext.ErrorStack))

span.SetTag(ext.Error, "something else")
assert.Equal(int32(1), span.error)
Expand All @@ -407,19 +397,15 @@ func TestSpanSetTag(t *testing.T) {

span.SetTag("some.bool", true)
assert.Equal("true", span.meta["some.bool"])
assert.Equal("true", span.Tag("some.bool"))

span.SetTag("some.other.bool", false)
assert.Equal("false", span.meta["some.other.bool"])
assert.Equal("false", span.Tag("some.other.bool"))

span.SetTag("time", (*time.Time)(nil))
assert.Equal("<nil>", span.meta["time"])
assert.Equal("<nil>", span.Tag("time"))

span.SetTag("nilStringer", (*nilStringer)(nil))
assert.Equal("<nil>", span.meta["nilStringer"])
assert.Equal("<nil>", span.Tag("nilStringer"))

span.SetTag("somestrings", []string{"foo", "bar"})
assert.Equal("foo", span.meta["somestrings.0"])
Expand Down Expand Up @@ -466,9 +452,10 @@ func TestSpanTagsStartSpan(t *testing.T) {

span := tr.StartSpan("operation-name", ServiceName("service"), Tag("tag", "value"))

assert.Equal("value", span.Tag("tag"))
assert.Equal("service", span.Tag(ext.ServiceName))
assert.Equal("operation-name", span.Tag(ext.SpanName))
tags := span.AsMap()
assert.Equal("value", tags["tag"])
assert.Equal("service", tags[ext.ServiceName])
assert.Equal("operation-name", tags[ext.SpanName])
}

type testMsgpStruct struct {
Expand Down

0 comments on commit c8ff8d7

Please sign in to comment.