add support for streaming processors (#7634)
This commit is contained in:
@@ -3,9 +3,24 @@ package processors
|
||||
import "github.com/influxdata/telegraf"
|
||||
|
||||
type Creator func() telegraf.Processor
|
||||
type StreamingCreator func() telegraf.StreamingProcessor
|
||||
|
||||
var Processors = map[string]Creator{}
|
||||
// all processors are streaming processors.
|
||||
// telegraf.Processor processors are upgraded to telegraf.StreamingProcessor
|
||||
var Processors = map[string]StreamingCreator{}
|
||||
|
||||
// Add adds a telegraf.Processor processor
|
||||
func Add(name string, creator Creator) {
|
||||
Processors[name] = upgradeToStreamingProcessor(creator)
|
||||
}
|
||||
|
||||
// AddStreaming adds a telegraf.StreamingProcessor streaming processor
|
||||
func AddStreaming(name string, creator StreamingCreator) {
|
||||
Processors[name] = creator
|
||||
}
|
||||
|
||||
func upgradeToStreamingProcessor(oldCreator Creator) StreamingCreator {
|
||||
return func() telegraf.StreamingProcessor {
|
||||
return NewStreamingProcessorFromProcessor(oldCreator())
|
||||
}
|
||||
}
|
||||
|
||||
49
plugins/processors/streamingprocessor.go
Normal file
49
plugins/processors/streamingprocessor.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package processors
|
||||
|
||||
import (
|
||||
"github.com/influxdata/telegraf"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (sp *streamingProcessor) SampleConfig() string {
|
||||
return sp.processor.SampleConfig()
|
||||
}
|
||||
|
||||
func (sp *streamingProcessor) Description() string {
|
||||
return sp.processor.Description()
|
||||
}
|
||||
|
||||
func (sp *streamingProcessor) Start(acc telegraf.Accumulator) error {
|
||||
sp.acc = acc
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sp *streamingProcessor) Add(m telegraf.Metric, acc telegraf.Accumulator) {
|
||||
for _, m := range sp.processor.Apply(m) {
|
||||
acc.AddMetric(m)
|
||||
}
|
||||
}
|
||||
|
||||
func (sp *streamingProcessor) Stop() error {
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user