Add container id as optional source tag to docker and docker_log input (#6473)
This commit is contained in:
committed by
Daniel Nelson
parent
e7cf8319b0
commit
a5294fde32
@@ -43,6 +43,9 @@ The docker plugin uses the [Official Docker Client][] to gather logs from the
|
||||
# docker_label_include = []
|
||||
# docker_label_exclude = []
|
||||
|
||||
## Set the source tag for the metrics to the container ID hostname, eg first 12 chars
|
||||
source_tag = false
|
||||
|
||||
## Optional TLS Config
|
||||
# tls_ca = "/etc/telegraf/ca.pem"
|
||||
# tls_cert = "/etc/telegraf/cert.pem"
|
||||
@@ -58,6 +61,17 @@ When using the `"ENV"` endpoint, the connection is configured using the
|
||||
|
||||
[env]: https://godoc.org/github.com/moby/moby/client#NewEnvClient
|
||||
|
||||
### source tag
|
||||
|
||||
Selecting the containers can be tricky if you have many containers with the same name.
|
||||
To alleviate this issue you can set the below value to `true`
|
||||
|
||||
```toml
|
||||
source_tag = true
|
||||
```
|
||||
|
||||
This will cause all data points to have the `source` tag be set to the first 12 characters of the container id. The first 12 characters is the common hostname for containers that have no explicit hostname set, as defined by docker.
|
||||
|
||||
### Metrics
|
||||
|
||||
- docker_log
|
||||
@@ -66,6 +80,7 @@ When using the `"ENV"` endpoint, the connection is configured using the
|
||||
- container_version
|
||||
- container_name
|
||||
- stream (stdout, stderr, or tty)
|
||||
- source
|
||||
- fields:
|
||||
- container_id
|
||||
- message
|
||||
|
||||
@@ -49,6 +49,9 @@ var sampleConfig = `
|
||||
# docker_label_include = []
|
||||
# docker_label_exclude = []
|
||||
|
||||
## Set the source tag for the metrics to the container ID hostname, eg first 12 chars
|
||||
source_tag = false
|
||||
|
||||
## Optional TLS Config
|
||||
# tls_ca = "/etc/telegraf/ca.pem"
|
||||
# tls_cert = "/etc/telegraf/cert.pem"
|
||||
@@ -82,6 +85,7 @@ type DockerLogs struct {
|
||||
ContainerExclude []string `toml:"container_name_exclude"`
|
||||
ContainerStateInclude []string `toml:"container_state_include"`
|
||||
ContainerStateExclude []string `toml:"container_state_exclude"`
|
||||
IncludeSourceTag bool `toml:"source_tag"`
|
||||
|
||||
tlsint.ClientConfig
|
||||
|
||||
@@ -258,6 +262,10 @@ func (d *DockerLogs) tailContainerLogs(
|
||||
"container_version": imageVersion,
|
||||
}
|
||||
|
||||
if d.IncludeSourceTag {
|
||||
tags["source"] = hostnameFromID(container.ID)
|
||||
}
|
||||
|
||||
// Add matching container labels as tags
|
||||
for k, label := range container.Labels {
|
||||
if d.labelFilter.Match(k) {
|
||||
@@ -435,3 +443,10 @@ func init() {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func hostnameFromID(id string) string {
|
||||
if len(id) > 12 {
|
||||
return id[0:12]
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ func Test(t *testing.T) {
|
||||
"container_image": "influxdata/telegraf",
|
||||
"container_version": "1.11.0",
|
||||
"stream": "tty",
|
||||
"source": "deadbeef",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"container_id": "deadbeef",
|
||||
@@ -141,6 +142,7 @@ func Test(t *testing.T) {
|
||||
"container_image": "influxdata/telegraf",
|
||||
"container_version": "1.11.0",
|
||||
"stream": "stdout",
|
||||
"source": "deadbeef",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"container_id": "deadbeef",
|
||||
@@ -155,9 +157,10 @@ func Test(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var acc testutil.Accumulator
|
||||
plugin := &DockerLogs{
|
||||
Timeout: internal.Duration{Duration: time.Second * 5},
|
||||
newClient: func(string, *tls.Config) (Client, error) { return tt.client, nil },
|
||||
containerList: make(map[string]context.CancelFunc),
|
||||
Timeout: internal.Duration{Duration: time.Second * 5},
|
||||
newClient: func(string, *tls.Config) (Client, error) { return tt.client, nil },
|
||||
containerList: make(map[string]context.CancelFunc),
|
||||
IncludeSourceTag: true,
|
||||
}
|
||||
|
||||
err := plugin.Init()
|
||||
|
||||
Reference in New Issue
Block a user