Skip to content

Commit

Permalink
Fix monitor duration calc v2
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioalvap committed Oct 18, 2023
1 parent cc72e06 commit 19cf7ff
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 13 deletions.
4 changes: 4 additions & 0 deletions heartbeat/monitors/wrappers/summarizer/plugdrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ func (d DropBrowserExtraEvents) BeforeSummary(event *beat.Event) BeforeSummaryAc
func (d DropBrowserExtraEvents) BeforeRetry() {
// noop
}

func (d DropBrowserExtraEvents) BeforeEachEvent(event *beat.Event) {
// noop
}
6 changes: 6 additions & 0 deletions heartbeat/monitors/wrappers/summarizer/plugerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func NewBrowserErrPlugin() *BrowserErrPlugin {
}
}

func (esp *BrowserErrPlugin) BeforeEachEvent(event *beat.Event) {} // noop

func (esp *BrowserErrPlugin) EachEvent(event *beat.Event, eventErr error) EachEventActions {
// track these to determine if the journey
// needs an error injected due to incompleteness
Expand Down Expand Up @@ -127,6 +129,10 @@ func (esp *LightweightErrPlugin) BeforeRetry() {
// noop
}

func (esp *LightweightErrPlugin) BeforeEachEvent(event *beat.Event) {
// noop
}

// errToFieldVal reflects on the error and returns either an *ecserr.ECSErr if possible, and a look.Reason otherwise
func errToFieldVal(eventErr error) (errVal interface{}) {
var asECS *ecserr.ECSErr
Expand Down
20 changes: 10 additions & 10 deletions heartbeat/monitors/wrappers/summarizer/plugmondur.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ type LightweightDurationPlugin struct {
}

func (lwdsp *LightweightDurationPlugin) EachEvent(event *beat.Event, _ error) EachEventActions {
// Effectively only runs once, on the first event
return 0 // noop
}

func (lwdsp *LightweightDurationPlugin) BeforeEachEvent(event *beat.Event) {
// Effectively capture on the first event, on the first event
if lwdsp.startedAt == nil {
lwdsp.setEventStartAt()
now := time.Now()
lwdsp.startedAt = &now
}
return 0
}

func (lwdsp *LightweightDurationPlugin) BeforeSummary(event *beat.Event) BeforeSummaryActions {
Expand All @@ -44,15 +48,10 @@ func (lwdsp *LightweightDurationPlugin) BeforeSummary(event *beat.Event) BeforeS
}

func (lwdsp *LightweightDurationPlugin) BeforeRetry() {
// Reset event.startAt
// Reset event start time
lwdsp.startedAt = nil
}

func (lwdsp *LightweightDurationPlugin) setEventStartAt() {
now := time.Now()
lwdsp.startedAt = &now
}

// BrowserDurationPlugin handles the logic for writing the `monitor.duration.us` field
// for browser monitors.
type BrowserDurationPlugin struct {
Expand Down Expand Up @@ -89,4 +88,5 @@ func (bwdsp *BrowserDurationPlugin) BeforeSummary(event *beat.Event) BeforeSumma
return 0
}

func (bwdsp *BrowserDurationPlugin) BeforeRetry() {}
func (bwdsp *BrowserDurationPlugin) BeforeRetry() {}
func (bwdsp *BrowserDurationPlugin) BeforeEachEvent(event *beat.Event) {} // noop
4 changes: 4 additions & 0 deletions heartbeat/monitors/wrappers/summarizer/plugstatestat.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func (ssp *BrowserStateStatusPlugin) BeforeRetry() {
ssp.cssp.BeforeRetry()
}

func (ssp *BrowserStateStatusPlugin) BeforeEachEvent(event *beat.Event) {} //noop

// LightweightStateStatusPlugin encapsulates the writing of the primary fields used by the summary,
// those being `state.*`, `status.*` , `event.type`, and `monitor.check_group`
type LightweightStateStatusPlugin struct {
Expand Down Expand Up @@ -113,6 +115,8 @@ func (ssp *LightweightStateStatusPlugin) BeforeRetry() {
ssp.cssp.BeforeRetry()
}

func (ssp *LightweightStateStatusPlugin) BeforeEachEvent(event *beat.Event) {} // noop

type commonSSP struct {
js *jobsummary.JobSummary
stateTracker *monitorstate.Tracker
Expand Down
2 changes: 2 additions & 0 deletions heartbeat/monitors/wrappers/summarizer/plugurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ func (busp *BrowserURLPlugin) BeforeSummary(event *beat.Event) BeforeSummaryActi
func (busp *BrowserURLPlugin) BeforeRetry() {
busp.urlFields = nil
}

func (busp *BrowserURLPlugin) BeforeEachEvent(event *beat.Event) {} //noop
17 changes: 15 additions & 2 deletions heartbeat/monitors/wrappers/summarizer/summarizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type Summarizer struct {
startedAt time.Time
}

func (s Summarizer) beforeEachEvent(event *beat.Event) {
for _, plugin := range s.plugins {
plugin.BeforeEachEvent(event)
}
}

// EachEventActions is a set of options using bitmasks to inform execution after the EachEvent callback
type EachEventActions uint8

Expand All @@ -58,6 +64,9 @@ const RetryBeforeSummary = 1
// in one location. Prior to this code was strewn about a bit more and following it was
// a bit trickier.
type SummarizerPlugin interface {
// BeforeEachEvent is called on each event, and allows for the mutation of events
// before monitor execution
BeforeEachEvent(event *beat.Event)
// EachEvent is called on each event, and allows for the mutation of events
EachEvent(event *beat.Event, err error) EachEventActions
// BeforeSummary is run on the final (summary) event for each monitor.
Expand Down Expand Up @@ -106,6 +115,10 @@ func (s *Summarizer) setupPlugins() {
// This adds the state and summary top level fields.
func (s *Summarizer) Wrap(j jobs.Job) jobs.Job {
return func(event *beat.Event) ([]jobs.Job, error) {

// call BeforeEachEvent for each plugin before running job
s.beforeEachEvent(event)

conts, eventErr := j(event)

s.mtx.Lock()
Expand Down Expand Up @@ -149,10 +162,10 @@ func (s *Summarizer) Wrap(j jobs.Job) jobs.Job {
for _, p := range s.plugins {
p.BeforeRetry()
}
return s.rootJob(event)
return s.Wrap(s.rootJob)(event)
}

conts = []jobs.Job{delayedRootJob}
return []jobs.Job{delayedRootJob}, eventErr
}
}

Expand Down
2 changes: 1 addition & 1 deletion x-pack/heartbeat/heartbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ heartbeat.config.monitors:
heartbeat.monitors:
- type: http
# Set enabled to true (or delete the following line) to enable this monitor
enabled: true
enabled: false
# ID used to uniquely identify this monitor in Elasticsearch even if the config changes
id: my-monitor
# Human readable display name for this service in Uptime UI and elsewhere
Expand Down

0 comments on commit 19cf7ff

Please sign in to comment.