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 @@
# Template Serializer
The `template` output data format outputs metrics using an user defined go template.
[Sprig](http://masterminds.github.io/sprig/) helper functions are also available.
## Configuration
```toml
[[outputs.file]]
## Files to write to, "stdout" is a specially handled file.
files = ["stdout", "/tmp/metrics.out"]
## Data format to output.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
data_format = "template"
## Go template which defines output format
template = '{{ .Tag "host" }} {{ .Field "available" }}'
## When used with output plugins that allow for batch serialisation
## the template for the entire batch can be defined
# use_batch_format = true # The 'file' plugin allows batch mode with this option
# batch_template = '''
{{range $metric := . -}}
{{$metric.Tag "host"}}: {{range $metric.Fields | keys | initial -}}
{{.}}={{get $metric.Fields .}}, {{end}}
{{- $metric.Fields|keys|last}}={{$metric.Fields|values|last}}
{{end -}}
'''
```
### Batch mode
When an output plugin emits multiple metrics in a batch fashion, by default the
template will just be repeated for each metric. If you would like to specifically
define how a batch should be formatted, you can use a `batch_template` instead.
In this mode, the context of the template (the 'dot') will be a slice of metrics.
```toml
batch_template = '''My batch metric names: {{range $index, $metric := . -}}
{{if $index}}, {{ end }}{{ $metric.Name }}
{{- end }}'''
```

View file

@ -0,0 +1,102 @@
package template
import (
"bytes"
"fmt"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/serializers"
)
type Serializer struct {
Template string `toml:"template"`
BatchTemplate string `toml:"batch_template"`
Log telegraf.Logger `toml:"-"`
tmplMetric *template.Template
tmplBatch *template.Template
}
func (s *Serializer) Init() error {
// Setting defaults
var err error
s.tmplMetric, err = template.New("template").Funcs(sprig.TxtFuncMap()).Parse(s.Template)
if err != nil {
return fmt.Errorf("creating template failed: %w", err)
}
if s.BatchTemplate == "" {
s.BatchTemplate = fmt.Sprintf("{{range .}}%s{{end}}", s.Template)
}
s.tmplBatch, err = template.New("batch template").Funcs(sprig.TxtFuncMap()).Parse(s.BatchTemplate)
if err != nil {
return fmt.Errorf("creating batch template failed: %w", err)
}
return nil
}
func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error) {
metricPlain := metric
if wm, ok := metric.(telegraf.UnwrappableMetric); ok {
metricPlain = wm.Unwrap()
}
m, ok := metricPlain.(telegraf.TemplateMetric)
if !ok {
s.Log.Errorf("metric of type %T is not a template metric", metricPlain)
return nil, nil
}
var b bytes.Buffer
// The template was defined for one metric, just execute it
if s.Template != "" {
if err := s.tmplMetric.Execute(&b, &m); err != nil {
s.Log.Errorf("failed to execute template: %v", err)
return nil, nil
}
return b.Bytes(), nil
}
// The template was defined for a batch of metrics, so wrap the metric into a slice
if s.BatchTemplate != "" {
metrics := []telegraf.TemplateMetric{m}
if err := s.tmplBatch.Execute(&b, &metrics); err != nil {
s.Log.Errorf("failed to execute batch template: %v", err)
return nil, nil
}
return b.Bytes(), nil
}
// No template was defined
return nil, nil
}
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
newMetrics := make([]telegraf.TemplateMetric, 0, len(metrics))
for _, metric := range metrics {
m, ok := metric.(telegraf.TemplateMetric)
if !ok {
s.Log.Errorf("metric of type %T is not a template metric", metric)
return nil, nil
}
newMetrics = append(newMetrics, m)
}
var b bytes.Buffer
if err := s.tmplBatch.Execute(&b, &newMetrics); err != nil {
s.Log.Errorf("failed to execute batch template: %v", err)
return nil, nil
}
return b.Bytes(), nil
}
func init() {
serializers.Add("template",
func() telegraf.Serializer {
return &Serializer{}
},
)
}

View file

