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