Skip to content
Open
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
8 changes: 7 additions & 1 deletion internal/beatcmd/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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},
Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions internal/beatcmd/beat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions internal/beatcmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 9 additions & 7 deletions internal/beatcmd/locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
Expand Down
12 changes: 0 additions & 12 deletions internal/beatcmd/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down