@ -0,0 +1,205 @@
package template
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/serializers"
)
func TestSerializer(t *testing.T) {
var tests = []struct {
name string
input telegraf.Metric
template string
output []byte
errReason string
}{
{
name: "name",
input: metric.New(
"cpu",
map[string]string{},
map[string]interface{}{},
time.Unix(100, 0),
),
template: "{{ .Name }}",
output: []byte("cpu"),
},
{
name: "time",
input: metric.New(
"cpu",
map[string]string{},
map[string]interface{}{},
time.Unix(100, 0),
),
template: "{{ .Time.Unix }}",
output: []byte("100"),
},
{
name: "specific field",
input: metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"x": 42.0,
"y": 43.0,
},
time.Unix(100, 0),
),
template: `{{ .Field "x" }}`,
output: []byte("42"),
},
{
name: "specific tag",
input: metric.New(
"cpu",
map[string]string{
"host": "localhost",
"cpu": "CPU0",
},
map[string]interface{}{},
time.Unix(100, 0),
),
template: `{{ .Tag "cpu" }}`,
output: []byte("CPU0"),
},
{
name: "all fields",
input: metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"x": 42.0,
"y": 43.0,
},
time.Unix(100, 0),
),
template: `{{ range $k, $v := .Fields }}{{$k}}={{$v}},{{end}}`,
output: []byte("x=42,y=43,"),
},
{
name: "all tags",
input: metric.New(
"cpu",
map[string]string{
"host": "localhost",
"cpu": "CPU0",
},
map[string]interface{}{},
time.Unix(100, 0),
),
template: `{{ range $k, $v := .Tags }}{{$k}}={{$v}},{{end}}`,
output: []byte("cpu=CPU0,host=localhost,"),
},
{
name: "string",
input: metric.New(
"cpu",
map[string]string{
"host": "localhost",
"cpu": "CPU0",
},
map[string]interface{}{
"x": 42.0,
"y": 43.0,
},
time.Unix(100, 0),
),
template: "{{ .String }}",
output: []byte("cpu map[cpu:CPU0 host:localhost] map[x:42 y:43] 100000000000"),
},
{
name: "complex",
input: metric.New(
"cpu",
map[string]string{
"tag1": "tag",
},
map[string]interface{}{
"value": 42.0,
},
time.Unix(0, 0),
),
template: `{{ .Name }} {{ range $k, $v := .Fields}}{{$k}}={{$v}}{{end}} {{ .Tag "tag1" }} {{.Time.UnixNano}} literal`,
output: []byte("cpu value=42 tag 0 literal"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
serializer := &Serializer{
Template: tt.template,
}
require.NoError(t, serializer.Init())
output, err := serializer.Serialize(tt.input)
if tt.errReason != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.errReason)
}
require.Equal(t, string(tt.output), string(output))
// Ensure we get the same output in batch mode
batchOutput, err := serializer.SerializeBatch([]telegraf.Metric{tt.input})
if tt.errReason != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.errReason)
}
require.Equal(t, string(tt.output), string(batchOutput))
})
}
}
func TestSerializeBatch(t *testing.T) {
m := metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 42.0,
},
time.Unix(0, 0),
)
metrics := []telegraf.Metric{m, m}
s := &Serializer{BatchTemplate: `{{ range $index, $metric := . }}{{$index}}: {{$metric.Name}} {{$metric.Field "value"}}
{{end}}`}
require.NoError(t, s.Init())
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)
require.Equal(
t,
`0: cpu 42
1: cpu 42
`, string(buf),
)
// A batch template should still work when serializing a single metric
singleBuf, err := s.Serialize(m)
require.NoError(t, err)
require.Equal(t, "0: cpu 42\n", string(singleBuf))
}
func BenchmarkSerialize(b *testing.B) {
s := &Serializer{}
require.NoError(b, s.Init())
metrics := serializers.BenchmarkMetrics(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := s.Serialize(metrics[i%len(metrics)])
require.NoError(b, err)
}
}
func BenchmarkSerializeBatch(b *testing.B) {
s := &Serializer{}
require.NoError(b, s.Init())
m := serializers.BenchmarkMetrics(b)
metrics := m[:]
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := s.SerializeBatch(metrics)
require.NoError(b, err)
}
}