Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddtrace/tracer: mark top level spans #782

Merged
merged 2 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ bin/
*.cov
*.lock
*.swp
.idea
dd-trace-go.iml

/contrib/google.golang.org/grpc.v12/vendor/

Expand Down
10 changes: 10 additions & 0 deletions ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type span struct {
TraceID uint64 `msg:"trace_id"` // identifier of the root span
ParentID uint64 `msg:"parent_id"` // identifier of the span's direct parent
Error int32 `msg:"error"` // error status of the span; 0 means no errors
TopLevel bool `msg:"top_level"` // boolean indicating if the span is a top level
gbbr marked this conversation as resolved.
Show resolved Hide resolved

noDebugStack bool `msg:"-"` // disables debug stack traces
finished bool `msg:"-"` // true if the span has been submitted to a tracer.
Expand Down Expand Up @@ -256,6 +257,12 @@ func (s *span) setMetric(key string, v float64) {
}
}

// unsetMetric unsets a numeric tag, in our case called a metric. This method
// is not safe for concurrent use.
func (s *span) unsetMetric(key string) {
gbbr marked this conversation as resolved.
Show resolved Hide resolved
delete(s.Metrics, key)
}

// Finish closes this Span (but not its children) providing the duration
// of its part of the tracing session.
func (s *span) Finish(opts ...ddtrace.FinishOption) {
Expand Down Expand Up @@ -381,4 +388,7 @@ const (
keyRulesSamplerAppliedRate = "_dd.rule_psr"
keyRulesSamplerLimiterRate = "_dd.limit_psr"
keyMeasured = "_dd.measured"
// topLevelKey is the key of top level metric indicating if a span is top level.
// a top level span is a local root (parent span of the local trace) or the first span of each service.
gbbr marked this conversation as resolved.
Show resolved Hide resolved
topLevelKey = "_top_level"
gbbr marked this conversation as resolved.
Show resolved Hide resolved
)
23 changes: 21 additions & 2 deletions ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ const (
func TestSpanSetMetric(t *testing.T) {
for name, tt := range map[string]func(assert *assert.Assertions, span *span){
"init": func(assert *assert.Assertions, span *span) {
assert.Equal(2, len(span.Metrics))
assert.Equal(3, len(span.Metrics))
_, ok := span.Metrics[keySamplingPriority]
assert.True(ok)
_, ok = span.Metrics[keySamplingPriorityRate]
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestSpanSetMetric(t *testing.T) {
"finished": func(assert *assert.Assertions, span *span) {
span.Finish()
span.SetTag("finished.test", 1337)
assert.Equal(2, len(span.Metrics))
assert.Equal(3, len(span.Metrics))
_, ok := span.Metrics["finished.test"]
assert.False(ok)
},
Expand Down Expand Up @@ -500,6 +500,25 @@ func TestSpanLog(t *testing.T) {
})
}

func TestUnsetMetric(t *testing.T) {
t.Run("default", func(t *testing.T) {
span := span{Metrics: map[string]float64{"key": 1}}
span.unsetMetric("key")
_, ok := span.Metrics["key"]
assert.False(t, ok)
})
t.Run("nil metrics", func(t *testing.T) {
span := span{}
span.unsetMetric("key")
assert.Nil(t, span.Metrics)
})
t.Run("empty metrics", func(t *testing.T) {
span := span{Metrics: make(map[string]float64)}
span.unsetMetric("key")
assert.Len(t, span.Metrics, 0)
})
}

func BenchmarkSetTagMetric(b *testing.B) {
span := newBasicSpan("bench.span")
keys := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down
5 changes: 5 additions & 0 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
for k, v := range t.config.globalTags {
span.SetTag(k, v)
}
if context == nil || context.span == nil || context.span.Service != span.Service {
span.setMetric(topLevelKey, 1)
// all top level spans are measured. So the measured tag is redundant.
span.unsetMetric(keyMeasured)
}
if t.config.version != "" && span.Service == t.config.serviceName {
span.SetTag(ext.Version, t.config.version)
}
Expand Down
15 changes: 11 additions & 4 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,18 @@ func TestTracerStartSpan(t *testing.T) {
assert.Equal(t, "/home/user", span.Resource)
})

t.Run("measured", func(t *testing.T) {
t.Run("measured_top_level", func(t *testing.T) {
tracer := newTracer()
span := tracer.StartSpan("/home/user", Measured()).(*span)
assert.Equal(t, 1.0, span.Metrics[keyMeasured])
_, ok := span.Metrics[keyMeasured]
assert.False(t, ok)
assert.Equal(t, 1.0, span.Metrics[topLevelKey])
})
gbbr marked this conversation as resolved.
Show resolved Hide resolved
t.Run("measured_non_top_level", func(t *testing.T) {
tracer := newTracer()
parent := tracer.StartSpan("/home/user").(*span)
child := tracer.StartSpan("home/user", Measured(), ChildOf(parent.context)).(*span)
assert.Equal(t, 1.0, child.Metrics[keyMeasured])
})
}

Expand Down Expand Up @@ -277,7 +285,6 @@ func TestTracerStartSpanOptions(t *testing.T) {
ResourceName("test.resource"),
StartTime(now),
WithSpanID(420),
Measured(),
}
span := tracer.StartSpan("web.request", opts...).(*span)
assert := assert.New(t)
Expand All @@ -287,7 +294,7 @@ func TestTracerStartSpanOptions(t *testing.T) {
assert.Equal(now.UnixNano(), span.Start)
assert.Equal(uint64(420), span.SpanID)
assert.Equal(uint64(420), span.TraceID)
assert.Equal(1.0, span.Metrics[keyMeasured])
assert.Equal(1.0, span.Metrics[topLevelKey])
}

func TestTracerStartChildSpan(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions ddtrace/tracer/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import (
"gopkg.in/DataDog/dd-trace-go.v1/internal/version"
)

const (
// headerComputedTopLevel specifies that the client has marked top-level spans, when set.
// Any non-empty value will mean 'yes'.
headerComputedTopLevel = "Datadog-Client-Computed-Top-Level"
)

var defaultClient = &http.Client{
// We copy the transport to avoid using the default one, as it might be
// augmented with tracing and we don't want these calls to be recorded.
Expand Down Expand Up @@ -114,6 +120,7 @@ func (t *httpTransport) send(p *payload) (body io.ReadCloser, err error) {
}
req.Header.Set(traceCountHeader, strconv.Itoa(p.itemCount()))
req.Header.Set("Content-Length", strconv.Itoa(p.size()))
req.Header.Set(headerComputedTopLevel, "yes")
response, err := t.client.Do(req)
if err != nil {
return nil, err
Expand Down