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.
|
|
|
|
|
* To be available within Telegraf itself, plugins must add themselves to the
|
|
|
|
|
`github.com/influxdata/telegraf/plugins/aggregators/all/all.go` file.
|
2022-04-06 06:11:09 +08:00
|
|
|
* Each plugin requires a file called `<plugin_name>_sample_config.go`, where `<plugin_name>` is replaced with the actual plugin name.
|
|
|
|
|
Copy the [example template](#sample-configuration-template) into this file, also updating `<plugin_name>` were appropriate.
|
|
|
|
|
This file is automatically updated during the build process to include the sample configuration from the `README.md`.
|
|
|
|
|
Please consult the [Sample Config][] page for the latest style guidelines.
|
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
|
|
|
|
|
|
|
|
|
|
```go
|
2022-04-06 06:11:09 +08:00
|
|
|
//go:generate go run ../../../tools/generate_plugindata/main.go
|
|
|
|
|
//go:generate go run ../../../tools/generate_plugindata/main.go --clean
|
2018-11-06 05:34:28 +08:00
|
|
|
package min
|
|
|
|
|
|
|
|
|
|
// min.go
|
|
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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-04-06 06:11:09 +08:00
|
|
|
### Sample Configuration Template
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
//go:generate go run ../../../tools/generate_plugindata/main.go
|
|
|
|
|
//go:generate go run ../../../tools/generate_plugindata/main.go --clean
|
|
|
|
|
// DON'T EDIT; This file is used as a template by tools/generate_plugindata
|
|
|
|
|
package <plugin_package>
|
|
|
|
|
|
|
|
|
|
func (k *<plugin_struct>) SampleConfig() string {
|
|
|
|
|
return `{{ .SampleConfig }}`
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
[telegraf.Aggregator]: https://godoc.org/github.com/influxdata/telegraf#Aggregator
|
2021-05-08 03:26:11 +08:00
|
|
|
[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
|