template

package
v1.30.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 9 Imported by: 1

README

Template Processor Plugin

The template processor applies a Go template to metrics to generate a new tag. The primary use case of this plugin is to create a tag that can be used for dynamic routing to multiple output plugins or using an output specific routing option.

The template has access to each metric's measurement name, tags, fields, and timestamp using the interface in /template_metric.go.

Read the full Go Template Documentation.

Global configuration options

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 for more details.

Configuration

# Uses a Go template to create a new tag
[[processors.template]]
  ## Go template used to create the tag name of the output. In order to
  ## ease TOML escaping requirements, you should use single quotes around
  ## the template string.
  tag = "topic"

  ## Go template used to create the tag value of the output. In order to
  ## ease TOML escaping requirements, you should use single quotes around
  ## the template string.
  template = '{{ .Tag "hostname" }}.{{ .Tag "level" }}'

Examples

Combine multiple tags to create a single tag
[[processors.template]]
  tag = "topic"
  template = '{{ .Tag "hostname" }}.{{ .Tag "level" }}'
- cpu,level=debug,hostname=localhost time_idle=42
+ cpu,level=debug,hostname=localhost,topic=localhost.debug time_idle=42
Use a field value as tag name
[[processors.template]]
  tag = '{{ .Field "type" }}'
  template = '{{ .Name }}'
- cpu,level=debug,hostname=localhost time_idle=42,type=sensor
+ cpu,level=debug,hostname=localhost,sensor=cpu time_idle=42,type=sensor
Add measurement name as a tag
[[processors.template]]
  tag = "measurement"
  template = '{{ .Name }}'
- cpu,hostname=localhost time_idle=42
+ cpu,hostname=localhost,measurement=cpu time_idle=42
Add the year as a tag, similar to the date processor
[[processors.template]]
  tag = "year"
  template = '{{.Time.UTC.Year}}'
Add all fields as a tag

Sometimes it is useful to pass all fields with their values into a single message for sending it to a monitoring system (e.g. Syslog, GroundWork), then you can use .Fields or .Tags:

[[processors.template]]
  tag = "message"
  template = 'Message about {{.Name}} fields: {{.Fields}}'
- cpu,hostname=localhost time_idle=42
+ cpu,hostname=localhost,message=Message\ about\ cpu\ fields:\ map[time_idle:42] time_idle=42

More advanced example, which might make more sense:

[[processors.template]]
  tag = "message"
  template = '''Message about {{.Name}} fields:
{{ range $field, $value := .Fields -}}
{{$field}}:{{$value}}
{{ end }}'''
- cpu,hostname=localhost time_idle=42
+ cpu,hostname=localhost,message=Message\ about\ cpu\ fields:\ntime_idle:42\n time_idle=42
Just add the current metric as a tag
[[processors.template]]
  tag = "metric"
  template = '{{.}}'
- cpu,hostname=localhost time_idle=42
+ cpu,hostname=localhost,metric=cpu\ map[hostname:localhost]\ map[time_idle:42]\ 1257894000000000000 time_idle=42

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TemplateMetric

type TemplateMetric struct {
	// contains filtered or unexported fields
}

func (*TemplateMetric) Field

func (m *TemplateMetric) Field(key string) interface{}

func (*TemplateMetric) FieldList added in v1.22.0

func (m *TemplateMetric) FieldList() map[string]interface{}

func (*TemplateMetric) Fields added in v1.28.0

func (m *TemplateMetric) Fields() map[string]interface{}

func (*TemplateMetric) Name added in v1.14.1

func (m *TemplateMetric) Name() string

func (*TemplateMetric) String added in v1.22.0

func (m *TemplateMetric) String() string

func (*TemplateMetric) Tag

func (m *TemplateMetric) Tag(key string) string

func (*TemplateMetric) TagList added in v1.22.0

func (m *TemplateMetric) TagList() map[string]string

func (*TemplateMetric) Tags added in v1.28.0

func (m *TemplateMetric) Tags() map[string]string

func (*TemplateMetric) Time

func (m *TemplateMetric) Time() time.Time

type TemplateProcessor

type TemplateProcessor struct {
	Tag      string          `toml:"tag"`
	Template string          `toml:"template"`
	Log      telegraf.Logger `toml:"-"`
	// contains filtered or unexported fields
}

func (*TemplateProcessor) Apply

func (r *TemplateProcessor) Apply(in ...telegraf.Metric) []telegraf.Metric

func (*TemplateProcessor) Init

func (r *TemplateProcessor) Init() error

func (*TemplateProcessor) SampleConfig

func (*TemplateProcessor) SampleConfig() string

Jump to

Keyboard shortcuts

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