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

[Heartbeat] Fix summarizer #36519

Merged
merged 41 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
cc98960
Cleanup summarizer code
andrewvc Sep 5, 2023
a609fe4
Separate concerns in summarizer
andrewvc Sep 6, 2023
3c5b677
Checkpoint
andrewvc Sep 6, 2023
04076e6
Fix failing tests
andrewvc Sep 6, 2023
ba9ef02
FMT
andrewvc Sep 7, 2023
0e1ca64
Tweaks
andrewvc Sep 7, 2023
f23be6e
Merge remote-tracking branch 'origin/main' into fix-summarizer
andrewvc Sep 7, 2023
7d5512c
Make linter happy
andrewvc Sep 7, 2023
2b38f1f
progress
andrewvc Sep 8, 2023
6776b3a
cleanup docs
andrewvc Sep 8, 2023
16f74bf
Bring back wrappers tests (partial)
andrewvc Sep 8, 2023
692e29a
Restore wrapper tests
andrewvc Sep 8, 2023
1ec207b
Fix failing tests
andrewvc Sep 8, 2023
6ed4e2f
Fix err handling
andrewvc Sep 8, 2023
9689e66
Re-init plugins on retry
andrewvc Sep 8, 2023
1c93f1f
Fix error field handling across retries
andrewvc Sep 8, 2023
cfe31c2
Incorporate PR feedback
andrewvc Sep 12, 2023
1bc7f6b
Type fix
andrewvc Sep 12, 2023
4c749de
Merge remote-tracking branch 'origin/main' into fix-summarizer
andrewvc Sep 12, 2023
9dcfafe
URLs now work, tests passing
andrewvc Sep 12, 2023
fafa379
Improved err handling
andrewvc Sep 12, 2023
969625c
Test fixes
andrewvc Sep 12, 2023
0294d1b
Cleanup naming
andrewvc Sep 13, 2023
089a72c
Fix handling of step counts / journey/end missing and also fix contin…
andrewvc Sep 13, 2023
e61563c
Fix failing tests around logging / logging behavior
andrewvc Sep 13, 2023
71df932
Rename OnRetry to BeforeRetry
andrewvc Sep 13, 2023
c9784ff
Move monitor.status calculation for browsers into summarizer
andrewvc Sep 13, 2023
6418d53
Cleanup status logic
andrewvc Sep 13, 2023
e51378c
More status consolidation
andrewvc Sep 13, 2023
98d9721
Fixed failing tests
andrewvc Sep 13, 2023
ebb07d8
Make monitor logger errors more understandable
andrewvc Sep 13, 2023
f56570a
Fix retry delay
andrewvc Sep 14, 2023
170464f
Fix retry delay
andrewvc Sep 14, 2023
99b1d66
Remove spurious 'wrapped:' in logs
andrewvc Sep 14, 2023
019e72f
Incorporate pr feedback
andrewvc Sep 14, 2023
7dec5a4
Fix dur
andrewvc Sep 14, 2023
acf0daf
Fix cmd status
andrewvc Sep 14, 2023
faa4164
Fix tests
andrewvc Sep 14, 2023
b533a1c
Fmt
andrewvc Sep 14, 2023
74d685a
Integrate PR feedback
andrewvc Sep 18, 2023
4c0a49f
Merge branch 'main' into fix-summarizer
vigneshshanmugam Sep 18, 2023
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
7 changes: 7 additions & 0 deletions heartbeat/monitors/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
Duration int64 `json:"-"`
Steps *int `json:"steps,omitempty"`
Status string `json:"status"`
Attempts uint16 `json:"attempts`

Check failure on line 47 in heartbeat/monitors/logger/logger.go

View workflow job for this annotation

GitHub Actions / lint (linux)

structtag: struct field tag `json:"attempts` not compatible with reflect.StructTag.Get: bad syntax for struct tag value (govet)
}

func (m *MonitorRunInfo) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -96,6 +97,11 @@
errors = append(errors, err)
}

attempts, err := event.GetValue("summary.attempt")
if err != nil {
errors = append(errors, err)
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
}

