2015-04-07 00:32:10 +08:00
|
|
|
package testutil
|
|
|
|
|
|
2015-05-27 13:14:42 +08:00
|
|
|
import (
|
2016-01-07 07:11:16 +08:00
|
|
|
"encoding/json"
|
2015-05-27 13:14:42 +08:00
|
|
|
"fmt"
|
2015-06-20 20:32:32 +08:00
|
|
|
"reflect"
|
2015-10-16 22:54:33 +08:00
|
|
|
"sync"
|
2016-07-28 01:16:29 +08:00
|
|
|
"sync/atomic"
|
2016-01-06 07:58:35 +08:00
|
|
|
"testing"
|
2015-10-17 02:54:05 +08:00
|
|
|
"time"
|
2016-01-06 07:58:35 +08:00
|
|
|
|
2023-10-26 05:04:00 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-10-13 03:19:47 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2015-05-27 13:14:42 +08:00
|
|
|
)
|
2015-04-07 05:53:43 +08:00
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
var (
|
|
|
|
|
lastID uint64
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func newTrackingID() telegraf.TrackingID {
|
2020-02-01 06:14:44 +08:00
|
|
|
id := atomic.AddUint64(&lastID, 1)
|
|
|
|
|
return telegraf.TrackingID(id)
|
2018-11-06 05:34:28 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
// Metric defines a single point measurement
|
|
|
|
|
type Metric struct {
|
2015-05-30 04:25:48 +08:00
|
|
|
Measurement string
|
|
|
|
|
Tags map[string]string
|
2015-11-14 03:51:37 +08:00
|
|
|
Fields map[string]interface{}
|
2015-05-30 04:25:48 +08:00
|
|
|
Time time.Time
|
2019-11-27 09:31:36 +08:00
|
|
|
Type telegraf.ValueType
|
2015-04-07 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
func (p *Metric) String() string {
|
2018-11-06 05:34:28 +08:00
|
|
|
return fmt.Sprintf("%s %v %v", p.Measurement, p.Tags, p.Fields)
|
2015-11-14 03:51:37 +08:00
|
|
|
}
|
|
|
|
|
|
2015-08-04 22:58:32 +08:00
|
|
|
// Accumulator defines a mocked out accumulator
|
2015-04-07 00:32:10 +08:00
|
|
|
type Accumulator struct {
|
2015-10-16 22:54:33 +08:00
|
|
|
sync.Mutex
|
2017-02-03 00:24:03 +08:00
|
|
|
*sync.Cond
|
2016-01-07 07:11:16 +08:00
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
Metrics []*Metric
|
|
|
|
|
nMetrics uint64
|
|
|
|
|
Discard bool
|
|
|
|
|
Errors []error
|
|
|
|
|
debug bool
|
|
|
|
|
delivered chan telegraf.DeliveryInfo
|
2019-04-24 02:14:35 +08:00
|
|
|
|
|
|
|
|
TimeFunc func() time.Time
|
2015-04-07 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
2016-07-28 01:16:29 +08:00
|
|
|
func (a *Accumulator) NMetrics() uint64 {
|
|
|
|
|
return atomic.LoadUint64(&a.nMetrics)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 08:32:49 +08:00
|
|
|
// GetTelegrafMetrics returns all the metrics collected by the accumulator
|
|
|
|
|
// If you are getting race conditions here then you are not waiting for all of your metrics to arrive: see Wait()
|
2019-04-24 02:14:35 +08:00
|
|
|
func (a *Accumulator) GetTelegrafMetrics() []telegraf.Metric {
|
|
|
|
|
metrics := []telegraf.Metric{}
|
|
|
|
|
for _, m := range a.Metrics {
|
|
|
|
|
metrics = append(metrics, FromTestMetric(m))
|
|
|
|
|
}
|
|
|
|
|
return metrics
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 02:59:39 +08:00
|
|
|
func (a *Accumulator) FirstError() error {
|
|
|
|
|
if len(a.Errors) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return a.Errors[0]
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 21:15:23 +08:00
|
|
|
func (a *Accumulator) ClearMetrics() {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2017-03-30 08:03:06 +08:00
|
|
|
atomic.StoreUint64(&a.nMetrics, 0)
|
2016-09-20 21:15:23 +08:00
|
|
|
a.Metrics = make([]*Metric, 0)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 03:19:47 +08:00
|
|
|
func (a *Accumulator) addMeasurement(
|
2015-05-30 04:25:48 +08:00
|
|
|
measurement string,
|
2015-05-27 13:14:42 +08:00
|
|
|
tags map[string]string,
|
2019-11-27 09:31:36 +08:00
|
|
|
fields map[string]interface{},
|
|
|
|
|
tp telegraf.ValueType,
|
2015-10-17 06:13:32 +08:00
|
|
|
timestamp ...time.Time,
|
2015-05-27 13:14:42 +08:00
|
|
|
) {
|
2017-02-03 00:24:03 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2017-03-30 08:03:06 +08:00
|
|
|
atomic.AddUint64(&a.nMetrics, 1)
|
2017-02-03 00:24:03 +08:00
|
|
|
if a.Cond != nil {
|
|
|
|
|
a.Cond.Broadcast()
|
|
|
|
|
}
|
2016-07-28 01:16:29 +08:00
|
|
|
if a.Discard {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-04-24 06:09:04 +08:00
|
|
|
|
2018-11-10 02:59:33 +08:00
|
|
|
if len(fields) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 06:09:04 +08:00
|
|
|
tagsCopy := map[string]string{}
|
|
|
|
|
for k, v := range tags {
|
|
|
|
|
tagsCopy[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fieldsCopy := map[string]interface{}{}
|
|
|
|
|
for k, v := range fields {
|
|
|
|
|
fieldsCopy[k] = v
|
2015-11-14 03:51:37 +08:00
|
|
|
}
|
|
|
|
|
|
2015-10-17 06:13:32 +08:00
|
|
|
var t time.Time
|
|
|
|
|
if len(timestamp) > 0 {
|
|
|
|
|
t = timestamp[0]
|
|
|
|
|
} else {
|
2019-04-24 02:14:35 +08:00
|
|
|
if a.TimeFunc == nil {
|
|
|
|
|
t = time.Now()
|
|
|
|
|
} else {
|
|
|
|
|
t = a.TimeFunc()
|
|
|
|
|
}
|
2015-10-17 06:13:32 +08:00
|
|
|
}
|
2015-11-14 03:51:37 +08:00
|
|
|
|
2016-01-07 07:11:16 +08: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)
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
p := &Metric{
|
2015-11-14 03:51:37 +08:00
|
|
|
Measurement: measurement,
|
2018-11-10 02:59:33 +08:00
|
|
|
Fields: fieldsCopy,
|
2018-04-24 06:09:04 +08:00
|
|
|
Tags: tagsCopy,
|
2015-11-14 03:51:37 +08:00
|
|
|
Time: t,
|
2019-11-27 09:31:36 +08:00
|
|
|
Type: tp,
|
2015-11-14 03:51:37 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
a.Metrics = append(a.Metrics, p)
|
2015-04-07 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-27 09:31:36 +08:00
|
|
|
// AddFields adds a measurement point with a specified timestamp.
|
|
|
|
|
func (a *Accumulator) AddFields(
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
timestamp ...time.Time,
|
|
|
|
|
) {
|
2022-10-13 03:19:47 +08:00
|
|
|
a.addMeasurement(measurement, tags, fields, telegraf.Untyped, timestamp...)
|
2019-11-27 09:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
2016-09-01 00:27:37 +08:00
|
|
|
func (a *Accumulator) AddCounter(
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
timestamp ...time.Time,
|
|
|
|
|
) {
|
2022-10-13 03:19:47 +08:00
|
|
|
a.addMeasurement(measurement, tags, fields, telegraf.Counter, timestamp...)
|
2016-09-01 00:27:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Accumulator) AddGauge(
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
timestamp ...time.Time,
|
|
|
|
|
) {
|
2022-10-13 03:19:47 +08:00
|
|
|
a.addMeasurement(measurement, tags, fields, telegraf.Gauge, timestamp...)
|
2016-09-01 00:27:37 +08:00
|
|
|
}
|
|
|
|
|
|
2016-11-07 16:34:46 +08:00
|
|
|
func (a *Accumulator) AddMetrics(metrics []telegraf.Metric) {
|
|
|
|
|
for _, m := range metrics {
|
2022-10-13 03:19:47 +08:00
|
|
|
a.addMeasurement(m.Name(), m.Tags(), m.Fields(), m.Type(), m.Time())
|
2016-11-07 16:34:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-25 07:28:52 +08:00
|
|
|
func (a *Accumulator) AddSummary(
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
timestamp ...time.Time,
|
|
|
|
|
) {
|
2022-10-13 03:19:47 +08:00
|
|
|
a.addMeasurement(measurement, tags, fields, telegraf.Summary, timestamp...)
|
2017-10-25 07:28:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Accumulator) AddHistogram(
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
timestamp ...time.Time,
|
|
|
|
|
) {
|
2022-10-13 03:19:47 +08:00
|
|
|
a.addMeasurement(measurement, tags, fields, telegraf.Histogram, timestamp...)
|
2017-10-25 07:28:52 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
func (a *Accumulator) AddMetric(m telegraf.Metric) {
|
2022-10-13 03:19:47 +08:00
|
|
|
a.addMeasurement(m.Name(), m.Tags(), m.Fields(), m.Type(), m.Time())
|
2018-11-06 05:34:28 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
func (a *Accumulator) WithTracking(_ int) telegraf.TrackingAccumulator {
|
2018-11-06 05:34:28 +08:00
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Accumulator) AddTrackingMetric(m telegraf.Metric) telegraf.TrackingID {
|
|
|
|
|
a.AddMetric(m)
|
|
|
|
|
return newTrackingID()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Accumulator) AddTrackingMetricGroup(group []telegraf.Metric) telegraf.TrackingID {
|
|
|
|
|
for _, m := range group {
|
|
|
|
|
a.AddMetric(m)
|
|
|
|
|
}
|
|
|
|
|
return newTrackingID()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Accumulator) Delivered() <-chan telegraf.DeliveryInfo {
|
2020-07-08 05:04:55 +08:00
|
|
|
a.Lock()
|
2018-11-06 05:34:28 +08:00
|
|
|
if a.delivered == nil {
|
|
|
|
|
a.delivered = make(chan telegraf.DeliveryInfo)
|
|
|
|
|
}
|
2020-07-08 05:04:55 +08:00
|
|
|
a.Unlock()
|
2018-11-06 05:34:28 +08:00
|
|
|
return a.delivered
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 20:09:49 +08:00
|
|
|
// 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-25 03:03:36 +08:00
|
|
|
if a.Cond != nil {
|
|
|
|
|
a.Cond.Broadcast()
|
|
|
|
|
}
|
2016-07-25 20:09:49 +08:00
|
|
|
a.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
func (a *Accumulator) SetPrecision(_ time.Duration) {
|
2016-06-13 22:21:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Accumulator) DisablePrecision() {
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-17 06:13:32 +08:00
|
|
|
func (a *Accumulator) Debug() bool {
|
|
|
|
|
// stub for implementing Accumulator interface.
|
2016-01-07 07:11:16 +08:00
|
|
|
return a.debug
|
2015-10-17 06:13:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Accumulator) SetDebug(debug bool) {
|
|
|
|
|
// stub for implementing Accumulator interface.
|
2016-01-07 07:11:16 +08:00
|
|
|
a.debug = debug
|
2015-10-17 06:13:32 +08:00
|
|
|
}
|
|
|
|
|
|
2015-08-04 22:58:32 +08:00
|
|
|
// Get gets the specified measurement point from the accumulator
|
2016-01-28 07:15:14 +08:00
|
|
|
func (a *Accumulator) Get(measurement string) (*Metric, bool) {
|
|
|
|
|
for _, p := range a.Metrics {
|
2015-05-30 04:25:48 +08:00
|
|
|
if p.Measurement == measurement {
|
2015-05-19 01:46:44 +08:00
|
|
|
return p, true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-18 07:49:11 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 12:53:57 +08:00
|
|
|
func (a *Accumulator) TagSetValue(measurement string, key string) string {
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
v, ok := p.Tags[key]
|
|
|
|
|
if ok {
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-18 07:49:11 +08:00
|
|
|
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 ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 03:19:47 +08:00
|
|
|
// GatherError calls the given Gather function and returns the first error found.
|
2017-04-25 02:13:26 +08:00
|
|
|
func (a *Accumulator) GatherError(gf func(telegraf.Accumulator) error) error {
|
|
|
|
|
if err := gf(a); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(a.Errors) > 0 {
|
|
|
|
|
return a.Errors[0]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-06 07:58:35 +08:00
|
|
|
// NFields returns the total number of fields in the accumulator, across all
|
|
|
|
|
// measurements
|
|
|
|
|
func (a *Accumulator) NFields() int {
|
2016-02-16 08:21:38 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2016-01-06 07:58:35 +08:00
|
|
|
counter := 0
|
2016-01-28 07:15:14 +08:00
|
|
|
for _, pt := range a.Metrics {
|
2018-10-20 04:32:54 +08:00
|
|
|
for range pt.Fields {
|
2016-01-06 07:58:35 +08:00
|
|
|
counter++
|
2015-04-07 01:34:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-06 07:58:35 +08:00
|
|
|
return counter
|
2015-04-08 02:54:21 +08:00
|
|
|
}
|
2015-05-19 01:46:44 +08:00
|
|
|
|
2017-03-25 03:03:36 +08:00
|
|
|
// Wait waits for the given number of metrics to be added to the accumulator.
|
|
|
|
|
func (a *Accumulator) Wait(n int) {
|
|
|
|
|
a.Lock()
|
2020-06-25 01:29:44 +08:00
|
|
|
defer a.Unlock()
|
2017-03-25 03:03:36 +08:00
|
|
|
if a.Cond == nil {
|
|
|
|
|
a.Cond = sync.NewCond(&a.Mutex)
|
|
|
|
|
}
|
|
|
|
|
for int(a.NMetrics()) < n {
|
|
|
|
|
a.Cond.Wait()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitError waits for the given number of errors to be added to the accumulator.
|
|
|
|
|
func (a *Accumulator) WaitError(n int) {
|
|
|
|
|
a.Lock()
|
2017-02-03 00:24:03 +08:00
|
|
|
if a.Cond == nil {
|
|
|
|
|
a.Cond = sync.NewCond(&a.Mutex)
|
|
|
|
|
}
|
2017-03-25 03:03:36 +08:00
|
|
|
for len(a.Errors) < n {
|
|
|
|
|
a.Cond.Wait()
|
|
|
|
|
}
|
|
|
|
|
a.Unlock()
|
2017-02-03 00:24:03 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 06:17:04 +08:00
|
|
|
func (a *Accumulator) AssertContainsTaggedFields(
|
|
|
|
|
t *testing.T,
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
) {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if !reflect.DeepEqual(tags, p.Tags) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Measurement == measurement && reflect.DeepEqual(fields, p.Fields) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-10 04:10:17 +08:00
|
|
|
// We've failed. spit out some debug logging
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
t.Log("measurement", p.Measurement, "tags", p.Tags, "fields", p.Fields)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
msg := fmt.Sprintf("unknown measurement %q with tags %v", measurement, tags)
|
2023-10-26 05:04:00 +08:00
|
|
|
require.Fail(t, msg)
|
2019-05-31 06:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
2017-07-22 05:25:17 +08:00
|
|
|
func (a *Accumulator) AssertDoesNotContainsTaggedFields(
|
|
|
|
|
t *testing.T,
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
) {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if !reflect.DeepEqual(tags, p.Tags) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 08:20:24 +08:00
|
|
|
if p.Measurement == measurement && reflect.DeepEqual(fields, p.Fields) {
|
|
|
|
|
msg := fmt.Sprintf(
|
|
|
|
|
"found measurement %s with tagged fields (tags %v) which should not be there",
|
|
|
|
|
measurement, tags)
|
2023-10-26 05:04:00 +08:00
|
|
|
require.Fail(t, msg)
|
2017-07-22 05:25:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-06 08:28:15 +08:00
|
|
|
func (a *Accumulator) AssertContainsFields(
|
|
|
|
|
t *testing.T,
|
|
|
|
|
measurement string,
|
|
|
|
|
fields map[string]interface{},
|
|
|
|
|
) {
|
2016-02-16 08:21:38 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2016-01-28 07:15:14 +08:00
|
|
|
for _, p := range a.Metrics {
|
2016-01-06 08:28:15 +08:00
|
|
|
if p.Measurement == measurement {
|
2023-10-26 05:04:00 +08:00
|
|
|
require.Equal(t, fields, p.Fields)
|
2016-01-06 08:28:15 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-10 04:10:17 +08:00
|
|
|
msg := fmt.Sprintf("unknown measurement %q", measurement)
|
2023-10-26 05:04:00 +08:00
|
|
|
require.Fail(t, msg)
|
2016-01-06 08:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
2017-10-10 06:02:57 +08:00
|
|
|
func (a *Accumulator) HasPoint(
|
|
|
|
|
measurement string,
|
|
|
|
|
tags map[string]string,
|
|
|
|
|
fieldKey string,
|
|
|
|
|
fieldValue interface{},
|
|
|
|
|
) bool {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement != measurement {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tags, p.Tags) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v, ok := p.Fields[fieldKey]
|
|
|
|
|
if ok && reflect.DeepEqual(v, fieldValue) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 19:33:39 +08:00
|
|
|
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)
|
2023-10-26 05:04:00 +08:00
|
|
|
require.Fail(t, msg)
|
2016-11-15 19:33:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 06:04:29 +08:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-28 02:47:22 +08:00
|
|
|
// HasField returns true if the given measurement has a field with the given
|
|
|
|
|
// name
|
|
|
|
|
func (a *Accumulator) HasField(measurement string, field string) bool {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
if _, ok := p.Fields[field]; ok {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 04:36:36 +08:00
|
|
|
// HasIntField returns true if the measurement has an Int value
|
2016-01-06 07:58:35 +08:00
|
|
|
func (a *Accumulator) HasIntField(measurement string, field string) bool {
|
2016-02-16 08:21:38 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2016-01-28 07:15:14 +08:00
|
|
|
for _, p := range a.Metrics {
|
2017-05-17 06:25:30 +08:00
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
_, ok := value.(int)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HasInt64Field returns true if the measurement has an Int64 value
|
|
|
|
|
func (a *Accumulator) HasInt64Field(measurement string, field string) bool {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
2015-05-30 04:25:48 +08:00
|
|
|
if p.Measurement == measurement {
|
2016-01-06 07:58:35 +08:00
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
_, ok := value.(int64)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-19 01:46:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 04:36:36 +08:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 03:19:47 +08:00
|
|
|
// HasStringField returns true if the measurement has a String value
|
2017-01-25 04:36:36 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 01:58:10 +08:00
|
|
|
// HasUIntField returns true if the measurement has a UInt value
|
2016-01-06 07:58:35 +08:00
|
|
|
func (a *Accumulator) HasUIntField(measurement string, field string) bool {
|
2016-02-16 08:21:38 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2016-01-28 07:15:14 +08:00
|
|
|
for _, p := range a.Metrics {
|
2015-08-05 06:48:19 +08:00
|
|
|
if p.Measurement == measurement {
|
2016-01-06 07:58:35 +08:00
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
_, ok := value.(uint64)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-05 06:48:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 01:58:10 +08:00
|
|
|
// HasFloatField returns true if the given measurement has a float value
|
2016-01-06 07:58:35 +08:00
|
|
|
func (a *Accumulator) HasFloatField(measurement string, field string) bool {
|
2016-02-16 08:21:38 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2016-01-28 07:15:14 +08:00
|
|
|
for _, p := range a.Metrics {
|
2015-05-30 04:25:48 +08:00
|
|
|
if p.Measurement == measurement {
|
2016-01-06 07:58:35 +08:00
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
_, ok := value.(float64)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-19 01:46:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
2015-09-03 07:16:52 +08:00
|
|
|
|
|
|
|
|
// HasMeasurement returns true if the accumulator has a measurement with the
|
|
|
|
|
// given name
|
|
|
|
|
func (a *Accumulator) HasMeasurement(measurement string) bool {
|
2016-02-16 08:21:38 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
2016-01-28 07:15:14 +08:00
|
|
|
for _, p := range a.Metrics {
|
2015-09-03 07:16:52 +08:00
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-05-10 07:21:04 +08:00
|
|
|
|
2017-09-26 01:58:10 +08:00
|
|
|
// IntField returns the int value of the given measurement and field or false.
|
2017-05-10 07:21:04 +08:00
|
|
|
func (a *Accumulator) IntField(measurement string, field string) (int, bool) {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
v, ok := value.(int)
|
|
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 01:58:10 +08:00
|
|
|
// Int64Field returns the int64 value of the given measurement and field or false.
|
|
|
|
|
func (a *Accumulator) Int64Field(measurement string, field string) (int64, bool) {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
v, ok := value.(int64)
|
|
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 07:12:05 +08:00
|
|
|
// Uint64Field returns the int64 value of the given measurement and field or false.
|
|
|
|
|
func (a *Accumulator) Uint64Field(measurement string, field string) (uint64, bool) {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
v, ok := value.(uint64)
|
|
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 01:58:10 +08:00
|
|
|
// Int32Field returns the int32 value of the given measurement and field or false.
|
|
|
|
|
func (a *Accumulator) Int32Field(measurement string, field string) (int32, bool) {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
v, ok := value.(int32)
|
|
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FloatField returns the float64 value of the given measurement and field or false.
|
2017-05-10 07:21:04 +08:00
|
|
|
func (a *Accumulator) FloatField(measurement string, field string) (float64, bool) {
|
|
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
|
|
|
|
v, ok := value.(float64)
|
|
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0.0, false
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 01:58:10 +08:00
|
|
|
// StringField returns the string value of the given measurement and field or false.
|
2017-05-10 07:21:04 +08:00
|
|
|
func (a *Accumulator) StringField(measurement string, field string) (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 {
|
|
|
|
|
v, ok := value.(string)
|
|
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
2017-09-26 01:58:10 +08:00
|
|
|
|
|
|
|
|
// BoolField returns the bool value of the given measurement and field or false.
|
2022-10-13 03:19:47 +08:00
|
|
|
func (a *Accumulator) BoolField(measurement string, field string) (v bool, ok bool) {
|
2017-09-26 01:58:10 +08:00
|
|
|
a.Lock()
|
|
|
|
|
defer a.Unlock()
|
|
|
|
|
for _, p := range a.Metrics {
|
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
|
if fieldname == field {
|
2022-10-13 03:19:47 +08:00
|
|
|
v, ok = value.(bool)
|
2017-09-26 01:58:10 +08:00
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false, false
|
|
|
|
|
}
|
2019-12-03 02:49:04 +08:00
|
|
|
|
|
|
|
|
// NopAccumulator is used for benchmarking to isolate the plugin from the internal
|
2020-05-14 15:41:58 +08:00
|
|
|
// telegraf accumulator machinery.
|
2019-12-03 02:49:04 +08:00
|
|
|
type NopAccumulator struct{}
|
|
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
func (n *NopAccumulator) AddFields(_ string, _ map[string]interface{}, _ map[string]string, _ ...time.Time) {
|
2019-12-03 02:49:04 +08:00
|
|
|
}
|
2021-03-23 01:21:36 +08:00
|
|
|
func (n *NopAccumulator) AddGauge(_ string, _ map[string]interface{}, _ map[string]string, _ ...time.Time) {
|
2019-12-03 02:49:04 +08:00
|
|
|
}
|
2021-03-23 01:21:36 +08:00
|
|
|
func (n *NopAccumulator) AddCounter(_ string, _ map[string]interface{}, _ map[string]string, _ ...time.Time) {
|
2019-12-03 02:49:04 +08:00
|
|
|
}
|
2021-03-23 01:21:36 +08:00
|
|
|
func (n *NopAccumulator) AddSummary(_ string, _ map[string]interface{}, _ map[string]string, _ ...time.Time) {
|
2019-12-03 02:49:04 +08:00
|
|
|
}
|
2021-03-23 01:21:36 +08:00
|
|
|
func (n *NopAccumulator) AddHistogram(_ string, _ map[string]interface{}, _ map[string]string, _ ...time.Time) {
|
2019-12-03 02:49:04 +08:00
|
|
|
}
|
2021-03-23 01:21:36 +08:00
|
|
|
func (n *NopAccumulator) AddMetric(telegraf.Metric) {}
|
|
|
|
|
func (n *NopAccumulator) SetPrecision(_ time.Duration) {}
|
|
|
|
|
func (n *NopAccumulator) AddError(_ error) {}
|
|
|
|
|
func (n *NopAccumulator) WithTracking(_ int) telegraf.TrackingAccumulator { return nil }
|