Skip to content

Commit 0fec46c

Browse files
mergify[bot]kruskalldependabot[bot]github-actions[bot]
authored
[9.0] (backport #16705) test: use logp test logger in tests (#16709)
* test: use logp test logger in tests (#16705) global logger is deprecated, use testing logger (cherry picked from commit 8f64c3c) # Conflicts: # internal/agentcfg/cache_test.go * lint: fix linter issues * build(deps): bump github.com/elastic/elastic-agent-libs from 0.18.11 to 0.18.12 (#16398) * build(deps): bump github.com/elastic/elastic-agent-libs Bumps [github.com/elastic/elastic-agent-libs](https://github.com/elastic/elastic-agent-libs) from 0.18.11 to 0.18.12. - [Release notes](https://github.com/elastic/elastic-agent-libs/releases) - [Commits](elastic/elastic-agent-libs@v0.18.11...v0.18.12) --- updated-dependencies: - dependency-name: github.com/elastic/elastic-agent-libs dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * Update NOTICE.txt * Update NOTICE-fips.txt --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: kruskall <99559985+kruskall@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent efd1f58 commit 0fec46c

File tree

18 files changed

+117
-83
lines changed

18 files changed

+117
-83
lines changed

NOTICE-fips.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,11 @@ SOFTWARE
711711

712712
--------------------------------------------------------------------------------
713713
Dependency : github.com/elastic/elastic-agent-libs
714-
Version: v0.18.10
714+
Version: v0.19.3
715715
Licence type (autodetected): Apache-2.0
716716
--------------------------------------------------------------------------------
717717

718-
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.18.10/LICENSE:
718+
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.19.3/LICENSE:
719719

720720
Apache License
721721
Version 2.0, January 2004

NOTICE.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,11 @@ SOFTWARE
711711

712712
--------------------------------------------------------------------------------
713713
Dependency : github.com/elastic/elastic-agent-libs
714-
Version: v0.18.10
714+
Version: v0.19.3
715715
Licence type (autodetected): Apache-2.0
716716
--------------------------------------------------------------------------------
717717

718-
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.18.10/LICENSE:
718+
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.19.3/LICENSE:
719719

720720
Apache License
721721
Version 2.0, January 2004

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/elastic/apm-data v1.16.0
1212
github.com/elastic/beats/v7 v7.0.0-alpha2.0.20250416141948-93a20b29921e
1313
github.com/elastic/elastic-agent-client/v7 v7.17.0
14-
github.com/elastic/elastic-agent-libs v0.18.10
14+
github.com/elastic/elastic-agent-libs v0.19.3
1515
github.com/elastic/elastic-agent-system-metrics v0.11.9
1616
github.com/elastic/gmux v0.3.2
1717
github.com/elastic/go-docappender/v2 v2.4.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ github.com/elastic/elastic-agent-autodiscover v0.9.0 h1:+iWIKh0u3e8I+CJa3FfWe9h0
182182
github.com/elastic/elastic-agent-autodiscover v0.9.0/go.mod h1:5iUxLHhVdaGSWYTveSwfJEY4RqPXTG13LPiFoxcpFd4=
183183
github.com/elastic/elastic-agent-client/v7 v7.17.0 h1:TPLrEHF4kJ3RkmQzZPffrniY4WeW4vriHZbOAzM1hFo=
184184
github.com/elastic/elastic-agent-client/v7 v7.17.0/go.mod h1:6h+f9QdIr3GO2ODC0Y8+aEXRwzbA5W4eV4dd/67z7nI=
185-
github.com/elastic/elastic-agent-libs v0.18.10 h1:ZmO8ihcWhULEyUA+9WyQR5W1+wxkTPOGiwLlqPy7s7I=
186-
github.com/elastic/elastic-agent-libs v0.18.10/go.mod h1:Repx7BMzE1v/gTipPogNIQeEnSGwOWGBC63h7h9c5aM=
185+
github.com/elastic/elastic-agent-libs v0.19.3 h1:DuSVJMowcV3tHbW44SDzCbVJv3Y696GTBRikcAxwr2A=
186+
github.com/elastic/elastic-agent-libs v0.19.3/go.mod h1:1HNxREH8C27kGrJCtKZh/ot8pV8joH8VREP21+FrH5s=
187187
github.com/elastic/elastic-agent-system-metrics v0.11.9 h1:UDgT4ygXKGRwyNyUDzBKbDW7bYphZNg1GOLtDFqZ4Wg=
188188
github.com/elastic/elastic-agent-system-metrics v0.11.9/go.mod h1:FgtshyeVEAxNqRoFkdL0MpCW0rHxQGFNEKjIsxfcrag=
189189
github.com/elastic/elastic-transport-go/v8 v8.6.1 h1:h2jQRqH6eLGiBSN4eZbQnJLtL4bC5b4lfVFRjw2R4e4=

internal/agentcfg/cache_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
"github.com/stretchr/testify/require"
2828

29-
"github.com/elastic/elastic-agent-libs/logp"
29+
"github.com/elastic/elastic-agent-libs/logp/logptest"
3030
)
3131

3232
var (
@@ -40,10 +40,10 @@ type cacheSetup struct {
4040
result Result
4141
}
4242

43-
func newCacheSetup(service string, exp time.Duration, init bool) cacheSetup {
43+
func newCacheSetup(t testing.TB, service string, exp time.Duration, init bool) cacheSetup {
4444
setup := cacheSetup{
4545
query: Query{Service: Service{Name: service}, Etag: "123"},
46-
cache: newCache(logp.NewLogger(""), exp),
46+
cache: newCache(logptest.NewTestingLogger(t, ""), exp),
4747
result: defaultResult,
4848
}
4949
if init {
@@ -67,7 +67,7 @@ func TestCache_fetchAndAdd(t *testing.T) {
6767
"NilDocFromFunction": {fetchFunc: testFnNil},
6868
} {
6969
t.Run(name, func(t *testing.T) {
70-
setup := newCacheSetup(name, exp, testCase.init)
70+
setup := newCacheSetup(t, name, exp, testCase.init)
7171

7272
doc, err := setup.cache.fetch(setup.query, testCase.fetchFunc)
7373
assert.Equal(t, testCase.doc, doc)
@@ -85,7 +85,7 @@ func TestCache_fetchAndAdd(t *testing.T) {
8585

8686
t.Run("CacheKeyExpires", func(t *testing.T) {
8787
exp := 100 * time.Millisecond
88-
setup := newCacheSetup(t.Name(), exp, false)
88+
setup := newCacheSetup(t, t.Name(), exp, false)
8989
doc, err := setup.cache.fetch(setup.query, testFn)
9090
require.NoError(t, err)
9191
require.NotNil(t, doc)
@@ -107,7 +107,7 @@ func BenchmarkFetchAndAdd(b *testing.B) {
107107
// intialize the cache and add a document to it before the benchmark,
108108
// to ensure docs are only fetched from cache
109109
exp := 5 * time.Minute
110-
setup := newCacheSetup(b.Name(), exp, true)
110+
setup := newCacheSetup(b, b.Name(), exp, true)
111111
for i := 0; i < b.N; i++ {
112112
setup.cache.fetch(setup.query, testFn)
113113
}
@@ -117,7 +117,7 @@ func BenchmarkFetchAndAdd(b *testing.B) {
117117
// intialize the cache, test adding random docs to cache
118118
// to ensure a fetch and add operation per call
119119
exp := 5 * time.Minute
120-
setup := newCacheSetup(b.Name(), exp, false)
120+
setup := newCacheSetup(b, b.Name(), exp, false)
121121
q := Query{Service: Service{}}
122122
for i := 0; i < b.N; i++ {
123123
q.Service.Name = fmt.Sprintf("%v", b.N)

internal/beater/beater_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141
"github.com/elastic/apm-server/internal/beater/config"
4242
"github.com/elastic/apm-server/internal/elasticsearch"
4343
agentconfig "github.com/elastic/elastic-agent-libs/config"
44-
"github.com/elastic/elastic-agent-libs/logp"
44+
"github.com/elastic/elastic-agent-libs/logp/logptest"
4545
"github.com/elastic/elastic-agent-libs/monitoring"
4646
"github.com/elastic/elastic-agent-libs/opt"
4747
"github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup"
@@ -183,7 +183,7 @@ func TestRunnerNewDocappenderConfig(t *testing.T) {
183183
t.Run(fmt.Sprintf("default/%vgb", c.memSize), func(t *testing.T) {
184184
r := Runner{
185185
elasticsearchOutputConfig: agentconfig.NewConfig(),
186-
logger: logp.NewLogger("test"),
186+
logger: logptest.NewTestingLogger(t, "test"),
187187
}
188188
docCfg, esCfg, err := r.newDocappenderConfig(nil, nil, c.memSize)
189189
require.NoError(t, err)
@@ -215,7 +215,7 @@ func TestRunnerNewDocappenderConfig(t *testing.T) {
215215
"flush_interval": "2s",
216216
"max_requests": 50,
217217
}),
218-
logger: logp.NewLogger("test"),
218+
logger: logptest.NewTestingLogger(t, "test"),
219219
}
220220
docCfg, esCfg, err := r.newDocappenderConfig(nil, nil, c.memSize)
221221
require.NoError(t, err)
@@ -328,7 +328,6 @@ func TestNewInstrumentationWithSampling(t *testing.T) {
328328
}
329329

330330
func TestProcessMemoryLimit(t *testing.T) {
331-
l := logp.NewLogger("test")
332331
const gb = 1 << 30
333332
for name, testCase := range map[string]struct {
334333
cgroups cgroupReader
@@ -408,7 +407,7 @@ func TestProcessMemoryLimit(t *testing.T) {
408407
},
409408
} {
410409
t.Run(name, func(t *testing.T) {
411-
memLimitGB := processMemoryLimit(testCase.cgroups, testCase.sys, l)
410+
memLimitGB := processMemoryLimit(testCase.cgroups, testCase.sys, logptest.NewTestingLogger(t, "test"))
412411
assert.Equal(t, testCase.wantMemLimitGB, memLimitGB)
413412
})
414413
}

internal/beater/config/rum_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
"github.com/elastic/apm-server/internal/elasticsearch"
2626
"github.com/elastic/elastic-agent-libs/config"
27-
"github.com/elastic/elastic-agent-libs/logp"
27+
"github.com/elastic/elastic-agent-libs/logp/logptest"
2828

2929
"github.com/stretchr/testify/assert"
3030
)
@@ -38,7 +38,7 @@ func TestRumSetup(t *testing.T) {
3838
"hosts": []interface{}{"cloud:9200"},
3939
})
4040

41-
err := rum.setup(logp.NewLogger("test"), esCfg)
41+
err := rum.setup(logptest.NewTestingLogger(t, "test"), esCfg)
4242

4343
require.NoError(t, err)
4444
assert.Equal(t, elasticsearch.Hosts{"cloud:9200"}, rum.SourceMapping.ESConfig.Hosts)

internal/beater/interceptors/logging_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ import (
2323
"testing"
2424

2525
"github.com/stretchr/testify/assert"
26-
"github.com/stretchr/testify/require"
26+
"go.uber.org/zap"
2727
"go.uber.org/zap/zapcore"
28+
"go.uber.org/zap/zaptest/observer"
2829
"google.golang.org/grpc"
2930
"google.golang.org/grpc/codes"
3031
"google.golang.org/grpc/status"
3132

32-
agentconfig "github.com/elastic/elastic-agent-libs/config"
3333
"github.com/elastic/elastic-agent-libs/logp"
34-
"github.com/elastic/elastic-agent-libs/logp/configure"
3534
)
3635

3736
func TestLogging(t *testing.T) {
@@ -70,16 +69,15 @@ func TestLogging(t *testing.T) {
7069
},
7170
},
7271
} {
73-
configure.Logging(
74-
"APM Server test",
75-
agentconfig.MustNewConfigFrom(`{"ecs":true}`),
76-
)
77-
require.NoError(t, logp.DevelopmentSetup(logp.ToObserverOutput()))
78-
logger := logp.NewLogger("interceptor.logging.test")
72+
observedCore, observedLogs := observer.New(zapcore.InfoLevel)
73+
wrapCore := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
74+
return observedCore
75+
})
76+
logger := logp.NewLogger("interceptor.logging.test", wrapCore)
7977

8078
i := Logging(logger)
8179
_, err := i(ctx, nil, info, tc.f)
82-
entries := logp.ObserverLogs().TakeAll()
80+
entries := observedLogs.All()
8381
assert.Len(t, entries, 1)
8482
entry := entries[0]
8583

internal/beater/interceptors/metrics_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
"github.com/elastic/apm-server/internal/beater/monitoringtest"
3434
"github.com/elastic/apm-server/internal/beater/request"
35-
"github.com/elastic/elastic-agent-libs/logp"
35+
"github.com/elastic/elastic-agent-libs/logp/logptest"
3636
)
3737

3838
func TestMetrics(t *testing.T) {
@@ -53,17 +53,6 @@ func TestMetrics(t *testing.T) {
5353
prefix: "apm-server.otlp.grpc.logs.",
5454
},
5555
} {
56-
reader := sdkmetric.NewManualReader(sdkmetric.WithTemporalitySelector(
57-
func(ik sdkmetric.InstrumentKind) metricdata.Temporality {
58-
return metricdata.DeltaTemporality
59-
},
60-
))
61-
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
62-
63-
logger := logp.NewLogger("interceptor.metrics.test")
64-
65-
interceptor := Metrics(logger, mp)
66-
6756
ctx := context.Background()
6857
info := &grpc.UnaryServerInfo{
6958
FullMethod: metrics.methodName,
@@ -159,6 +148,16 @@ func TestMetrics(t *testing.T) {
159148
},
160149
} {
161150
t.Run(tc.name, func(t *testing.T) {
151+
reader := sdkmetric.NewManualReader(sdkmetric.WithTemporalitySelector(
152+
func(ik sdkmetric.InstrumentKind) metricdata.Temporality {
153+
return metricdata.DeltaTemporality
154+
},
155+
))
156+
157+
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
158+
logger := logptest.NewTestingLogger(t, "interceptor.metrics.test")
159+
interceptor := Metrics(logger, mp)
160+
162161
interceptor(ctx, nil, info, tc.f)
163162

164163
expectedMetrics := make(map[string]any, 2*len(tc.expectedOtel))
@@ -186,7 +185,7 @@ func TestMetrics_ConcurrentSafe(t *testing.T) {
186185
},
187186
))
188187
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
189-
logger := logp.NewLogger("interceptor.metrics.test")
188+
logger := logptest.NewTestingLogger(t, "interceptor.metrics.test")
190189
interceptor := Metrics(logger, mp)
191190

192191
ctx := context.Background()

internal/beater/middleware/log_middleware_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
"github.com/stretchr/testify/require"
2828
"go.uber.org/zap/zapcore"
29+
"go.uber.org/zap/zaptest/observer"
2930

3031
"go.elastic.co/apm/v2"
3132
"go.elastic.co/apm/v2/apmtest"
3233

33-
agentconfig "github.com/elastic/elastic-agent-libs/config"
3434
"github.com/elastic/elastic-agent-libs/logp"
35-
"github.com/elastic/elastic-agent-libs/logp/configure"
3635
"github.com/elastic/elastic-agent-libs/mapstr"
3736

3837
"github.com/elastic/apm-server/internal/beater/headers"
@@ -99,9 +98,15 @@ func TestLogMiddleware(t *testing.T) {
9998
for _, tc := range testCases {
10099
t.Run(tc.name, func(t *testing.T) {
101100
// log setup
102-
configure.Logging("APM Server test",
103-
agentconfig.MustNewConfigFrom(`{"ecs":true}`))
104-
require.NoError(t, logp.DevelopmentSetup(logp.ToObserverOutput()))
101+
observedCore, observedLogs := observer.New(zapcore.DebugLevel)
102+
err := logp.ConfigureWithOutputs(logp.Config{
103+
Level: logp.DebugLevel,
104+
ToStderr: false,
105+
ToSyslog: false,
106+
ToFiles: false,
107+
ToEventLog: false,
108+
}, observedCore)
109+
require.NoError(t, err)
105110

106111
// prepare and record request
107112
c, rec := DefaultContextWithResponseRecorder()
@@ -115,7 +120,7 @@ func TestLogMiddleware(t *testing.T) {
115120

116121
// check log lines
117122
assert.Equal(t, tc.code, rec.Code)
118-
entries := logp.ObserverLogs().TakeAll()
123+
entries := observedLogs.All()
119124
require.Equal(t, 1, len(entries))
120125
entry := entries[0]
121126
assert.Equal(t, logs.Request, entry.LoggerName)
@@ -140,8 +145,15 @@ func TestLogMiddleware(t *testing.T) {
140145
}
141146

142147
func TestLogMiddlewareRequestBodyBytes(t *testing.T) {
143-
configure.Logging("APM Server test", agentconfig.MustNewConfigFrom(`{"ecs":true}`))
144-
require.NoError(t, logp.DevelopmentSetup(logp.ToObserverOutput()))
148+
observedCore, observedLogs := observer.New(zapcore.DebugLevel)
149+
err := logp.ConfigureWithOutputs(logp.Config{
150+
Level: logp.DebugLevel,
151+
ToStderr: false,
152+
ToSyslog: false,
153+
ToFiles: false,
154+
ToEventLog: false,
155+
}, observedCore)
156+
require.NoError(t, err)
145157

146158
c := request.NewContext()
147159
c.Reset(
@@ -150,7 +162,7 @@ func TestLogMiddlewareRequestBodyBytes(t *testing.T) {
150162
)
151163
Apply(LogMiddleware(), HandlerIdle)(c)
152164

153-
entries := logp.ObserverLogs().TakeAll()
165+
entries := observedLogs.All()
154166
require.Equal(t, 1, len(entries))
155167
field := entries[0].ContextMap()["http.request.body.bytes"]
156168
assert.Equal(t, int64(len("content")), field)

0 commit comments

Comments
 (0)