-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
enrich.go
183 lines (159 loc) · 4.33 KB
/
enrich.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package synthexec
import (
"fmt"
"time"
"github.com/elastic/beats/v7/libbeat/beat/events"
"github.com/elastic/beats/v7/libbeat/processors/add_data_stream"
"github.com/gofrs/uuid"
"github.com/elastic/beats/v7/heartbeat/eventext"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
)
type enricher func(event *beat.Event, se *SynthEvent) error
type streamEnricher struct {
je *journeyEnricher
}
func (e *streamEnricher) enrich(event *beat.Event, se *SynthEvent) error {
if e.je == nil || (se != nil && se.Type == "journey/start") {
e.je = newJourneyEnricher()
}
return e.je.enrich(event, se)
}
// journeyEnricher holds state across received SynthEvents retaining fields
// where relevant to properly enrich *beat.Event instances.
type journeyEnricher struct {
journeyComplete bool
journey *Journey
checkGroup string
errorCount int
firstError error
stepCount int
// The first URL we visit is the URL for this journey, which is set on the summary event.
// We store the URL fields here for use on the summary event.
urlFields common.MapStr
start time.Time
end time.Time
}
func newJourneyEnricher() *journeyEnricher {
return &journeyEnricher{
checkGroup: makeUuid(),
}
}
func makeUuid() string {
u, err := uuid.NewV1()
if err != nil {
panic("Cannot generate v1 UUID, this should never happen!")
}
return u.String()
}
func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent) error {
if se == nil {
return nil
}
if !se.Timestamp().IsZero() {
event.Timestamp = se.Timestamp()
// Record start and end so we can calculate journey duration accurately later
switch se.Type {
case "journey/start":
je.firstError = nil
je.checkGroup = makeUuid()
je.journey = se.Journey
je.start = event.Timestamp
case "journey/end":
je.end = event.Timestamp
}
} else {
event.Timestamp = time.Now()
}
eventext.MergeEventFields(event, common.MapStr{
"monitor": common.MapStr{
"check_group": je.checkGroup,
},
})
// Inline jobs have no journey
if je.journey != nil {
eventext.MergeEventFields(event, common.MapStr{
"monitor": common.MapStr{
"id": je.journey.Id,
"name": je.journey.Name,
},
})
}
return je.enrichSynthEvent(event, se)
}
func (je *journeyEnricher) enrichSynthEvent(event *beat.Event, se *SynthEvent) error {
var jobErr error
if se.Error != nil {
jobErr = stepError(se.Error)
je.errorCount++
if je.firstError == nil {
je.firstError = jobErr
}
}
switch se.Type {
case "journey/end":
je.journeyComplete = true
return je.createSummary(event)
case "step/end":
je.stepCount++
case "step/screenshot":
fallthrough
case "step/screenshot_ref":
fallthrough
case "screenshot/block":
add_data_stream.SetEventDataset(event, "browser.screenshot")
case "journey/network_info":
add_data_stream.SetEventDataset(event, "browser.network")
}
if se.Id != "" {
event.SetID(se.Id)
// This is only relevant for screenshots, which have a specific ID
// In that case we always want to issue an update op
event.Meta.Put(events.FieldMetaOpType, events.OpTypeCreate)
}
eventext.MergeEventFields(event, se.ToMap())
if je.urlFields == nil {
if urlFields, err := event.GetValue("url"); err == nil {
if ufMap, ok := urlFields.(common.MapStr); ok {
je.urlFields = ufMap
}
}
}
return jobErr
}
func (je *journeyEnricher) createSummary(event *beat.Event) error {
var up, down int
if je.errorCount > 0 {
up = 0
down = 1
} else {
up = 1
down = 0
}
if je.journeyComplete {
eventext.MergeEventFields(event, common.MapStr{
"url": je.urlFields,
"synthetics": common.MapStr{
"type": "heartbeat/summary",
"journey": je.journey,
},
"monitor": common.MapStr{
"duration": common.MapStr{
"us": int64(je.end.Sub(je.start) / time.Microsecond),
},
},
"summary": common.MapStr{
"up": up,
"down": down,
},
})
return je.firstError
}
return fmt.Errorf("journey did not finish executing, %d steps ran", je.stepCount)
}
func stepError(e *SynthError) error {
return fmt.Errorf("error executing step: %s", e.String())
}