Skip to content

Commit

Permalink
[Elastic-Agent] Stop monitoring on config change (#18284) (#18337)
Browse files Browse the repository at this point in the history
[Elastic-Agent] Stop monitoring on config change (#18284) (#18337)
  • Loading branch information
michalpristas authored May 7, 2020
1 parent f9d2f31 commit 04f060a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
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 make sure the collected logs or metrics include streams information. {pull}18261[18261]
- Fix version to 7.8 {pull}18286[18286]
- Fix an issue where the checkin_frequency, jitter, and backoff options where not configurable. {pull}17843[17843]
- 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

0 comments on commit 04f060a

Please sign in to comment.