Skip to content

Commit 8382ef0

Browse files
authored
refactor: replace lopg.ObserverLogs with logptest (#252)
## What does this PR do? ObserverLogs is relying on a testing package in a non-test package move to logptest ## Why is it important? <!-- Mandatory Explain here the WHY, or the rationale/motivation for the changes. --> ## Checklist <!-- Mandatory Add a checklist of things that are required to be reviewed in order to have the PR approved List here all the items you have verified BEFORE sending this PR. Please DO NOT remove any item, striking through those that do not apply. (Just in case, strikethrough uses two tildes. ~~Scratch this.~~) --> - [ ] My code follows the style guidelines of this project - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added an entry in `CHANGELOG.md` ## Author's Checklist <!-- Recommended Add a checklist of things that are required to be reviewed in order to have the PR approved --> - [ ] ## Related issues <!-- Recommended Link related issues below. Insert the issue link or reference after the word "Closes" if merging this should automatically close it. - Closes #123 - Relates #123 - Requires #123 - Superseds #123 --> -
1 parent 8cc791d commit 8382ef0

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.23.0
44

55
require (
66
github.com/docker/docker v26.1.5+incompatible
7-
github.com/elastic/elastic-agent-libs v0.20.0
7+
github.com/elastic/elastic-agent-libs v0.21.6
88
github.com/elastic/go-licenser v0.4.2
99
github.com/elastic/go-structform v0.0.9
1010
github.com/elastic/go-sysinfo v1.14.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh
2121
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
2222
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
2323
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
24-
github.com/elastic/elastic-agent-libs v0.20.0 h1:MPjenwuEr+QfMeQRV4BK817ZbiNS38SXJE7QGHDwhUs=
25-
github.com/elastic/elastic-agent-libs v0.20.0/go.mod h1:1HNxREH8C27kGrJCtKZh/ot8pV8joH8VREP21+FrH5s=
24+
github.com/elastic/elastic-agent-libs v0.21.6 h1:hvBAi4KHaYf4hn+rTc9m6A35eZjqb1uoE2exklIdWm0=
25+
github.com/elastic/elastic-agent-libs v0.21.6/go.mod h1:xSeIP3NtOIT4N2pPS4EyURmS1Q8mK0lWZ8Wd1Du6q3w=
2626
github.com/elastic/go-licenser v0.4.2 h1:bPbGm8bUd8rxzSswFOqvQh1dAkKGkgAmrPxbUi+Y9+A=
2727
github.com/elastic/go-licenser v0.4.2/go.mod h1:W8eH6FaZDR8fQGm+7FnVa7MxI1b/6dAqxz+zPB8nm5c=
2828
github.com/elastic/go-structform v0.0.9 h1:HpcS7xljL4kSyUfDJ8cXTJC6rU5ChL1wYb6cx3HLD+o=

metric/system/process/process_common.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ var PidStates = map[byte]PidState{
157157
// Init initializes a Stats instance. It returns errors if the provided process regexes
158158
// cannot be compiled.
159159
func (procStats *Stats) Init() error {
160-
procStats.logger = logp.NewLogger("processes")
160+
if procStats.logger == nil {
161+
procStats.logger = logp.NewLogger("processes")
162+
}
161163
var err error
162164
procStats.host, err = sysinfo.Host()
163165
if err != nil {

metric/system/process/process_linux_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/stretchr/testify/require"
3131

3232
"github.com/elastic/elastic-agent-libs/logp"
33+
"github.com/elastic/elastic-agent-libs/logp/logptest"
3334
"github.com/elastic/elastic-agent-libs/opt"
3435
"github.com/elastic/elastic-agent-system-metrics/dev-tools/systemtests"
3536
"github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup"
@@ -182,12 +183,14 @@ func TestParseProcStat(t *testing.T) {
182183

183184
func TestCgroupsBadCgroupsConfig(t *testing.T) {
184185
rootfs := systemtests.DockerTestResolver()
185-
_ = logp.DevelopmentSetup(logp.ToObserverOutput())
186-
testStats := Stats{CPUTicks: true,
186+
logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "")
187+
testStats := Stats{
188+
CPUTicks: true,
187189
EnableCgroups: true,
188190
EnableNetwork: true,
189191
Hostfs: rootfs,
190192
Procs: []string{".*"},
193+
logger: logger,
191194
CgroupOpts: cgroup.ReaderOptions{RootfsMountpoint: resolve.NewTestResolver("testdata")}, // procs here have no cgroup data, leading to errors
192195
}
193196
err := testStats.Init()
@@ -202,7 +205,7 @@ func TestCgroupsBadCgroupsConfig(t *testing.T) {
202205
t.Logf("got %d procs", len(procs))
203206
require.NotEmpty(t, procs)
204207

205-
gotLogs := logp.ObserverLogs().TakeAll()
208+
gotLogs := observedLogs.TakeAll()
206209
// check to see if we got the "correct" error message
207210
foundLogEntry := false
208211

0 commit comments

Comments
 (0)