2020-02-06 20:40:03 +00:00
|
|
|
# Template Processor
|
|
|
|
|
2020-02-06 21:34:36 +00:00
|
|
|
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.
|
2020-02-06 20:40:03 +00:00
|
|
|
|
2020-02-06 21:34:36 +00:00
|
|
|
The template has access to each metric's measurement name, tags, fields, and
|
|
|
|
timestamp using the [interface in `/template_metric.go`](template_metric.go).
|
|
|
|
|
|
|
|
Read the full [Go Template Documentation][].
|
2020-02-06 20:40:03 +00:00
|
|
|
|
|
|
|
### Configuration
|
|
|
|
|
|
|
|
```toml
|
2020-02-06 21:34:36 +00:00
|
|
|
[[processors.template]]
|
|
|
|
## Tag to set with the output of the template.
|
|
|
|
tag = "topic"
|
|
|
|
|
|
|
|
## Go template used to create the tag value. In order to ease TOML
|
|
|
|
## escaping requirements, you may wish to use single quotes around the
|
|
|
|
## template string.
|
|
|
|
template = '{{ .Tag "hostname" }}.{{ .Tag "level" }}'
|
2020-02-06 20:40:03 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
2020-02-06 21:34:36 +00:00
|
|
|
Combine multiple tags to create a single tag:
|
|
|
|
```toml
|
|
|
|
[[processors.template]]
|
|
|
|
tag = "topic"
|
|
|
|
template = '{{ .Tag "hostname" }}.{{ .Tag "level" }}'
|
|
|
|
```
|
|
|
|
|
|
|
|
```diff
|
|
|
|
- cpu,level=debug,hostname=localhost time_idle=42
|
|
|
|
+ cpu,level=debug,hostname=localhost,topic=localhost.debug time_idle=42
|
|
|
|
```
|
|
|
|
|
|
|
|
Add measurement name as a tag:
|
|
|
|
```toml
|
|
|
|
[[processors.template]]
|
|
|
|
tag = "measurement"
|
|
|
|
template = '{{ .Name }}'
|
|
|
|
```
|
|
|
|
|
2020-02-06 20:40:03 +00:00
|
|
|
```diff
|
2020-02-06 21:34:36 +00:00
|
|
|
- cpu,hostname=localhost time_idle=42
|
2020-05-14 07:41:58 +00:00
|
|
|
+ cpu,hostname=localhost,measurement=cpu time_idle=42
|
2020-02-06 21:34:36 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Add the year as a tag, similar to the date processor:
|
|
|
|
```toml
|
|
|
|
[[processors.template]]
|
|
|
|
tag = "year"
|
|
|
|
template = '{{.Time.UTC.Year}}'
|
2020-02-06 20:40:03 +00:00
|
|
|
```
|
|
|
|
|
2020-02-06 21:34:36 +00:00
|
|
|
[Go Template Documentation]: https://golang.org/pkg/text/template/
|