Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control fluentd log tag from Docker

Current setup is running Docker containers with the fluentd driver:

docker run --log-driver=fluentd my-container

That works quite easily...sends stdout to the locally running fluentd system on the host.

I'd like to control the fluentd tag using some of the labels which are set on the container. For this example, assume some labels (docker inspect snippet):

"Config": {
    "Labels": {
       "com.amazonaws.ecs.container-name": "web",
       "com.amazonaws.ecs.task-definition-version": "3"
    }
}

How can I set the fluentd tag by extracting out one or more of the Labels?

The Docker docs on this list out the currently supported tags: https://docs.docker.com/engine/admin/logging/log_tags/

Those docs make note of ExtraAttributes...however I can figure out how to use that to look up one or more labels. Reading through the Docker code it's not clear to me: https://github.com/docker/docker/blob/master/daemon/logger/context.go#L29

This is trivial to do with docker inspect:

docker inspect --format '{{index .ContainerLabels "com.amazonaws.ecs.container-name"}}'

I'd like to grep out the same information and get it into log-opt tag=?

like image 715
brianz Avatar asked Oct 27 '25 17:10

brianz


1 Answers

The ContainerLabels map is one of the items available in dockers logging Context and the fluentd driver supports ParseLogTag so you can use go template formatting.

In straight docker this would look like:

docker run \
  --label alabel=1value \
  --log-driver=fluentd \
  --log-opt tag="{{ .ContainerLabels.alabel }}" \
  busybox \
  echo "$(date) test log"

So if you can inject a --log-opt into your ECS startup as follows:

--log-opt tag='{{ index .ContainerLabels "com.amazonaws.ecs.container-name" }}' 

You should get your tag.

index is required here to access map keys with non alpha numeric characters

like image 191
Matt Avatar answered Oct 29 '25 09:10

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!