Skip to content

Commit c549ab9

Browse files
committed
Throughout telegraf, use telegraf.Metric rather than client.Point
closes influxdata#599
1 parent 9c0d14b commit c549ab9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+391
-437
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
### Release Notes
44

55
### Features
6+
- [#564](https://github.com/influxdata/telegraf/issues/564): features for plugin writing simplification. Internal metric data type.
67

78
### Bugfixes
9+
- [#599](https://github.com/influxdata/telegraf/issues/599): datadog plugin tags not working.
810

911
## v0.10.1 [2016-01-27]
1012

CONTRIBUTING.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ and submit new inputs.
3737

3838
### Input Plugin Guidelines
3939

40-
* A plugin must conform to the `inputs.Input` interface.
40+
* A plugin must conform to the `telegraf.Input` interface.
4141
* Input Plugins should call `inputs.Add` in their `init` function to register themselves.
4242
See below for a quick example.
4343
* Input Plugins must be added to the
@@ -97,7 +97,10 @@ package simple
9797

9898
// simple.go
9999

100-
import "github.com/influxdata/telegraf/plugins/inputs"
100+
import (
101+
"github.com/influxdata/telegraf"
102+
"github.com/influxdata/telegraf/plugins/inputs"
103+
)
101104

102105
type Simple struct {
103106
Ok bool
@@ -122,7 +125,7 @@ func (s *Simple) Gather(acc inputs.Accumulator) error {
122125
}
123126

124127
func init() {
125-
inputs.Add("simple", func() inputs.Input { return &Simple{} })
128+
inputs.Add("simple", func() telegraf.Input { return &Simple{} })
126129
}
127130
```
128131

@@ -182,7 +185,7 @@ type Output interface {
182185
Close() error
183186
Description() string
184187
SampleConfig() string
185-
Write(points []*client.Point) error
188+
Write(metrics []telegraf.Metric) error
186189
}
187190
```
188191

@@ -193,7 +196,10 @@ package simpleoutput
193196

194197
// simpleoutput.go
195198

196-
import "github.com/influxdata/telegraf/plugins/outputs"
199+
import (
200+
"github.com/influxdata/telegraf"
201+
"github.com/influxdata/telegraf/plugins/outputs"
202+
)
197203

198204
type Simple struct {
199205
Ok bool
@@ -217,15 +223,15 @@ func (s *Simple) Close() error {
217223
return nil
218224
}
219225

220-
func (s *Simple) Write(points []*client.Point) error {
226+
func (s *Simple) Write(metrics []telegraf.Metric) error {
221227
for _, pt := range points {
222228
// write `pt` to the output sink here
223229
}
224230
return nil
225231
}
226232

227233
func init() {
228-
outputs.Add("simpleoutput", func() outputs.Output { return &Simple{} })
234+
outputs.Add("simpleoutput", func() telegraf.Output { return &Simple{} })
229235
}
230236

231237
```
@@ -253,7 +259,7 @@ type ServiceOutput interface {
253259
Close() error
254260
Description() string
255261
SampleConfig() string
256-
Write(points []*client.Point) error
262+
Write(metrics []telegraf.Metric) error
257263
Start() error
258264
Stop()
259265
}

agent/accumulator.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@ import (
77
"sync"
88
"time"
99

10+
"github.com/influxdata/telegraf"
1011
"github.com/influxdata/telegraf/internal/models"
11-
12-
"github.com/influxdata/influxdb/client/v2"
1312
)
1413

1514
func NewAccumulator(
1615
inputConfig *internal_models.InputConfig,
17-
points chan *client.Point,
16+
metrics chan telegraf.Metric,
1817
) *accumulator {
1918
acc := accumulator{}
20-
acc.points = points
19+
acc.metrics = metrics
2120
acc.inputConfig = inputConfig
2221
return &acc
2322
}
2423

2524
type accumulator struct {
2625
sync.Mutex
2726

28-
points chan *client.Point
27+
metrics chan telegraf.Metric
2928

3029
defaultTags map[string]string
3130

@@ -136,15 +135,15 @@ func (ac *accumulator) AddFields(
136135
measurement = ac.prefix + measurement
137136
}
138137

139-
pt, err := client.NewPoint(measurement, tags, result, timestamp)
138+
m, err := telegraf.NewMetric(measurement, tags, result, timestamp)
140139
if err != nil {
141140
log.Printf("Error adding point [%s]: %s\n", measurement, err.Error())
142141
return
143142
}
144143
if ac.debug {
145-
fmt.Println("> " + pt.String())
144+
fmt.Println("> " + m.String())
146145
}
147-
ac.points <- pt
146+
ac.metrics <- m
148147
}
149148

150149
func (ac *accumulator) Debug() bool {

agent/agent.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"github.com/influxdata/telegraf"
1515
"github.com/influxdata/telegraf/internal/config"
1616
"github.com/influxdata/telegraf/internal/models"
17-
18-
"github.com/influxdata/influxdb/client/v2"
1917
)
2018

2119
// Agent runs telegraf and collects data based on the given config
@@ -101,7 +99,7 @@ func panicRecover(input *internal_models.RunningInput) {
10199

102100
// gatherParallel runs the inputs that are using the same reporting interval
103101
// as the telegraf agent.
104-
func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
102+
func (a *Agent) gatherParallel(metricC chan telegraf.Metric) error {
105103
var wg sync.WaitGroup
106104

107105
start := time.Now()
@@ -118,7 +116,7 @@ func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
118116
defer panicRecover(input)
119117
defer wg.Done()
120118

121-
acc := NewAccumulator(input.Config, pointChan)
119+
acc := NewAccumulator(input.Config, metricC)
122120
acc.SetDebug(a.Config.Agent.Debug)
123121
acc.setDefaultTags(a.Config.Tags)
124122

@@ -159,7 +157,7 @@ func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
159157
func (a *Agent) gatherSeparate(
160158
shutdown chan struct{},
161159
input *internal_models.RunningInput,
162-
pointChan chan *client.Point,
160+
metricC chan telegraf.Metric,
163161
) error {
164162
defer panicRecover(input)
165163

@@ -169,7 +167,7 @@ func (a *Agent) gatherSeparate(
169167
var outerr error
170168
start := time.Now()
171169

172-
acc := NewAccumulator(input.Config, pointChan)
170+
acc := NewAccumulator(input.Config, metricC)
173171
acc.SetDebug(a.Config.Agent.Debug)
174172
acc.setDefaultTags(a.Config.Tags)
175173

@@ -201,13 +199,13 @@ func (a *Agent) gatherSeparate(
201199
func (a *Agent) Test() error {
202200
shutdown := make(chan struct{})
203201
defer close(shutdown)
204-
pointChan := make(chan *client.Point)
202+
metricC := make(chan telegraf.Metric)
205203

206204
// dummy receiver for the point channel
207205
go func() {
208206
for {
209207
select {
210-
case <-pointChan:
208+
case <-metricC:
211209
// do nothing
212210
case <-shutdown:
213211
return
@@ -216,7 +214,7 @@ func (a *Agent) Test() error {
216214
}()
217215

218216
for _, input := range a.Config.Inputs {
219-
acc := NewAccumulator(input.Config, pointChan)
217+
acc := NewAccumulator(input.Config, metricC)
220218
acc.SetDebug(true)
221219

222220
fmt.Printf("* Plugin: %s, Collection 1\n", input.Name)
@@ -263,7 +261,7 @@ func (a *Agent) flush() {
263261
}
264262

265263
// flusher monitors the points input channel and flushes on the minimum interval
266-
func (a *Agent) flusher(shutdown chan struct{}, pointChan chan *client.Point) error {
264+
func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) error {
267265
// Inelegant, but this sleep is to allow the Gather threads to run, so that
268266
// the flusher will flush after metrics are collected.
269267
time.Sleep(time.Millisecond * 200)
@@ -278,9 +276,9 @@ func (a *Agent) flusher(shutdown chan struct{}, pointChan chan *client.Point) er
278276
return nil
279277
case <-ticker.C:
280278
a.flush()
281-
case pt := <-pointChan:
279+
case m := <-metricC:
282280
for _, o := range a.Config.Outputs {
283-
o.AddPoint(pt)
281+
o.AddPoint(m)
284282
}
285283
}
286284
}
@@ -321,7 +319,7 @@ func (a *Agent) Run(shutdown chan struct{}) error {
321319
a.Config.Agent.Hostname, a.Config.Agent.FlushInterval.Duration)
322320

323321
// channel shared between all input threads for accumulating points
324-
pointChan := make(chan *client.Point, 1000)
322+
metricC := make(chan telegraf.Metric, 1000)
325323

326324
// Round collection to nearest interval by sleeping
327325
if a.Config.Agent.RoundInterval {
@@ -333,7 +331,7 @@ func (a *Agent) Run(shutdown chan struct{}) error {
333331
wg.Add(1)
334332
go func() {
335333
defer wg.Done()
336-
if err := a.flusher(shutdown, pointChan); err != nil {
334+
if err := a.flusher(shutdown, metricC); err != nil {
337335
log.Printf("Flusher routine failed, exiting: %s\n", err.Error())
338336
close(shutdown)
339337
}
@@ -358,7 +356,7 @@ func (a *Agent) Run(shutdown chan struct{}) error {
358356
wg.Add(1)
359357
go func(input *internal_models.RunningInput) {
360358
defer wg.Done()
361-
if err := a.gatherSeparate(shutdown, input, pointChan); err != nil {
359+
if err := a.gatherSeparate(shutdown, input, metricC); err != nil {
362360
log.Printf(err.Error())
363361
}
364362
}(input)
@@ -368,7 +366,7 @@ func (a *Agent) Run(shutdown chan struct{}) error {
368366
defer wg.Wait()
369367

370368
for {
371-
if err := a.gatherParallel(pointChan); err != nil {
369+
if err := a.gatherParallel(metricC); err != nil {
372370
log.Printf(err.Error())
373371
}
374372

internal/models/filter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package internal_models
33
import (
44
"strings"
55

6-
"github.com/influxdata/influxdb/client/v2"
6+
"github.com/influxdata/telegraf"
77
"github.com/influxdata/telegraf/internal"
88
)
99

@@ -24,8 +24,8 @@ type Filter struct {
2424
IsActive bool
2525
}
2626

27-
func (f Filter) ShouldPointPass(point *client.Point) bool {
28-
if f.ShouldPass(point.Name()) && f.ShouldTagsPass(point.Tags()) {
27+
func (f Filter) ShouldMetricPass(metric telegraf.Metric) bool {
28+
if f.ShouldPass(metric.Name()) && f.ShouldTagsPass(metric.Tags()) {
2929
return true
3030
}
3131
return false

internal/models/running_output.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"time"
66

77
"github.com/influxdata/telegraf"
8-
9-
"github.com/influxdata/influxdb/client/v2"
108
)
119

1210
const DEFAULT_POINT_BUFFER_LIMIT = 10000
@@ -18,7 +16,7 @@ type RunningOutput struct {
1816
Quiet bool
1917
PointBufferLimit int
2018

21-
points []*client.Point
19+
metrics []telegraf.Metric
2220
overwriteCounter int
2321
}
2422

@@ -29,42 +27,42 @@ func NewRunningOutput(
2927
) *RunningOutput {
3028
ro := &RunningOutput{
3129
Name: name,
32-
points: make([]*client.Point, 0),
30+
metrics: make([]telegraf.Metric, 0),
3331
Output: output,
3432
Config: conf,
3533
PointBufferLimit: DEFAULT_POINT_BUFFER_LIMIT,
3634
}
3735
return ro
3836
}
3937

40-
func (ro *RunningOutput) AddPoint(point *client.Point) {
38+
func (ro *RunningOutput) AddPoint(point telegraf.Metric) {
4139
if ro.Config.Filter.IsActive {
42-
if !ro.Config.Filter.ShouldPointPass(point) {
40+
if !ro.Config.Filter.ShouldMetricPass(point) {
4341
return
4442
}
4543
}
4644

47-
if len(ro.points) < ro.PointBufferLimit {
48-
ro.points = append(ro.points, point)
45+
if len(ro.metrics) < ro.PointBufferLimit {
46+
ro.metrics = append(ro.metrics, point)
4947
} else {
50-
if ro.overwriteCounter == len(ro.points) {
48+
if ro.overwriteCounter == len(ro.metrics) {
5149
ro.overwriteCounter = 0
5250
}
53-
ro.points[ro.overwriteCounter] = point
51+
ro.metrics[ro.overwriteCounter] = point
5452
ro.overwriteCounter++
5553
}
5654
}
5755

5856
func (ro *RunningOutput) Write() error {
5957
start := time.Now()
60-
err := ro.Output.Write(ro.points)
58+
err := ro.Output.Write(ro.metrics)
6159
elapsed := time.Since(start)
6260
if err == nil {
6361
if !ro.Quiet {
6462
log.Printf("Wrote %d metrics to output %s in %s\n",
65-
len(ro.points), ro.Name, elapsed)
63+
len(ro.metrics), ro.Name, elapsed)
6664
}
67-
ro.points = make([]*client.Point, 0)
65+
ro.metrics = make([]telegraf.Metric, 0)
6866
ro.overwriteCounter = 0
6967
}
7068
return err

0 commit comments

Comments
 (0)