Skip to content

Commit

Permalink
ddtrace/tracer: add manual.keep and manual.drop tags (#430)
Browse files Browse the repository at this point in the history
Add tag aliases for simplifying user-controlled sampling priorities.
  • Loading branch information
gbbr authored Apr 18, 2019
1 parent d6797f5 commit eed4d38
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
8 changes: 8 additions & 0 deletions ddtrace/ext/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ const (
// AnalyticsEvent specifies whether the span should be recorded as a Trace
// Search & Analytics event.
AnalyticsEvent = "analytics.event"

// ManualKeep is a tag which specifies that the trace to which this span
// belongs to should be kept when set to true.
ManualKeep = "manual.keep"

// ManualDrop is a tag which specifies that the trace to which this span
// belongs to should be dropped when set to true.
ManualDrop = "manual.drop"
)
41 changes: 30 additions & 11 deletions ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,10 @@ func (s *span) SetTag(key string, value interface{}) {
case ext.Error:
s.setTagError(value, true)
return
case ext.AnalyticsEvent:
// "analytics.event" is a boolean alias for setting the event sampling
// rate to 0.0 or 1.0
if set, ok := value.(bool); ok {
if set {
s.setTagNumeric(ext.EventSampleRate, 1.0)
} else {
s.setTagNumeric(ext.EventSampleRate, 0.0)
}
return
}
}
if v, ok := value.(bool); ok {
s.setTagBool(key, v)
return
}
if v, ok := value.(string); ok {
s.setTagString(key, v)
Expand Down Expand Up @@ -158,6 +151,32 @@ func (s *span) setTagString(key, v string) {
}
}

// setTagBool sets a boolean tag on the span.
func (s *span) setTagBool(key string, v bool) {
switch key {
case ext.AnalyticsEvent:
if v {
s.setTagNumeric(ext.EventSampleRate, 1.0)
} else {
s.setTagNumeric(ext.EventSampleRate, 0.0)
}
case ext.ManualDrop:
if v {
s.setTagNumeric(ext.SamplingPriority, ext.PriorityUserReject)
}
case ext.ManualKeep:
if v {
s.setTagNumeric(ext.SamplingPriority, ext.PriorityUserKeep)
}
default:
if v {
s.setTagString(key, "true")
} else {
s.setTagString(key, "false")
}
}
}

// setTagNumeric sets a numeric tag, in our case called a metric. This method
// is not safe for concurrent use.
func (s *span) setTagNumeric(key string, v float64) {
Expand Down
12 changes: 12 additions & 0 deletions ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ func TestSpanSetTag(t *testing.T) {

span.SetTag(ext.AnalyticsEvent, false)
assert.Equal(0.0, span.Metrics[ext.EventSampleRate])

span.SetTag(ext.ManualDrop, true)
assert.Equal(-1., span.Metrics[keySamplingPriority])

span.SetTag(ext.ManualKeep, true)
assert.Equal(2., span.Metrics[keySamplingPriority])

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

span.SetTag("some.other.bool", false)
assert.Equal("false", span.Meta["some.other.bool"])
}

func TestSpanSetDatadogTags(t *testing.T) {
Expand Down

0 comments on commit eed4d38

Please sign in to comment.