/
per_week_stream_logs_strategy.go
411 lines (370 loc) · 15.5 KB
/
per_week_stream_logs_strategy.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package stream_logs_strategy
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/hpcloud/tail"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/logs_clock"
"github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/volume_consts"
"github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/volume_filesystem"
"github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/logline"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"io"
"os"
"strconv"
"strings"
"time"
)
const (
oneWeek = 7 * 24 * time.Hour
)
// PerWeekStreamLogsStrategy pulls logs from filesystem where there is a log file per year, per week, per enclave, per service
// Weeks are denoted 01-52
// e.g.
// [.../28/d3e8832d671f/61830789f03a.json] is the file containing logs from service with uuid 61830789f03a, in enclave with uuid d3e8832d671f,
// in the 28th week of the current year
type PerWeekStreamLogsStrategy struct {
time logs_clock.LogsClock
}
func NewPerWeekStreamLogsStrategy(time logs_clock.LogsClock) *PerWeekStreamLogsStrategy {
return &PerWeekStreamLogsStrategy{
time: time,
}
}
func (strategy *PerWeekStreamLogsStrategy) StreamLogs(
ctx context.Context,
fs volume_filesystem.VolumeFilesystem,
logsByKurtosisUserServiceUuidChan chan map[service.ServiceUUID][]logline.LogLine,
streamErrChan chan error,
enclaveUuid enclave.EnclaveUUID,
serviceUuid service.ServiceUUID,
conjunctiveLogLinesFiltersWithRegex []logline.LogLineFilterWithRegex,
shouldFollowLogs bool,
shouldReturnAllLogs bool,
numLogLines uint32,
) {
paths, err := strategy.getLogFilePaths(fs, volume_consts.LogRetentionPeriodInWeeks, string(enclaveUuid), string(serviceUuid))
if err != nil {
streamErrChan <- stacktrace.Propagate(err, "An error occurred retrieving log file paths for service '%v' in enclave '%v'.", serviceUuid, enclaveUuid)
return
}
if len(paths) == 0 {
streamErrChan <- stacktrace.NewError(
`No logs file paths for service '%v' in enclave '%v' were found. This means either:
1) No logs for this service were detected/stored.
2) Logs were manually removed.`,
serviceUuid, enclaveUuid)
return
}
if len(paths) > volume_consts.LogRetentionPeriodInWeeks {
logrus.Warnf(
`We expected to retrieve logs going back '%v' weeks, but instead retrieved logs going back '%v' weeks.
This means logs past the retention period are being returned, likely a bug in Kurtosis.`,
volume_consts.LogRetentionPeriodInWeeks, len(paths))
}
logsReader, files, err := getLogsReader(fs, paths)
if err != nil {
streamErrChan <- stacktrace.Propagate(err, "An error occurred creating a logs reader for service '%v' in enclave '%v'", serviceUuid, enclaveUuid)
return
}
defer func() {
for _, file := range files {
_ = file.Close()
}
}()
if shouldReturnAllLogs {
if err := strategy.streamAllLogs(ctx, logsReader, logsByKurtosisUserServiceUuidChan, serviceUuid, conjunctiveLogLinesFiltersWithRegex); err != nil {
streamErrChan <- stacktrace.Propagate(err, "An error occurred streaming all logs for service '%v' in enclave '%v'", serviceUuid, enclaveUuid)
return
}
} else {
if err := strategy.streamTailLogs(ctx, logsReader, numLogLines, logsByKurtosisUserServiceUuidChan, serviceUuid, conjunctiveLogLinesFiltersWithRegex); err != nil {
streamErrChan <- stacktrace.Propagate(err, "An error occurred streaming '%v' logs for service '%v' in enclave '%v'", numLogLines, serviceUuid, enclaveUuid)
return
}
}
if shouldFollowLogs {
latestLogFile := paths[len(paths)-1]
if err := strategy.followLogs(latestLogFile, logsByKurtosisUserServiceUuidChan, serviceUuid, conjunctiveLogLinesFiltersWithRegex); err != nil {
streamErrChan <- stacktrace.Propagate(err, "An error occurred creating following logs for service '%v' in enclave '%v'", serviceUuid, enclaveUuid)
return
}
logrus.Debugf("Following logs...")
}
}
// [getLogFilePaths] returns a list of log file paths containing logs for [serviceUuid] in [enclaveUuid]
// going [retentionPeriodInWeeks] back from the [currentWeek].
// Notes:
// - File paths are of the format '/week/enclave uuid/service uuid.json' where 'week' is %W strftime specifier
// - The list of file paths is returned in order of oldest logs to most recent logs e.g. [ 3/80124/1234.json, /4/801234/1234.json, ...]
// - If a file path does not exist, the function with exits and returns whatever file paths were found
func (strategy *PerWeekStreamLogsStrategy) getLogFilePaths(filesystem volume_filesystem.VolumeFilesystem, retentionPeriodInWeeks int, enclaveUuid, serviceUuid string) ([]string, error) {
var paths []string
currentTime := strategy.time.Now()
// scan for first existing log file
firstWeekWithLogs := 0
for i := 0; i < retentionPeriodInWeeks; i++ {
year, week := currentTime.Add(time.Duration(-i) * oneWeek).ISOWeek()
filePathStr := fmt.Sprintf(volume_consts.PerWeekFilePathFmtStr, volume_consts.LogsStorageDirpath, strconv.Itoa(year), strconv.Itoa(week), enclaveUuid, serviceUuid, volume_consts.Filetype)
if _, err := filesystem.Stat(filePathStr); err == nil {
paths = append(paths, filePathStr)
firstWeekWithLogs = i
break
} else {
// return if error is not due to nonexistent file path
if !os.IsNotExist(err) {
return paths, err
}
}
}
// scan for remaining files as far back as they exist
for i := firstWeekWithLogs + 1; i < retentionPeriodInWeeks; i++ {
year, week := currentTime.Add(time.Duration(-i) * oneWeek).ISOWeek()
filePathStr := fmt.Sprintf(volume_consts.PerWeekFilePathFmtStr, volume_consts.LogsStorageDirpath, strconv.Itoa(year), strconv.Itoa(week), enclaveUuid, serviceUuid, volume_consts.Filetype)
if _, err := filesystem.Stat(filePathStr); err != nil {
break
}
paths = append(paths, filePathStr)
}
// reverse for oldest to most recent
slices.Reverse(paths)
return paths, nil
}
// Returns a Reader over all logs in [logFilePaths] and the open file descriptors of the associated [logFilePaths]
func getLogsReader(filesystem volume_filesystem.VolumeFilesystem, logFilePaths []string) (*bufio.Reader, []volume_filesystem.VolumeFile, error) {
var fileReaders []io.Reader
var files []volume_filesystem.VolumeFile
// get a reader for each log file
for _, pathStr := range logFilePaths {
logsFile, err := filesystem.Open(pathStr)
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred opening the logs file at the following path: %v", pathStr)
}
fileReaders = append(fileReaders, logsFile)
files = append(files, logsFile)
}
// combine log file readers into a single reader
combinedLogsReader := io.MultiReader(fileReaders...)
return bufio.NewReader(combinedLogsReader), files, nil
}
func (strategy *PerWeekStreamLogsStrategy) streamAllLogs(
ctx context.Context,
logsReader *bufio.Reader,
logsByKurtosisUserServiceUuidChan chan map[service.ServiceUUID][]logline.LogLine,
serviceUuid service.ServiceUUID,
conjunctiveLogLinesFiltersWithRegex []logline.LogLineFilterWithRegex) error {
for {
select {
case <-ctx.Done():
logrus.Debugf("Context was canceled, stopping streaming service logs for service '%v'", serviceUuid)
return nil
default:
jsonLogStr, err := getCompleteJsonLogString(logsReader)
if isValidJsonEnding(jsonLogStr) {
jsonLog, err := convertStringToJson(jsonLogStr)
if err != nil {
return stacktrace.Propagate(err, "An error occurred converting the json log string '%v' into json.", jsonLogStr)
}
if err = strategy.sendJsonLogLine(jsonLog, logsByKurtosisUserServiceUuidChan, serviceUuid, conjunctiveLogLinesFiltersWithRegex); err != nil {
return err
}
}
if err != nil {
// if we've reached end of logs, return success, otherwise return the error
if errors.Is(err, io.EOF) {
return nil
} else {
return err
}
}
}
}
}
// tail -n X
func (strategy *PerWeekStreamLogsStrategy) streamTailLogs(
ctx context.Context,
logsReader *bufio.Reader,
numLogLines uint32,
logsByKurtosisUserServiceUuidChan chan map[service.ServiceUUID][]logline.LogLine,
serviceUuid service.ServiceUUID,
conjunctiveLogLinesFiltersWithRegex []logline.LogLineFilterWithRegex) error {
tailLogLines := make([]string, 0, numLogLines)
for {
select {
case <-ctx.Done():
logrus.Debugf("Context was canceled, stopping streaming service logs for service '%v'", serviceUuid)
return nil
default:
jsonLogStr, err := getCompleteJsonLogString(logsReader)
if isValidJsonEnding(jsonLogStr) {
// collect all log lines in tail log lines
tailLogLines = append(tailLogLines, jsonLogStr)
if len(tailLogLines) > int(numLogLines) {
tailLogLines = tailLogLines[1:]
}
continue
}
if err != nil {
// stop reading if end of logs reached, otherwise return the error
if errors.Is(err, io.EOF) {
break
} else {
return err
}
}
}
break
}
for _, jsonLogStr := range tailLogLines {
jsonLog, err := convertStringToJson(jsonLogStr)
if err != nil {
return stacktrace.Propagate(err, "An error occurred converting the json log string '%v' into json.", jsonLogStr)
}
if err := strategy.sendJsonLogLine(jsonLog, logsByKurtosisUserServiceUuidChan, serviceUuid, conjunctiveLogLinesFiltersWithRegex); err != nil {
return err
}
}
return nil
}
// Returns the next complete json log string from [logsReader], unless err is reached in which case an incomplete json log line could be returned
func getCompleteJsonLogString(logsReader *bufio.Reader) (string, error) {
var completeJsonLogStr string
for {
jsonLogStr, isJsonEnding, err := getJsonLogString(logsReader)
completeJsonLogStr = completeJsonLogStr + jsonLogStr
// err could be EOF or something else so return the string and err for caller to handle
if err != nil {
return completeJsonLogStr, err
}
// if appended string is valid json ending, can now return a complete json log
if isJsonEnding {
return completeJsonLogStr, nil
}
}
}
// Return the next json log string from [logsReader] and whether the string ends in valid json or not
func getJsonLogString(logsReader *bufio.Reader) (string, bool, error) {
jsonLogStr, err := logsReader.ReadString(volume_consts.NewLineRune)
jsonLogStr = strings.TrimSuffix(jsonLogStr, string(volume_consts.NewLineRune))
return jsonLogStr, isValidJsonEnding(jsonLogStr), err
}
func isValidJsonEnding(line string) bool {
if len(line) < len(volume_consts.EndOfJsonLine) {
return false
}
endOfLine := line[len(line)-len(volume_consts.EndOfJsonLine):]
return endOfLine == volume_consts.EndOfJsonLine
}
func (strategy *PerWeekStreamLogsStrategy) sendJsonLogLine(
jsonLog JsonLog,
logsByKurtosisUserServiceUuidChan chan map[service.ServiceUUID][]logline.LogLine,
serviceUuid service.ServiceUUID,
conjunctiveLogLinesFiltersWithRegex []logline.LogLineFilterWithRegex) error {
// each logLineStr is of the following structure: {"enclave_uuid": "...", "service_uuid":"...", "log": "...",.. "timestamp":"..."}
// eg. {"container_type":"api-container", "container_id":"8f8558ba", "container_name":"/kurtosis-api--ffd",
// "log":"hi","timestamp":"2023-08-14T14:57:49Z"}
// Then extract the actual log message using the vectors log field
logMsgStr, found := jsonLog[volume_consts.LogLabel]
if !found {
return stacktrace.NewError("An error retrieving the log field '%v' from json log: %v\n", volume_consts.LogLabel, jsonLog)
}
// Extract the timestamp using vectors timestamp field
logTimestamp, err := parseTimestampFromJsonLogLine(jsonLog)
if err != nil {
return stacktrace.Propagate(err, "An error occurred parsing timestamp from json log line.")
}
logLine := logline.NewLogLine(logMsgStr, *logTimestamp)
// Then filter by checking if the log message is valid based on requested filters
validLogLine, err := logLine.IsValidLogLineBaseOnFilters(conjunctiveLogLinesFiltersWithRegex)
if err != nil {
return stacktrace.Propagate(err, "An error occurred filtering log line '%+v' using filters '%+v'", logLine, conjunctiveLogLinesFiltersWithRegex)
}
if !validLogLine {
return nil
}
// ensure this log line is within the retention period if it has a timestamp
withinRetentionPeriod, err := strategy.isWithinRetentionPeriod(logLine)
if err != nil {
return stacktrace.Propagate(err, "An error occurred filtering log line '%+v' using filters '%+v'", logLine, conjunctiveLogLinesFiltersWithRegex)
}
if !withinRetentionPeriod {
return nil
}
// send the log line
logLines := []logline.LogLine{*logLine}
userServicesLogLinesMap := map[service.ServiceUUID][]logline.LogLine{
serviceUuid: logLines,
}
logsByKurtosisUserServiceUuidChan <- userServicesLogLinesMap
return nil
}
// Returns true if [logLine] has no timestamp
func (strategy *PerWeekStreamLogsStrategy) isWithinRetentionPeriod(logLine *logline.LogLine) (bool, error) {
retentionPeriod := strategy.time.Now().Add(time.Duration(-volume_consts.LogRetentionPeriodInWeeks) * oneWeek)
timestamp := logLine.GetTimestamp()
return timestamp.After(retentionPeriod), nil
}
// Continue streaming log lines as they are written to log file (tail -f [filepath])
func (strategy *PerWeekStreamLogsStrategy) followLogs(
filepath string,
logsByKurtosisUserServiceUuidChan chan map[service.ServiceUUID][]logline.LogLine,
serviceUuid service.ServiceUUID,
conjunctiveLogLinesFiltersWithRegex []logline.LogLineFilterWithRegex,
) error {
logTail, err := tail.TailFile(filepath, tail.Config{
Location: &tail.SeekInfo{
Offset: 0,
Whence: io.SeekEnd, // start tailing from end of log file
},
ReOpen: false,
MustExist: true,
Poll: false,
Pipe: false,
Follow: true,
MaxLineSize: 0,
RateLimiter: nil,
Logger: logrus.StandardLogger()})
if err != nil {
return stacktrace.Propagate(err, "An error occurred while attempting to tail the log file.")
}
for logLine := range logTail.Lines {
if logLine.Err != nil {
return stacktrace.Propagate(logLine.Err, "hpcloud/tail encountered an error with the following log line: %v", logLine.Text)
}
jsonLog, err := convertStringToJson(logLine.Text)
if err != nil {
// if tail package fails to parse a valid new line, fail fast
return stacktrace.NewError("hpcloud/tail returned the following line: '%v' that was not valid json.\nThis is potentially a bug in tailing package.", logLine.Text)
}
err = strategy.sendJsonLogLine(jsonLog, logsByKurtosisUserServiceUuidChan, serviceUuid, conjunctiveLogLinesFiltersWithRegex)
if err != nil {
return stacktrace.Propagate(err, "An error occurred sending json log line '%v'.", logLine.Text)
}
}
return nil
}
func convertStringToJson(line string) (JsonLog, error) {
var jsonLog JsonLog
if err := json.Unmarshal([]byte(line), &jsonLog); err != nil {
return nil, err
}
return jsonLog, nil
}
// Converts a string in UTC format to a time.Time, returns error if no time is found or time is incorrectly formatted
func parseTimestampFromJsonLogLine(logLine JsonLog) (*time.Time, error) {
timestampStr, found := logLine[volume_consts.TimestampLabel]
if !found {
return nil, stacktrace.NewError("An error occurred retrieving the timestamp field '%v' from json: %v", volume_consts.TimestampLabel, logLine)
}
timestamp, err := time.Parse(time.RFC3339, timestampStr)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred parsing the timestamp string '%v' from UTC to a time.Time object.", timestampStr)
}
return ×tamp, nil
}