Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Elastic Agent] Send Agent logs to elasticsearch #19811

Merged
merged 12 commits into from
Jul 14, 2020
6 changes: 4 additions & 2 deletions libbeat/logp/configure/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"strings"

"go.uber.org/zap/zapcore"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
)
Expand All @@ -45,7 +47,7 @@ func init() {

// Logging builds a logp.Config based on the given common.Config and the specified
// CLI flags.
func Logging(beatName string, cfg *common.Config) error {
func Logging(beatName string, cfg *common.Config, wrapSink ...func(zapcore.Core) zapcore.Core) error {
config := logp.DefaultConfig(environment)
config.Beat = beatName
if cfg != nil {
Expand All @@ -55,7 +57,7 @@ func Logging(beatName string, cfg *common.Config) error {
}

applyFlags(&config)
return logp.Configure(config)
return logp.Configure(config, wrapSink...)
}

func applyFlags(cfg *logp.Config) {
Expand Down
17 changes: 10 additions & 7 deletions libbeat/logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type coreLogger struct {
}

// Configure configures the logp package.
func Configure(cfg Config) error {
func Configure(cfg Config, wrapSink ...func(zapcore.Core) zapcore.Core) error {
var (
sink zapcore.Core
observedLogs *observer.ObservedLogs
Expand All @@ -70,7 +70,7 @@ func Configure(cfg Config) error {

// Build a single output (stderr has priority if more than one are enabled).
if cfg.toObserver {
sink, observedLogs = observer.New(cfg.Level.zapLevel())
sink, observedLogs = observer.New(cfg.Level.ZapLevel())
} else {
sink, err = createLogOutput(cfg)
}
Expand Down Expand Up @@ -105,6 +105,9 @@ func Configure(cfg Config) error {
sink = selectiveWrapper(sink, selectors)
}

for _, wrapper := range wrapSink {
sink = wrapper(sink)
}
root := zap.New(sink, makeOptions(cfg)...)
storeLogger(&coreLogger{
selectors: selectors,
Expand Down Expand Up @@ -191,24 +194,24 @@ func makeOptions(cfg Config) []zap.Option {

func makeStderrOutput(cfg Config) (zapcore.Core, error) {
stderr := zapcore.Lock(os.Stderr)
return newCore(cfg, buildEncoder(cfg), stderr, cfg.Level.zapLevel()), nil
return newCore(cfg, buildEncoder(cfg), stderr, cfg.Level.ZapLevel()), nil
}

func makeDiscardOutput(cfg Config) (zapcore.Core, error) {
discard := zapcore.AddSync(ioutil.Discard)
return newCore(cfg, buildEncoder(cfg), discard, cfg.Level.zapLevel()), nil
return newCore(cfg, buildEncoder(cfg), discard, cfg.Level.ZapLevel()), nil
}

func makeSyslogOutput(cfg Config) (zapcore.Core, error) {
core, err := newSyslog(buildEncoder(cfg), cfg.Level.zapLevel())
core, err := newSyslog(buildEncoder(cfg), cfg.Level.ZapLevel())
if err != nil {
return nil, err
}
return wrappedCore(cfg, core), nil
}

func makeEventLogOutput(cfg Config) (zapcore.Core, error) {
core, err := newEventLog(cfg.Beat, buildEncoder(cfg), cfg.Level.zapLevel())
core, err := newEventLog(cfg.Beat, buildEncoder(cfg), cfg.Level.ZapLevel())
if err != nil {
return nil, err
}
Expand All @@ -234,7 +237,7 @@ func makeFileOutput(cfg Config) (zapcore.Core, error) {
return nil, errors.Wrap(err, "failed to create file rotator")
}

return newCore(cfg, buildEncoder(cfg), rotator, cfg.Level.zapLevel()), nil
return newCore(cfg, buildEncoder(cfg), rotator, cfg.Level.ZapLevel()), nil
}

func newCore(cfg Config, enc zapcore.Encoder, ws zapcore.WriteSyncer, enab zapcore.LevelEnabler) zapcore.Core {
Expand Down
14 changes: 7 additions & 7 deletions libbeat/logp/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func buildEncoder(cfg Config) zapcore.Encoder {
var encCfg zapcore.EncoderConfig
var encCreator encoderCreator
if cfg.JSON {
encCfg = jsonEncoderConfig()
encCfg = JSONEncoderConfig()
encCreator = zapcore.NewJSONEncoder
} else if cfg.ToSyslog {
encCfg = syslogEncoderConfig()
encCfg = SyslogEncoderConfig()
encCreator = zapcore.NewConsoleEncoder
} else {
encCfg = consoleEncoderConfig()
encCfg = ConsoleEncoderConfig()
encCreator = zapcore.NewConsoleEncoder
}

Expand All @@ -60,19 +60,19 @@ func buildEncoder(cfg Config) zapcore.Encoder {
return encCreator(encCfg)
}

func jsonEncoderConfig() zapcore.EncoderConfig {
func JSONEncoderConfig() zapcore.EncoderConfig {
return baseEncodingConfig
}

func consoleEncoderConfig() zapcore.EncoderConfig {
func ConsoleEncoderConfig() zapcore.EncoderConfig {
c := baseEncodingConfig
c.EncodeLevel = zapcore.CapitalLevelEncoder
c.EncodeName = bracketedNameEncoder
return c
}

func syslogEncoderConfig() zapcore.EncoderConfig {
c := consoleEncoderConfig()
func SyslogEncoderConfig() zapcore.EncoderConfig {
c := ConsoleEncoderConfig()
// Time is generally added by syslog.
// But when logging with ECS the empty TimeKey will be
// ignored and @timestamp is still added to log line
Expand Down
2 changes: 1 addition & 1 deletion libbeat/logp/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (l Level) MarshalJSON() ([]byte, error) {
return nil, errors.Errorf("invalid level '%d'", l)
}

func (l Level) zapLevel() zapcore.Level {
func (l Level) ZapLevel() zapcore.Level {
z, found := zapLevels[l]
if found {
return z
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@
- Agent now installs/uninstalls Elastic Endpoint {pull}19248[19248]
- Agent now downloads Elastic Endpoint {pull}19503[19503]
- Agent now load balances across multiple Kibana instances {pull}19628[19628]
- Agent now sends its own logs to elasticsearch {pull}19811[19811]
10 changes: 8 additions & 2 deletions x-pack/elastic-agent/pkg/agent/application/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var (
homePath string
dataPath string
logsPath string
)

func init() {
Expand All @@ -21,6 +22,7 @@ func init() {
fs := flag.CommandLine
fs.StringVar(&homePath, "path.home", exePath, "Agent root path")
fs.StringVar(&dataPath, "path.data", filepath.Join(exePath, "data"), "Data path contains Agent managed binaries")
fs.StringVar(&logsPath, "path.logs", exePath, "Logs path contains Agent log output")
}

// Home returns a directory where binary lives
Expand All @@ -29,13 +31,17 @@ func Home() string {
return homePath
}

// Data returns a home directory of current user
// Data returns the data directory for Agent
func Data() string {
return dataPath
}

func retrieveExecutablePath() string {
// Logs returns a the log directory for Agent
func Logs() string {
return logsPath
}

func retrieveExecutablePath() string {
execPath, err := os.Executable()
if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/pkg/agent/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {

cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.home"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.data"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.logs"))

cmd.PersistentFlags().StringVarP(&flags.PathConfigFile, "", "c", defaultConfig, fmt.Sprintf(`Configuration file, relative to path.config (default "%s")`, defaultConfig))
cmd.PersistentFlags().StringVarP(&flags.PathConfig, "path.config", "", "${path.home}", "Configuration path")
Expand Down
Loading