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

[8.15](backport #41077) [Heartbeat] Add managed status reporter at monitor factory level #41126

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
18 changes: 18 additions & 0 deletions heartbeat/monitors/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -71,6 +72,12 @@ type Monitor struct {
stats plugin.RegistryRecorder

monitorStateTracker *monitorstate.Tracker
statusReporter status.StatusReporter
}

// SetStatusReporter
func (m *Monitor) SetStatusReporter(statusReporter status.StatusReporter) {
m.statusReporter = statusReporter
}

// String prints a description of the monitor in a threadsafe way. It is important that this use threadsafe
Expand Down Expand Up @@ -175,6 +182,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
}}

Expand Down Expand Up @@ -237,6 +247,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
Expand All @@ -262,4 +273,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)
}
}
61 changes: 61 additions & 0 deletions heartbeat/monitors/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
package monitors

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/config"

Check failure on line 28 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

ST1019: package "github.com/elastic/elastic-agent-libs/config" is being imported more than once (stylecheck)

Check failure on line 28 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

ST1019: package "github.com/elastic/elastic-agent-libs/config" is being imported more than once (stylecheck)
conf "github.com/elastic/elastic-agent-libs/config"

Check failure on line 29 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

ST1019(related information): other import of "github.com/elastic/elastic-agent-libs/config" (stylecheck)

Check failure on line 29 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

ST1019(related information): other import of "github.com/elastic/elastic-agent-libs/config" (stylecheck)
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/monitoring"
"github.com/elastic/go-lookslike"
Expand All @@ -32,7 +34,9 @@
"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
Expand Down Expand Up @@ -131,3 +135,60 @@

require.Error(t, checkMonitorConfig(serverMonConf, reg))
}

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",
}
conf, err := config.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, config *config.C) (plugin.Plugin, error) {
return plugin.Plugin{}, fmt.Errorf("error plugin")
},
Stats: plugin.NewPluginCountersRecorder("fail", monReg),
}
reg.Add(mockDegradedPluginFactory)

Check failure on line 170 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `reg.Add` is not checked (errcheck)

Check failure on line 170 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Error return value of `reg.Add` is not checked (errcheck)

sched := scheduler.Create(1, monitoring.NewRegistry(), time.Local, nil, true)
defer sched.Stop()

c, err := pipel.Connect()
require.NoError(t, err)
m, err := newMonitor(conf, reg, c, sched.Add, nil, nil)
require.NoError(t, err)

// Track status marked as failed during run_once execution
var failed bool = false

Check failure on line 181 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

ST1023: should omit type bool from declaration; it will be inferred from the right-hand side (stylecheck)

Check failure on line 181 in heartbeat/monitors/monitor_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

ST1023: should omit type bool from declaration; it will be inferred from the right-hand side (stylecheck)
m.SetStatusReporter(&MockStatusReporter{
us: func(s status.Status, msg string) {
if s == status.Failed {
failed = true
}
},
})
m.Start()

sched.WaitForRunOnce()

require.True(t, failed)
}
Loading