Skip to content

Commit 63bdfd3

Browse files
[add_docker_metadata] log the error instead of returning it (#47760) (#47778)
* go.mod and notice * go.mod and notice * prometheus * fix watcher.Start() * changelog * dockerAvailable false * comments * unit test (cherry picked from commit 04732ce) Co-authored-by: Vihas Makwana <121151420+VihasMakwana@users.noreply.github.com>
1 parent 8580e1a commit 63bdfd3

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# REQUIRED
2+
# Kind can be one of:
3+
# - breaking-change: a change to previously-documented behavior
4+
# - deprecation: functionality that is being removed in a later release
5+
# - bug-fix: fixes a problem in a previous version
6+
# - enhancement: extends functionality but does not break or fix existing behavior
7+
# - feature: new functionality
8+
# - known-issue: problems that we are aware of in a given version
9+
# - security: impacts on the security of a product or a user’s deployment.
10+
# - upgrade: important information for someone upgrading from a prior version
11+
# - other: does not fit into any of the other categories
12+
kind: bug-fix
13+
14+
# REQUIRED for all kinds
15+
# Change summary; a 80ish characters long description of the change.
16+
summary: Not being able to start the add_docker_metadata processor is now consistently a non-fatal error when Docker is not available.
17+
18+
# REQUIRED for breaking-change, deprecation, known-issue
19+
# Long description; in case the summary is not enough to describe the change
20+
# this field accommodate a description without length limits.
21+
# description:
22+
23+
# REQUIRED for breaking-change, deprecation, known-issue
24+
# impact:
25+
26+
# REQUIRED for breaking-change, deprecation, known-issue
27+
# action:
28+
29+
# REQUIRED for all kinds
30+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
31+
component: all
32+
33+
# AUTOMATED
34+
# OPTIONAL to manually add other PR URLs
35+
# PR URL: A link the PR that added the changeset.
36+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
37+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
38+
# Please provide it if you are adding a fragment for a different PR.
39+
# pr: https://github.com/owner/repo/1234
40+
41+
# AUTOMATED
42+
# OPTIONAL to manually add other issue URLs
43+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
44+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
45+
# issue: https://github.com/owner/repo/1234

libbeat/processors/add_docker_metadata/add_docker_metadata.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ func buildDockerMetadataProcessor(log *logp.Logger, cfg *conf.C, watcherConstruc
9393
dockerAvailable = true
9494
log.Debugf("%v: docker environment detected", processorName)
9595
if err = watcher.Start(); err != nil {
96-
return nil, fmt.Errorf("failed to start watcher: %w", err)
96+
// mark dockerAvailable as false because watcher creation failed
97+
dockerAvailable = false
98+
log.Infof("unable to start the docker watcher: %v", err)
9799
}
98100
}
99101

libbeat/processors/add_docker_metadata/add_docker_metadata_test.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package add_docker_metadata
2121

2222
import (
23+
"errors"
2324
"fmt"
2425
"os"
2526
"runtime"
@@ -34,6 +35,7 @@ import (
3435
"github.com/elastic/elastic-agent-autodiscover/docker"
3536
"github.com/elastic/elastic-agent-libs/config"
3637
"github.com/elastic/elastic-agent-libs/logp"
38+
"github.com/elastic/elastic-agent-libs/logp/logptest"
3739
"github.com/elastic/elastic-agent-libs/mapstr"
3840
"github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup"
3941
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
@@ -114,7 +116,7 @@ func TestInitializationNoDocker(t *testing.T) {
114116
func TestInitialization(t *testing.T) {
115117
var testConfig = config.NewConfig()
116118

117-
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil))
119+
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil, nil))
118120
assert.NoError(t, err, "initializing add_docker_metadata processor")
119121

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

133-
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil))
135+
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil, nil))
134136
assert.NoError(t, err, "initializing add_docker_metadata processor")
135137

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

151-
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil))
153+
p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil, nil))
152154
assert.NoError(t, err, "initializing add_docker_metadata processor")
153155

154156
input := mapstr.M{
@@ -179,7 +181,7 @@ func TestMatchContainer(t *testing.T) {
179181
"b.foo": "3",
180182
},
181183
},
182-
}))
184+
}, nil))
183185
assert.NoError(t, err, "initializing add_docker_metadata processor")
184186

185187
input := mapstr.M{
@@ -227,7 +229,7 @@ func TestMatchContainerWithDedot(t *testing.T) {
227229
"b.foo": "3",
228230
},
229231
},
230-
}))
232+
}, nil))
231233
assert.NoError(t, err, "initializing add_docker_metadata processor")
232234

233235
input := mapstr.M{
@@ -269,7 +271,7 @@ func TestMatchSource(t *testing.T) {
269271
"b": "2",
270272
},
271273
},
272-
}))
274+
}, nil))
273275
assert.NoError(t, err, "initializing add_docker_metadata processor")
274276

275277
var inputSource string
@@ -328,7 +330,7 @@ func TestDisableSource(t *testing.T) {
328330
"b": "2",
329331
},
330332
},
331-
}))
333+
}, nil))
332334
assert.NoError(t, err, "initializing add_docker_metadata processor")
333335

334336
input := mapstr.M{
@@ -354,6 +356,7 @@ func TestMatchPIDs(t *testing.T) {
354356
},
355357
},
356358
},
359+
nil,
357360
))
358361
assert.NoError(t, err, "initializing add_docker_metadata processor")
359362

@@ -436,22 +439,45 @@ func TestMatchPIDs(t *testing.T) {
436439
})
437440
}
438441

442+
func TestWatcherError(t *testing.T) {
443+
logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "")
444+
testConfig, err := config.NewConfigFrom(map[string]interface{}{
445+
"match_fields": []string{"foo"},
446+
})
447+
assert.NoError(t, err)
448+
449+
p, err := buildDockerMetadataProcessor(logger, testConfig, MockWatcherFactory(nil, errors.New("mock error")))
450+
assert.NoError(t, err, "initializing add_docker_metadata processor")
451+
assert.Len(t, observedLogs.FilterMessageSnippet("unable to start the docker watcher").TakeAll(), 1)
452+
453+
input := mapstr.M{
454+
"field": "value",
455+
}
456+
result, err := p.Run(&beat.Event{Fields: input})
457+
assert.NoError(t, err, "processing an event")
458+
assert.Equal(t, mapstr.M{"field": "value"}, result.Fields)
459+
}
460+
439461
// Mock container watcher
440462

441-
func MockWatcherFactory(containers map[string]*docker.Container) docker.WatcherConstructor {
463+
func MockWatcherFactory(containers map[string]*docker.Container, startErr error) docker.WatcherConstructor {
442464
if containers == nil {
443465
containers = make(map[string]*docker.Container)
444466
}
445467
return func(_ *logp.Logger, host string, tls *docker.TLSConfig, shortID bool) (docker.Watcher, error) {
446-
return &mockWatcher{containers: containers}, nil
468+
return &mockWatcher{containers: containers, startErr: startErr}, nil
447469
}
448470
}
449471

450472
type mockWatcher struct {
451473
containers map[string]*docker.Container
474+
startErr error
452475
}
453476

454477
func (m *mockWatcher) Start() error {
478+
if m.startErr != nil {
479+
return m.startErr
480+
}
455481
return nil
456482
}
457483

0 commit comments

Comments
 (0)