Skip to content

Commit

Permalink
ddtrace/mocktracer: update mockspan to also hold source
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahkm committed Dec 10, 2024
1 parent 7601d35 commit 13ecd2c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions ddtrace/mocktracer/mockspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Span interface {
func newSpan(t *mocktracer, operationName string, cfg *ddtrace.StartSpanConfig) *mockspan {
if cfg.Tags == nil {
cfg.Tags = make(map[string]interface{})
cfg.Tags[ext.Component] = "manual"
}
if cfg.Tags[ext.ResourceName] == nil {
cfg.Tags[ext.ResourceName] = operationName
Expand Down Expand Up @@ -102,6 +103,7 @@ type mockspan struct {
tags map[string]interface{}
finishTime time.Time
finished bool
source string

startTime time.Time
parentID uint64
Expand All @@ -128,6 +130,9 @@ func (s *mockspan) SetTag(key string, value interface{}) {
s.context.setSamplingPriority(int(p))
}
}
if key == ext.Component {
s.source = value.(string)
}
s.tags[key] = value
}

Expand Down Expand Up @@ -284,3 +289,10 @@ func (s *mockspan) Root() tracer.Span {
root, _ := current.(*mockspan)
return root
}

// Source returns the component from which the mockspan was created.
// This is used to test the source tag of the `datadog.tracer.spans_{started,finished}`
// health metrics.
func (s *mockspan) Source() string {
return s.source
}
25 changes: 24 additions & 1 deletion ddtrace/mocktracer/mockspan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestNewSpan(t *testing.T) {
assert.NotNil(s.context)
assert.NotZero(s.context.spanID)
assert.Equal(s.context.spanID, s.context.traceID)
assert.Equal("manual", s.source)
})

t.Run("options", func(t *testing.T) {
Expand Down Expand Up @@ -65,6 +66,20 @@ func TestNewSpan(t *testing.T) {
assert.Equal(uint64(2), s.context.traceID)
assert.Equal(baggage, s.context.baggage)
})
t.Run("custom_source", func(t *testing.T) {
tr := new(mocktracer)
parentctx := &spanContext{spanID: 1, traceID: 2}
tags := make(map[string]interface{})
tags[ext.Component] = "sourceName"
opts := &ddtrace.StartSpanConfig{Parent: parentctx, Tags: tags}
s := newSpan(tr, "opname", opts)

assert := assert.New(t)
assert.NotNil(s.context)
assert.Equal(uint64(1), s.parentID)
assert.Equal(uint64(2), s.context.traceID)
assert.Equal("sourceName", s.Source())
})
}

func TestSpanSetTag(t *testing.T) {
Expand All @@ -73,7 +88,7 @@ func TestSpanSetTag(t *testing.T) {
s.SetTag("c", "d")

assert := assert.New(t)
assert.Len(s.Tags(), 3)
assert.Len(s.Tags(), 4)
assert.Equal("http.request", s.Tag(ext.ResourceName))
assert.Equal("b", s.Tag("a"))
assert.Equal("d", s.Tag("c"))
Expand All @@ -88,6 +103,14 @@ func TestSpanSetTagPriority(t *testing.T) {
assert.Equal(-1, s.context.samplingPriority())
}

func TestSpanSetTagComponent(t *testing.T) {
assert := assert.New(t)
s := basicSpan("http.request")
assert.Equal(s.Source(), "manual")
s.SetTag(ext.Component, "custom")
assert.Equal(s.Source(), "custom")
}

func TestSpanTagImmutability(t *testing.T) {
s := basicSpan("http.request")
s.SetTag("a", "b")
Expand Down

0 comments on commit 13ecd2c

Please sign in to comment.