refactor: Prevent import from going the wrong way (#12004)

This commit is contained in:
suprajanarasimhan 2022-10-13 13:11:24 -07:00 committed by GitHub
parent 94e39fa018
commit 90d8f426b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 32 deletions

View File

@ -8,10 +8,10 @@ import (
"regexp" "regexp"
"strings" "strings"
wavefront "github.com/wavefronthq/wavefront-sdk-go/senders"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/outputs"
serializer "github.com/influxdata/telegraf/plugins/serializers/wavefront"
wavefront "github.com/wavefronthq/wavefront-sdk-go/senders"
) )
//go:embed sample.conf //go:embed sample.conf
@ -65,14 +65,6 @@ var tagValueReplacer = strings.NewReplacer("*", "-")
var pathReplacer = strings.NewReplacer("_", "_") var pathReplacer = strings.NewReplacer("_", "_")
type MetricPoint struct {
Metric string
Value float64
Timestamp int64
Source string
Tags map[string]string
}
func (*Wavefront) SampleConfig() string { func (*Wavefront) SampleConfig() string {
return sampleConfig return sampleConfig
} }
@ -153,8 +145,8 @@ func (w *Wavefront) Write(metrics []telegraf.Metric) error {
return nil return nil
} }
func (w *Wavefront) buildMetrics(m telegraf.Metric) []*MetricPoint { func (w *Wavefront) buildMetrics(m telegraf.Metric) []*serializer.MetricPoint {
ret := make([]*MetricPoint, 0) ret := make([]*serializer.MetricPoint, 0)
for fieldName, value := range m.Fields() { for fieldName, value := range m.Fields() {
var name string var name string
@ -176,7 +168,7 @@ func (w *Wavefront) buildMetrics(m telegraf.Metric) []*MetricPoint {
name = pathReplacer.Replace(name) name = pathReplacer.Replace(name)
} }
metric := &MetricPoint{ metric := &serializer.MetricPoint{
Metric: name, Metric: name,
Timestamp: m.Time().Unix(), Timestamp: m.Time().Unix(),
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/outputs"
serializer "github.com/influxdata/telegraf/plugins/serializers/wavefront"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -45,25 +46,25 @@ func TestBuildMetrics(t *testing.T) {
var metricTests = []struct { var metricTests = []struct {
metric telegraf.Metric metric telegraf.Metric
metricPoints []MetricPoint metricPoints []serializer.MetricPoint
}{ }{
{ {
testutil.TestMetric(float64(1), "testing_just*a%metric:float", "metric2"), testutil.TestMetric(float64(1), "testing_just*a%metric:float", "metric2"),
[]MetricPoint{ []serializer.MetricPoint{
{Metric: w.Prefix + "testing.just-a-metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}}, {Metric: w.Prefix + "testing.just-a-metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
{Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}}, {Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
}, },
}, },
{ {
testutil.TestMetric(float64(1), "testing_just/another,metric:float", "metric2"), testutil.TestMetric(float64(1), "testing_just/another,metric:float", "metric2"),
[]MetricPoint{ []serializer.MetricPoint{
{Metric: w.Prefix + "testing.just-another-metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}}, {Metric: w.Prefix + "testing.just-another-metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
{Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}}, {Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
}, },
}, },
{ {
testMetric1, testMetric1,
[]MetricPoint{{Metric: w.Prefix + "test.simple.metric", Value: 123, Timestamp: timestamp, Source: "testHost", Tags: map[string]string{"tag1": "value1"}}}, []serializer.MetricPoint{{Metric: w.Prefix + "test.simple.metric", Value: 123, Timestamp: timestamp, Source: "testHost", Tags: map[string]string{"tag1": "value1"}}},
}, },
} }
@ -88,18 +89,18 @@ func TestBuildMetricsStrict(t *testing.T) {
var metricTests = []struct { var metricTests = []struct {
metric telegraf.Metric metric telegraf.Metric
metricPoints []MetricPoint metricPoints []serializer.MetricPoint
}{ }{
{ {
testutil.TestMetric(float64(1), "testing_just*a%metric:float", "metric2"), testutil.TestMetric(float64(1), "testing_just*a%metric:float", "metric2"),
[]MetricPoint{ []serializer.MetricPoint{
{Metric: w.Prefix + "testing.just-a-metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}}, {Metric: w.Prefix + "testing.just-a-metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
{Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}}, {Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
}, },
}, },
{ {
testutil.TestMetric(float64(1), "testing_just/another,metric:float", "metric2"), testutil.TestMetric(float64(1), "testing_just/another,metric:float", "metric2"),
[]MetricPoint{ []serializer.MetricPoint{
{Metric: w.Prefix + "testing.just/another,metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag/1": "value1", "tag,2": "value2"}}, {Metric: w.Prefix + "testing.just/another,metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag/1": "value1", "tag,2": "value2"}},
{Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag/1": "value1", "tag,2": "value2"}}, {Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag/1": "value1", "tag,2": "value2"}},
}, },
@ -132,15 +133,15 @@ func TestBuildMetricsWithSimpleFields(t *testing.T) {
var metricTests = []struct { var metricTests = []struct {
metric telegraf.Metric metric telegraf.Metric
metricLines []MetricPoint metricLines []serializer.MetricPoint
}{ }{
{ {
testutil.TestMetric(float64(1), "testing_just*a%metric:float"), testutil.TestMetric(float64(1), "testing_just*a%metric:float"),
[]MetricPoint{{Metric: w.Prefix + "testing.just-a-metric-float.value", Value: 1}}, []serializer.MetricPoint{{Metric: w.Prefix + "testing.just-a-metric-float.value", Value: 1}},
}, },
{ {
testMetric1, testMetric1,
[]MetricPoint{{Metric: w.Prefix + "test.simple.metric.value", Value: 123}}, []serializer.MetricPoint{{Metric: w.Prefix + "test.simple.metric.value", Value: 123}},
}, },
} }

View File

@ -7,7 +7,6 @@ import (
"sync" "sync"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs/wavefront" // TODO: this dependency is going the wrong way: Move MetricPoint into the serializer.
) )
// WavefrontSerializer : WavefrontSerializer struct // WavefrontSerializer : WavefrontSerializer struct
@ -41,6 +40,14 @@ var tagValueReplacer = strings.NewReplacer("\"", "\\\"", "*", "-")
var pathReplacer = strings.NewReplacer("_", ".") var pathReplacer = strings.NewReplacer("_", ".")
type MetricPoint struct {
Metric string
Value float64
Timestamp int64
Source string
Tags map[string]string
}
func NewSerializer(prefix string, useStrict bool, sourceOverride []string, disablePrefixConversion bool) (*WavefrontSerializer, error) { func NewSerializer(prefix string, useStrict bool, sourceOverride []string, disablePrefixConversion bool) (*WavefrontSerializer, error) {
s := &WavefrontSerializer{ s := &WavefrontSerializer{
Prefix: prefix, Prefix: prefix,
@ -79,7 +86,7 @@ func (s *WavefrontSerializer) serializeMetric(m telegraf.Metric) {
continue continue
} }
source, tags := buildTags(m.Tags(), s) source, tags := buildTags(m.Tags(), s)
metric := wavefront.MetricPoint{ metric := MetricPoint{
Metric: name, Metric: name,
Timestamp: m.Time().Unix(), Timestamp: m.Time().Unix(),
Value: metricValue, Value: metricValue,
@ -162,7 +169,7 @@ func buildValue(v interface{}, name string) (val float64, valid bool) {
} }
} }
func formatMetricPoint(b *buffer, metricPoint *wavefront.MetricPoint, s *WavefrontSerializer) []byte { func formatMetricPoint(b *buffer, metricPoint *MetricPoint, s *WavefrontSerializer) []byte {
b.WriteChar('"') b.WriteChar('"')
b.WriteString(metricPoint.Metric) b.WriteString(metricPoint.Metric)
b.WriteString(`" `) b.WriteString(`" `)

View File

@ -11,7 +11,6 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/outputs/wavefront"
) )
func TestBuildTags(t *testing.T) { func TestBuildTags(t *testing.T) {
@ -106,11 +105,11 @@ func TestBuildTagsHostTag(t *testing.T) {
func TestFormatMetricPoint(t *testing.T) { func TestFormatMetricPoint(t *testing.T) {
var pointTests = []struct { var pointTests = []struct {
ptIn *wavefront.MetricPoint ptIn *MetricPoint
out string out string
}{ }{
{ {
&wavefront.MetricPoint{ &MetricPoint{
Metric: "cpu.idle", Metric: "cpu.idle",
Value: 1, Value: 1,
Timestamp: 1554172967, Timestamp: 1554172967,
@ -120,7 +119,7 @@ func TestFormatMetricPoint(t *testing.T) {
"\"cpu.idle\" 1.000000 1554172967 source=\"testHost\" \"aaa\"=\"bbb\"\n", "\"cpu.idle\" 1.000000 1554172967 source=\"testHost\" \"aaa\"=\"bbb\"\n",
}, },
{ {
&wavefront.MetricPoint{ &MetricPoint{
Metric: "cpu.idle", Metric: "cpu.idle",
Value: 1, Value: 1,
Timestamp: 1554172967, Timestamp: 1554172967,
@ -144,11 +143,11 @@ func TestFormatMetricPoint(t *testing.T) {
func TestUseStrict(t *testing.T) { func TestUseStrict(t *testing.T) {
var pointTests = []struct { var pointTests = []struct {
ptIn *wavefront.MetricPoint ptIn *MetricPoint
out string out string
}{ }{
{ {
&wavefront.MetricPoint{ &MetricPoint{
Metric: "cpu.idle", Metric: "cpu.idle",
Value: 1, Value: 1,
Timestamp: 1554172967, Timestamp: 1554172967,