Skip to content

Commit

Permalink
refactor: Prevent import from going the wrong way (influxdata#12004)
Browse files Browse the repository at this point in the history
  • Loading branch information
suprajanarasimhan authored Oct 13, 2022
1 parent 94e39fa commit 90d8f42
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
18 changes: 5 additions & 13 deletions plugins/outputs/wavefront/wavefront.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"regexp"
"strings"

wavefront "github.com/wavefronthq/wavefront-sdk-go/senders"

"github.com/influxdata/telegraf"
"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
Expand Down Expand Up @@ -65,14 +65,6 @@ var tagValueReplacer = 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 {
return sampleConfig
}
Expand Down Expand Up @@ -153,8 +145,8 @@ func (w *Wavefront) Write(metrics []telegraf.Metric) error {
return nil
}

func (w *Wavefront) buildMetrics(m telegraf.Metric) []*MetricPoint {
ret := make([]*MetricPoint, 0)
func (w *Wavefront) buildMetrics(m telegraf.Metric) []*serializer.MetricPoint {
ret := make([]*serializer.MetricPoint, 0)

for fieldName, value := range m.Fields() {
var name string
Expand All @@ -176,7 +168,7 @@ func (w *Wavefront) buildMetrics(m telegraf.Metric) []*MetricPoint {
name = pathReplacer.Replace(name)
}

metric := &MetricPoint{
metric := &serializer.MetricPoint{
Metric: name,
Timestamp: m.Time().Unix(),
}
Expand Down
21 changes: 11 additions & 10 deletions plugins/outputs/wavefront/wavefront_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/outputs"
serializer "github.com/influxdata/telegraf/plugins/serializers/wavefront"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -45,25 +46,25 @@ func TestBuildMetrics(t *testing.T) {

var metricTests = []struct {
metric telegraf.Metric
metricPoints []MetricPoint
metricPoints []serializer.MetricPoint
}{
{
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.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
},
},
{
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.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
},
},
{
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"}}},
},
}

Expand All @@ -88,18 +89,18 @@ func TestBuildMetricsStrict(t *testing.T) {

var metricTests = []struct {
metric telegraf.Metric
metricPoints []MetricPoint
metricPoints []serializer.MetricPoint
}{
{
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.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
},
},
{
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.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag/1": "value1", "tag,2": "value2"}},
},
Expand Down Expand Up @@ -132,15 +133,15 @@ func TestBuildMetricsWithSimpleFields(t *testing.T) {

var metricTests = []struct {
metric telegraf.Metric
metricLines []MetricPoint
metricLines []serializer.MetricPoint
}{
{
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,
[]MetricPoint{{Metric: w.Prefix + "test.simple.metric.value", Value: 123}},
[]serializer.MetricPoint{{Metric: w.Prefix + "test.simple.metric.value", Value: 123}},
},
}

Expand Down
13 changes: 10 additions & 3 deletions plugins/serializers/wavefront/wavefront.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"

"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
Expand Down Expand Up @@ -41,6 +40,14 @@ var tagValueReplacer = 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) {
s := &WavefrontSerializer{
Prefix: prefix,
Expand Down Expand Up @@ -79,7 +86,7 @@ func (s *WavefrontSerializer) serializeMetric(m telegraf.Metric) {
continue
}
source, tags := buildTags(m.Tags(), s)
metric := wavefront.MetricPoint{
metric := MetricPoint{
Metric: name,
Timestamp: m.Time().Unix(),
Value: metricValue,
Expand Down Expand Up @@ -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.WriteString(metricPoint.Metric)
b.WriteString(`" `)
Expand Down
11 changes: 5 additions & 6 deletions plugins/serializers/wavefront/wavefront_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/outputs/wavefront"
)

func TestBuildTags(t *testing.T) {
Expand Down Expand Up @@ -106,11 +105,11 @@ func TestBuildTagsHostTag(t *testing.T) {

func TestFormatMetricPoint(t *testing.T) {
var pointTests = []struct {
ptIn *wavefront.MetricPoint
ptIn *MetricPoint
out string
}{
{
&wavefront.MetricPoint{
&MetricPoint{
Metric: "cpu.idle",
Value: 1,
Timestamp: 1554172967,
Expand All @@ -120,7 +119,7 @@ func TestFormatMetricPoint(t *testing.T) {
"\"cpu.idle\" 1.000000 1554172967 source=\"testHost\" \"aaa\"=\"bbb\"\n",
},
{
&wavefront.MetricPoint{
&MetricPoint{
Metric: "cpu.idle",
Value: 1,
Timestamp: 1554172967,
Expand All @@ -144,11 +143,11 @@ func TestFormatMetricPoint(t *testing.T) {

func TestUseStrict(t *testing.T) {
var pointTests = []struct {
ptIn *wavefront.MetricPoint
ptIn *MetricPoint
out string
}{
{
&wavefront.MetricPoint{
&MetricPoint{
Metric: "cpu.idle",
Value: 1,
Timestamp: 1554172967,
Expand Down

0 comments on commit 90d8f42

Please sign in to comment.