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] Stop monitoring on config change #18284

Merged
merged 2 commits into from
May 7, 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 @@ -32,6 +32,7 @@
- Fix panic and flaky tests for the Agent. {pull}18135[18135]
- Fix default configuration after enroll {pull}18232[18232]
- Fix make sure the collected logs or metrics include streams information. {pull}18261[18261]
- Stop monitoring on config change {pull}18284[18284]

==== New features

Expand Down
15 changes: 8 additions & 7 deletions x-pack/elastic-agent/pkg/agent/operation/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (o *Operator) handleStartSidecar(s configrequest.Step) (result error) {

o.isMonitoring = true

for _, step := range o.getMonitoringSteps(s) {
for _, step := range o.getMonitoringSteps(s, o.monitor.WatchLogs(), o.monitor.WatchMetrics()) {
p, cfg, err := getProgramFromStepWithTags(step, o.config.DownloadConfig, monitoringTags())
if err != nil {
return errors.New(err,
Expand All @@ -57,7 +57,7 @@ func (o *Operator) handleStartSidecar(s configrequest.Step) (result error) {
}

func (o *Operator) handleStopSidecar(s configrequest.Step) (result error) {
for _, step := range o.getMonitoringSteps(s) {
for _, step := range o.getMonitoringSteps(s, true, true) {
p, _, err := getProgramFromStepWithTags(step, o.config.DownloadConfig, monitoringTags())
if err != nil {
return errors.New(err,
Expand All @@ -66,6 +66,7 @@ func (o *Operator) handleStopSidecar(s configrequest.Step) (result error) {
"operator.handleStopSidecar failed to create program")
}

o.logger.Debugf("stopping program %v", p)
if err := o.stop(p); err != nil {
result = multierror.Append(err, err)
}
Expand All @@ -86,7 +87,7 @@ func monitoringTags() map[app.Tag]string {
}
}

func (o *Operator) getMonitoringSteps(step configrequest.Step) []configrequest.Step {
func (o *Operator) getMonitoringSteps(step configrequest.Step, watchLogs, watchMetrics bool) []configrequest.Step {
// get output
config, err := getConfigFromStep(step)
if err != nil {
Expand All @@ -112,13 +113,13 @@ func (o *Operator) getMonitoringSteps(step configrequest.Step) []configrequest.S
return nil
}

return o.generateMonitoringSteps(step.Version, output)
return o.generateMonitoringSteps(step.Version, output, watchLogs, watchMetrics)
}

func (o *Operator) generateMonitoringSteps(version string, output interface{}) []configrequest.Step {
func (o *Operator) generateMonitoringSteps(version string, output interface{}, watchLogs, watchMetrics bool) []configrequest.Step {
var steps []configrequest.Step

if o.monitor.WatchLogs() {
if watchLogs {
fbConfig, any := o.getMonitoringFilebeatConfig(output)
stepID := configrequest.StepRun
if !any {
Expand All @@ -136,7 +137,7 @@ func (o *Operator) generateMonitoringSteps(version string, output interface{}) [
steps = append(steps, filebeatStep)
}

if o.monitor.WatchMetrics() {
if watchMetrics {
mbConfig, any := o.getMonitoringMetricbeatConfig(output)
stepID := configrequest.StepRun
if !any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestGenerateSteps(t *testing.T) {
t.Run(tc.Name, func(t *testing.T) {
m := &testMonitor{monitorLogs: tc.Config.MonitorLogs, monitorMetrics: tc.Config.MonitorMetrics}
operator, _ := getMonitorableTestOperator(t, "tests/scripts", m)
steps := operator.generateMonitoringSteps("8.0", sampleOutput)
steps := operator.generateMonitoringSteps("8.0", sampleOutput, tc.Config.MonitorLogs, tc.Config.MonitorMetrics)
if actualSteps := len(steps); actualSteps != tc.ExpectedSteps {
t.Fatalf("invalid number of steps, expected %v, got %v", tc.ExpectedSteps, actualSteps)
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/pkg/agent/operation/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (o *Operator) getApp(p Descriptor) (Application, error) {

id := p.ID()

o.logger.Debugf("operator is looking for %s in app collection: %v", p.ID(), o.apps)
if a, ok := o.apps[id]; ok {
return a, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (o *Operator) handleRun(step configrequest.Step) error {
}

func (o *Operator) handleRemove(step configrequest.Step) error {
o.logger.Debugf("stopping process %s: %s", step.Process, step.ID)
if step.Process == monitoringName {
return o.handleStopSidecar(step)
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/pkg/core/plugin/app/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (a *Application) Configure(ctx context.Context, config map[string]interface
return errors.New(ErrClientNotConfigurable, errors.TypeApplication)
}

a.logger.Debugf("configuring application %s: %s", a.Name(), string(rawYaml))
err = configClient.Config(ctx, string(rawYaml))

if netErr, ok := err.(net.Error); ok && (netErr.Timeout() || netErr.Temporary()) {
Expand Down