diff --git a/internal/beatcmd/beat.go b/internal/beatcmd/beat.go index af127cf2794..8fa9b651501 100644 --- a/internal/beatcmd/beat.go +++ b/internal/beatcmd/beat.go @@ -97,6 +97,9 @@ type BeatParams struct { // ElasticLicensed indicates whether this build of APM Server // is licensed with the Elastic License v2. ElasticLicensed bool + + // Logger holds the logger used by the runner + Logger *logp.Logger } // NewBeat creates a new Beat. @@ -144,6 +147,7 @@ func NewBeat(args BeatParams) (*Beat, error) { Hostname: hostname, StartTime: time.Now(), EphemeralID: ephemeralID, + Logger: args.Logger, }, Keystore: keystore, Config: &beat.BeatConfig{Output: cfg.Output}, @@ -170,7 +174,9 @@ func (b *Beat) init() error { if err := configureLogging(b.Config); err != nil { return fmt.Errorf("failed to configure logging: %w", err) } - b.Beat.Info.Logger = logp.NewLogger("") + if b.Info.Logger == nil { + b.Info.Logger = logp.NewLogger("") + } // log paths values to help with troubleshooting b.Info.Logger.Infof("%s", paths.Paths.String()) diff --git a/internal/beatcmd/beat_test.go b/internal/beatcmd/beat_test.go index cb9b049a9c9..0267bf7afc8 100644 --- a/internal/beatcmd/beat_test.go +++ b/internal/beatcmd/beat_test.go @@ -663,6 +663,7 @@ func newBeat(t testing.TB, configYAML string, newRunner NewRunnerFunc) *Beat { beat, err := NewBeat(BeatParams{ NewRunner: newRunner, ElasticLicensed: true, + Logger: logptest.NewTestingLogger(t, ""), }) require.NoError(t, err) return beat diff --git a/internal/beatcmd/config_test.go b/internal/beatcmd/config_test.go index 8d8c96a8f76..fa31752c11f 100644 --- a/internal/beatcmd/config_test.go +++ b/internal/beatcmd/config_test.go @@ -92,8 +92,13 @@ processors: } func initCfgfile(t testing.TB, content string) (home string) { - home = t.TempDir() - content += "\npath.home: " + home + _, after, _ := strings.Cut(content, "\npath.home: ") + home, _, _ = strings.Cut(after, "\n") + + if home == "" { + home = t.TempDir() + content += "\npath.home: " + home + } origConfigPath := cfgfile.GetPathConfig() origConfigFile := strings.TrimSuffix(cfgfile.GetDefaultCfgfile(), ".yml") diff --git a/internal/beatcmd/locker_test.go b/internal/beatcmd/locker_test.go index e1bb02b0bf7..dabefe3a5a1 100644 --- a/internal/beatcmd/locker_test.go +++ b/internal/beatcmd/locker_test.go @@ -29,8 +29,12 @@ import ( ) func TestLocker(t *testing.T) { + configYAML := "output.console.enabled: true" + home := t.TempDir() + configYAML += "\npath.home: " + home + running := make(chan struct{}) - beat1 := newBeat(t, `output.console.enabled: true`, func(RunnerParams) (Runner, error) { + beat1 := newBeat(t, configYAML, func(RunnerParams) (Runner, error) { return runnerFunc(func(ctx context.Context) error { close(running) <-ctx.Done() @@ -46,13 +50,11 @@ func TestLocker(t *testing.T) { // Create another Beat using the same configuration and data directory; // its Run method should fail to acquire the lock while beat1 is running. - beat2, err := NewBeat(BeatParams{ - NewRunner: func(RunnerParams) (Runner, error) { - panic("should not be called") - }, + beat2 := newBeat(t, configYAML, func(RunnerParams) (Runner, error) { + t.Fatal("should not be called") + return nil, nil }) - require.NoError(t, err) - err = beat2.Run(context.Background()) + err := beat2.Run(context.Background()) require.ErrorIs(t, err, ErrAlreadyLocked) assert.NoError(t, stopBeat1()) diff --git a/internal/beatcmd/logging_test.go b/internal/beatcmd/logging_test.go index 8cf035a4b47..153637165ea 100644 --- a/internal/beatcmd/logging_test.go +++ b/internal/beatcmd/logging_test.go @@ -27,18 +27,6 @@ import ( "github.com/elastic/elastic-agent-libs/logp" ) -func init() { - // Configure tests to log at debug level, and to send - // logs to logp.ObserverLogs(). It is important to not - // log to files by default in tests, as logp will keep - // the files open and prevent temporary directories from - // being removed on Windows. - logOptions = append(logOptions, - logp.ToObserverOutput(), - logp.WithLevel(logp.DebugLevel), - ) -} - var environments = []logp.Environment{ logp.DefaultEnvironment, logp.SystemdEnvironment,