Skip to content

Commit

Permalink
Filter on all container names, use proper name
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton committed Jul 5, 2018
1 parent a7fb1c2 commit a0a6729
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
25 changes: 25 additions & 0 deletions plugins/inputs/docker/dev/telegraf.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# [agent]
# interval="1s"
# flush_interval="1s"

[[inputs.docker]]
endpoint = "unix:///var/run/docker.sock"
timeout = "5s"
perdevice = true
total = false
# container_names = []
container_names = ["test_mysql_1"]
[inputs.docker.tags]
alerting_cpu = "over90-ctnr"
[[inputs.docker]]
endpoint = "unix:///var/run/docker.sock"
timeout = "5s"
perdevice = true
total = false
container_names = []
[inputs.docker.tags]
alerting_cpu = "over90-ctnr"
# container_names = ["grafana"]

[[outputs.file]]
files = ["stdout"]
32 changes: 23 additions & 9 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,22 @@ func (d *Docker) gatherContainer(
var v *types.StatsJSON
// Parse container name
cname := "unknown"
if len(container.Names) > 0 {
// Not sure what to do with other names, just take the first.
cname = strings.TrimPrefix(container.Names[0], "/")
match := false
if len(container.Names) == 0 { // for tests
match = true
}

for i := range container.Names {
if !match {
match = d.containerFilter.Match(strings.TrimPrefix(container.Names[i], "/"))
if match {
cname = strings.TrimPrefix(container.Names[i], "/")
}
}
}

if !match {
return nil
}

// the image name sometimes has a version part, or a private repo
Expand All @@ -391,10 +404,6 @@ func (d *Docker) gatherContainer(
"container_version": imageVersion,
}

if !d.containerFilter.Match(cname) {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()
r, err := d.client.ContainerStats(ctx, container.ID, false)
Expand All @@ -411,6 +420,11 @@ func (d *Docker) gatherContainer(
}
daemonOSType := r.OSType

// use common (printed at `docker ps`) name for container
if cname != strings.TrimPrefix(v.Name, "/") && v.Name != "" {
tags["container_name"] = strings.TrimPrefix(v.Name, "/")
}

// Add labels to tags
for k, label := range container.Labels {
if d.labelFilter.Match(k) {
Expand Down Expand Up @@ -461,12 +475,12 @@ func (d *Docker) gatherContainer(
acc.AddFields("docker_container_health", healthfields, tags, time.Now())
}

gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)
parseContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)

return nil
}

func gatherContainerStats(
func parseContainerStats(
stat *types.StatsJSON,
acc telegraf.Accumulator,
tags map[string]string,
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestDockerGatherContainerStats(t *testing.T) {
"container_image": "redis/image",
}

gatherContainerStats(stats, &acc, tags, "123456789", true, true, "linux")
parseContainerStats(stats, &acc, tags, "123456789", true, true, "linux")

// test docker_container_net measurement
netfields := map[string]interface{}{
Expand Down
6 changes: 3 additions & 3 deletions plugins/processors/regex/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, converter := range r.Tags {
if value, ok := metric.GetTag(converter.Key); ok {
k, v := r.convert(converter, value)
if k != "" && v != "" {
if k != "" {
metric.AddTag(k, v)
}
}
Expand All @@ -78,8 +78,8 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
if value, ok := metric.GetField(converter.Key); ok {
switch value := value.(type) {
case string:
k, v := r.convert(converter, value)
if k != "" && v != "" {
k, _ := r.convert(converter, value)
if k != "" {
metric.AddField(r.convert(converter, value))
}
}
Expand Down
5 changes: 3 additions & 2 deletions plugins/processors/regex/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,16 @@ func TestNoMatches(t *testing.T) {
},
},
{
message: "Shouldn't emit empty string when result_key given but regex doesn't match",
message: "Should emit empty string when result_key given but regex doesn't match",
converter: converter{
Key: "request",
Pattern: "not_match",
Replacement: "x",
ResultKey: "new_field",
},
expectedFields: map[string]interface{}{
"request": "/users/42/",
"request": "/users/42/",
"new_field": "",
},
},
}
Expand Down

0 comments on commit a0a6729

Please sign in to comment.