Compare commits
1 Commits
bugfix/437
...
bugfix/437
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b319ddd2ea |
@@ -366,9 +366,22 @@ func (d *Docker) gatherContainer(
|
|||||||
var v *types.StatsJSON
|
var v *types.StatsJSON
|
||||||
// Parse container name
|
// Parse container name
|
||||||
cname := "unknown"
|
cname := "unknown"
|
||||||
if len(container.Names) > 0 {
|
match := false
|
||||||
// Not sure what to do with other names, just take the first.
|
if len(container.Names) == 0 { // for tests
|
||||||
cname = strings.TrimPrefix(container.Names[0], "/")
|
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
|
// the image name sometimes has a version part, or a private repo
|
||||||
@@ -391,10 +404,6 @@ func (d *Docker) gatherContainer(
|
|||||||
"container_version": imageVersion,
|
"container_version": imageVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !d.containerFilter.Match(cname) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
|
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
r, err := d.client.ContainerStats(ctx, container.ID, false)
|
r, err := d.client.ContainerStats(ctx, container.ID, false)
|
||||||
@@ -411,6 +420,11 @@ func (d *Docker) gatherContainer(
|
|||||||
}
|
}
|
||||||
daemonOSType := r.OSType
|
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
|
// Add labels to tags
|
||||||
for k, label := range container.Labels {
|
for k, label := range container.Labels {
|
||||||
if d.labelFilter.Match(k) {
|
if d.labelFilter.Match(k) {
|
||||||
@@ -461,12 +475,12 @@ func (d *Docker) gatherContainer(
|
|||||||
acc.AddFields("docker_container_health", healthfields, tags, time.Now())
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatherContainerStats(
|
func parseContainerStats(
|
||||||
stat *types.StatsJSON,
|
stat *types.StatsJSON,
|
||||||
acc telegraf.Accumulator,
|
acc telegraf.Accumulator,
|
||||||
tags map[string]string,
|
tags map[string]string,
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ func TestDockerGatherContainerStats(t *testing.T) {
|
|||||||
"container_image": "redis/image",
|
"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
|
// test docker_container_net measurement
|
||||||
netfields := map[string]interface{}{
|
netfields := map[string]interface{}{
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ const sampleConfig = `
|
|||||||
# key = "request"
|
# key = "request"
|
||||||
# ## All the power of the Go regular expressions available here
|
# ## All the power of the Go regular expressions available here
|
||||||
# ## For example, named subgroups
|
# ## For example, named subgroups
|
||||||
# pattern = "^/api(?P<method>/[\\w/]+)\\S*"
|
# pattern = "^/api(?P<method>/[\\w/]+)\\S*"
|
||||||
# replacement = "${method}"
|
# replacement = "${method}"
|
||||||
# ## If result_key is present, a new field will be created
|
# ## If result_key is present, a new field will be created
|
||||||
# ## instead of changing existing field
|
# ## instead of changing existing field
|
||||||
@@ -67,10 +67,7 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
|||||||
for _, metric := range in {
|
for _, metric := range in {
|
||||||
for _, converter := range r.Tags {
|
for _, converter := range r.Tags {
|
||||||
if value, ok := metric.GetTag(converter.Key); ok {
|
if value, ok := metric.GetTag(converter.Key); ok {
|
||||||
k, v := r.convert(converter, value)
|
metric.AddTag(r.convert(converter, value))
|
||||||
if k != "" {
|
|
||||||
metric.AddTag(k, v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,10 +75,7 @@ func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
|||||||
if value, ok := metric.GetField(converter.Key); ok {
|
if value, ok := metric.GetField(converter.Key); ok {
|
||||||
switch value := value.(type) {
|
switch value := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
k, _ := r.convert(converter, value)
|
metric.AddField(r.convert(converter, value))
|
||||||
if k != "" {
|
|
||||||
metric.AddField(r.convert(converter, value))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user