Add Azure Monitor output plugin (#4089)

This commit is contained in:
Gunnar
2018-09-05 14:50:32 -07:00
committed by Daniel Nelson
parent a47149765e
commit f70d6519e7
12 changed files with 1318 additions and 88 deletions

View File

@@ -1,16 +1,89 @@
package testutil
import (
"sort"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/telegraf"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/metric"
)
// MustEqual requires a and b to be identical.
func MustEqual(t *testing.T, got telegraf.Metric, want Metric) {
require.Equal(t, want.Measurement, got.Name())
require.Equal(t, want.Fields, got.Fields())
require.Equal(t, want.Tags, got.Tags())
require.Equal(t, want.Time, got.Time())
type metricDiff struct {
Measurement string
Tags []*telegraf.Tag
Fields []*telegraf.Field
Type telegraf.ValueType
Time time.Time
}
func newMetricDiff(metric telegraf.Metric) *metricDiff {
m := &metricDiff{}
m.Measurement = metric.Name()
for _, tag := range metric.TagList() {
m.Tags = append(m.Tags, tag)
}
sort.Slice(m.Tags, func(i, j int) bool {
return m.Tags[i].Key < m.Tags[j].Key
})
for _, field := range metric.FieldList() {
m.Fields = append(m.Fields, field)
}
sort.Slice(m.Fields, func(i, j int) bool {
return m.Fields[i].Key < m.Fields[j].Key
})
m.Type = metric.Type()
m.Time = metric.Time()
return m
}
func RequireMetricEqual(t *testing.T, expected, actual telegraf.Metric) {
t.Helper()
var lhs, rhs *metricDiff
if expected != nil {
lhs = newMetricDiff(expected)
}
if actual != nil {
rhs = newMetricDiff(actual)
}
if diff := cmp.Diff(lhs, rhs); diff != "" {
t.Fatalf("telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
}
func RequireMetricsEqual(t *testing.T, expected, actual []telegraf.Metric) {
t.Helper()
lhs := make([]*metricDiff, len(expected))
for _, m := range expected {
lhs = append(lhs, newMetricDiff(m))
}
rhs := make([]*metricDiff, len(actual))
for _, m := range actual {
rhs = append(rhs, newMetricDiff(m))
}
if diff := cmp.Diff(lhs, rhs); diff != "" {
t.Fatalf("[]telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
}
// Metric creates a new metric or panics on error.
func MustMetric(
name string,
tags map[string]string,
fields map[string]interface{},
tm time.Time,
tp ...telegraf.ValueType,
) telegraf.Metric {
m, err := metric.New(name, tags, fields, tm, tp...)
if err != nil {
panic("MustMetric")
}
return m
}

View File

@@ -8,13 +8,11 @@ import (
"github.com/influxdata/telegraf/metric"
)
func TestMustEqual(t *testing.T) {
type args struct {
}
func TestRequireMetricsEqual(t *testing.T) {
tests := []struct {
name string
got telegraf.Metric
want Metric
want telegraf.Metric
}{
{
name: "telegraf and testutil metrics should be equal",
@@ -34,24 +32,27 @@ func TestMustEqual(t *testing.T) {
)
return m
}(),
want: Metric{
Measurement: "test",
Tags: map[string]string{
"t1": "v1",
"t2": "v2",
},
Fields: map[string]interface{}{
"f1": int64(1),
"f2": 3.14,
"f3": "v3",
},
Time: time.Unix(0, 0),
},
want: func() telegraf.Metric {
m, _ := metric.New(
"test",
map[string]string{
"t1": "v1",
"t2": "v2",
},
map[string]interface{}{
"f1": int64(1),
"f2": 3.14,
"f3": "v3",
},
time.Unix(0, 0),
)
return m
}(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
MustEqual(t, tt.got, tt.want)
RequireMetricEqual(t, tt.want, tt.got)
})
}
}