Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loki.source.docker: deduplicate matching container IDs #6055

Merged
merged 9 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ Main (unreleased)
- Add `max_cache_size` to `prometheus.relabel` to allow configurability instead of hard coded 100,000. (@mattdurham)

- Add support for `http_sd_config` within a `scrape_config` for prometheus to flow config conversion. (@erikbaranowski)

- `discovery.lightsail` now supports additional parameters for configuring HTTP client settings. (@ptodev)

- `loki.source.docker` now deduplicates targets which report the same container
ID. (@tpaschalis)

### Bugfixes

- Update `pyroscope.ebpf` to fix a logical bug causing to profile to many kthreads instead of regular processes https://github.com/grafana/pyroscope/pull/2778 (@korniltsev)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ lint: agentlint
# more without -race for packages that have known race detection issues.
test:
$(GO_ENV) go test $(GO_FLAGS) -race $(shell go list ./... | grep -v /integration-tests/)
$(GO_ENV) go test $(GO_FLAGS) ./pkg/integrations/node_exporter ./pkg/logs ./pkg/operator ./pkg/util/k8s ./component/otelcol/processor/tail_sampling ./component/loki/source/file
$(GO_ENV) go test $(GO_FLAGS) ./pkg/integrations/node_exporter ./pkg/logs ./pkg/operator ./pkg/util/k8s ./component/otelcol/processor/tail_sampling ./component/loki/source/file ./component/loki/source/docker

test-packages:
docker pull $(BUILD_IMAGE)
Expand Down
5 changes: 5 additions & 0 deletions component/loki/source/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,18 @@ 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]
if !ok {
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 {
Expand Down
40 changes: 40 additions & 0 deletions component/loki/source/docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
//go:build !race

package docker

import (
"context"
"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"
)

Expand All @@ -33,3 +37,39 @@ 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:9376"
targets = [
{__meta_docker_container_id = "foo", __meta_docker_port_private = "8080"},
{__meta_docker_container_id = "foo", __meta_docker_port_private = "8081"},
]
forward_to = []
`

var args Arguments
err := river.Unmarshal([]byte(cfg), &args)
require.NoError(t, err)

ctrl, err := componenttest.NewControllerFromID(util.TestLogger(t), "loki.source.docker")
require.NoError(t, err)

go func() {
err := ctrl.Run(context.Background(), args)
require.NoError(t, err)
}()

require.NoError(t, ctrl.WaitRunning(time.Minute))

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, 1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ fully qualified name) to store its _positions file_. The positions file
stores the read offsets so that if there is a component or Agent restart,
`loki.source.docker` can pick up tailing from the same spot.

If the target's argument contains multiple entries with the same container
ID (for example as a result of `discovery.docker` picking up multiple exposed
ports or networks), `loki.source.docker` will deduplicate them, and only keep
the first of each container ID instances, based on the
`__meta_docker_container_id` label. As such, the Docker daemon is queried
for each container ID only once, and only one target will be available in the
component's debug info.

## Example

This example collects log entries from the files specified in the `targets`
Expand Down