telegraf/testutil/accumulator.go

393 lines
7.8 KiB
Go
Raw Normal View History

package testutil
import (
"encoding/json"
"fmt"
2015-06-20 12:32:32 +00:00
"reflect"
2015-10-16 14:54:33 +00:00
"sync"
"sync/atomic"
2016-01-05 23:58:35 +00:00
"testing"
2015-10-16 18:54:05 +00:00
"time"
2016-01-05 23:58:35 +00:00
"github.com/influxdata/telegraf"
2016-01-05 23:58:35 +00:00
"github.com/stretchr/testify/assert"
)
2015-04-06 21:53:43 +00:00
// Metric defines a single point measurement
type Metric struct {
2015-05-29 20:25:48 +00:00
Measurement string
Tags map[string]string
2015-11-13 19:51:37 +00:00
Fields map[string]interface{}
2015-05-29 20:25:48 +00:00
Time time.Time
}
func (p *Metric) String() string {
2015-11-13 19:51:37 +00:00
return fmt.Sprintf("%s %v", p.Measurement, p.Fields)
}
// Accumulator defines a mocked out accumulator
type Accumulator struct {
2015-10-16 14:54:33 +00:00
sync.Mutex
*sync.Cond
Metrics []*Metric
nMetrics uint64
Discard bool
Errors []error
debug bool
}
func (a *Accumulator) NMetrics() uint64 {
return atomic.LoadUint64(&a.nMetrics)
}
func (a *Accumulator) ClearMetrics() {
atomic.StoreUint64(&a.nMetrics, 0)
a.Lock()
defer a.Unlock()
a.Metrics = make([]*Metric, 0)
}
// AddFields adds a measurement point with a specified timestamp.
func (a *Accumulator) AddFields(
2015-05-29 20:25:48 +00:00
measurement string,
2015-11-13 19:51:37 +00:00
fields map[string]interface{},
tags map[string]string,
timestamp ...time.Time,
) {
atomic.AddUint64(&a.nMetrics, 1)
a.Lock()
defer a.Unlock()
if a.Cond != nil {
a.Cond.Broadcast()
}
if a.Discard {
return
}
2015-11-13 19:51:37 +00:00
if tags == nil {
tags = map[string]string{}
}
if len(fields) == 0 {
return
}
var t time.Time
if len(timestamp) > 0 {
t = timestamp[0]
} else {
t = time.Now()
}
2015-11-13 19:51:37 +00:00
if a.debug {
pretty, _ := json.MarshalIndent(fields, "", " ")
prettyTags, _ := json.MarshalIndent(tags, "", " ")
msg := fmt.Sprintf("Adding Measurement [%s]\nFields:%s\nTags:%s\n",
measurement, string(pretty), string(prettyTags))
fmt.Print(msg)
}
p := &Metric{
2015-11-13 19:51:37 +00:00
Measurement: measurement,
Fields: fields,
Tags: tags,
Time: t,
}
a.Metrics = append(a.Metrics, p)
}
func (a *Accumulator) AddCounter(
measurement string,
fields map[string]interface{},
tags map[string]string,
timestamp ...time.Time,
) {
a.AddFields(measurement, fields, tags, timestamp...)
}
func (a *Accumulator) AddGauge(
measurement string,
fields map[string]interface{},
tags map[string]string,
timestamp ...time.Time,
) {
a.AddFields(measurement, fields, tags, timestamp...)
}
func (a *Accumulator) AddMetrics(metrics []telegraf.Metric) {
for _, m := range metrics {
a.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time())
}
}
// AddError appends the given error to Accumulator.Errors.
func (a *Accumulator) AddError(err error) {
if err == nil {
return
}
a.Lock()
a.Errors = append(a.Errors, err)
2017-03-24 19:03:36 +00:00
if a.Cond != nil {
a.Cond.Broadcast()
}
a.Unlock()
}
func (a *Accumulator) SetPrecision(precision, interval time.Duration) {
return
}
func (a *Accumulator) DisablePrecision() {
return
}
func (a *Accumulator) Debug() bool {
// stub for implementing Accumulator interface.
return a.debug
}
func (a *Accumulator) SetDebug(debug bool) {
// stub for implementing Accumulator interface.
a.debug = debug
}
// Get gets the specified measurement point from the accumulator
func (a *Accumulator) Get(measurement string) (*Metric, bool) {
for _, p := range a.Metrics {
2015-05-29 20:25:48 +00:00
if p.Measurement == measurement {
2015-05-18 17:46:44 +00:00
return p, true
}
}
return nil, false
}
func (a *Accumulator) HasTag(measurement string, key string) bool {
for _, p := range a.Metrics {
if p.Measurement == measurement {
_, ok := p.Tags[key]
return ok
}
}
return false
}
func (a *Accumulator) TagValue(measurement string, key string) string {
for _, p := range a.Metrics {
if p.Measurement == measurement {
v, ok := p.Tags[key]
if !ok {
return ""
}
return v
}
}
return ""
}
2016-01-05 23:58:35 +00:00
// NFields returns the total number of fields in the accumulator, across all
// measurements
func (a *Accumulator) NFields() int {
a.Lock()
defer a.Unlock()
2016-01-05 23:58:35 +00:00
counter := 0
for _, pt := range a.Metrics {
2016-01-05 23:58:35 +00:00
for _, _ = range pt.Fields {
counter++
2015-04-06 17:34:55 +00:00
}
}
2016-01-05 23:58:35 +00:00
return counter
2015-04-07 18:54:21 +00:00
}
2015-05-18 17:46:44 +00:00
2017-03-24 19:03:36 +00:00
// Wait waits for the given number of metrics to be added to the accumulator.
func (a *Accumulator) Wait(n int) {
a.Lock()
if a.Cond == nil {
a.Cond = sync.NewCond(&a.Mutex)
}
for int(a.NMetrics()) < n {
a.Cond.Wait()
}
a.Unlock()
}
// WaitError waits for the given number of errors to be added to the accumulator.
func (a *Accumulator) WaitError(n int) {
a.Lock()
if a.Cond == nil {
a.Cond = sync.NewCond(&a.Mutex)
}
2017-03-24 19:03:36 +00:00
for len(a.Errors) < n {
a.Cond.Wait()
}
a.Unlock()
}
func (a *Accumulator) AssertContainsTaggedFields(
2016-01-05 23:58:35 +00:00
t *testing.T,
measurement string,
fields map[string]interface{},
tags map[string]string,
2016-01-05 23:58:35 +00:00
) {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
if !reflect.DeepEqual(tags, p.Tags) {
continue
}
if p.Measurement == measurement {
assert.Equal(t, fields, p.Fields)
2016-01-05 23:58:35 +00:00
return
}
}
2016-01-05 23:58:35 +00:00
msg := fmt.Sprintf("unknown measurement %s with tags %v", measurement, tags)
assert.Fail(t, msg)
}
func (a *Accumulator) AssertContainsFields(
t *testing.T,
measurement string,
fields map[string]interface{},
) {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
if p.Measurement == measurement {
assert.Equal(t, fields, p.Fields)
return
}
}
msg := fmt.Sprintf("unknown measurement %s", measurement)
assert.Fail(t, msg)
}
func (a *Accumulator) AssertDoesNotContainMeasurement(t *testing.T, measurement string) {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
if p.Measurement == measurement {
msg := fmt.Sprintf("found unexpected measurement %s", measurement)
assert.Fail(t, msg)
}
}
}
// HasTimestamp returns true if the measurement has a matching Time value
func (a *Accumulator) HasTimestamp(measurement string, timestamp time.Time) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
if p.Measurement == measurement {
return timestamp.Equal(p.Time)
}
}
return false
}
// HasIntField returns true if the measurement has an Int value
2016-01-05 23:58:35 +00:00
func (a *Accumulator) HasIntField(measurement string, field string) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
2015-05-29 20:25:48 +00:00
if p.Measurement == measurement {
2016-01-05 23:58:35 +00:00
for fieldname, value := range p.Fields {
if fieldname == field {
_, ok := value.(int64)
return ok
}
}
2015-05-18 17:46:44 +00:00
}
}
return false
}
// HasInt32Field returns true if the measurement has an Int value
func (a *Accumulator) HasInt32Field(measurement string, field string) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
if p.Measurement == measurement {
for fieldname, value := range p.Fields {
if fieldname == field {
_, ok := value.(int32)
return ok
}
}
}
}
return false
}
// HasStringField returns true if the measurement has an String value
func (a *Accumulator) HasStringField(measurement string, field string) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
if p.Measurement == measurement {
for fieldname, value := range p.Fields {
if fieldname == field {
_, ok := value.(string)
return ok
}
}
}
}
return false
}
2015-08-04 22:48:19 +00:00
// HasUIntValue returns true if the measurement has a UInt value
2016-01-05 23:58:35 +00:00
func (a *Accumulator) HasUIntField(measurement string, field string) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
2015-08-04 22:48:19 +00:00
if p.Measurement == measurement {
2016-01-05 23:58:35 +00:00
for fieldname, value := range p.Fields {
if fieldname == field {
_, ok := value.(uint64)
return ok
}
}
2015-08-04 22:48:19 +00:00
}
}
return false
}
// HasFloatValue returns true if the given measurement has a float value
2016-01-05 23:58:35 +00:00
func (a *Accumulator) HasFloatField(measurement string, field string) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
2015-05-29 20:25:48 +00:00
if p.Measurement == measurement {
2016-01-05 23:58:35 +00:00
for fieldname, value := range p.Fields {
if fieldname == field {
_, ok := value.(float64)
return ok
}
}
2015-05-18 17:46:44 +00:00
}
}
return false
}
2015-09-02 23:16:52 +00:00
// HasMeasurement returns true if the accumulator has a measurement with the
// given name
func (a *Accumulator) HasMeasurement(measurement string) bool {
a.Lock()
defer a.Unlock()
for _, p := range a.Metrics {
2015-09-02 23:16:52 +00:00
if p.Measurement == measurement {
return true
}
}
return false
}