Compare commits
3 Commits
bugfix/437
...
bugfix/437
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71a93ed6b1 | ||
|
|
a7fb1c280f | ||
|
|
2e724d8d02 |
@@ -366,22 +366,9 @@ func (d *Docker) gatherContainer(
|
||||
var v *types.StatsJSON
|
||||
// Parse container name
|
||||
cname := "unknown"
|
||||
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
|
||||
if len(container.Names) > 0 {
|
||||
// Not sure what to do with other names, just take the first.
|
||||
cname = strings.TrimPrefix(container.Names[0], "/")
|
||||
}
|
||||
|
||||
// the image name sometimes has a version part, or a private repo
|
||||
@@ -404,6 +391,10 @@ 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)
|
||||
@@ -420,11 +411,6 @@ 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) {
|
||||
@@ -475,12 +461,12 @@ func (d *Docker) gatherContainer(
|
||||
acc.AddFields("docker_container_health", healthfields, tags, time.Now())
|
||||
}
|
||||
|
||||
parseContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)
|
||||
gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseContainerStats(
|
||||
func gatherContainerStats(
|
||||
stat *types.StatsJSON,
|
||||
acc telegraf.Accumulator,
|
||||
tags map[string]string,
|
||||
|
||||
@@ -107,7 +107,7 @@ func TestDockerGatherContainerStats(t *testing.T) {
|
||||
"container_image": "redis/image",
|
||||
}
|
||||
|
||||
parseContainerStats(stats, &acc, tags, "123456789", true, true, "linux")
|
||||
gatherContainerStats(stats, &acc, tags, "123456789", true, true, "linux")
|
||||
|
||||
// test docker_container_net measurement
|
||||
netfields := map[string]interface{}{
|
||||
|
||||
@@ -67,7 +67,10 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
||||
for _, metric := range in {
|
||||
for _, converter := range r.Tags {
|
||||
if value, ok := metric.GetTag(converter.Key); ok {
|
||||
metric.AddTag(r.convert(converter, value))
|
||||
k, v := r.convert(converter, value)
|
||||
if k != "" {
|
||||
metric.AddTag(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,11 +78,14 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
||||
if value, ok := metric.GetField(converter.Key); ok {
|
||||
switch value := value.(type) {
|
||||
case string:
|
||||
k, _ := r.convert(converter, value)
|
||||
if k != "" {
|
||||
metric.AddField(r.convert(converter, value))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return in
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user