Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 8837a66

Browse files
Add adapter for new plugins (#333)
* add vendor dependencies and update SignalFx golib * add adapter for additional plugins
1 parent 4d82b5a commit 8837a66

File tree

22 files changed

+1632
-15
lines changed

22 files changed

+1632
-15
lines changed

Gopkg.lock

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
[[constraint]]
7676
name = "github.com/signalfx/golib"
77-
revision = "8321403a729d6c8fa60a3dcf2c60bf20f71f6062"
77+
revision = "b1be94c3ec8d1fdeccd62735b851489f0b3798e0"
7878

7979
[[constraint]]
8080
name = "github.com/signalfx/metricproxy"
@@ -142,3 +142,7 @@
142142
[[constraint]]
143143
name = "github.com/Microsoft/go-winio"
144144
revision = "ab35fc04b6365e8fcb18e6e9e41ea4a02b10b175"
145+
146+
[[constraint]]
147+
name = "github.com/signalfx/telegraf"
148+
revision = "df5485433c763fbe76fd8bfc6ad80a81e93bb9c5"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package accumulator
2+
3+
import (
4+
"time"
5+
6+
"github.com/signalfx/golib/datapoint"
7+
"github.com/signalfx/signalfx-agent/internal/monitors/telegraf/common/emitter"
8+
)
9+
10+
// Accumulator is an interface used to accumulate telegraf measurements from
11+
// Telegraf plugins.
12+
type Accumulator struct {
13+
emit emitter.Emitter
14+
}
15+
16+
// AddFields receives a measurement with tags and a time stamp to the accumulator.
17+
// Measurements are passed to the Accumulator's Emitter.
18+
func (ac *Accumulator) AddFields(measurement string, fields map[string]interface{},
19+
tags map[string]string, t ...time.Time) {
20+
ac.emit.Add(measurement, fields, tags, datapoint.Gauge, "untyped", t...)
21+
}
22+
23+
// AddGauge receives a measurement as a "Gauge" with tags and a time stamp to
24+
// the accumulator. Measurements are passed to the Accumulator's Emitter.
25+
func (ac *Accumulator) AddGauge(measurement string, fields map[string]interface{},
26+
tags map[string]string, t ...time.Time) {
27+
ac.emit.Add(measurement, fields, tags, datapoint.Gauge, "", t...)
28+
}
29+
30+
// AddCounter receives a measurement as a "Counter" with tags and a time stamp
31+
// to the accumulator. Measurements are passed to the Accumulator's Emitter.
32+
func (ac *Accumulator) AddCounter(measurement string, fields map[string]interface{},
33+
tags map[string]string, t ...time.Time) {
34+
ac.emit.Add(measurement, fields, tags, datapoint.Counter, "", t...)
35+
}
36+
37+
// AddSummary receives a measurement as a "Counter" with tags and a time stamp
38+
// to the accumulator. Measurements are passed to the Accumulator's Emitter.
39+
func (ac *Accumulator) AddSummary(measurement string, fields map[string]interface{},
40+
tags map[string]string, t ...time.Time) {
41+
ac.emit.Add(measurement, fields, tags, datapoint.Gauge, "summary", t...)
42+
}
43+
44+
// AddHistogram receives a measurement as a "Counter" with tags and a time stamp
45+
// to the accumulator. Measurements are passed to the Accumulator's Emitter.
46+
func (ac *Accumulator) AddHistogram(measurement string, fields map[string]interface{},
47+
tags map[string]string, t ...time.Time) {
48+
ac.emit.Add(measurement, fields, tags, datapoint.Gauge, "histogram", t...)
49+
}
50+
51+
// SetPrecision - SignalFx does not implement this
52+
func (ac *Accumulator) SetPrecision(precision, interval time.Duration) {
53+
}
54+
55+
// AddError - log an error returned by the plugin
56+
func (ac *Accumulator) AddError(err error) {
57+
ac.emit.AddError(err)
58+
}
59+
60+
// NewAccumulator returns a pointer to an Accumulator
61+
func NewAccumulator(e emitter.Emitter) *Accumulator {
62+
return &Accumulator{
63+
emit: e,
64+
}
65+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package accumulator
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
"time"
8+
9+
"github.com/signalfx/golib/datapoint"
10+
)
11+
12+
type testEmitter struct {
13+
measurement string
14+
fields map[string]interface{}
15+
tags map[string]string
16+
metricType datapoint.MetricType
17+
originalMetricType string
18+
t time.Time
19+
err error
20+
}
21+
22+
func (e *testEmitter) Add(measurement string, fields map[string]interface{},
23+
tags map[string]string, metricType datapoint.MetricType, originalMetricType string, t ...time.Time) {
24+
e.measurement = measurement
25+
e.fields = fields
26+
e.tags = tags
27+
e.metricType = metricType
28+
e.originalMetricType = originalMetricType
29+
e.t = t[0]
30+
}
31+
func (e *testEmitter) IncludeEvent(string) {}
32+
func (e *testEmitter) IncludeEvents([]string) {}
33+
func (e *testEmitter) ExcludeDatum(string) {}
34+
func (e *testEmitter) ExcludeData([]string) {}
35+
func (e *testEmitter) AddTag(string, string) {}
36+
func (e *testEmitter) AddTags(map[string]string) {}
37+
func (e *testEmitter) OmitTag(string) {}
38+
func (e *testEmitter) OmitTags([]string) {}
39+
func (e *testEmitter) AddError(err error) {
40+
e.err = err
41+
}
42+
43+
func TestAccumulator(t *testing.T) {
44+
ac := NewAccumulator(&testEmitter{})
45+
tests := []struct {
46+
name string
47+
want *testEmitter
48+
fn func(string, map[string]interface{}, map[string]string, ...time.Time)
49+
}{
50+
{
51+
name: "AddFields()",
52+
want: &testEmitter{
53+
measurement: "field_measurement",
54+
fields: map[string]interface{}{"dim1": "dimval1"},
55+
tags: map[string]string{"tag1": "tagval1"},
56+
metricType: datapoint.Gauge,
57+
originalMetricType: "untyped",
58+
t: time.Now(),
59+
},
60+
fn: ac.AddFields,
61+
},
62+
{
63+
name: "AddGauge()",
64+
want: &testEmitter{
65+
measurement: "gauge_measurement",
66+
fields: map[string]interface{}{"dim1": "dimval1"},
67+
tags: map[string]string{"tag1": "tagval1"},
68+
metricType: datapoint.Gauge,
69+
originalMetricType: "gauge",
70+
t: time.Now(),
71+
},
72+
fn: ac.AddGauge,
73+
},
74+
{
75+
name: "AddCounter()",
76+
want: &testEmitter{
77+
measurement: "counter_measurement",
78+
fields: map[string]interface{}{"dim1": "dimval1"},
79+
tags: map[string]string{"tag1": "tagval1"},
80+
metricType: datapoint.Counter,
81+
originalMetricType: "counter",
82+
t: time.Now(),
83+
},
84+
fn: ac.AddCounter,
85+
},
86+
{
87+
name: "AddSummary()",
88+
want: &testEmitter{
89+
measurement: "summary_measurement",
90+
fields: map[string]interface{}{"dim1": "dimval1"},
91+
tags: map[string]string{"tag1": "tagval1"},
92+
metricType: datapoint.Gauge,
93+
originalMetricType: "summary",
94+
t: time.Now(),
95+
},
96+
fn: ac.AddSummary,
97+
},
98+
{
99+
name: "AddHistogram()",
100+
want: &testEmitter{
101+
measurement: "histogram_measurement",
102+
fields: map[string]interface{}{"dim1": "dimval1"},
103+
tags: map[string]string{"tag1": "tagval1"},
104+
metricType: datapoint.Gauge,
105+
originalMetricType: "histogram",
106+
t: time.Now(),
107+
},
108+
fn: ac.AddHistogram,
109+
},
110+
}
111+
for _, tt := range tests {
112+
t.Run(tt.name, func(t *testing.T) {
113+
ac.emit = &testEmitter{}
114+
tt.fn(tt.want.measurement, tt.want.fields, tt.want.tags, tt.want.t)
115+
if ac.emit.(*testEmitter).measurement != tt.want.measurement ||
116+
!reflect.DeepEqual(ac.emit.(*testEmitter).fields, tt.want.fields) ||
117+
!reflect.DeepEqual(ac.emit.(*testEmitter).tags, tt.want.tags) {
118+
t.Errorf("Accumulator_AddFields() = %v, want %v", ac.emit, tt.want)
119+
}
120+
})
121+
}
122+
t.Run("SetPrecision()", func(t *testing.T) {
123+
ac.emit = &testEmitter{}
124+
ac.SetPrecision(time.Second*1, time.Second*1)
125+
})
126+
t.Run("AddError()", func(t *testing.T) {
127+
ac.emit = &testEmitter{}
128+
err := fmt.Errorf("Test Error")
129+
ac.AddError(err)
130+
if ac.emit.(*testEmitter).err != err {
131+
t.Errorf("AddError() = %v, want %v", ac.emit.(*testEmitter).err, err)
132+
}
133+
})
134+
}

0 commit comments

Comments
 (0)