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

tracer: dual sampling, make sure priorities -1 and 2 are handled #137

Merged
merged 3 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions tracer/ext/priority.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ext

// Priority is a hint given to the backend so that it knows which traces to reject or kept.
// In a distributed context, it should be set before any context propagation (fork, RPC calls) to be effective.

const (
// PriorityUserReject informs the backend that a trace should be rejected and not stored.
// This should be used by user code overriding default priority.
PriorityUserReject = -1
// PriorityAutoReject informs the backend that a trace should be rejected and not stored.
// This is used by the builtin sampler.
PriorityAutoReject = 0
// PriorityAutoReject informs the backend that a trace should be kept and not stored.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ufoot Looks like the name here (PriorityAutoReject) doesn't match the description (PriorityAutoKeep)? Typo?

// This is used by the builtin sampler.
PriorityAutoKeep = 1
// PriorityUserReject informs the backend that a trace should be kept and not stored.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ufoot And here

// This should be used by user code overriding default priority.
PriorityUserKeep = 2
)
6 changes: 1 addition & 5 deletions tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,8 @@ func (s *Span) Tracer() *Tracer {
}

// SetSamplingPriority sets the sampling priority.
// Default is 0, any higher value is interpreted as a hint on
// how interesting this span is, and should be kept by the backend.
func (s *Span) SetSamplingPriority(priority int) {
if priority >= 0 {
s.SetMetric(samplingPriorityKey, float64(priority))
}
s.SetMetric(samplingPriorityKey, float64(priority))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Maybe add the -1 case to the method's comment?

}

// HasSamplingPriority returns true if sampling priority is set.
Expand Down
48 changes: 17 additions & 31 deletions tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"github.com/stretchr/testify/assert"

"github.com/DataDog/dd-trace-go/tracer/ext"
)

func TestSpanStart(t *testing.T) {
Expand Down Expand Up @@ -265,37 +267,21 @@ func TestSpanSamplingPriority(t *testing.T) {
assert.Equal(span.HasSamplingPriority(), childSpan.HasSamplingPriority())
assert.Equal(span.GetSamplingPriority(), childSpan.GetSamplingPriority())

span.SetSamplingPriority(0)
assert.Equal(0.0, span.Metrics["_sampling_priority_v1"], "key has been deleted")
assert.Equal(0, span.GetSamplingPriority(), "by default, sampling priority is 0")
childSpan = tracer.NewChildSpan("my.child", span)
assert.Equal(span.Metrics["_sampling_priority_v1"], childSpan.Metrics["_sampling_priority_v1"])
assert.Equal(span.HasSamplingPriority(), childSpan.HasSamplingPriority())
assert.Equal(span.GetSamplingPriority(), childSpan.GetSamplingPriority())

span.SetSamplingPriority(-1)
assert.Equal(0.0, span.Metrics["_sampling_priority_v1"], "key has been deleted")
assert.Equal(0, span.GetSamplingPriority(), "by default, sampling priority can't be negative")
childSpan = tracer.NewChildSpan("my.child", span)
assert.Equal(span.Metrics["_sampling_priority_v1"], childSpan.Metrics["_sampling_priority_v1"])
assert.Equal(span.HasSamplingPriority(), childSpan.HasSamplingPriority())
assert.Equal(span.GetSamplingPriority(), childSpan.GetSamplingPriority())

span.SetSamplingPriority(1)
assert.Equal(1.0, span.Metrics["_sampling_priority_v1"], "sampling priority is now 1")
assert.Equal(1, span.GetSamplingPriority(), "sampling priority is now 1")
childSpan = tracer.NewChildSpan("my.child", span)
assert.Equal(span.Metrics["_sampling_priority_v1"], childSpan.Metrics["_sampling_priority_v1"])
assert.Equal(span.HasSamplingPriority(), childSpan.HasSamplingPriority())
assert.Equal(span.GetSamplingPriority(), childSpan.GetSamplingPriority())

span.SetSamplingPriority(42)
assert.Equal(42.0, span.Metrics["_sampling_priority_v1"], "sampling priority works for values above 1")
assert.Equal(42, span.GetSamplingPriority(), "sampling priority works for values above 1")
childSpan = tracer.NewChildSpan("my.child", span)
assert.Equal(span.Metrics["_sampling_priority_v1"], childSpan.Metrics["_sampling_priority_v1"])
assert.Equal(span.HasSamplingPriority(), childSpan.HasSamplingPriority())
assert.Equal(span.GetSamplingPriority(), childSpan.GetSamplingPriority())
for _, priority := range []int{
ext.PriorityUserReject,
ext.PriorityAutoReject,
ext.PriorityAutoKeep,
ext.PriorityUserKeep,
999, // not used yet, but we should allow it
} {
span.SetSamplingPriority(priority)
assert.True(span.HasSamplingPriority())
assert.Equal(priority, span.GetSamplingPriority())
childSpan = tracer.NewChildSpan("my.child", span)
assert.Equal(span.Metrics["_sampling_priority_v1"], childSpan.Metrics["_sampling_priority_v1"])
assert.Equal(span.HasSamplingPriority(), childSpan.HasSamplingPriority())
assert.Equal(span.GetSamplingPriority(), childSpan.GetSamplingPriority())
}
}

type boomError struct{}
Expand Down