Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
92
plugins/aggregators/final/README.md
Normal file
92
plugins/aggregators/final/README.md
Normal file
|
@ -0,0 +1,92 @@
|
|||
# Final Aggregator Plugin
|
||||
|
||||
This plugin emits the last metric of a contiguous series, defined as a
|
||||
series which receives updates within the time period in `series_timeout`. The
|
||||
contiguous series may be longer than the time interval defined by `period`.
|
||||
When a series has not been updated within the `series_timeout`, the last metric
|
||||
is emitted.
|
||||
|
||||
Alternatively, the plugin emits the last metric in the `period` for the
|
||||
`periodic` output strategy.
|
||||
|
||||
This is useful for getting the final value for data sources that produce
|
||||
discrete time series such as procstat, cgroup, kubernetes etc. or to downsample
|
||||
metrics collected at a higher frequency.
|
||||
|
||||
> [!NOTE]
|
||||
> All emited metrics do have fields with `_final` appended to the field-name
|
||||
> by default.
|
||||
|
||||
⭐ Telegraf v1.11.0
|
||||
🏷️ sampling
|
||||
💻 all
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Report the final metric of a series
|
||||
[[aggregators.final]]
|
||||
## The period on which to flush & clear the aggregator.
|
||||
# period = "30s"
|
||||
|
||||
## If true, the original metric will be dropped by the
|
||||
## aggregator and will not get sent to the output plugins.
|
||||
# drop_original = false
|
||||
|
||||
## If false, _final is added to every field name
|
||||
# keep_original_field_names = false
|
||||
|
||||
## The time that a series is not updated until considering it final. Ignored
|
||||
## when output_strategy is "periodic".
|
||||
# series_timeout = "5m"
|
||||
|
||||
## Output strategy, supported values:
|
||||
## timeout -- output a metric if no new input arrived for `series_timeout`
|
||||
## periodic -- output the last received metric every `period`
|
||||
# output_strategy = "timeout"
|
||||
```
|
||||
|
||||
### Output strategy
|
||||
|
||||
By default (`output_strategy = "timeout"`) the plugin will only emit a metric
|
||||
for the period if the last received one is older than the series_timeout. This
|
||||
will not guarantee a regular output of a `final` metric e.g. if the
|
||||
series-timeout is a multiple of the gathering interval for an input. In this
|
||||
case metric sporadically arrive in the timeout phase of the period and emitting
|
||||
the `final` metric is suppressed.
|
||||
|
||||
Contrary to this, `output_strategy = "periodic"` will always output a `final`
|
||||
metric at the end of the period irrespectively of when the last metric arrived,
|
||||
the `series_timeout` is ignored.
|
||||
|
||||
## Metrics
|
||||
|
||||
Measurement and tags are unchanged, fields are emitted with the suffix
|
||||
`_final`.
|
||||
|
||||
## Example Output
|
||||
|
||||
```text
|
||||
counter,host=bar i_final=3,j_final=6 1554281635115090133
|
||||
counter,host=foo i_final=3,j_final=6 1554281635112992012
|
||||
```
|
||||
|
||||
Original input:
|
||||
|
||||
```text
|
||||
counter,host=bar i=1,j=4 1554281633101153300
|
||||
counter,host=foo i=1,j=4 1554281633099323601
|
||||
counter,host=bar i=2,j=5 1554281634107980073
|
||||
counter,host=foo i=2,j=5 1554281634105931116
|
||||
counter,host=bar i=3,j=6 1554281635115090133
|
||||
counter,host=foo i=3,j=6 1554281635112992012
|
||||
```
|
90
plugins/aggregators/final/final.go
Normal file
90
plugins/aggregators/final/final.go
Normal file
|
@ -0,0 +1,90 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package final
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/plugins/aggregators"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
type Final struct {
|
||||
OutputStrategy string `toml:"output_strategy"`
|
||||
SeriesTimeout config.Duration `toml:"series_timeout"`
|
||||
KeepOriginalFieldNames bool `toml:"keep_original_field_names"`
|
||||
|
||||
// The last metric for all series which are active
|
||||
metricCache map[uint64]telegraf.Metric
|
||||
}
|
||||
|
||||
func NewFinal() *Final {
|
||||
return &Final{
|
||||
SeriesTimeout: config.Duration(5 * time.Minute),
|
||||
}
|
||||
}
|
||||
|
||||
func (*Final) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (m *Final) Init() error {
|
||||
// Check options and set defaults
|
||||
switch m.OutputStrategy {
|
||||
case "":
|
||||
m.OutputStrategy = "timeout"
|
||||
case "timeout", "periodic":
|
||||
// Do nothing, those are valid
|
||||
default:
|
||||
return fmt.Errorf("invalid 'output_strategy': %q", m.OutputStrategy)
|
||||
}
|
||||
|
||||
// Initialize the cache
|
||||
m.metricCache = make(map[uint64]telegraf.Metric)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Final) Add(in telegraf.Metric) {
|
||||
id := in.HashID()
|
||||
m.metricCache[id] = in
|
||||
}
|
||||
|
||||
func (m *Final) Push(acc telegraf.Accumulator) {
|
||||
// Preserve timestamp of original metric
|
||||
acc.SetPrecision(time.Nanosecond)
|
||||
|
||||
for id, metric := range m.metricCache {
|
||||
if m.OutputStrategy == "timeout" && time.Since(metric.Time()) <= time.Duration(m.SeriesTimeout) {
|
||||
// We output on timeout but the last metric of the series was
|
||||
// younger than that. So skip the output for this period.
|
||||
continue
|
||||
}
|
||||
var fields map[string]any
|
||||
if m.KeepOriginalFieldNames {
|
||||
fields = metric.Fields()
|
||||
} else {
|
||||
fields = make(map[string]any, len(metric.FieldList()))
|
||||
for _, field := range metric.FieldList() {
|
||||
fields[field.Key+"_final"] = field.Value
|
||||
}
|
||||
}
|
||||
|
||||
acc.AddFields(metric.Name(), fields, metric.Tags(), metric.Time())
|
||||
delete(m.metricCache, id)
|
||||
}
|
||||
}
|
||||
|
||||
func (*Final) Reset() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
aggregators.Add("final", func() telegraf.Aggregator {
|
||||
return NewFinal()
|
||||
})
|
||||
}
|
308
plugins/aggregators/final/final_test.go
Normal file
308
plugins/aggregators/final/final_test.go
Normal file
|
@ -0,0 +1,308 @@
|
|||
package final
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestSimple(t *testing.T) {
|
||||
acc := testutil.Accumulator{}
|
||||
final := NewFinal()
|
||||
require.NoError(t, final.Init())
|
||||
|
||||
tags := map[string]string{"foo": "bar"}
|
||||
m1 := metric.New("m1",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(1)},
|
||||
time.Unix(1530939936, 0))
|
||||
m2 := metric.New("m1",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(2)},
|
||||
time.Unix(1530939937, 0))
|
||||
m3 := metric.New("m1",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(3)},
|
||||
time.Unix(1530939938, 0))
|
||||
final.Add(m1)
|
||||
final.Add(m2)
|
||||
final.Add(m3)
|
||||
final.Push(&acc)
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
testutil.MustMetric(
|
||||
"m1",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 3,
|
||||
},
|
||||
time.Unix(1530939938, 0),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics())
|
||||
}
|
||||
|
||||
func TestTwoTags(t *testing.T) {
|
||||
acc := testutil.Accumulator{}
|
||||
final := NewFinal()
|
||||
require.NoError(t, final.Init())
|
||||
|
||||
tags1 := map[string]string{"foo": "bar"}
|
||||
tags2 := map[string]string{"foo": "baz"}
|
||||
|
||||
m1 := metric.New("m1",
|
||||
tags1,
|
||||
map[string]interface{}{"a": int64(1)},
|
||||
time.Unix(1530939936, 0))
|
||||
m2 := metric.New("m1",
|
||||
tags2,
|
||||
map[string]interface{}{"a": int64(2)},
|
||||
time.Unix(1530939937, 0))
|
||||
m3 := metric.New("m1",
|
||||
tags1,
|
||||
map[string]interface{}{"a": int64(3)},
|
||||
time.Unix(1530939938, 0))
|
||||
final.Add(m1)
|
||||
final.Add(m2)
|
||||
final.Add(m3)
|
||||
final.Push(&acc)
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
testutil.MustMetric(
|
||||
"m1",
|
||||
tags2,
|
||||
map[string]interface{}{
|
||||
"a_final": 2,
|
||||
},
|
||||
time.Unix(1530939937, 0),
|
||||
),
|
||||
testutil.MustMetric(
|
||||
"m1",
|
||||
tags1,
|
||||
map[string]interface{}{
|
||||
"a_final": 3,
|
||||
},
|
||||
time.Unix(1530939938, 0),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.SortMetrics())
|
||||
}
|
||||
|
||||
func TestLongDifference(t *testing.T) {
|
||||
acc := testutil.Accumulator{}
|
||||
final := NewFinal()
|
||||
final.SeriesTimeout = config.Duration(30 * time.Second)
|
||||
require.NoError(t, final.Init())
|
||||
tags := map[string]string{"foo": "bar"}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
m1 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(1)},
|
||||
now.Add(time.Second*-290))
|
||||
m2 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(2)},
|
||||
now.Add(time.Second*-275))
|
||||
m3 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(3)},
|
||||
now.Add(time.Second*-100))
|
||||
m4 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(4)},
|
||||
now.Add(time.Second*-20))
|
||||
final.Add(m1)
|
||||
final.Add(m2)
|
||||
final.Push(&acc)
|
||||
final.Add(m3)
|
||||
final.Push(&acc)
|
||||
final.Add(m4)
|
||||
final.Push(&acc)
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
testutil.MustMetric(
|
||||
"m",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 2,
|
||||
},
|
||||
now.Add(time.Second*-275),
|
||||
),
|
||||
testutil.MustMetric(
|
||||
"m",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 3,
|
||||
},
|
||||
now.Add(time.Second*-100),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.SortMetrics())
|
||||
}
|
||||
|
||||
func TestOutputStrategyInvalid(t *testing.T) {
|
||||
final := &Final{
|
||||
OutputStrategy: "no way",
|
||||
SeriesTimeout: config.Duration(30 * time.Second),
|
||||
}
|
||||
require.ErrorContains(t, final.Init(), `invalid 'output_strategy'`)
|
||||
}
|
||||
|
||||
func TestOutputStrategyTimeout(t *testing.T) {
|
||||
final := &Final{
|
||||
OutputStrategy: "timeout",
|
||||
SeriesTimeout: config.Duration(30 * time.Second),
|
||||
}
|
||||
require.NoError(t, final.Init())
|
||||
|
||||
now := time.Now()
|
||||
tags := map[string]string{"foo": "bar"}
|
||||
m1 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(1)},
|
||||
now.Add(time.Second*-290))
|
||||
m2 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(2)},
|
||||
now.Add(time.Second*-275))
|
||||
m3 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(3)},
|
||||
now.Add(time.Second*-100))
|
||||
m4 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(4)},
|
||||
now.Add(time.Second*-20))
|
||||
|
||||
var acc testutil.Accumulator
|
||||
final.Add(m1)
|
||||
final.Add(m2)
|
||||
final.Push(&acc)
|
||||
final.Add(m3)
|
||||
final.Push(&acc)
|
||||
final.Add(m4)
|
||||
final.Push(&acc)
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
testutil.MustMetric(
|
||||
"m",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 2,
|
||||
},
|
||||
now.Add(time.Second*-275),
|
||||
),
|
||||
testutil.MustMetric(
|
||||
"m",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 3,
|
||||
},
|
||||
now.Add(time.Second*-100),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.SortMetrics())
|
||||
}
|
||||
|
||||
func TestOutputStrategyPeriodic(t *testing.T) {
|
||||
final := &Final{
|
||||
OutputStrategy: "periodic",
|
||||
SeriesTimeout: config.Duration(30 * time.Second),
|
||||
}
|
||||
require.NoError(t, final.Init())
|
||||
|
||||
now := time.Now()
|
||||
tags := map[string]string{"foo": "bar"}
|
||||
m1 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(1)},
|
||||
now.Add(time.Second*-290))
|
||||
m2 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(2)},
|
||||
now.Add(time.Second*-275))
|
||||
m3 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(3)},
|
||||
now.Add(time.Second*-100))
|
||||
m4 := metric.New("m",
|
||||
tags,
|
||||
map[string]interface{}{"a": int64(4)},
|
||||
now.Add(time.Second*-20))
|
||||
|
||||
var acc testutil.Accumulator
|
||||
final.Add(m1)
|
||||
final.Add(m2)
|
||||
final.Push(&acc)
|
||||
final.Add(m3)
|
||||
final.Push(&acc)
|
||||
final.Add(m4)
|
||||
final.Push(&acc)
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"m",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 2,
|
||||
},
|
||||
now.Add(time.Second*-275),
|
||||
),
|
||||
metric.New(
|
||||
"m",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 3,
|
||||
},
|
||||
now.Add(time.Second*-100),
|
||||
),
|
||||
metric.New(
|
||||
"m",
|
||||
tags,
|
||||
map[string]interface{}{
|
||||
"a_final": 4,
|
||||
},
|
||||
now.Add(time.Second*-20),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.SortMetrics())
|
||||
}
|
||||
|
||||
func TestKeepOriginalFieldNames(t *testing.T) {
|
||||
final := &Final{
|
||||
OutputStrategy: "periodic",
|
||||
SeriesTimeout: config.Duration(30 * time.Second),
|
||||
KeepOriginalFieldNames: true,
|
||||
}
|
||||
|
||||
require.NoError(t, final.Init())
|
||||
|
||||
now := time.Now()
|
||||
tags := map[string]string{"foo": "bar"}
|
||||
m1 := metric.New("m",
|
||||
tags,
|
||||
map[string]any{"a": 3},
|
||||
now.Add(time.Second*-90))
|
||||
|
||||
var acc testutil.Accumulator
|
||||
final.Add(m1)
|
||||
final.Push(&acc)
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"m",
|
||||
tags,
|
||||
map[string]any{"a": 3},
|
||||
now.Add(time.Second*-90),
|
||||
),
|
||||
}
|
||||
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.SortMetrics())
|
||||
}
|
20
plugins/aggregators/final/sample.conf
Normal file
20
plugins/aggregators/final/sample.conf
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Report the final metric of a series
|
||||
[[aggregators.final]]
|
||||
## The period on which to flush & clear the aggregator.
|
||||
# period = "30s"
|
||||
|
||||
## If true, the original metric will be dropped by the
|
||||
## aggregator and will not get sent to the output plugins.
|
||||
# drop_original = false
|
||||
|
||||
## If false, _final is added to every field name
|
||||
# keep_original_field_names = false
|
||||
|
||||
## The time that a series is not updated until considering it final. Ignored
|
||||
## when output_strategy is "periodic".
|
||||
# series_timeout = "5m"
|
||||
|
||||
## Output strategy, supported values:
|
||||
## timeout -- output a metric if no new input arrived for `series_timeout`
|
||||
## periodic -- output the last received metric every `period`
|
||||
# output_strategy = "timeout"
|
Loading…
Add table
Add a link
Reference in a new issue