From 812392ec9151fda1dcfa01efd124cdb50111aa62 Mon Sep 17 00:00:00 2001 From: Paschalis Tsilias Date: Fri, 5 Jan 2024 14:22:17 +0200 Subject: [PATCH] loki.source.docker: filter out duplicate container IDs Signed-off-by: Paschalis Tsilias --- component/loki/source/docker/docker.go | 5 ++++ component/loki/source/docker/docker_test.go | 29 +++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/component/loki/source/docker/docker.go b/component/loki/source/docker/docker.go index 400b1d30b5e6..193f27f1d7a2 100644 --- a/component/loki/source/docker/docker.go +++ b/component/loki/source/docker/docker.go @@ -215,6 +215,7 @@ func (c *Component) Update(args component.Arguments) error { // Convert input targets into targets to give to tailer. targets := make([]*dt.Target, 0, len(newArgs.Targets)) + seenTargets := make(map[string]struct{}, len(newArgs.Targets)) for _, target := range newArgs.Targets { containerID, ok := target[dockerLabelContainerID] @@ -222,6 +223,10 @@ func (c *Component) Update(args component.Arguments) error { level.Debug(c.opts.Logger).Log("msg", "docker target did not include container ID label:"+dockerLabelContainerID) continue } + if _, seen := seenTargets[containerID]; seen { + continue + } + seenTargets[containerID] = struct{}{} var labels = make(model.LabelSet) for k, v := range target { diff --git a/component/loki/source/docker/docker_test.go b/component/loki/source/docker/docker_test.go index 51c2a4568cff..0fa0a9921322 100644 --- a/component/loki/source/docker/docker_test.go +++ b/component/loki/source/docker/docker_test.go @@ -5,9 +5,11 @@ import ( "testing" "time" + "github.com/grafana/agent/component" "github.com/grafana/agent/pkg/flow/componenttest" "github.com/grafana/agent/pkg/util" "github.com/grafana/river" + "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/require" ) @@ -33,3 +35,30 @@ func Test(t *testing.T) { require.NoError(t, ctrl.WaitRunning(time.Minute)) } + +func TestDuplicateTargets(t *testing.T) { + // Use host that works on all platforms (including Windows). + var cfg = ` + host = "tcp://127.0.0.1:9375" + targets = [ + {__meta_docker_container_id = "foo", __meta_docker_port_private = "8080"}, + {__meta_docker_container_id = "foo", __meta_docker_port_private = "8080"}, + {__meta_docker_container_id = "bar", __meta_docker_port_private = "8080"}, + ] + forward_to = [] + ` + + var args Arguments + err := river.Unmarshal([]byte(cfg), &args) + require.NoError(t, err) + + cmp, err := New(component.Options{ + ID: "loki.source.docker.test", + Logger: util.TestFlowLogger(t), + Registerer: prometheus.NewRegistry(), + DataPath: t.TempDir(), + }, args) + require.NoError(t, err) + + require.Len(t, cmp.manager.tasks, 2) +}