2021-11-25 02:45:19 +08:00
# Aggregator Plugins
2018-11-06 05:34:28 +08:00
This section is for developers who want to create a new aggregator plugin.
2021-11-25 02:45:19 +08:00
## Aggregator Plugin Guidelines
2018-11-06 05:34:28 +08:00
* A aggregator must conform to the [telegraf.Aggregator][] interface.
* Aggregators should call `aggregators.Add` in their `init` function to
register themselves. See below for a quick example.
2022-08-16 04:44:59 +08:00
* To be available within Telegraf itself, plugins must register themselves
using a file in `github.com/influxdata/telegraf/plugins/aggregators/all`
2023-03-21 23:27:38 +08:00
named according to the plugin name. Make sure you also add build-tags to
2022-08-16 04:44:59 +08:00
conditionally build the plugin.
2022-05-26 00:25:51 +08:00
* Each plugin requires a file called `sample.conf` containing the sample configuration
for the plugin in TOML format.
2022-04-06 06:11:09 +08:00
Please consult the [Sample Config][] page for the latest style guidelines.
2022-05-26 00:25:51 +08:00
* Each plugin `README.md` file should include the `sample.conf` file in a section
describing the configuration by specifying a `toml` section in the form `toml @sample.conf` . The specified file(s) are then injected automatically into the Readme.
2018-11-06 05:34:28 +08:00
* The Aggregator plugin will need to keep caches of metrics that have passed
through it. This should be done using the builtin `HashID()` function of
each metric.
* When the `Reset()` function is called, all caches should be cleared.
2021-11-25 02:45:19 +08:00
* Follow the recommended [Code Style][].
2018-11-06 05:34:28 +08:00
### Aggregator Plugin Example
2022-08-16 04:44:59 +08:00
Content of your plugin file e.g. `min.go`
2018-11-06 05:34:28 +08:00
```go
2022-05-26 00:25:51 +08:00
//go:generate ../../../tools/readme_config_includer/generator
2018-11-06 05:34:28 +08:00
package min
// min.go
import (
2022-05-26 00:25:51 +08:00
_ "embed"
2021-11-25 02:45:19 +08:00
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/aggregators"
2018-11-06 05:34:28 +08:00
)
2022-05-26 00:25:51 +08:00
//go:embed sample.conf
var sampleConfig string
2018-11-06 05:34:28 +08:00
type Min struct {
2021-11-25 02:45:19 +08:00
// caches for metric fields, names, and tags
fieldCache map[uint64]map[string]float64
nameCache map[uint64]string
tagCache map[uint64]map[string]string
2018-11-06 05:34:28 +08:00
}
func NewMin() telegraf.Aggregator {
2021-11-25 02:45:19 +08:00
m := & Min{}
m.Reset()
return m
2018-11-06 05:34:28 +08:00
}
2022-05-26 00:25:51 +08:00
func (*Min) SampleConfig() string {
return sampleConfig
}
2019-06-15 06:12:27 +08:00
func (m *Min) Init() error {
2021-11-25 02:45:19 +08:00
return nil
2019-06-15 06:12:27 +08:00
}
2018-11-06 05:34:28 +08:00
func (m *Min) Add(in telegraf.Metric) {
2021-11-25 02:45:19 +08:00
id := in.HashID()
if _, ok := m.nameCache[id]; !ok {
// hit an uncached metric, create caches for first time:
m.nameCache[id] = in.Name()
m.tagCache[id] = in.Tags()
m.fieldCache[id] = make(map[string]float64)
for k, v := range in.Fields() {
if fv, ok := convert(v); ok {
m.fieldCache[id][k] = fv
}
}
} else {
for k, v := range in.Fields() {
if fv, ok := convert(v); ok {
if _, ok := m.fieldCache[id][k]; !ok {
// hit an uncached field of a cached metric
m.fieldCache[id][k] = fv
continue
}
if fv < m.fieldCache [ id ] [ k ] {
2018-11-06 05:34:28 +08:00
// set new minimum
2021-11-25 02:45:19 +08:00
m.fieldCache[id][k] = fv
}
}
}
}
2018-11-06 05:34:28 +08:00
}
func (m *Min) Push(acc telegraf.Accumulator) {
2021-11-25 02:45:19 +08:00
for id, _ := range m.nameCache {
fields := map[string]interface{}{}
for k, v := range m.fieldCache[id] {
fields[k+"_min"] = v
}
acc.AddFields(m.nameCache[id], fields, m.tagCache[id])
}
2018-11-06 05:34:28 +08:00
}
func (m *Min) Reset() {
2021-11-25 02:45:19 +08:00
m.fieldCache = make(map[uint64]map[string]float64)
m.nameCache = make(map[uint64]string)
m.tagCache = make(map[uint64]map[string]string)
2018-11-06 05:34:28 +08:00
}
func convert(in interface{}) (float64, bool) {
2021-11-25 02:45:19 +08:00
switch v := in.(type) {
case float64:
return v, true
case int64:
return float64(v), true
default:
return 0, false
}
2018-11-06 05:34:28 +08:00
}
func init() {
2021-11-25 02:45:19 +08:00
aggregators.Add("min", func() telegraf.Aggregator {
return NewMin()
})
2018-11-06 05:34:28 +08:00
}
```
2022-08-16 04:44:59 +08:00
Registration of the plugin on `plugins/aggregators/all/min.go` :
```go
//go:build !custom || aggregators || aggregators.min
package all
import _ "github.com/influxdata/telegraf/plugins/aggregators/min" // register plugin
```
The _build-tags_ in the first line allow to selectively include/exclude your
plugin when customizing Telegraf.
[Sample Config]: https://github.com/influxdata/telegraf/blob/master/docs/developers/SAMPLE_CONFIG.md
[Code Style]: https://github.com/influxdata/telegraf/blob/master/docs/developers/CODE_STYLE.md
[telegraf.Aggregator]: https://godoc.org/github.com/influxdata/telegraf#Aggregator