Skip to content

Commit

Permalink
Updating handling of sample priority extraction (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlawrienyt authored Jun 26, 2024
1 parent 4d5a468 commit f97dc3d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
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

0 comments on commit f97dc3d

Please sign in to comment.