Skip to content

Commit

Permalink
Fix use of reflect.StringHeader in prometheus/metric.go (#133)
Browse files Browse the repository at this point in the history
The `go vet` command in Go 1.16 reports a warning for inappropriate
use of reflect.StringHeader.

golang/go#40701

Its use in prometheus/metric.go to convert a byte array to a string in
place began to trigger the warning. That code has been replaced with a
safer variant that avoids the `vet` warning and still converts the array
without allocating new memory.

https://stackoverflow.com/a/66865482

Additionally, the CircleCI test now uses a pinned influxdb image of
1.8.9. The 2.x influxdb Docker images require authentication to use.

Co-authored-by: Collin Van Dyck <collin@segment.com>
  • Loading branch information
bhavanki and Collin Van Dyck authored Aug 12, 2021
1 parent 1cae955 commit bed3e79
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
working_directory: /go/src/github.com/segmentio/stats
docker:
- image: circleci/golang
- image: influxdb:alpine
- image: influxdb:1.8.9-alpine
ports: ['8086:8086']
steps:
- checkout
Expand Down
16 changes: 12 additions & 4 deletions prometheus/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,19 @@ func le(buckets []stats.Value) string {
}
b = appendFloat(b, valueOf(v))
}
return unsafeByteSliceToString(b)
}

return *(*string)(unsafe.Pointer(&reflect.StringHeader{
Data: uintptr(unsafe.Pointer(&b[0])),
Len: len(b),
}))
// This function converts the byte array to a string without additional
// memory allocation.
// Source: https://stackoverflow.com/a/66865482 (license: CC BY-SA 4.0)
func unsafeByteSliceToString(b []byte) string {
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var s string
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
sh.Data = sliceHeader.Data
sh.Len = sliceHeader.Len
return s
}

func nextLe(s string) (head string, tail string) {
Expand Down
41 changes: 41 additions & 0 deletions prometheus/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,47 @@ import (
"github.com/segmentio/stats/v4"
)

func TestUnsafeByteSliceToString(t *testing.T) {
for _, test := range []struct {
name string
input []byte
expected string
}{
{
name: "nil bytes",
input: nil,
expected: "",
},
{
name: "no bytes",
input: []byte{},
expected: "",
},
{
name: "list of floats",
input: []byte("1.2:3.4:5.6:7.8"),
expected: "1.2:3.4:5.6:7.8",
},
{
name: "deadbeef",
input: []byte{0xde, 0xad, 0xbe, 0xef},
expected: "\xde\xad\xbe\xef",
},
{
name: "embedded zero",
input: []byte("this\x00that"),
expected: "this\x00that",
},
} {
t.Run(test.name, func(t *testing.T) {
res := unsafeByteSliceToString(test.input)
if res != test.expected {
t.Errorf("Expected %q but got %q", test.expected, res)
}
})
}
}

func TestMetricStore(t *testing.T) {
input := []metric{
{mtype: counter, scope: "test", name: "A", value: 1},
Expand Down

0 comments on commit bed3e79

Please sign in to comment.