template

package
v1.38.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: MIT Imports: 6 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Template Serializer

The template output data format outputs metrics using an user defined go template. Sprig helper functions are also available.

Configuration

[[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.

batch_template = '''My batch metric names: {{range $index, $metric := . -}}
{{if $index}}, {{ end }}{{ $metric.Name }}
{{- end }}'''

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Serializer

type Serializer struct {
	Template      string          `toml:"template"`
	BatchTemplate string          `toml:"batch_template"`
	Log           telegraf.Logger `toml:"-"`
	// contains filtered or unexported fields
}

func (*Serializer) Init

func (s *Serializer) Init() error

func (*Serializer) Serialize

func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error)

func (*Serializer) SerializeBatch

func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL