1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,45 @@
# Override Processor Plugin
The override processor plugin allows overriding all modifications that are
supported by input plugins and aggregators:
* name_override
* name_prefix
* name_suffix
* tags
All metrics passing through this processor will be modified accordingly. Select
the metrics to modify using the standard [metric
filtering](../../../docs/CONFIGURATION.md#metric-filtering) options.
Values of *name_override*, *name_prefix*, *name_suffix* and already present
*tags* with conflicting keys will be overwritten. Absent *tags* will be
created.
Use-case of this plugin encompass ensuring certain tags or naming conventions
are adhered to irrespective of input plugin configurations, e.g. by
`taginclude`.
## 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
# Apply metric modifications using override semantics.
[[processors.override]]
## All modifications on inputs and aggregators can be overridden:
# name_override = "new_name"
# name_prefix = "new_name_prefix"
# name_suffix = "new_name_suffix"
## Tags to be added (all values must be strings)
# [processors.override.tags]
# additional_tag = "tag_value"
```

View file

@ -0,0 +1,47 @@
//go:generate ../../../tools/readme_config_includer/generator
package override
import (
_ "embed"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/processors"
)
//go:embed sample.conf
var sampleConfig string
type Override struct {
NameOverride string `toml:"name_override"`
NamePrefix string `toml:"name_prefix"`
NameSuffix string `toml:"name_suffix"`
Tags map[string]string `toml:"tags"`
}
func (*Override) SampleConfig() string {
return sampleConfig
}
func (p *Override) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, metric := range in {
if len(p.NameOverride) > 0 {
metric.SetName(p.NameOverride)
}
if len(p.NamePrefix) > 0 {
metric.AddPrefix(p.NamePrefix)
}
if len(p.NameSuffix) > 0 {
metric.AddSuffix(p.NameSuffix)
}
for key, value := range p.Tags {
metric.AddTag(key, value)
}
}
return in
}
func init() {
processors.Add("override", func() telegraf.Processor {
return &Override{}
})
}

View file

@ -0,0 +1,164 @@
package override
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
)
func createTestMetric() telegraf.Metric {
m := metric.New("m1",
map[string]string{"metric_tag": "from_metric"},
map[string]interface{}{"value": int64(1)},
time.Now(),
)
return m
}
func calculateProcessedTags(processor Override, m telegraf.Metric) map[string]string {
processed := processor.Apply(m)
return processed[0].Tags()
}
func TestRetainsTags(t *testing.T) {
processor := Override{}
tags := calculateProcessedTags(processor, createTestMetric())
value, present := tags["metric_tag"]
require.True(t, present, "Tag of metric was not present")
require.Equal(t, "from_metric", value, "Value of Tag was changed")
}
func TestAddTags(t *testing.T) {
processor := Override{Tags: map[string]string{"added_tag": "from_config", "another_tag": ""}}
tags := calculateProcessedTags(processor, createTestMetric())
value, present := tags["added_tag"]
require.True(t, present, "Additional Tag of metric was not present")
require.Equal(t, "from_config", value, "Value of Tag was changed")
require.Len(t, tags, 3, "Should have one previous and two added tags.")
}
func TestOverwritesPresentTagValues(t *testing.T) {
processor := Override{Tags: map[string]string{"metric_tag": "from_config"}}
tags := calculateProcessedTags(processor, createTestMetric())
value, present := tags["metric_tag"]
require.True(t, present, "Tag of metric was not present")
require.Len(t, tags, 1, "Should only have one tag.")
require.Equal(t, "from_config", value, "Value of Tag was not changed")
}
func TestOverridesName(t *testing.T) {
processor := Override{NameOverride: "overridden"}
processed := processor.Apply(createTestMetric())
require.Equal(t, "overridden", processed[0].Name(), "Name was not overridden")
}
func TestNamePrefix(t *testing.T) {
processor := Override{NamePrefix: "Pre-"}
processed := processor.Apply(createTestMetric())
require.Equal(t, "Pre-m1", processed[0].Name(), "Prefix was not applied")
}
func TestNameSuffix(t *testing.T) {
processor := Override{NameSuffix: "-suff"}
processed := processor.Apply(createTestMetric())
require.Equal(t, "m1-suff", processed[0].Name(), "Suffix was not applied")
}
func TestTracking(t *testing.T) {
// Setup raw input and expected output
inputRaw := []telegraf.Metric{
metric.New(
"zero_uint64",
map[string]string{},
map[string]interface{}{"value": uint64(3)},
time.Unix(0, 0),
),
metric.New(
"zero_int64",
map[string]string{},
map[string]interface{}{"value": int64(4)},
time.Unix(0, 0),
),
metric.New(
"zero_float",
map[string]string{},
map[string]interface{}{"value": float64(5.5)},
time.Unix(0, 0),
),
}
expected := []telegraf.Metric{
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": uint64(3)},
time.Unix(0, 0),
),
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": int64(4)},
time.Unix(0, 0),
),
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": float64(5.5)},
time.Unix(0, 0),
),
}
// Create fake notification for testing
var mu sync.Mutex
delivered := make([]telegraf.DeliveryInfo, 0, len(inputRaw))
notify := func(di telegraf.DeliveryInfo) {
mu.Lock()
defer mu.Unlock()
delivered = append(delivered, di)
}
// Convert raw input to tracking metric
input := make([]telegraf.Metric, 0, len(inputRaw))
for _, m := range inputRaw {
tm, _ := metric.WithTracking(m, notify)
input = append(input, tm)
}
// Prepare and start the plugin
plugin := &Override{
NameOverride: "test",
}
// Process expected metrics and compare with resulting metrics
actual := plugin.Apply(input...)
testutil.RequireMetricsEqual(t, expected, actual)
// Simulate output acknowledging delivery
for _, m := range actual {
m.Accept()
}
// Check delivery
require.Eventuallyf(t, func() bool {
mu.Lock()
defer mu.Unlock()
return len(input) == len(delivered)
}, time.Second, 100*time.Millisecond, "%d delivered but %d expected", len(delivered), len(expected))
}

View file

@ -0,0 +1,10 @@
# Apply metric modifications using override semantics.
[[processors.override]]
## All modifications on inputs and aggregators can be overridden:
# name_override = "new_name"
# name_prefix = "new_name_prefix"
# name_suffix = "new_name_suffix"
## Tags to be added (all values must be strings)
# [processors.override.tags]
# additional_tag = "tag_value"