Skip to content

Commit 06c810c

Browse files
Heartbeat: set duration to zero for syntax errors (#30227)
* Heartbeat: set duration to 0 for syntax errors * set duration to null
1 parent 3a311f1 commit 06c810c

File tree

3 files changed

+116
-21
lines changed

3 files changed

+116
-21
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,13 @@ func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent, fields StdS
7676
je.checkGroup = makeUuid()
7777
je.journey = se.Journey
7878
je.start = event.Timestamp
79-
case "journey/end":
79+
case "journey/end", "cmd/status":
8080
je.end = event.Timestamp
8181
}
8282
} else {
8383
event.Timestamp = time.Now()
8484
}
8585

86-
eventext.MergeEventFields(event, common.MapStr{
87-
"event": common.MapStr{
88-
"type": se.Type,
89-
},
90-
"monitor": common.MapStr{
91-
"check_group": je.checkGroup,
92-
},
93-
})
94-
9586
// Id and name differs for inline and suite monitors
9687
// - We use the monitor id and name for inline journeys ignoring the
9788
// autogenerated `inline`journey id and name.
@@ -104,10 +95,14 @@ func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent, fields StdS
10495
name = fmt.Sprintf("%s - %s", name, je.journey.Name)
10596
}
10697
eventext.MergeEventFields(event, common.MapStr{
98+
"event": common.MapStr{
99+
"type": se.Type,
100+
},
107101
"monitor": common.MapStr{
108-
"id": id,
109-
"name": name,
110-
"type": fields.Type,
102+
"check_group": je.checkGroup,
103+
"id": id,
104+
"name": name,
105+
"type": fields.Type,
111106
},
112107
})
113108

@@ -139,7 +134,6 @@ func (je *journeyEnricher) enrichSynthEvent(event *beat.Event, se *SynthEvent) e
139134
// when an `afterAll` hook fails, for example, we don't wan't to include
140135
// a summary in the cmd/status event.
141136
if !je.journeyComplete {
142-
je.end = event.Timestamp
143137
return je.createSummary(event)
144138
}
145139
case "journey/end":
@@ -186,17 +180,27 @@ func (je *journeyEnricher) createSummary(event *beat.Event) error {
186180
down = 0
187181
}
188182

183+
// Incase of syntax errors or incorrect runner options, the Synthetics
184+
// runner would exit immediately with exitCode 1 and we do not set the duration
185+
// to inform the journey never ran
186+
if !je.start.IsZero() {
187+
eventext.MergeEventFields(event, common.MapStr{
188+
"monitor": common.MapStr{
189+
"duration": common.MapStr{
190+
"us": int64(je.end.Sub(je.start) / time.Microsecond),
191+
},
192+
},
193+
})
194+
}
189195
eventext.MergeEventFields(event, common.MapStr{
190196
"url": je.urlFields,
197+
"event": common.MapStr{
198+
"type": "heartbeat/summary",
199+
},
191200
"synthetics": common.MapStr{
192201
"type": "heartbeat/summary",
193202
"journey": je.journey,
194203
},
195-
"monitor": common.MapStr{
196-
"duration": common.MapStr{
197-
"us": int64(je.end.Sub(je.start) / time.Microsecond),
198-
},
199-
},
200204
"summary": common.MapStr{
201205
"up": up,
202206
"down": down,

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

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ func TestJourneyEnricher(t *testing.T) {
145145
tests := []struct {
146146
name string
147147
isInline bool
148-
se []*SynthEvent
149148
}{
150149
{
151150
name: "suite monitor",
@@ -406,3 +405,96 @@ func TestNoSummaryOnAfterHook(t *testing.T) {
406405
})
407406
}
408407
}
408+
409+
func TestCreateSummaryEvent(t *testing.T) {
410+
tests := []struct {
411+
name string
412+
je *journeyEnricher
413+
expected common.MapStr
414+
wantErr bool
415+
}{{
416+
name: "completed without errors",
417+
je: &journeyEnricher{
418+
journey: &Journey{},
419+
start: time.Now(),
420+
end: time.Now().Add(10 * time.Microsecond),
421+
journeyComplete: true,
422+
},
423+
expected: common.MapStr{
424+
"monitor.duration.us": int64(10),
425+
"summary": common.MapStr{
426+
"down": 0,
427+
"up": 1,
428+
},
429+
},
430+
wantErr: false,
431+
}, {
432+
name: "completed with error",
433+
je: &journeyEnricher{
434+
journey: &Journey{},
435+
start: time.Now(),
436+
end: time.Now().Add(10 * time.Microsecond),
437+
journeyComplete: true,
438+
errorCount: 1,
439+
firstError: fmt.Errorf("journey errored"),
440+
},
441+
expected: common.MapStr{
442+
"monitor.duration.us": int64(10),
443+
"summary": common.MapStr{
444+
"down": 1,
445+
"up": 0,
446+
},
447+
},
448+
wantErr: true,
449+
}, {
450+
name: "started, but exited without running steps",
451+
je: &journeyEnricher{
452+
journey: &Journey{},
453+
start: time.Now(),
454+
end: time.Now().Add(10 * time.Microsecond),
455+
journeyComplete: false,
456+
},
457+
expected: common.MapStr{
458+
"monitor.duration.us": int64(10),
459+
"summary": common.MapStr{
460+
"down": 0,
461+
"up": 1,
462+
},
463+
},
464+
wantErr: true,
465+
}, {
466+
name: "syntax error - exited without starting",
467+
je: &journeyEnricher{
468+
journey: &Journey{},
469+
end: time.Now().Add(10 * time.Microsecond),
470+
journeyComplete: false,
471+
errorCount: 1,
472+
},
473+
expected: common.MapStr{
474+
"summary": common.MapStr{
475+
"down": 1,
476+
"up": 0,
477+
},
478+
},
479+
wantErr: true,
480+
}}
481+
482+
for _, tt := range tests {
483+
t.Run(tt.name, func(t *testing.T) {
484+
e := &beat.Event{}
485+
err := tt.je.createSummary(e)
486+
if tt.wantErr {
487+
require.Error(t, err)
488+
} else {
489+
require.NoError(t, err)
490+
}
491+
common.MergeFields(tt.expected, common.MapStr{
492+
"url": common.MapStr{},
493+
"event.type": "heartbeat/summary",
494+
"synthetics.type": "heartbeat/summary",
495+
"synthetics.journey": Journey{},
496+
}, true)
497+
testslike.Test(t, lookslike.Strict(lookslike.MustCompile(tt.expected)), e.Fields)
498+
})
499+
}
500+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,4 @@ func (j Journey) ToMap() common.MapStr {
163163
"name": j.Name,
164164
"id": j.Id,
165165
}
166-
167166
}

0 commit comments

Comments
 (0)