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

Updating handling of sample priority extraction #51

Merged
merged 1 commit into from
Jun 26, 2024
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
17 changes: 12 additions & 5 deletions propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ const (
priorityHeaderKey = "x-datadog-sampling-priority"
// TODO: other headers: https://github.com/DataDog/dd-trace-go/blob/4f0b6ac22e14082ee1443d502a35a99cd9459ee0/ddtrace/tracer/textmap.go#L73-L96

// These are typical sampling values for Datadog, but Datadog libraries actually support any integer values
// values >=1 mean the trace is sampled, values <= 0 mean the trace is not sampled
notSampled = "0"
isSampled = "1"
)

var (
empty = trace.SpanContext{}

errMalformedTraceID = errors.New("cannot parse Datadog trace ID as 64bit unsigned int from header")
errMalformedSpanID = errors.New("cannot parse Datadog span ID as 64bit unsigned int from header")
errInvalidTraceIDHeader = errors.New("invalid Datadog trace ID header found")
errInvalidSpanIDHeader = errors.New("invalid Datadog span ID header found")
errMalformedTraceID = errors.New("cannot parse Datadog trace ID as 64bit unsigned int from header")
errMalformedSpanID = errors.New("cannot parse Datadog span ID as 64bit unsigned int from header")
errInvalidTraceIDHeader = errors.New("invalid Datadog trace ID header found")
errInvalidSpanIDHeader = errors.New("invalid Datadog span ID header found")
errInvalidSamplingPriorityHeader = errors.New("invalid Datadog sampling priority header found")
)

// Propagator serializes Span Context to/from Datadog headers.
Expand Down Expand Up @@ -115,7 +118,11 @@ func extract(traceID, spanID, sampled string) (trace.SpanContext, error) {
}
}

if sampled == isSampled {
sampledInt, err := strconv.Atoi(sampled)
if err != nil {
return empty, errInvalidSamplingPriorityHeader
}
if sampledInt >= 1 {
scc.TraceFlags = trace.FlagsSampled
}

Expand Down
29 changes: 24 additions & 5 deletions propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ func TestExtractMultiple(t *testing.T) {
},
{
ddTraceIDSmall, ddParentIDSmall, "",
trace.SpanContextConfig{
TraceID: traceIDSmall,
SpanID: spanIDSmall,
},
nil,
trace.SpanContextConfig{},
errInvalidSamplingPriorityHeader,
},
{
"", ddParentID, "",
Expand All @@ -79,6 +76,28 @@ func TestExtractMultiple(t *testing.T) {
trace.SpanContextConfig{},
errInvalidSpanIDHeader,
},
{
ddTraceID, ddParentID, "foo",
trace.SpanContextConfig{},
errInvalidSamplingPriorityHeader,
},
{
ddTraceID, ddParentID, "2",
trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
},
nil,
},
{
ddTraceID, ddParentID, "-1",
trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
},
nil,
},
}

for _, test := range tests {
Expand Down