Skip to content

Commit

Permalink
Add line protocol uint64 support (#3946)
Browse files Browse the repository at this point in the history
  • Loading branch information
ragzilla authored and danielnelson committed Mar 28, 2018
1 parent 741abbf commit 006ccbf
Show file tree
Hide file tree
Showing 12 changed files with 5,388 additions and 4,096 deletions.
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

0 comments on commit 006ccbf

Please sign in to comment.