From 51c23b4c58a82e17a01c0c8d61b0992fd4599d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Alvarez=20Pi=C3=B1eiro?= <95703246+emilioalvap@users.noreply.github.com> Date: Fri, 4 Oct 2024 21:08:41 +0200 Subject: [PATCH 1/5] [Heartbeat] Fix linting issues introduced by auto-merge #41077 (#41128) (cherry picked from commit efb563c8904f792ae7ca54036e0f442c4fcbab61) # Conflicts: # heartbeat/monitors/monitor.go # heartbeat/monitors/monitor_test.go --- heartbeat/monitors/monitor.go | 8 ++++ heartbeat/monitors/monitor_test.go | 60 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/heartbeat/monitors/monitor.go b/heartbeat/monitors/monitor.go index 29e7713145c..678dac37675 100644 --- a/heartbeat/monitors/monitor.go +++ b/heartbeat/monitors/monitor.go @@ -71,6 +71,14 @@ type Monitor struct { stats plugin.RegistryRecorder monitorStateTracker *monitorstate.Tracker +<<<<<<< HEAD +======= + statusReporter status.StatusReporter +} + +func (m *Monitor) SetStatusReporter(statusReporter status.StatusReporter) { + m.statusReporter = statusReporter +>>>>>>> efb563c890 ([Heartbeat] Fix linting issues introduced by auto-merge #41077 (#41128)) } // String prints a description of the monitor in a threadsafe way. It is important that this use threadsafe diff --git a/heartbeat/monitors/monitor_test.go b/heartbeat/monitors/monitor_test.go index 3176d27a2fa..6e0afd18f24 100644 --- a/heartbeat/monitors/monitor_test.go +++ b/heartbeat/monitors/monitor_test.go @@ -131,3 +131,63 @@ func TestCheckInvalidConfig(t *testing.T) { require.Error(t, checkMonitorConfig(serverMonConf, reg)) } +<<<<<<< HEAD +======= + +type MockStatusReporter struct { + us func(status status.Status, msg string) +} + +func (sr *MockStatusReporter) UpdateStatus(status status.Status, msg string) { + sr.us(status, msg) +} + +func TestStatusReporter(t *testing.T) { + confMap := map[string]interface{}{ + "type": "fail", + "urls": []string{"http://example.net"}, + "schedule": "@every 1ms", + "name": "myName", + "id": "myId", + } + cfg, err := conf.NewConfigFrom(confMap) + require.NoError(t, err) + + reg, _, _ := mockPluginsReg() + pipel := &MockPipeline{} + monReg := monitoring.NewRegistry() + + mockDegradedPluginFactory := plugin.PluginFactory{ + Name: "fail", + Aliases: []string{"failAlias"}, + Make: func(s string, cfg *conf.C) (plugin.Plugin, error) { + return plugin.Plugin{}, fmt.Errorf("error plugin") + }, + Stats: plugin.NewPluginCountersRecorder("fail", monReg), + } + _ = reg.Add(mockDegradedPluginFactory) + + sched := scheduler.Create(1, monitoring.NewRegistry(), time.Local, nil, true) + defer sched.Stop() + + c, err := pipel.Connect() + require.NoError(t, err) + m, err := newMonitor(cfg, reg, c, sched.Add, nil, nil) + require.NoError(t, err) + + // Track status marked as failed during run_once execution + failed := false + m.SetStatusReporter(&MockStatusReporter{ + us: func(s status.Status, msg string) { + if s == status.Failed { + failed = true + } + }, + }) + m.Start() + + sched.WaitForRunOnce() + + require.True(t, failed) +} +>>>>>>> efb563c890 ([Heartbeat] Fix linting issues introduced by auto-merge #41077 (#41128)) From 0c3f92bf16be079398008eac21402dfb65bb23f8 Mon Sep 17 00:00:00 2001 From: emilioalvap Date: Fri, 4 Oct 2024 21:26:39 +0200 Subject: [PATCH 2/5] Manual merge --- heartbeat/monitors/monitor.go | 4 +--- heartbeat/monitors/monitor_test.go | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/heartbeat/monitors/monitor.go b/heartbeat/monitors/monitor.go index 678dac37675..c30da5a73b3 100644 --- a/heartbeat/monitors/monitor.go +++ b/heartbeat/monitors/monitor.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/heartbeat/monitors/wrappers" "github.com/elastic/beats/v7/heartbeat/scheduler" "github.com/elastic/beats/v7/libbeat/beat" + "github.com/elastic/beats/v7/libbeat/management/status" ) // ErrMonitorDisabled is returned when the monitor plugin is marked as disabled. @@ -71,14 +72,11 @@ type Monitor struct { stats plugin.RegistryRecorder monitorStateTracker *monitorstate.Tracker -<<<<<<< HEAD -======= statusReporter status.StatusReporter } func (m *Monitor) SetStatusReporter(statusReporter status.StatusReporter) { m.statusReporter = statusReporter ->>>>>>> efb563c890 ([Heartbeat] Fix linting issues introduced by auto-merge #41077 (#41128)) } // String prints a description of the monitor in a threadsafe way. It is important that this use threadsafe diff --git a/heartbeat/monitors/monitor_test.go b/heartbeat/monitors/monitor_test.go index 6e0afd18f24..0890a1697be 100644 --- a/heartbeat/monitors/monitor_test.go +++ b/heartbeat/monitors/monitor_test.go @@ -18,6 +18,7 @@ package monitors import ( + "fmt" "testing" "time" @@ -32,7 +33,9 @@ import ( "github.com/elastic/go-lookslike/testslike" "github.com/elastic/go-lookslike/validator" + "github.com/elastic/beats/v7/heartbeat/monitors/plugin" "github.com/elastic/beats/v7/heartbeat/scheduler" + "github.com/elastic/beats/v7/libbeat/management/status" ) // TestMonitorBasic tests a basic config @@ -131,8 +134,6 @@ func TestCheckInvalidConfig(t *testing.T) { require.Error(t, checkMonitorConfig(serverMonConf, reg)) } -<<<<<<< HEAD -======= type MockStatusReporter struct { us func(status status.Status, msg string) @@ -190,4 +191,3 @@ func TestStatusReporter(t *testing.T) { require.True(t, failed) } ->>>>>>> efb563c890 ([Heartbeat] Fix linting issues introduced by auto-merge #41077 (#41128)) From 3804e14d9a733171dac174ea61d301e7e2546587 Mon Sep 17 00:00:00 2001 From: emilioalvap Date: Wed, 2 Oct 2024 16:21:54 +0200 Subject: [PATCH 3/5] [Heartbeat] Add status reporter at monitor factory level --- heartbeat/monitors/monitor.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/heartbeat/monitors/monitor.go b/heartbeat/monitors/monitor.go index c30da5a73b3..8fa82c10ea1 100644 --- a/heartbeat/monitors/monitor.go +++ b/heartbeat/monitors/monitor.go @@ -181,6 +181,9 @@ func newMonitorUnsafe( logp.L().Error(fullErr) p.Jobs = []jobs.Job{func(event *beat.Event) ([]jobs.Job, error) { + // if statusReporter is set, as it is for running managed-mode, update the input status + // to failed, specifying the error + m.updateStatus(status.Failed, fmt.Sprintf("monitor could not be started: %s, err: %s", m.stdFields.ID, fullErr)) return nil, fullErr }} @@ -243,6 +246,7 @@ func (m *Monitor) Start() { m.stats.StartMonitor(int64(m.endpoints)) m.state = MON_STARTED + m.updateStatus(status.Running, "") } // Stop stops the monitor without freeing it in global dedup @@ -268,4 +272,11 @@ func (m *Monitor) Stop() { m.stats.StopMonitor(int64(m.endpoints)) m.state = MON_STOPPED + m.updateStatus(status.Stopped, "") +} + +func (m *Monitor) updateStatus(status status.Status, msg string) { + if m.statusReporter != nil { + m.statusReporter.UpdateStatus(status, msg) + } } From 92ba032a12b85bb97e55dac0dfa91861f4678a3f Mon Sep 17 00:00:00 2001 From: emilioalvap Date: Fri, 4 Oct 2024 17:21:06 +0200 Subject: [PATCH 4/5] Add unit test and changelog --- CHANGELOG.next.asciidoc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 05a1e91cdbc..09cf1937f84 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -190,6 +190,9 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff] *Heartbeat* - Added status to monitor run log report. +- Upgrade node to latest LTS v18.20.3. {pull}40038[40038] +- Add journey duration to synthetics browser events. {pull}40230[40230] +- Add monitor status reporter under managed mode. {pull}41077[41077] *Metricbeat* From 039a541b46dc8da5ec3bf1109ab028be92e3a893 Mon Sep 17 00:00:00 2001 From: emilioalvap Date: Thu, 10 Oct 2024 13:04:56 +0200 Subject: [PATCH 5/5] Fix changelog --- CHANGELOG.next.asciidoc | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 09cf1937f84..c12c6933b0d 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -190,8 +190,6 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff] *Heartbeat* - Added status to monitor run log report. -- Upgrade node to latest LTS v18.20.3. {pull}40038[40038] -- Add journey duration to synthetics browser events. {pull}40230[40230] - Add monitor status reporter under managed mode. {pull}41077[41077] *Metricbeat*