Skip to content

Commit

Permalink
Utilizing new client and overhauling Accumulator interface
Browse files Browse the repository at this point in the history
Fixes #280
Fixes #281
  • Loading branch information
sparrc committed Oct 16, 2015
1 parent 73f1ed4 commit 727dcbb
Showing 1 changed file with 120 additions and 6 deletions.
126 changes: 120 additions & 6 deletions accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,129 @@ import (
"sync"
"time"

"github.com/influxdb/influxdb/client"
oldclient "github.com/influxdb/influxdb/client"
"github.com/influxdb/influxdb/client/v2"
)

type Accumulator interface {
Add(measurement string, fields map[string]interface{},
tags map[string]string, t ...time.Time)

BatchPoints() client.BatchPoints

SetDefaultTags(tags map[string]string)
AddDefaultTag(key, value string)

Prefix() string
SetPrefix(prefix string)

Debug() bool
SetDebug(enabled bool)
}

func NewAccumulator(
precision string,
database string,
config *ConfiguredPlugin,
) (Accumulator, error) {
var err error
acc := accumulator{}
bpconfig := client.BatchPointsConfig{
Precision: precision,
Database: database,
}
if acc.batch, err = client.NewBatchPoints(bpconfig); err != nil {
return nil, err
}
acc.config = config
return &acc, nil
}

type accumulator struct {
sync.Mutex

batch client.BatchPoints

defaultTags map[string]string

debug bool

config *ConfiguredPlugin

prefix string
}

func (ac *accumulator) Add(
measurement string,
fields map[string]interface{},
tags map[string]string,
t ...time.Time,
) {
ac.Lock()
defer ac.Unlock()

var timestamp time.Time
if len(t) > 0 {
timestamp = t[0]
} else {
timestamp = time.Now()
}

if ac.config != nil {
if !ac.config.ShouldPass(measurement, tags) {
return
}
}

for k, v := range ac.defaultTags {
if _, ok := tags[k]; !ok {
tags[k] = v
}
}

pt := client.NewPoint(measurement, tags, fields, timestamp)
if ac.debug {
fmt.Println(pt.PrecisionString(ac.batch.Precision()))
}
ac.batch.AddPoint(pt)
}

func (ac *accumulator) BatchPoints() client.BatchPoints {
ac.Lock()
defer ac.Unlock()
return ac.batch
}

func (ac *accumulator) SetDefaultTags(tags map[string]string) {
ac.defaultTags = tags
}

func (ac *accumulator) AddDefaultTag(key, value string) {
ac.defaultTags[key] = value
}

func (ac *accumulator) Prefix() string {
return ac.prefix
}

func (ac *accumulator) SetPrefix(prefix string) {
ac.prefix = prefix
}

func (ac *accumulator) Debug() bool {
return ac.debug
}

func (ac *accumulator) SetDebug(enabled bool) {
ac.debug = enabled
}

// BatchPoints is used to send a batch of data in a single write from telegraf
// to influx
type BatchPoints struct {
sync.Mutex

client.BatchPoints
oldclient.BatchPoints

Debug bool

Expand All @@ -39,9 +153,9 @@ func (bp *BatchPoints) deepcopy() *BatchPoints {
bpc.Tags[k] = v
}

var pts []client.Point
var pts []oldclient.Point
for _, pt := range bp.Points {
var ptc client.Point
var ptc oldclient.Point

ptc.Measurement = pt.Measurement
ptc.Time = pt.Time
Expand Down Expand Up @@ -116,7 +230,7 @@ func (bp *BatchPoints) AddFieldsWithTime(
// fmt.Printf("> [%s] %s %s\n", strings.Join(tg, " "), measurement, strings.Join(vals, " "))
// }

// bp.Points = append(bp.Points, client.Point{
// bp.Points = append(bp.Points, oldclient.Point{
// Measurement: measurement,
// Tags: tags,
// Fields: fields,
Expand Down Expand Up @@ -171,7 +285,7 @@ func (bp *BatchPoints) AddFields(
fmt.Printf("> [%s] %s %s\n", strings.Join(tg, " "), measurement, strings.Join(vals, " "))
}

bp.Points = append(bp.Points, client.Point{
bp.Points = append(bp.Points, oldclient.Point{
Measurement: measurement,
Tags: tags,
Fields: fields,
Expand Down

0 comments on commit 727dcbb

Please sign in to comment.