From 35d3d5b61ef3d06b00242eca338e69c63837ae14 Mon Sep 17 00:00:00 2001 From: vigneshshanmugam Date: Fri, 4 Feb 2022 10:56:05 -0800 Subject: [PATCH 1/2] Heartbeat: set duration to 0 for syntax errors --- .../monitors/browser/synthexec/enrich.go | 37 +++++--- .../monitors/browser/synthexec/enrich_test.go | 95 ++++++++++++++++++- .../monitors/browser/synthexec/synthtypes.go | 1 - 3 files changed, 116 insertions(+), 17 deletions(-) diff --git a/x-pack/heartbeat/monitors/browser/synthexec/enrich.go b/x-pack/heartbeat/monitors/browser/synthexec/enrich.go index 57c5fe5a5a2..a34d2598262 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/enrich.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/enrich.go @@ -76,22 +76,13 @@ func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent, fields StdS je.checkGroup = makeUuid() je.journey = se.Journey je.start = event.Timestamp - case "journey/end": + case "journey/end", "cmd/status": je.end = event.Timestamp } } else { event.Timestamp = time.Now() } - eventext.MergeEventFields(event, common.MapStr{ - "event": common.MapStr{ - "type": se.Type, - }, - "monitor": common.MapStr{ - "check_group": je.checkGroup, - }, - }) - // Id and name differs for inline and suite monitors // - We use the monitor id and name for inline journeys ignoring the // autogenerated `inline`journey id and name. @@ -104,10 +95,14 @@ func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent, fields StdS name = fmt.Sprintf("%s - %s", name, je.journey.Name) } eventext.MergeEventFields(event, common.MapStr{ + "event": common.MapStr{ + "type": se.Type, + }, "monitor": common.MapStr{ - "id": id, - "name": name, - "type": fields.Type, + "check_group": je.checkGroup, + "id": id, + "name": name, + "type": fields.Type, }, }) @@ -139,7 +134,6 @@ func (je *journeyEnricher) enrichSynthEvent(event *beat.Event, se *SynthEvent) e // when an `afterAll` hook fails, for example, we don't wan't to include // a summary in the cmd/status event. if !je.journeyComplete { - je.end = event.Timestamp return je.createSummary(event) } case "journey/end": @@ -186,15 +180,28 @@ func (je *journeyEnricher) createSummary(event *beat.Event) error { down = 0 } + // Incase of syntax errors or incorrect runner options, the Synthetics + // runner would exit immediately with exitCode 1 andwe set the duration + // to 0 as to inform the journey never ran and there were no `journey/start` + // event fired from the agent. + var durationUs int64 + if je.start.IsZero() { + durationUs = 0 + } else { + durationUs = int64(je.end.Sub(je.start) / time.Microsecond) + } eventext.MergeEventFields(event, common.MapStr{ "url": je.urlFields, + "event": common.MapStr{ + "type": "heartbeat/summary", + }, "synthetics": common.MapStr{ "type": "heartbeat/summary", "journey": je.journey, }, "monitor": common.MapStr{ "duration": common.MapStr{ - "us": int64(je.end.Sub(je.start) / time.Microsecond), + "us": durationUs, }, }, "summary": common.MapStr{ diff --git a/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go b/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go index 140ec86bdec..9fb6467e762 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go @@ -145,7 +145,6 @@ func TestJourneyEnricher(t *testing.T) { tests := []struct { name string isInline bool - se []*SynthEvent }{ { name: "suite monitor", @@ -406,3 +405,97 @@ func TestNoSummaryOnAfterHook(t *testing.T) { }) } } + +func TestCreateSummaryEvent(t *testing.T) { + tests := []struct { + name string + je *journeyEnricher + expected common.MapStr + wantErr bool + }{{ + name: "completed without errors", + je: &journeyEnricher{ + journey: &Journey{}, + start: time.Now(), + end: time.Now().Add(10 * time.Microsecond), + journeyComplete: true, + }, + expected: common.MapStr{ + "monitor.duration.us": int64(10), + "summary": common.MapStr{ + "down": 0, + "up": 1, + }, + }, + wantErr: false, + }, { + name: "completed with error", + je: &journeyEnricher{ + journey: &Journey{}, + start: time.Now(), + end: time.Now().Add(10 * time.Microsecond), + journeyComplete: true, + errorCount: 1, + firstError: fmt.Errorf("journey errored"), + }, + expected: common.MapStr{ + "monitor.duration.us": int64(10), + "summary": common.MapStr{ + "down": 1, + "up": 0, + }, + }, + wantErr: true, + }, { + name: "started, but exited without running steps", + je: &journeyEnricher{ + journey: &Journey{}, + start: time.Now(), + end: time.Now().Add(10 * time.Microsecond), + journeyComplete: false, + }, + expected: common.MapStr{ + "monitor.duration.us": int64(10), + "summary": common.MapStr{ + "down": 0, + "up": 1, + }, + }, + wantErr: true, + }, { + name: "syntax error - exited without starting", + je: &journeyEnricher{ + journey: &Journey{}, + end: time.Now().Add(10 * time.Microsecond), + journeyComplete: false, + errorCount: 1, + }, + expected: common.MapStr{ + "monitor.duration.us": int64(0), + "summary": common.MapStr{ + "down": 1, + "up": 0, + }, + }, + wantErr: true, + }} + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &beat.Event{} + err := tt.je.createSummary(e) + if tt.wantErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } + common.MergeFields(tt.expected, common.MapStr{ + "url": common.MapStr{}, + "event.type": "heartbeat/summary", + "synthetics.type": "heartbeat/summary", + "synthetics.journey": Journey{}, + }, true) + testslike.Test(t, lookslike.Strict(lookslike.MustCompile(tt.expected)), e.Fields) + }) + } +} diff --git a/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go b/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go index 6a2bc8515f9..3fe625ef286 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go @@ -163,5 +163,4 @@ func (j Journey) ToMap() common.MapStr { "name": j.Name, "id": j.Id, } - } From 5a706b3c704dcc2df37e27b56e18a2b30a81f8c8 Mon Sep 17 00:00:00 2001 From: vigneshshanmugam Date: Fri, 11 Feb 2022 11:20:10 -0800 Subject: [PATCH 2/2] set duration to null --- .../monitors/browser/synthexec/enrich.go | 23 ++++++++----------- .../monitors/browser/synthexec/enrich_test.go | 1 - 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/x-pack/heartbeat/monitors/browser/synthexec/enrich.go b/x-pack/heartbeat/monitors/browser/synthexec/enrich.go index a34d2598262..162bd3f5727 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/enrich.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/enrich.go @@ -181,14 +181,16 @@ func (je *journeyEnricher) createSummary(event *beat.Event) error { } // Incase of syntax errors or incorrect runner options, the Synthetics - // runner would exit immediately with exitCode 1 andwe set the duration - // to 0 as to inform the journey never ran and there were no `journey/start` - // event fired from the agent. - var durationUs int64 - if je.start.IsZero() { - durationUs = 0 - } else { - durationUs = int64(je.end.Sub(je.start) / time.Microsecond) + // runner would exit immediately with exitCode 1 and we do not set the duration + // to inform the journey never ran + if !je.start.IsZero() { + eventext.MergeEventFields(event, common.MapStr{ + "monitor": common.MapStr{ + "duration": common.MapStr{ + "us": int64(je.end.Sub(je.start) / time.Microsecond), + }, + }, + }) } eventext.MergeEventFields(event, common.MapStr{ "url": je.urlFields, @@ -199,11 +201,6 @@ func (je *journeyEnricher) createSummary(event *beat.Event) error { "type": "heartbeat/summary", "journey": je.journey, }, - "monitor": common.MapStr{ - "duration": common.MapStr{ - "us": durationUs, - }, - }, "summary": common.MapStr{ "up": up, "down": down, diff --git a/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go b/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go index 9fb6467e762..e8a874ec3aa 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go @@ -471,7 +471,6 @@ func TestCreateSummaryEvent(t *testing.T) { errorCount: 1, }, expected: common.MapStr{ - "monitor.duration.us": int64(0), "summary": common.MapStr{ "down": 1, "up": 0,