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

Cherry-pick #18723 to 7.x: Avoid watching monitor logs #18761

Merged
merged 2 commits into from
May 27, 2020
Merged
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
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Avoid Chown on windows {pull}18512[18512]
- Remove fleet admin from setup script {pull}18611[18611]
- Clean action store after enrolling to new configuration {pull}18656[18656]
- Avoid watching monitor logs {pull}18723[18723]

==== New features

Expand Down
11 changes: 7 additions & 4 deletions x-pack/elastic-agent/pkg/agent/operation/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/stateresolver"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/app"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/app/monitoring"
monitoringConfig "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/app/monitoring/config"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/process"
Expand Down Expand Up @@ -137,9 +138,11 @@ type testMonitorableApp struct {
monitor monitoring.Monitor
}

func (*testMonitorableApp) Name() string { return "" }
func (*testMonitorableApp) Start(_ context.Context, cfg map[string]interface{}) error { return nil }
func (*testMonitorableApp) Stop() {}
func (*testMonitorableApp) Name() string { return "" }
func (*testMonitorableApp) Start(_ context.Context, _ app.Taggable, cfg map[string]interface{}) error {
return nil
}
func (*testMonitorableApp) Stop() {}
func (*testMonitorableApp) Configure(_ context.Context, config map[string]interface{}) error {
return nil
}
Expand All @@ -153,7 +156,7 @@ type testMonitor struct {

// EnrichArgs enriches arguments provided to application, in order to enable
// monitoring
func (b *testMonitor) EnrichArgs(_ string, _ string, args []string) []string { return args }
func (b *testMonitor) EnrichArgs(_ string, _ string, args []string, _ bool) []string { return args }

// Cleanup cleans up all drops.
func (b *testMonitor) Cleanup(string, string) error { return nil }
Expand Down
4 changes: 3 additions & 1 deletion x-pack/elastic-agent/pkg/agent/operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package operation
import (
"context"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/app"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/app/monitoring"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/state"
)
Expand All @@ -30,7 +31,7 @@ type operation interface {
// Application is an application capable of being started, stopped and configured.
type Application interface {
Name() string
Start(ctx context.Context, cfg map[string]interface{}) error
Start(ctx context.Context, p app.Taggable, cfg map[string]interface{}) error
Stop()
Configure(ctx context.Context, config map[string]interface{}) error
State() state.State
Expand All @@ -45,4 +46,5 @@ type Descriptor interface {
ID() string
Directory() string
IsGrpcConfigurable() bool
Tags() map[app.Tag]string
}
7 changes: 4 additions & 3 deletions x-pack/elastic-agent/pkg/agent/operation/operation_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/operation/config"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/app"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/process"
)

// operationStart start installed process
// skips if process is already running
type operationStart struct {
program app.Descriptor
logger *logger.Logger
program Descriptor
operatorConfig *config.Config
cfg map[string]interface{}
eventProcessor callbackHooks
Expand All @@ -28,13 +27,15 @@ type operationStart struct {

func newOperationStart(
logger *logger.Logger,
program Descriptor,
operatorConfig *config.Config,
cfg map[string]interface{},
eventProcessor callbackHooks) *operationStart {
// TODO: make configurable

return &operationStart{
logger: logger,
program: program,
operatorConfig: operatorConfig,
cfg: cfg,
eventProcessor: eventProcessor,
Expand Down Expand Up @@ -72,5 +73,5 @@ func (o *operationStart) Run(ctx context.Context, application Application) (err
}
}()

return application.Start(ctx, o.cfg)
return application.Start(ctx, o.program, o.cfg)
}
17 changes: 4 additions & 13 deletions x-pack/elastic-agent/pkg/agent/operation/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (o *Operator) start(p Descriptor, cfg map[string]interface{}) (err error) {
newOperationFetch(o.logger, p, o.config, o.downloader, o.eventProcessor),
newOperationVerify(o.eventProcessor),
newOperationInstall(o.logger, p, o.config, o.installer, o.eventProcessor),
newOperationStart(o.logger, o.config, cfg, o.eventProcessor),
newOperationStart(o.logger, p, o.config, cfg, o.eventProcessor),
newOperationConfig(o.logger, o.config, cfg, o.eventProcessor),
}
return o.runFlow(p, flow)
Expand Down Expand Up @@ -180,7 +180,7 @@ func (o *Operator) pushConfig(p Descriptor, cfg map[string]interface{}) error {
flow = []operation{
// updates a configuration file and restarts a process
newOperationStop(o.logger, o.config, o.eventProcessor),
newOperationStart(o.logger, o.config, cfg, o.eventProcessor),
newOperationStart(o.logger, p, o.config, cfg, o.eventProcessor),
}
}

Expand Down Expand Up @@ -263,15 +263,6 @@ func (o *Operator) getApp(p Descriptor) (Application, error) {
}

func isMonitorable(descriptor Descriptor) bool {
type taggable interface {
Tags() map[app.Tag]string
}

if taggable, ok := descriptor.(taggable); ok {
tags := taggable.Tags()
_, isSidecar := tags[app.TagSidecar]
return !isSidecar // everything is monitorable except sidecar
}

return false
isSidecar := app.IsSidecar(descriptor)
return !isSidecar // everything is monitorable except sidecar
}
4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/core/plugin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (a *Application) State() state.State {
return a.state
}

func (a *Application) watch(ctx context.Context, proc *os.Process, cfg map[string]interface{}) {
func (a *Application) watch(ctx context.Context, p Taggable, proc *os.Process, cfg map[string]interface{}) {
go func() {
var procState *os.ProcessState

Expand All @@ -179,7 +179,7 @@ func (a *Application) watch(ctx context.Context, proc *os.Process, cfg map[strin
// it was a crash, report it async not to block
// process management with networking issues
go a.reportCrash(ctx)
a.Start(ctx, cfg)
a.Start(ctx, p, cfg)
}
}()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,30 @@ func (b *Monitor) generateLoggingPath(process, pipelineID string) string {

// EnrichArgs enriches arguments provided to application, in order to enable
// monitoring
func (b *Monitor) EnrichArgs(process, pipelineID string, args []string) []string {
func (b *Monitor) EnrichArgs(process, pipelineID string, args []string, isSidecar bool) []string {
appendix := make([]string, 0, 7)

monitoringEndpoint := b.generateMonitoringEndpoint(process, pipelineID)
if monitoringEndpoint != "" {
endpoint := monitoringEndpoint
if isSidecar {
endpoint += "_monitor"
}
appendix = append(appendix,
"-E", "http.enabled=true",
"-E", "http.host="+monitoringEndpoint,
"-E", "http.host="+endpoint,
)
}

loggingPath := b.generateLoggingPath(process, pipelineID)
if loggingPath != "" {
logFile := process
if isSidecar {
logFile += "_monitor"
}
appendix = append(appendix,
"-E", "logging.files.path="+loggingPath,
"-E", "logging.files.name="+process,
"-E", "logging.files.name="+logFile,
"-E", "logging.files.keepfiles=7",
"-E", "logging.files.permission=0644",
"-E", "logging.files.interval=1h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Monitor interface {
MetricsPathPrefixed(process, pipelineID string) string

Prepare(process, pipelineID string, uid, gid int) error
EnrichArgs(string, string, []string) []string
EnrichArgs(string, string, []string, bool) []string
Cleanup(process, pipelineID string) error
Reload(cfg *config.Config) error
IsMonitoringEnabled() bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewMonitor() *Monitor {

// EnrichArgs enriches arguments provided to application, in order to enable
// monitoring
func (b *Monitor) EnrichArgs(_ string, _ string, args []string) []string {
func (b *Monitor) EnrichArgs(_ string, _ string, args []string, _ bool) []string {
return args
}

Expand Down
9 changes: 6 additions & 3 deletions x-pack/elastic-agent/pkg/core/plugin/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type stateClient interface {
}

// Start starts the application with a specified config.
func (a *Application) Start(ctx context.Context, cfg map[string]interface{}) (err error) {
func (a *Application) Start(ctx context.Context, t Taggable, cfg map[string]interface{}) (err error) {
defer func() {
if err != nil {
// inject App metadata
Expand Down Expand Up @@ -83,7 +83,10 @@ func (a *Application) Start(ctx context.Context, cfg map[string]interface{}) (er
}

spec.Args = injectLogLevel(a.logLevel, spec.Args)
spec.Args = a.monitor.EnrichArgs(a.name, a.pipelineID, spec.Args)

// use separate file
isSidecar := IsSidecar(t)
spec.Args = a.monitor.EnrichArgs(a.name, a.pipelineID, spec.Args, isSidecar)

// specify beat name to avoid data lock conflicts
// as for https://github.com/elastic/beats/v7/pull/14030 more than one instance
Expand Down Expand Up @@ -111,7 +114,7 @@ func (a *Application) Start(ctx context.Context, cfg map[string]interface{}) (er
a.state.Status = state.Running

// setup watcher
a.watch(ctx, a.state.ProcessInfo.Process, cfg)
a.watch(ctx, t, a.state.ProcessInfo.Process, cfg)

return nil
}
Expand Down
12 changes: 12 additions & 0 deletions x-pack/elastic-agent/pkg/core/plugin/app/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ type Tag string

// TagSidecar tags a sidecar process
const TagSidecar = "sidecar"

// Taggable is an object containing tags.
type Taggable interface {
Tags() map[Tag]string
}

// IsSidecar returns true if tags contains sidecar flag.
func IsSidecar(descriptor Taggable) bool {
tags := descriptor.Tags()
_, isSidecar := tags[TagSidecar]
return isSidecar
}