if len(errors) > 0 {
return nil, fmt.Errorf("logErrors: %+v", errors)
}
Expand All @@ -105,6 +111,7 @@
Type: monType.(string),
Duration: durationUs.(int64),
Status: status.(string),
Attempts: attempts.(uint16),
}

sc, _ := event.Meta.GetValue(META_STEP_COUNT)
Expand Down
47 changes: 28 additions & 19 deletions heartbeat/monitors/wrappers/summarizer/plugerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package summarizer

import (
"errors"
"fmt"

"github.com/elastic/beats/v7/heartbeat/ecserr"
"github.com/elastic/beats/v7/heartbeat/eventext"
Expand All @@ -31,32 +32,53 @@ import (
// for browser monitors, preferentially using the journey/end event's
// error field for errors.
type BrowserErrPlugin struct {
summaryErrVal interface{}
summaryErrVal interface{}
summaryErr error
stepCount int
journeyEndRcvd bool
}

func (esp *BrowserErrPlugin) EachEvent(event *beat.Event, eventErr error) EachEventActions {
// track these to determine if the journey
// needs an error injected due to incompleteness
st := synthType(event)
switch st {
case "step/end":
esp.stepCount++
case "journey/end":
esp.journeyEndRcvd = true
}

// Nothing else to do if there's no error
if eventErr == nil {
return 0
}

// Merge the error value into the event's "error" field
errVal := errToFieldVal(eventErr)
mergeErrVal(event, errVal)

isJourneyEnd := false
if synthType(event) == "journey/end" {
isJourneyEnd = true
}
if esp.summaryErrVal == nil || isJourneyEnd {
// If there is no error value OR this is the journey end event
// record this as the definitive error
if esp.summaryErrVal == nil || st == "journey/end" {
esp.summaryErr = eventErr
esp.summaryErrVal = errVal
}

return DropErrEvent
}

func (esp *BrowserErrPlugin) OnSummary(event *beat.Event) OnSummaryActions {
// If no journey end was received, make that the summary error
if !esp.journeyEndRcvd {
esp.summaryErr = fmt.Errorf("journey did not finish executing, %d steps ran: %w", esp.stepCount, esp.summaryErr)
esp.summaryErrVal = errToFieldVal(esp.summaryErr)
}

if esp.summaryErrVal != nil {
mergeErrVal(event, esp.summaryErrVal)
}

return 0
}

Expand Down Expand Up @@ -104,16 +126,3 @@ func errToFieldVal(eventErr error) (errVal interface{}) {
func mergeErrVal(event *beat.Event, errVal interface{}) {
eventext.MergeEventFields(event, mapstr.M{"error": errVal})
}

func synthType(event *beat.Event) string {
synthType, err := event.GetValue("synthetics.type")
if err != nil {
return ""
}

str, ok := synthType.(string)
if !ok {
return ""
}
return str
}
33 changes: 33 additions & 0 deletions heartbeat/monitors/wrappers/summarizer/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package summarizer

import "github.com/elastic/beats/v7/libbeat/beat"

func synthType(event *beat.Event) string {
synthType, err := event.GetValue("synthetics.type")
if err != nil {
return ""
}

str, ok := synthType.(string)
if !ok {
return ""
}
return str
}
3 changes: 1 addition & 2 deletions x-pack/heartbeat/scenarios/stateloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/heartbeat/monitors/wrappers/monitorstate"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/x-pack/heartbeat/scenarios/framework"
)
Expand All @@ -35,7 +34,7 @@ func TestStateContinuity(t *testing.T) {

lastSS := framework.LastState(mtr.Events())

assert.Equal(t, monitorstate.StatusUp, lastSS.State.Status, "monitor was unexpectedly down, synthetics console output: %s, errors", sout, errors)
assert.Equal(t, mtr.Meta.Status, lastSS.State.Status, "monitor had unexpected state %v, synthetics console output: %s, errors", lastSS.State.Status, sout, errors)

allSS := framework.AllStates(mtr.Events())
assert.Len(t, allSS, numRuns)
Expand Down
Loading