Follow up work on docker_log input (#6008)
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/filter"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
"github.com/influxdata/telegraf/internal/docker"
|
||||
tlsint "github.com/influxdata/telegraf/internal/tls"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
@@ -361,44 +362,12 @@ func (d *Docker) gatherInfo(acc telegraf.Accumulator) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseImage(image string) (string, string) {
|
||||
// Adapts some of the logic from the actual Docker library's image parsing
|
||||
// routines:
|
||||
// https://github.com/docker/distribution/blob/release/2.7/reference/normalize.go
|
||||
domain := ""
|
||||
remainder := ""
|
||||
|
||||
i := strings.IndexRune(image, '/')
|
||||
|
||||
if i == -1 || (!strings.ContainsAny(image[:i], ".:") && image[:i] != "localhost") {
|
||||
remainder = image
|
||||
} else {
|
||||
domain, remainder = image[:i], image[i+1:]
|
||||
}
|
||||
|
||||
imageName := ""
|
||||
imageVersion := "unknown"
|
||||
|
||||
i = strings.LastIndex(remainder, ":")
|
||||
if i > -1 {
|
||||
imageVersion = remainder[i+1:]
|
||||
imageName = remainder[:i]
|
||||
} else {
|
||||
imageName = remainder
|
||||
}
|
||||
|
||||
if domain != "" {
|
||||
imageName = domain + "/" + imageName
|
||||
}
|
||||
|
||||
return imageName, imageVersion
|
||||
}
|
||||
|
||||
func (d *Docker) gatherContainer(
|
||||
container types.Container,
|
||||
acc telegraf.Accumulator,
|
||||
) error {
|
||||
var v *types.StatsJSON
|
||||
|
||||
// Parse container name
|
||||
var cname string
|
||||
for _, name := range container.Names {
|
||||
@@ -414,7 +383,7 @@ func (d *Docker) gatherContainer(
|
||||
return nil
|
||||
}
|
||||
|
||||
imageName, imageVersion := parseImage(container.Image)
|
||||
imageName, imageVersion := docker.ParseImage(container.Image)
|
||||
|
||||
tags := map[string]string{
|
||||
"engine_host": d.engineHost,
|
||||
|
||||
@@ -9,10 +9,9 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -945,54 +944,3 @@ func TestContainerName(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseImage(t *testing.T) {
|
||||
tests := []struct {
|
||||
image string
|
||||
parsedName string
|
||||
parsedVersion string
|
||||
}{
|
||||
{
|
||||
image: "postgres",
|
||||
parsedName: "postgres",
|
||||
parsedVersion: "unknown",
|
||||
},
|
||||
{
|
||||
image: "postgres:latest",
|
||||
parsedName: "postgres",
|
||||
parsedVersion: "latest",
|
||||
},
|
||||
{
|
||||
image: "coreos/etcd",
|
||||
parsedName: "coreos/etcd",
|
||||
parsedVersion: "unknown",
|
||||
},
|
||||
{
|
||||
image: "coreos/etcd:latest",
|
||||
parsedName: "coreos/etcd",
|
||||
parsedVersion: "latest",
|
||||
},
|
||||
{
|
||||
image: "quay.io/postgres",
|
||||
parsedName: "quay.io/postgres",
|
||||
parsedVersion: "unknown",
|
||||
},
|
||||
{
|
||||
image: "quay.io:4443/coreos/etcd",
|
||||
parsedName: "quay.io:4443/coreos/etcd",
|
||||
parsedVersion: "unknown",
|
||||
},
|
||||
{
|
||||
image: "quay.io:4443/coreos/etcd:latest",
|
||||
parsedName: "quay.io:4443/coreos/etcd",
|
||||
parsedVersion: "latest",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run("parse name "+tt.image, func(t *testing.T) {
|
||||
imageName, imageVersion := parseImage(tt.image)
|
||||
require.Equal(t, tt.parsedName, imageName)
|
||||
require.Equal(t, tt.parsedVersion, imageVersion)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user