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

uint64 support #3946

Merged
merged 1 commit into from
Mar 28, 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
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
COMMIT := $(shell git rev-parse --short HEAD)
GOFILES ?= $(shell git ls-files '*.go')
GOFMT ?= $(shell gofmt -l $(filter-out plugins/parsers/influx/machine.go, $(GOFILES)))
BUILDFLAGS ?=

ifdef GOBIN
PATH := $(GOBIN):$(PATH)
Expand Down Expand Up @@ -35,7 +36,7 @@ deps:
gdm restore

telegraf:
go build -i -o $(TELEGRAF) -ldflags "$(LDFLAGS)" ./cmd/telegraf/telegraf.go
go build -i -o $(TELEGRAF) -ldflags "$(LDFLAGS)" $(BUILDFLAGS) ./cmd/telegraf/telegraf.go

go-install:
go install -ldflags "-w -s $(LDFLAGS)" ./cmd/telegraf
Expand All @@ -61,6 +62,9 @@ fmtcheck:
fi
@echo '[INFO] done.'

uint64:
BUILDFLAGS="-tags uint64" $(MAKE) all

lint:
golint ./...

Expand Down Expand Up @@ -99,4 +103,4 @@ docker-image:
plugins/parsers/influx/machine.go: plugins/parsers/influx/machine.go.rl
ragel -Z -G2 $^ -o $@

.PHONY: deps telegraf install test test-windows lint vet test-all package clean docker-image fmtcheck
.PHONY: deps telegraf install test test-windows lint vet test-all package clean docker-image fmtcheck uint64
21 changes: 17 additions & 4 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (

const MaxInt = int(^uint(0) >> 1)

// enableUint64Support will enable uint64 support if set to true.
var enableUint64Support = false

// EnableUintSupport manually enables uint support for convertValue.
// This function will be removed in the future and only exists for unit tests during the
// transition.
func EnableUintSupport() {
enableUint64Support = true
}

type metric struct {
name string
tags []*telegraf.Tag
Expand Down Expand Up @@ -265,11 +275,14 @@ func convertField(v interface{}) interface{} {
return int64(MaxInt)
}
case uint64:
if v <= uint64(MaxInt) {
return int64(v)
} else {
return int64(MaxInt)
if enableUint64Support == false {
if v <= uint64(MaxInt) {
return int64(v)
} else {
return int64(MaxInt)
}
}
return uint64(v)
case []byte:
return string(v)
case int32:
Expand Down
7 changes: 7 additions & 0 deletions metric/uint_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build uint64

package metric

func init() {
EnableUintSupport()
}
6 changes: 6 additions & 0 deletions plugins/parsers/influx/escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func parseIntBytes(b []byte, base int, bitSize int) (i int64, err error) {
return strconv.ParseInt(s, base, bitSize)
}

// parseUintBytes is a zero-alloc wrapper around strconv.ParseUint.
func parseUintBytes(b []byte, base int, bitSize int) (i uint64, err error) {
s := unsafeBytesToString(b)
return strconv.ParseUint(s, base, bitSize)
}

// parseFloatBytes is a zero-alloc wrapper around strconv.ParseFloat.
func parseFloatBytes(b []byte, bitSize int) (float64, error) {
s := unsafeBytesToString(b)
Expand Down
10 changes: 10 additions & 0 deletions plugins/parsers/influx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func (h *MetricHandler) AddInt(key []byte, value []byte) {
h.builder.AddField(fk, fv)
}

func (h *MetricHandler) AddUint(key []byte, value []byte) {
fk := unescape(key)
fv, err := parseUintBytes(bytes.TrimSuffix(value, []byte("u")), 10, 64)
if err != nil {
log.Errorf("E! Received unparseable uint value: %q", value)
return
}
h.builder.AddField(fk, fv)
}

func (h *MetricHandler) AddFloat(key []byte, value []byte) {
fk := unescape(key)
fv, err := parseFloatBytes(value, 64)
Expand Down
Loading