forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamingprocessor.go
61 lines (52 loc) · 1.47 KB
/
streamingprocessor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package processors
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/models"
)
// NewStreamingProcessorFromProcessor is a converter that turns a standard
// processor into a streaming processor
func NewStreamingProcessorFromProcessor(p telegraf.Processor) telegraf.StreamingProcessor {
sp := &streamingProcessor{
processor: p,
}
return sp
}
type streamingProcessor struct {
processor telegraf.Processor
acc telegraf.Accumulator
Log telegraf.Logger
}
func (sp *streamingProcessor) SampleConfig() string {
return sp.processor.SampleConfig()
}
func (sp *streamingProcessor) Start(acc telegraf.Accumulator) error {
sp.acc = acc
return nil
}
func (sp *streamingProcessor) Add(m telegraf.Metric, acc telegraf.Accumulator) error {
for _, m := range sp.processor.Apply(m) {
acc.AddMetric(m)
}
return nil
}
func (sp *streamingProcessor) Stop() {
}
// Make the streamingProcessor of type Initializer to be able
// to call the Init method of the wrapped processor if
// needed
func (sp *streamingProcessor) Init() error {
models.SetLoggerOnPlugin(sp.processor, sp.Log)
if p, ok := sp.processor.(telegraf.Initializer); ok {
err := p.Init()
if err != nil {
return err
}
}
return nil
}
// Unwrap lets you retrieve the original telegraf.Processor from the
// StreamingProcessor. This is necessary because the toml Unmarshaller won't
// look inside composed types.
func (sp *streamingProcessor) Unwrap() telegraf.Processor {
return sp.processor
}