Documentation

Template output data format

Use the template output data format (serializer) to format and output Telegraf metrics using custom Go templates. Sprig helper functions are also available for enhanced template functionality.

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 for formatting a single metric
  template = '{{ .Tag "host" }} {{ .Field "available" }}'

Configuration options

  • template: Go template string that defines the output format for a single metric. The template context (the “dot”) is a single metric object with methods to access tags and fields.

Template methods

Within the template, use the following methods to access metric data:

MethodDescriptionExample
.NameReturns the metric name{{ .Name }}
.Tag "key"Returns the value of the specified tag{{ .Tag "host" }}
.Field "key"Returns the value of the specified field{{ .Field "available" }}
.TagsReturns a map of all tags{{ .Tags }}
.FieldsReturns a map of all fields{{ .Fields }}
.TimeReturns the metric timestamp{{ .Time }}

Examples

Basic example

Output host and a field value:

[[outputs.file]]
  files = ["stdout"]
  data_format = "template"
  template = '{{ .Tag "host" }}: {{ .Field "usage_idle" }}'

Input metric:

cpu,host=server01 usage_idle=98.5,usage_user=1.2 1640000000000000000

Output:

server01: 98.5

Multiple fields example

Output multiple fields with formatting:

[[outputs.file]]
  files = ["stdout"]
  data_format = "template"
  template = '{{ .Name }} on {{ .Tag "host" }}: idle={{ .Field "usage_idle" }}, user={{ .Field "usage_user" }}'

Output:

cpu on server01: idle=98.5, user=1.2

Batch mode

When an output plugin emits multiple metrics in a batch, the template repeats for each metric by default. To define custom formatting for batches, use batch_template with use_batch_format = true.

In batch mode, the template context (the “dot”) is a slice of metrics instead of a single metric.

Batch configuration

[[outputs.file]]
  files = ["stdout"]
  data_format = "template"

  ## Enable batch mode (required for batch_template)
  use_batch_format = true

  ## Template for formatting multiple metrics together
  batch_template = '''
{{- range $index, $metric := . -}}
{{- if $index }}, {{ end -}}
{{ $metric.Name }}
{{- end -}}
'''

Batch example with Sprig functions

Use Sprig functions for advanced batch formatting:

[[outputs.file]]
  files = ["stdout"]
  data_format = "template"
  use_batch_format = true
  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 -}}
'''

Sprig functions

The template serializer supports Sprig template functions for string manipulation, math operations, and data transformations.

Common Sprig functions:

FunctionDescriptionExample
keysReturns map keys{{ .Fields | keys }}
valuesReturns map values{{ .Fields | values }}
getGets value by key{{ get .Fields "cpu" }}
initialAll but last element{{ .Fields | keys | initial }}
lastLast element{{ .Fields | keys | last }}
upperUppercase string{{ .Name | upper }}
lowerLowercase string{{ .Tag "host" | lower }}

For the complete list of available functions, see the Sprig documentation.


Was this page helpful?

Thank you for your feedback!


New in InfluxDB 3.7

Key enhancements in InfluxDB 3.7 and the InfluxDB 3 Explorer 1.5.

See the Blog Post

InfluxDB 3.7 is now available for both Core and Enterprise, landing alongside version 1.5 of the InfluxDB 3 Explorer UI. This release focuses on giving developers faster visibility into what their system is doing with one-click monitoring, a streamlined installation pathway, and broader updates that simplify day-to-day operations.

For more information, check out:

InfluxDB Docker latest tag changing to InfluxDB 3 Core

On February 3, 2026, the latest tag for InfluxDB Docker images will point to InfluxDB 3 Core. To avoid unexpected upgrades, use specific version tags in your Docker deployments.

If using Docker to install and run InfluxDB, the latest tag will point to InfluxDB 3 Core. To avoid unexpected upgrades, use specific version tags in your Docker deployments. For example, if using Docker to run InfluxDB v2, replace the latest version tag with a specific version tag in your Docker pull command–for example:

docker pull influxdb:2