logspout
logspout copied to clipboard
Add template function to syslog adapter to traverse .Container.Config.Env
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
@josegonzalez any chance to get this merged? This would help us a lot in identifying logs from specific services.
Ping. Another hopeful user.
Hope this PR could be merged soon.
+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
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
-
If I'm not wrong this feature was released. you can use:
- name: SYSLOG_TAG value: '{{ index .Container.Config.Env "PROGRAM_NAME" }}'When
PROGRAM_NAMEis your environment variable. -
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-1Use 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
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 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...