Skip to content

Commit a5264c2

Browse files
author
Lucas Fernandes da Costa
committed
fix: avoid cmd/status when journey has already finished
1 parent d5f1daa commit a5264c2

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

x-pack/heartbeat/monitors/browser/synthexec/enrich.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent) error {
7676
je.checkGroup = makeUuid()
7777
je.journey = se.Journey
7878
je.start = event.Timestamp
79-
case "cmd/status":
80-
fallthrough
8179
case "journey/end":
8280
je.end = event.Timestamp
8381
}
@@ -115,8 +113,13 @@ func (je *journeyEnricher) enrichSynthEvent(event *beat.Event, se *SynthEvent) e
115113

116114
switch se.Type {
117115
case "cmd/status":
118-
je.journeyComplete = false
119-
return je.createSummary(event)
116+
// If a command failed _after_ the journey was complete, as it happens
117+
// when an `afterAll` hook fails, for example, we don't wan't to include
118+
// a summary in the cmd/status event.
119+
if !je.journeyComplete {
120+
je.end = event.Timestamp
121+
return je.createSummary(event)
122+
}
120123
case "journey/end":
121124
je.journeyComplete = true
122125
return je.createSummary(event)

x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ import (
2121
"github.com/elastic/go-lookslike/testslike"
2222
)
2323

24+
func makeStepEvent(typ string, ts float64, name string, index int, status string, urlstr string, err *SynthError) *SynthEvent {
25+
return &SynthEvent{
26+
Type: typ,
27+
TimestampEpochMicros: 1000 + ts,
28+
PackageVersion: "1.0.0",
29+
Step: &Step{Name: name, Index: index, Status: status},
30+
Error: err,
31+
Payload: common.MapStr{},
32+
URL: urlstr,
33+
}
34+
}
35+
2436
func TestJourneyEnricher(t *testing.T) {
2537
journey := &Journey{
2638
Name: "A Journey Name",
@@ -50,17 +62,6 @@ func TestJourneyEnricher(t *testing.T) {
5062
Journey: journey,
5163
Payload: common.MapStr{},
5264
}
53-
makeStepEvent := func(typ string, ts float64, name string, index int, status string, urlstr string, err *SynthError) *SynthEvent {
54-
return &SynthEvent{
55-
Type: typ,
56-
TimestampEpochMicros: 1000 + ts,
57-
PackageVersion: "1.0.0",
58-
Step: &Step{Name: name, Index: index, Status: status},
59-
Error: err,
60-
Payload: common.MapStr{},
61-
URL: urlstr,
62-
}
63-
}
6465
url1 := "http://example.net/url1"
6566
url2 := "http://example.net/url2"
6667
url3 := "http://example.net/url3"
@@ -213,3 +214,75 @@ func TestEnrichSynthEvent(t *testing.T) {
213214
})
214215
}
215216
}
217+
218+
func TestNoSummaryOnAfterHook(t *testing.T) {
219+
journey := &Journey{
220+
Name: "A journey that fails after completing",
221+
Id: "my-bad-after-all-hook",
222+
}
223+
journeyStart := &SynthEvent{
224+
Type: "journey/start",
225+
TimestampEpochMicros: 1000,
226+
PackageVersion: "1.0.0",
227+
Journey: journey,
228+
Payload: common.MapStr{},
229+
}
230+
syntherr := &SynthError{
231+
Message: "my-errmsg",
232+
Name: "my-errname",
233+
Stack: "my\nerr\nstack",
234+
}
235+
journeyEnd := &SynthEvent{
236+
Type: "journey/end",
237+
TimestampEpochMicros: 2000,
238+
PackageVersion: "1.0.0",
239+
Journey: journey,
240+
Payload: common.MapStr{},
241+
}
242+
cmdStatus := &SynthEvent{
243+
Type: "cmd/status",
244+
Error: &SynthError{Name: "cmdexit", Message: "cmd err msg"},
245+
TimestampEpochMicros: 3000,
246+
}
247+
248+
badStepUrl := "https://example.com/bad-step"
249+
synthEvents := []*SynthEvent{
250+
journeyStart,
251+
makeStepEvent("step/start", 10, "Step1", 1, "", "", nil),
252+
makeStepEvent("step/end", 20, "Step1", 1, "failed", badStepUrl, syntherr),
253+
journeyEnd,
254+
cmdStatus,
255+
}
256+
257+
je := &journeyEnricher{}
258+
259+
for idx, se := range synthEvents {
260+
e := &beat.Event{}
261+
t.Run(fmt.Sprintf("event %d", idx), func(t *testing.T) {
262+
enrichErr := je.enrich(e, se)
263+
264+
if se != nil && se.Type == "cmd/status" {
265+
t.Run("no summary in cmd/status", func(t *testing.T) {
266+
require.NotContains(t, e.Fields, "summary")
267+
})
268+
}
269+
270+
// Only the journey/end event should get a summary when
271+
// it's emitted before the cmd/status (when an afterX hook fails).
272+
if se != nil && se.Type == "journey/end" {
273+
require.Equal(t, stepError(syntherr), enrichErr)
274+
275+
u, _ := url.Parse(badStepUrl)
276+
t.Run("summary in journey/end", func(t *testing.T) {
277+
v := lookslike.MustCompile(common.MapStr{
278+
"synthetics.type": "heartbeat/summary",
279+
"url": wrappers.URLFields(u),
280+
"monitor.duration.us": int64(journeyEnd.Timestamp().Sub(journeyStart.Timestamp()) / time.Microsecond),
281+
})
282+
283+
testslike.Test(t, v, e.Fields)
284+
})
285+
}
286+
})
287+
}
288+
}

0 commit comments

Comments
 (0)