forked from evergreen-ci/evergreen
/
task_log.go
298 lines (261 loc) · 8.05 KB
/
task_log.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package model
import (
"time"
"github.com/evergreen-ci/evergreen/apimodels"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/util"
"github.com/mongodb/anser/bsonutil"
adb "github.com/mongodb/anser/db"
"go.mongodb.org/mongo-driver/bson"
mgobson "gopkg.in/mgo.v2/bson"
)
const (
TaskLogDB = "logs"
TaskLogCollection = "task_logg"
MessagesPerLog = 10
)
// a single chunk of a task log
type TaskLog struct {
Id string `bson:"_id" json:"_id,omitempty"`
TaskId string `bson:"t_id" json:"t_id"`
Execution int `bson:"e" json:"e"`
Timestamp time.Time `bson:"ts" json:"ts"`
MessageCount int `bson:"c" json:"c"`
Messages []apimodels.LogMessage `bson:"m" json:"m"`
}
func (t *TaskLog) MarshalBSON() ([]byte, error) { return mgobson.Marshal(t) }
func (t *TaskLog) UnmarshalBSON(in []byte) error { return mgobson.Unmarshal(in, t) }
var (
// bson fields for the task log struct
TaskLogIdKey = bsonutil.MustHaveTag(TaskLog{}, "Id")
TaskLogTaskIdKey = bsonutil.MustHaveTag(TaskLog{}, "TaskId")
TaskLogExecutionKey = bsonutil.MustHaveTag(TaskLog{}, "Execution")
TaskLogTimestampKey = bsonutil.MustHaveTag(TaskLog{}, "Timestamp")
TaskLogMessageCountKey = bsonutil.MustHaveTag(TaskLog{}, "MessageCount")
TaskLogMessagesKey = bsonutil.MustHaveTag(TaskLog{}, "Messages")
// bson fields for the log message struct
LogMessageTypeKey = bsonutil.MustHaveTag(apimodels.LogMessage{}, "Type")
LogMessageSeverityKey = bsonutil.MustHaveTag(apimodels.LogMessage{}, "Severity")
LogMessageMessageKey = bsonutil.MustHaveTag(apimodels.LogMessage{}, "Message")
LogMessageTimestampKey = bsonutil.MustHaveTag(apimodels.LogMessage{}, "Timestamp")
)
// helper for getting the correct db
func getSessionAndDB() (adb.Session, adb.Database, error) {
session, _, err := db.GetGlobalSessionFactory().GetSession()
if err != nil {
return nil, nil, err
}
return session, session.DB(TaskLogDB), nil
}
/******************************************************
Functions that operate on entire TaskLog documents
******************************************************/
func (self *TaskLog) Insert() error {
session, db, err := getSessionAndDB()
if err != nil {
return err
}
defer session.Close()
self.Id = mgobson.NewObjectId().Hex()
return db.C(TaskLogCollection).Insert(self)
}
func (self *TaskLog) AddLogMessage(msg apimodels.LogMessage) error {
session, db, err := getSessionAndDB()
if err != nil {
return err
}
defer session.Close()
// NOTE: this was previously set to fire-and-forget writes,
// but removed during the database migration
self.Messages = append(self.Messages, msg)
self.MessageCount = self.MessageCount + 1
return db.C(TaskLogCollection).UpdateId(self.Id,
bson.M{
"$inc": bson.M{
TaskLogMessageCountKey: 1,
},
"$push": bson.M{
TaskLogMessagesKey: msg,
},
},
)
}
func FindAllTaskLogs(taskId string, execution int) ([]TaskLog, error) {
session, db, err := getSessionAndDB()
if err != nil {
return nil, err
}
defer session.Close()
result := []TaskLog{}
err = db.C(TaskLogCollection).Find(
bson.M{
TaskLogTaskIdKey: taskId,
TaskLogExecutionKey: execution,
},
).Sort("-" + TaskLogTimestampKey).All(&result)
if adb.ResultsNotFound(err) {
return nil, nil
}
return result, err
}
func FindMostRecentTaskLogs(taskId string, execution int, limit int) ([]TaskLog, error) {
session, db, err := getSessionAndDB()
if err != nil {
return nil, err
}
defer session.Close()
result := []TaskLog{}
err = db.C(TaskLogCollection).Find(
bson.M{
TaskLogTaskIdKey: taskId,
TaskLogExecutionKey: execution,
},
).Sort("-" + TaskLogTimestampKey).Limit(limit).All(&result)
if adb.ResultsNotFound(err) {
return nil, nil
}
return result, err
}
func FindTaskLogsBeforeTime(taskId string, execution int, ts time.Time, limit int) ([]TaskLog, error) {
session, db, err := getSessionAndDB()
if err != nil {
return nil, err
}
defer session.Close()
query := bson.M{
TaskLogTaskIdKey: taskId,
TaskLogExecutionKey: execution,
TaskLogTimestampKey: bson.M{
"$lt": ts,
},
}
result := []TaskLog{}
err = db.C(TaskLogCollection).Find(query).Sort("-" + TaskLogTimestampKey).Limit(limit).All(&result)
if adb.ResultsNotFound(err) {
return nil, nil
}
return result, err
}
func GetRawTaskLogChannel(taskId string, execution int, severities []string,
msgTypes []string) (chan apimodels.LogMessage, error) {
session, db, err := getSessionAndDB()
if err != nil {
return nil, err
}
logObj := TaskLog{}
// 100 is an arbitrary magic number. Unbuffered channel would be bad for
// performance, so just picked a buffer size out of thin air.
channel := make(chan apimodels.LogMessage, 100)
var query bson.M
if execution == 0 {
query = bson.M{"$and": []bson.M{
{TaskLogTaskIdKey: taskId},
{"$or": []bson.M{
{TaskLogExecutionKey: 0},
{TaskLogExecutionKey: nil},
}}}}
} else {
query = bson.M{
TaskLogTaskIdKey: taskId,
TaskLogExecutionKey: execution,
}
}
iter := db.C(TaskLogCollection).Find(query).Sort(TaskLogTimestampKey).Iter()
oldMsgTypes := []string{}
for _, msgType := range msgTypes {
switch msgType {
case apimodels.SystemLogPrefix:
oldMsgTypes = append(oldMsgTypes, "system")
case apimodels.AgentLogPrefix:
oldMsgTypes = append(oldMsgTypes, "agent")
case apimodels.TaskLogPrefix:
oldMsgTypes = append(oldMsgTypes, "task")
}
}
go func() {
defer session.Close()
defer close(channel)
defer iter.Close()
for iter.Next(&logObj) {
for _, logMsg := range logObj.Messages {
if len(severities) > 0 &&
!util.StringSliceContains(severities, logMsg.Severity) {
continue
}
if len(msgTypes) > 0 {
if !(util.StringSliceContains(msgTypes, logMsg.Type) ||
util.StringSliceContains(oldMsgTypes, logMsg.Type)) {
continue
}
}
channel <- logMsg
}
}
}()
return channel, nil
}
/******************************************************
Functions that operate on individual log messages
******************************************************/
// note: to ignore severity or type filtering, pass in empty slices
func FindMostRecentLogMessages(taskId string, execution int, numMsgs int,
severities []string, msgTypes []string) ([]apimodels.LogMessage, error) {
logMsgs := []apimodels.LogMessage{}
numMsgsNeeded := numMsgs
lastTimeStamp := time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC)
oldMsgTypes := []string{}
for _, msgType := range msgTypes {
switch msgType {
case apimodels.SystemLogPrefix:
oldMsgTypes = append(oldMsgTypes, "system")
case apimodels.AgentLogPrefix:
oldMsgTypes = append(oldMsgTypes, "agent")
case apimodels.TaskLogPrefix:
oldMsgTypes = append(oldMsgTypes, "task")
}
}
// keep grabbing task logs from farther back until there are enough messages
for numMsgsNeeded != 0 {
numTaskLogsToFetch := numMsgsNeeded / MessagesPerLog
taskLogs, err := FindTaskLogsBeforeTime(taskId, execution, lastTimeStamp,
numTaskLogsToFetch)
if err != nil {
return nil, err
}
// if we've exhausted the stored logs, break
if len(taskLogs) == 0 {
break
}
// otherwise, grab all applicable log messages out of the returned task
// log documents
for _, taskLog := range taskLogs {
// reverse
messages := make([]apimodels.LogMessage, len(taskLog.Messages))
for idx, msg := range taskLog.Messages {
messages[len(taskLog.Messages)-1-idx] = msg
}
for _, logMsg := range messages {
// filter by severity and type
if len(severities) != 0 &&
!util.StringSliceContains(severities, logMsg.Severity) {
continue
}
if len(msgTypes) != 0 {
if !(util.StringSliceContains(msgTypes, logMsg.Type) ||
util.StringSliceContains(oldMsgTypes, logMsg.Type)) {
continue
}
}
// the message is relevant, store it
logMsgs = append(logMsgs, logMsg)
numMsgsNeeded--
if numMsgsNeeded == 0 {
return logMsgs, nil
}
}
}
// store the last timestamp
lastTimeStamp = taskLogs[len(taskLogs)-1].Timestamp
}
return logMsgs, nil
}