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: support int64 ids for the text map reader #326

Merged
merged 3 commits into from
Sep 11, 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
4 changes: 2 additions & 2 deletions ddtrace/tracer/textmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ func (p *propagator) extractTextMap(reader TextMapReader) (ddtrace.SpanContext,
key := strings.ToLower(k)
switch key {
case p.cfg.TraceHeader:
ctx.traceID, err = strconv.ParseUint(v, 10, 64)
ctx.traceID, err = parseUint64(v)
if err != nil {
return ErrSpanContextCorrupted
}
case p.cfg.ParentHeader:
ctx.spanID, err = strconv.ParseUint(v, 10, 64)
ctx.spanID, err = parseUint64(v)
if err != nil {
return ErrSpanContextCorrupted
}
Expand Down
18 changes: 18 additions & 0 deletions ddtrace/tracer/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package tracer

import (
"strconv"
"strings"
)

// toFloat64 attempts to convert value into a float64. If it succeeds it returns
// the value and true, otherwise 0 and false.
func toFloat64(value interface{}) (f float64, ok bool) {
Expand Down Expand Up @@ -30,3 +35,16 @@ func toFloat64(value interface{}) (f float64, ok bool) {
return 0, false
}
}

// parseUint64 parses a uint64 from either an unsigned 64 bit base-10 string
// or a signed 64 bit base-10 string representing an unsigned integer
func parseUint64(str string) (uint64, error) {
if strings.HasPrefix(str, "-") {
id, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0, err
}
return uint64(id), nil
}
return strconv.ParseUint(str, 10, 64)
}
22 changes: 22 additions & 0 deletions ddtrace/tracer/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package tracer

import (
"fmt"
"math"
"testing"

"github.com/stretchr/testify/assert"
)

func TestToFloat64(t *testing.T) {
Expand Down Expand Up @@ -36,3 +39,22 @@ func TestToFloat64(t *testing.T) {
})
}
}

func TestParseUint64(t *testing.T) {
t.Run("negative", func(t *testing.T) {
id, err := parseUint64("-8809075535603237910")
assert.NoError(t, err)
assert.Equal(t, uint64(9637668538106313706), id)
})

t.Run("positive", func(t *testing.T) {
id, err := parseUint64(fmt.Sprintf("%d", uint64(math.MaxUint64)))
assert.NoError(t, err)
assert.Equal(t, uint64(math.MaxUint64), id)
})

t.Run("invalid", func(t *testing.T) {
_, err := parseUint64("abcd")
assert.Error(t, err)
})
}