2018-11-06 05:34:28 +08:00
|
|
|
### Processor Plugins
|
|
|
|
|
|
|
|
|
|
This section is for developers who want to create a new processor plugin.
|
|
|
|
|
|
|
|
|
|
### Processor Plugin Guidelines
|
|
|
|
|
|
|
|
|
|
* A processor must conform to the [telegraf.Processor][] interface.
|
|
|
|
|
* Processors should call `processors.Add` in their `init` function to register
|
|
|
|
|
themselves. See below for a quick example.
|
|
|
|
|
* To be available within Telegraf itself, plugins must add themselves to the
|
|
|
|
|
`github.com/influxdata/telegraf/plugins/processors/all/all.go` file.
|
|
|
|
|
* The `SampleConfig` function should return valid toml that describes how the
|
|
|
|
|
processor can be configured. This is include in the output of `telegraf
|
|
|
|
|
config`.
|
|
|
|
|
- The `SampleConfig` function should return valid toml that describes how the
|
|
|
|
|
plugin can be configured. This is included in `telegraf config`. Please
|
|
|
|
|
consult the [SampleConfig][] page for the latest style guidelines.
|
|
|
|
|
* The `Description` function should say in one line what this processor does.
|
2018-12-29 05:02:16 +08:00
|
|
|
- Follow the recommended [CodeStyle][].
|
2018-11-06 05:34:28 +08:00
|
|
|
|
|
|
|
|
### Processor Plugin Example
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
package printer
|
|
|
|
|
|
|
|
|
|
// printer.go
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/processors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Printer struct {
|
2020-10-14 23:12:41 +08:00
|
|
|
Log telegraf.Logger `toml:"-"`
|
2018-11-06 05:34:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (p *Printer) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Printer) Description() string {
|
|
|
|
|
return "Print all metrics that pass through this filter."
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 23:12:41 +08:00
|
|
|
// Init is for setup, and validating config.
|
2019-06-15 06:12:27 +08:00
|
|
|
func (p *Printer) Init() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
func (p *Printer) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
|
for _, metric := range in {
|
|
|
|
|
fmt.Println(metric.String())
|
|
|
|
|
}
|
|
|
|
|
return in
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
processors.Add("printer", func() telegraf.Processor {
|
|
|
|
|
return &Printer{}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-07 02:23:44 +08:00
|
|
|
### Streaming Processors
|
|
|
|
|
|
|
|
|
|
Streaming processors are a new processor type available to you. They are
|
|
|
|
|
particularly useful to implement processor types that use background processes
|
|
|
|
|
or goroutines to process multiple metrics at the same time. Some examples of this
|
|
|
|
|
are the execd processor, which pipes metrics out to an external process over stdin
|
|
|
|
|
and reads them back over stdout, and the reverse_dns processor, which does reverse
|
|
|
|
|
dns lookups on IP addresses in fields. While both of these come with a speed cost,
|
|
|
|
|
it would be significantly worse if you had to process one metric completely from
|
|
|
|
|
start to finish before handling the next metric, and thus they benefit
|
|
|
|
|
significantly from a streaming-pipe approach.
|
|
|
|
|
|
|
|
|
|
Some differences from classic Processors:
|
|
|
|
|
|
|
|
|
|
* Streaming processors must conform to the [telegraf.StreamingProcessor][] interface.
|
|
|
|
|
* Processors should call `processors.AddStreaming` in their `init` function to register
|
|
|
|
|
themselves. See below for a quick example.
|
|
|
|
|
|
|
|
|
|
### Streaming Processor Example
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
package printer
|
|
|
|
|
|
|
|
|
|
// printer.go
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/processors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Printer struct {
|
2020-10-14 23:12:41 +08:00
|
|
|
Log telegraf.Logger `toml:"-"`
|
2020-07-07 02:23:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (p *Printer) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Printer) Description() string {
|
|
|
|
|
return "Print all metrics that pass through this filter."
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 23:12:41 +08:00
|
|
|
// Init is for setup, and validating config.
|
2020-07-07 02:23:44 +08:00
|
|
|
func (p *Printer) Init() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 23:12:41 +08:00
|
|
|
// Start is called once when the plugin starts; it is only called once per
|
|
|
|
|
// plugin instance, and never in parallel.
|
|
|
|
|
// Start should return once it is ready to receive metrics.
|
|
|
|
|
// The passed in accumulator is the same as the one passed to Add(), so you
|
|
|
|
|
// can choose to save it in the plugin, or use the one received from Add().
|
2020-07-07 02:23:44 +08:00
|
|
|
func (p *Printer) Start(acc telegraf.Accumulator) error {
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 23:12:41 +08:00
|
|
|
// Add is called for each metric to be processed. The Add() function does not
|
|
|
|
|
// need to wait for the metric to be processed before returning, and it may
|
|
|
|
|
// be acceptable to let background goroutine(s) handle the processing if you
|
|
|
|
|
// have slow processing you need to do in parallel.
|
|
|
|
|
// Keep in mind Add() should not spawn unbounded goroutines, so you may need
|
|
|
|
|
// to use a semaphore or pool of workers (eg: reverse_dns plugin does this).
|
|
|
|
|
// Metrics you don't want to pass downstream should have metric.Drop() called,
|
|
|
|
|
// rather than simply omitting the acc.AddMetric() call
|
2020-07-07 02:23:44 +08:00
|
|
|
func (p *Printer) Add(metric telegraf.Metric, acc telegraf.Accumulator) error {
|
|
|
|
|
// print!
|
|
|
|
|
fmt.Println(metric.String())
|
|
|
|
|
// pass the metric downstream, or metric.Drop() it.
|
|
|
|
|
// Metric will be dropped if this function returns an error.
|
|
|
|
|
acc.AddMetric(metric)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 23:12:41 +08:00
|
|
|
// Stop gives you an opportunity to gracefully shut down the processor.
|
|
|
|
|
// Once Stop() is called, Add() will not be called any more. If you are using
|
|
|
|
|
// goroutines, you should wait for any in-progress metrics to be processed
|
|
|
|
|
// before returning from Stop().
|
|
|
|
|
// When stop returns, you should no longer be writing metrics to the
|
|
|
|
|
// accumulator.
|
2020-07-07 02:23:44 +08:00
|
|
|
func (p *Printer) Stop() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
processors.AddStreaming("printer", func() telegraf.StreamingProcessor {
|
|
|
|
|
return &Printer{}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
[SampleConfig]: https://github.com/influxdata/telegraf/wiki/SampleConfig
|
2018-12-29 05:02:16 +08:00
|
|
|
[CodeStyle]: https://github.com/influxdata/telegraf/wiki/CodeStyle
|
2018-11-06 05:34:28 +08:00
|
|
|
[telegraf.Processor]: https://godoc.org/github.com/influxdata/telegraf#Processor
|
2020-07-07 02:23:44 +08:00
|
|
|
[telegraf.StreamingProcessor]: https://godoc.org/github.com/influxdata/telegraf#StreamingProcessor
|