Skip to content

Commit

Permalink
ddtrace/tracer: support int64 ids for the text map reader (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
dd-caleb authored and gbbr committed Sep 11, 2018
1 parent 8efc9a7 commit 6d7a955
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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)
})
}

0 comments on commit 6d7a955

Please sign in to comment.