How to write a Telegraf Processor to change the hostname (fot GCE for instance)

telegraf
#1

I am using Telegraf to collect docker and system metrics (so far). It is deployed on a swarm on Google Cloud.

If I use the official plugins I don’t have the right hostname (as in a GCE swarm). On my custom plugins, I simply launch a request to http://metadata.google.internal/computeMetadata/v1/instance/name (go code):

	req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/name", nil)
	if err != nil {
		return "", err
	}
	req.Header.Set("Metadata-Flavor", "Google")

It works but I wish to use the official plugins (docker and system at least) as much as possible.

It seems that rewriting the hostname could be a task done by a Processor (as presented in https://docs.influxdata.com/telegraf/v1.3/concepts/aggregator_processor_plugins/).
But I can’t find any example of processors that could help me create my own.

So I have 3 questions :
1- can a processor solve my problem, ie rewrite the hostname tag (which is added by telegraf if I am not mistaken)
2- can a processor be a program (in my case in go) ?
3- is there such an example (in any language) and where ?

#2

@Nilct I’ve done this in Kubernetes by using the Downward API to inject a HOSTNAME into the telegraf container. It looks like you could do the same with that gocode you list above. I use that env to explicitly set the the [agent]hostname = "$HOSTNAME" in the config file. Does that work for for you?

#3

That would could work on our pods (the deployment depends on k8s config files) but we also have containers on workers deployed via docker-compose on a swarm (the container is built on the manager then deployed on any worker as needed).

I will nevertheless investigate this API, thanks !

1 Like