Skip to content
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
45 changes: 45 additions & 0 deletions changelog/fragments/1764007304-bump-elastic-autodiscover.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# REQUIRED
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: bug-fix

# REQUIRED for all kinds
# Change summary; a 80ish characters long description of the change.
summary: Not being able to start the add_docker_metadata processor is now consistently a non-fatal error when Docker is not available.

# REQUIRED for breaking-change, deprecation, known-issue
# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# description:

# REQUIRED for breaking-change, deprecation, known-issue
# impact:

# REQUIRED for breaking-change, deprecation, known-issue
# action:

# REQUIRED for all kinds
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component: all

# AUTOMATED
# OPTIONAL to manually add other PR URLs
# PR URL: A link the PR that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
# pr: https://github.com/owner/repo/1234

# AUTOMATED
# OPTIONAL to manually add other issue URLs
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
# issue: https://github.com/owner/repo/1234
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
// initCgroupPaths initializes a new cgroup reader. This enables
// unit testing by allowing us to stub the OS interface.
var initCgroupPaths processors.InitCgroupHandler = func(rootfsMountpoint resolve.Resolver, ignoreRootCgroups bool) (processors.CGReader, error) {
return cgroup.NewReader(rootfsMountpoint, ignoreRootCgroups)

Check failure on line 53 in libbeat/processors/add_docker_metadata/add_docker_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

SA1019: cgroup.NewReader is deprecated: use NewReaderOptions (staticcheck)
}

func init() {
Expand Down Expand Up @@ -93,7 +93,9 @@
dockerAvailable = true
log.Debugf("%v: docker environment detected", processorName)
if err = watcher.Start(); err != nil {
return nil, fmt.Errorf("failed to start watcher: %w", err)
// mark dockerAvailable as false because watcher creation failed
dockerAvailable = false
log.Infof("unable to start the docker watcher: %v", err)
}
}

Expand Down Expand Up @@ -264,7 +266,7 @@
if d.cgroups != nil {
if cid := d.cgroups.Get(pid); cid != nil {
d.log.Debugf("Using cached cgroups for pid=%v", pid)
return cid.(string), nil

Check failure on line 269 in libbeat/processors/add_docker_metadata/add_docker_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Error return value is not checked (errcheck)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package add_docker_metadata

import (
"errors"
"fmt"
"os"
"runtime"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/elastic/elastic-agent-autodiscover/docker"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
Expand Down Expand Up @@ -114,7 +116,7 @@ func TestInitializationNoDocker(t *testing.T) {
func TestInitialization(t *testing.T) {
var testConfig = config.NewConfig()

p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil))
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil, nil))
assert.NoError(t, err, "initializing add_docker_metadata processor")

input := mapstr.M{}
Expand All @@ -130,7 +132,7 @@ func TestNoMatch(t *testing.T) {
})
assert.NoError(t, err)

p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil))
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil, nil))
assert.NoError(t, err, "initializing add_docker_metadata processor")

input := mapstr.M{
Expand All @@ -148,7 +150,7 @@ func TestMatchNoContainer(t *testing.T) {
})
assert.NoError(t, err)

p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil))
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil, nil))
assert.NoError(t, err, "initializing add_docker_metadata processor")

input := mapstr.M{
Expand Down Expand Up @@ -179,7 +181,7 @@ func TestMatchContainer(t *testing.T) {
"b.foo": "3",
},
},
}))
}, nil))
assert.NoError(t, err, "initializing add_docker_metadata processor")

input := mapstr.M{
Expand Down Expand Up @@ -227,7 +229,7 @@ func TestMatchContainerWithDedot(t *testing.T) {
"b.foo": "3",
},
},
}))
}, nil))
assert.NoError(t, err, "initializing add_docker_metadata processor")

input := mapstr.M{
Expand Down Expand Up @@ -269,7 +271,7 @@ func TestMatchSource(t *testing.T) {
"b": "2",
},
},
}))
}, nil))
assert.NoError(t, err, "initializing add_docker_metadata processor")

var inputSource string
Expand Down Expand Up @@ -328,7 +330,7 @@ func TestDisableSource(t *testing.T) {
"b": "2",
},
},
}))
}, nil))
assert.NoError(t, err, "initializing add_docker_metadata processor")

input := mapstr.M{
Expand All @@ -354,6 +356,7 @@ func TestMatchPIDs(t *testing.T) {
},
},
},
nil,
))
assert.NoError(t, err, "initializing add_docker_metadata processor")

Expand Down Expand Up @@ -436,22 +439,45 @@ func TestMatchPIDs(t *testing.T) {
})
}

func TestWatcherError(t *testing.T) {
logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "")
testConfig, err := config.NewConfigFrom(map[string]interface{}{
"match_fields": []string{"foo"},
})
assert.NoError(t, err)

p, err := buildDockerMetadataProcessor(logger, testConfig, MockWatcherFactory(nil, errors.New("mock error")))
assert.NoError(t, err, "initializing add_docker_metadata processor")
assert.Len(t, observedLogs.FilterMessageSnippet("unable to start the docker watcher").TakeAll(), 1)

input := mapstr.M{
"field": "value",
}
result, err := p.Run(&beat.Event{Fields: input})
assert.NoError(t, err, "processing an event")
assert.Equal(t, mapstr.M{"field": "value"}, result.Fields)
}

// Mock container watcher

func MockWatcherFactory(containers map[string]*docker.Container) docker.WatcherConstructor {
func MockWatcherFactory(containers map[string]*docker.Container, startErr error) docker.WatcherConstructor {
if containers == nil {
containers = make(map[string]*docker.Container)
}
return func(_ *logp.Logger, host string, tls *docker.TLSConfig, shortID bool) (docker.Watcher, error) {
return &mockWatcher{containers: containers}, nil
return &mockWatcher{containers: containers, startErr: startErr}, nil
}
}

type mockWatcher struct {
containers map[string]*docker.Container
startErr error
}

func (m *mockWatcher) Start() error {
if m.startErr != nil {
return m.startErr
}
return nil
}

Expand Down
Loading