How to control fluentd log tag from Docker

青春壹個敷衍的年華 提交于 2020-01-14 13:46:32

问题


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=?


回答1:


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



来源:https://stackoverflow.com/questions/38797531/how-to-control-fluentd-log-tag-from-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!