Skip to content

Commit

Permalink
not log start up info when on managed mode (#40390)
Browse files Browse the repository at this point in the history
When on managed mode the beats no longer log its startup information: the build info, the host info, the Go info and the process info.
The host info is the same collected by the Elastic Agent and all the others are known or can be found based on the Elastic Agent version.
  • Loading branch information
AndersonQ committed Aug 1, 2024
1 parent 5fccb0d commit 482e3e2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]

- Update Go version to 1.22.5. {pull}40082[40082]
- Fix FQDN being lowercased when used as `host.hostname` {issue}39993[39993]
- Beats won't log start up information when running under the Elastic Agent {40390}40390[40390]

*Auditbeat*

Expand Down
19 changes: 12 additions & 7 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,9 @@ func (b *Beat) createBeater(bt beat.Creator) (beat.Beater, error) {
return nil, err
}

logSystemInfo(b.Info)
logp.Info("Setup Beat: %s; Version: %s", b.Info.Beat, b.Info.Version)
log := logp.NewLogger("beat")
log.Infof("Setup Beat: %s; Version: %s", b.Info.Beat, b.Info.Version)
b.logSystemInfo(log)

err = b.registerESVersionCheckCallback()
if err != nil {
Expand Down Expand Up @@ -1360,15 +1361,19 @@ func handleError(err error) error {
// in debugging. This information includes data about the beat, build, go
// runtime, host, and process. If any of the data is not available it will be
// omitted.
func logSystemInfo(info beat.Info) {
func (b *Beat) logSystemInfo(log *logp.Logger) {
defer logp.Recover("An unexpected error occurred while collecting " +
"information about the system.")
log := logp.NewLogger("beat").With(logp.Namespace("system_info"))
log = log.With(logp.Namespace("system_info"))

if b.Manager.Enabled() {
return
}

// Beat
beat := mapstr.M{
"type": info.Beat,
"uuid": info.ID,
"type": b.Info.Beat,
"uuid": b.Info.ID,
"path": mapstr.M{
"config": paths.Resolve(paths.Config, ""),
"data": paths.Resolve(paths.Data, ""),
Expand All @@ -1382,7 +1387,7 @@ func logSystemInfo(info beat.Info) {
build := mapstr.M{
"commit": version.Commit(),
"time": version.BuildTime(),
"version": info.Version,
"version": b.Info.Version,
"libbeat": version.GetDefaultVersion(),
}
log.Infow("Build info", "build", build)
Expand Down
60 changes: 60 additions & 0 deletions libbeat/cmd/instance/beat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
package instance

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/beats/v7/libbeat/management/status"
"github.com/elastic/beats/v7/libbeat/outputs"
"github.com/elastic/beats/v7/libbeat/publisher/queue/memqueue"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/go-ucfg/yaml"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -433,3 +437,59 @@ output:
})
}
}

func TestLogSystemInfo(t *testing.T) {
tcs := []struct {
name string
managed bool
assertFn func(*testing.T, *bytes.Buffer)
}{
{
name: "managed mode", managed: true,
assertFn: func(t *testing.T, b *bytes.Buffer) {
assert.Empty(t, b, "logSystemInfo should not have produced any log")
},
},
{
name: "stand alone", managed: false,
assertFn: func(t *testing.T, b *bytes.Buffer) {
logs := b.String()
assert.Contains(t, logs, "Beat info")
assert.Contains(t, logs, "Build info")
assert.Contains(t, logs, "Go runtime info")
},
},
}
log, buff := logp.NewInMemory("beat", logp.ConsoleEncoderConfig())
log.WithOptions()

b, err := NewBeat("testingbeat", "test-idx", "42", false, nil)
require.NoError(t, err, "could not create beat")

for _, tc := range tcs {
buff.Reset()

b.Manager = mockManager{enabled: tc.managed}
b.logSystemInfo(log)

tc.assertFn(t, buff)
}
}

type mockManager struct {
enabled bool
}

func (m mockManager) AgentInfo() client.AgentInfo { return client.AgentInfo{} }
func (m mockManager) CheckRawConfig(cfg *config.C) error { return nil }
func (m mockManager) Enabled() bool { return m.enabled }
func (m mockManager) RegisterAction(action client.Action) {}
func (m mockManager) RegisterDiagnosticHook(name, description, filename, contentType string, hook client.DiagnosticHook) {
}
func (m mockManager) SetPayload(payload map[string]interface{}) {}
func (m mockManager) SetStopCallback(f func()) {}
func (m mockManager) Start() error { return nil }
func (m mockManager) Status() status.Status { return status.Status(-42) }
func (m mockManager) Stop() {}
func (m mockManager) UnregisterAction(action client.Action) {}
func (m mockManager) UpdateStatus(status status.Status, msg string) {}

0 comments on commit 482e3e2

Please sign in to comment.