logspout icon indicating copy to clipboard operation
logspout copied to clipboard

Add template function to syslog adapter to traverse .Container.Config.Env

Open Carles-Figuerola opened this issue 9 years ago • 8 comments
trafficstars

This was created as a way to pass a specific environment variable that's inside of the docker container. The variable .Container.Config.Env is formed of an array of key=value so it's not sortable or findable from the templating engine. Calling the container with something like this:

sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock -e SYSLOG_TAG="{{getEnvVar .Container.Config.Env \"MARATHON_APP_ID\"}}"  logspout:dev

will create a line as such:

Apr 27 15:50:56 87d5094fd8f8 nginx-marathon[19644]: 10.213.48.218 - - [27/Apr/2016:15:50:56 +0000] "GET / HTTP/1.1" 304 0 "-" "asdfsadf" "-"

where nginx-marathon is part of the .Container.Config.Env:

$ docker inspect 87d5094fd8f8 
<sic>
           "Env": [
                "MARATHON_APP_ID=nginx-marathon",
                "TEST=test",
                "SOMETHING_ELSE=false",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.9.14-1~jessie"
            ],
<sic>

This would also help solve #163 and #137

PS. several envs can be also concatenated:

sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock -e SYSLOG_TAG="{{getEnvVar .Container.Config.Env \"MARATHON_APP_ID\"}}, {{getEnvVar .Container.Config.Env \"TEST\"}}"  logspout:dev

Carles-Figuerola avatar Apr 27 '16 16:04 Carles-Figuerola

@josegonzalez any chance to get this merged? This would help us a lot in identifying logs from specific services.

JeanMertz avatar Jul 18 '16 08:07 JeanMertz

Ping. Another hopeful user.

timbunce avatar Sep 13 '16 14:09 timbunce

Hope this PR could be merged soon.

billryan avatar Oct 20 '16 08:10 billryan

+1, we really need this. Envvars are the only way to pass additional info about the container in case if your container orchestrator doesn't allow setting a container labels, but allows setting container envvars. The good example is Kubernetes, which does not allow (https://github.com/kubernetes/kubernetes/issues/3764) setting container labels, only pod labels

daniilyar avatar Nov 15 '16 00:11 daniilyar

Did somebody try to merge this and the CI somehow failed? If this PR has been approved many of us would like to use it.

jorisw avatar Oct 12 '17 09:10 jorisw

@jorisw

  1. If I'm not wrong this feature was released. you can use:

    - name: SYSLOG_TAG
      value: '{{ index .Container.Config.Env "PROGRAM_NAME" }}'
    

    When PROGRAM_NAME is your environment variable.

  2. In my environment the use case was different I just needed to know in which region I'm running, so I used configMap instead, I create configMap per environment and include the data for this specific environment. For example: Create configMap:

    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cluster-info
      namespace: kube-system
    data:
      name: amazon-web-services-us-east-1
    
    

    Use this configMap data in logspout yaml:

    env:
      - name: SYSLOG_HOSTNAME
        valueFrom:
          configMapKeyRef:
            name: cluster-info
            key: name
    

Hope this info was helpful for you :)

mashayev avatar Oct 12 '17 14:10 mashayev

@mashayev

When I try your solution I get

error calling index: cannot index slice/array with type string

This would be a great improvement for me. I'm not sure if this commit dropped off the radar because no one is available to fix the test or because people have found other ways to include individual env vars in the log messages. Does anyone following this have a working solution?

mmoore0011 avatar Mar 12 '18 15:03 mmoore0011

@mmoore0011 Just ran into this from another issue here. I'm really close to having to do something like was suggested here, but it's just essentially checking each character to see if it matches the environment variable name up until the =, the uses that. I've been working on one for the better part of an hour, but Split wasn't available...

ksmithut avatar Jun 07 '19 22:06 ksmithut