Storing constants in the main config

Hi,

I have multiple instances of telegraf running on different servers. To make configuration easier, configs under telegraf.d should be identical with small differences filled in with constants. Is there a way besides using secret stores or environment variables to store individual configuration variables in the main config file?

The differences between the configs are not sensitive information and should be easy to find/change.

Yes, there are several ways to store constants in the main Telegraf configuration file without using secret stores or environment variables. Here are the most practical approaches:

Global Tags

The simplest approach is using Telegraf’s global tags feature in the main telegraf.conf file:

[global_tags]
  server_name = "web-server-01"
  datacenter = "us-east-1"
  environment = "production"
  custom_identifier = "unique-value-per-server"

These tags are automatically added to all metrics and can be referenced in your telegraf.d configs using template functions.

Template Functions with Global Tags

In your telegraf.d configs, you can use template functions to reference global tags:

# In telegraf.d/example.conf
[[inputs.http]]
  urls = ["http://{{.Tag "server_name"}}.example.com/metrics"]
  name_override = "custom_{{.Tag "environment"}}_metrics"

File-based Constants Pattern

Create a separate constants file per server and include it:

# constants.conf (different per server)
[global_tags]
  server_id = "srv-001"
  region = "us-west"
  
# Include in main telegraf.conf

Hostname and Built-in Variables

Telegraf provides some built-in variables you can leverage:

[global_tags]
  host = "$HOSTNAME"  # This works even without env vars
  
[[inputs.cpu]]
  # Will automatically tag with hostname

The global tags approach is most recommended because it’s:

  • Native to Telegraf
  • Easy to modify per server
  • Automatically applied to all metrics
  • Visible in a single location
  • No additional complexity

Hi, first of all thanks for pointing me in the direction of global tags.
I already use some tags within telegraf so some additional ones won’t hurt. I prefix them with "internal_" and exclude these tags at the output to the db.

Second, do you have any reference on using these template functions?
All I could find was this; and this only seems to be for the template function itself.

Using this method in the main config gives TOML parsing errors or invalid character when te plugin starts. I need it to work for the input.http_listener_v2 and output.http IP-addresses.

[[inputs.http_listener_v2]]
  service_address = 'tcp://{{.Tag "internal_local_endpoint"}}'
  paths = ["/telegraf"]
[[outputs.http]]
  url = 'http://{{.Tag "internal_remote_endpoint"}}/telegraf'
  method = "POST"

I don’t think you can insert tags like this into the configuration of a plugin.

You also mention to include it in the main .conf. I assume you mean paste it in the file or is there a way to reference another file inside a config, like including a library? That would be neat.

# Include in main telegraf.conf

I am going to answer my own question as a reference document for myself :smiley:

You can add environment variables to you configuration like ${TELEGRAF_HOME} (see Set environment variables)
Most importantly, you don’t have to put them in windows registry keys.

Example
[[inputs.mock]]
  # Set the metric name to use for reporting
  metric_name = "mock"
  interval = "1s"
  # Optional string key-value pairs of tags to add to all metrics
  [inputs.mock.tags]
  "key" = '${TELEGRAF_HOME}'

  [[inputs.mock.sine_wave]]
    name = "wave"
    amplitude = 1.0
    period = 0.5
    phase = 20.0
    base_line = 0.0

Batch file

To set these variables on windows, you need to set environment variables in the terminal.

You can do this by running set TELEGRAF_HOME=c:\Program Files\telegraf before you start the telegraf executable.

You can put this together in a bat-file and run that for testing. You can even make the config dynamic.

batch example
@echo off
set TELEGRAF_HOME=c:\Program Files\telegraf
cmd /c ““c:\Program Files\telegraf\telegraf.exe” --config “%TELEGRAF_HOME%\Env_Test.conf” --watch-config poll”

Telegraf service

To do this for telegraf running as a service, I used nssm instead of the provided install command.

Configuration
Path: C:\Program Files\Telegraf\telegraf.exe
Startup directory: C:\Program Files\Telegraf
Arguments: --config “C:\Program Files\Telegraf\Env_Test.conf” --watch-config poll"

Environment variables: TELEGRAF_HOME=c:\Program Files\telegraf

Use case

The most important reason that I will use it now are references to starlark scripts. I made a duplicate of my telegraf folder to do some testing and had to change all the references to these files to not reference the ones in production.