-
Notifications
You must be signed in to change notification settings - Fork 18
/
logs.go
319 lines (279 loc) · 9.37 KB
/
logs.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
package executor
import (
"bufio"
"bytes"
"context"
"fmt"
"github.com/avast/retry-go/v4"
"github.com/cirruslabs/cirrus-cli/internal/agent/client"
"github.com/cirruslabs/cirrus-cli/internal/agent/environment"
"github.com/cirruslabs/cirrus-cli/pkg/api"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
"io"
"log"
"os"
"sync"
"time"
)
type LogUploader struct {
taskIdentification *api.TaskIdentification
commandName string
client api.CirrusCIService_StreamLogsClient
storedOutput *os.File
erroredChunks int
logsChannel chan []byte
doneLogUpload chan bool
env *environment.Environment
closed bool
// Fields related to the CIRRUS_LOG_TIMESTAMP behavioral environment variable
LogTimestamps bool
GetTimestamp func() time.Time
OweTimestamp bool
mutex sync.RWMutex
}
func NewLogUploader(ctx context.Context, executor *Executor, commandName string) (*LogUploader, error) {
logClient, err := InitializeLogStreamClient(ctx, executor.taskIdentification, commandName, false)
if err != nil {
return nil, err
}
EnsureFolderExists(os.TempDir())
file, err := os.CreateTemp(os.TempDir(), commandName)
if err != nil {
return nil, err
}
logUploader := LogUploader{
taskIdentification: executor.taskIdentification,
commandName: commandName,
client: logClient,
storedOutput: file,
erroredChunks: 0,
logsChannel: make(chan []byte, 128),
doneLogUpload: make(chan bool),
env: executor.env,
closed: false,
LogTimestamps: executor.env.Get("CIRRUS_LOG_TIMESTAMP") == "true",
GetTimestamp: time.Now,
OweTimestamp: true,
}
go logUploader.StreamLogs()
return &logUploader, nil
}
func (uploader *LogUploader) reInitializeClient(ctx context.Context) error {
err := uploader.client.CloseSend()
if err != nil {
log.Printf("Failed to close log for %s for reinitialization: %s\n", uploader.commandName, err.Error())
}
logClient, err := InitializeLogStreamClient(ctx, uploader.taskIdentification, uploader.commandName, false)
if err != nil {
return err
}
uploader.client = logClient
return nil
}
func (uploader *LogUploader) WithTimestamps(input []byte) []byte {
var result []byte
timestampPrefix := uploader.GetTimestamp().Format("[15:04:05.000]") + " "
// Insert a timestamp if we owe one, either because it's
// the first log chunk in the stream or because the previous
// chunk was ending with \n
if uploader.OweTimestamp {
result = append(result, []byte(timestampPrefix)...)
uploader.OweTimestamp = false
}
// How many \n's are going to get a timestamp prefix
numTimestamps := bytes.Count(input, []byte{'\n'})
// If the chunk ends with \n — don't insert the timestamp at the end
// right now, but remember to do this in the future to avoid empty
// lines with timestamps at the log's end
if bytes.HasSuffix(input, []byte{'\n'}) {
numTimestamps--
uploader.OweTimestamp = true
}
// Insert timestamps
input = bytes.Replace(input, []byte("\n"), []byte("\n"+timestampPrefix), numTimestamps)
result = append(result, input...)
return result
}
func (uploader *LogUploader) Write(bytes []byte) (int, error) {
if len(bytes) == 0 {
return 0, nil
}
// Make potential bytes expansion below transparent to the caller
originalLen := len(bytes)
if uploader.LogTimestamps {
bytes = uploader.WithTimestamps(bytes)
}
uploader.mutex.RLock()
defer uploader.mutex.RUnlock()
if !uploader.closed {
bytesCopy := make([]byte, len(bytes))
copy(bytesCopy, bytes)
uploader.logsChannel <- bytesCopy
}
return originalLen, nil
}
func (uploader *LogUploader) StreamLogs() {
ctx := context.Background()
for {
logs, finished := uploader.ReadAvailableChunks()
_, err := uploader.WriteChunk(logs)
if finished {
log.Printf("Finished streaming logs for %s!\n", uploader.commandName)
break
}
if err == io.EOF {
log.Printf("Got EOF while streaming logs for %s! Trying to reinitilize logs uploader...\n", uploader.commandName)
err := uploader.reInitializeClient(ctx)
if err == nil {
log.Printf("Successfully reinitilized log uploader for %s!\n", uploader.commandName)
} else {
log.Printf("Failed to reinitilized log uploader for %s: %s\n", uploader.commandName, err.Error())
}
}
}
uploader.client.CloseAndRecv()
err := uploader.UploadStoredOutput(ctx)
if err != nil {
log.Printf("Failed to upload stored logs for %s: %s", uploader.commandName, err.Error())
} else {
log.Printf("Uploaded stored logs for %s!", uploader.commandName)
}
uploader.storedOutput.Close()
os.Remove(uploader.storedOutput.Name())
uploader.doneLogUpload <- true
}
func (uploader *LogUploader) ReadAvailableChunks() ([]byte, bool) {
const maxBytesPerInvocation = 1 * 1024 * 1024
// Make sure we wait first to avoid busy loop in StreamLogs()
result := <-uploader.logsChannel
// Read log chunks from the channel, but no more than maxBytesPerInvocation bytes
//
// This assumes that log chunks are small by themselves (e.g. 32,000 bytes).
for {
select {
case nextChunk, more := <-uploader.logsChannel:
result = append(result, nextChunk...)
if !more {
log.Printf("No more log chunks for %s\n", uploader.commandName)
return result, true
}
default:
return result, false
}
if len(result) > maxBytesPerInvocation {
return result, false
}
}
}
func (uploader *LogUploader) WriteChunk(bytesToWrite []byte) (int, error) {
if len(bytesToWrite) == 0 {
return 0, nil
}
for _, valueToMask := range uploader.env.SensitiveValues() {
bytesToWrite = bytes.Replace(bytesToWrite, []byte(valueToMask), []byte("HIDDEN-BY-CIRRUS-CI"), -1)
}
uploader.storedOutput.Write(bytesToWrite)
dataChunk := api.DataChunk{Data: bytesToWrite}
logEntry := api.LogEntry_Chunk{Chunk: &dataChunk}
err := uploader.client.Send(&api.LogEntry{Value: &logEntry})
if err != nil {
log.Printf("Failed to send logs! %s For %s", err.Error(), string(bytesToWrite))
uploader.erroredChunks++
return 0, err
}
return len(bytesToWrite), nil
}
func (uploader *LogUploader) Finalize() {
log.Printf("Finalizing log uploading for %s!\n", uploader.commandName)
uploader.mutex.Lock()
uploader.closed = true
close(uploader.logsChannel)
uploader.mutex.Unlock()
<-uploader.doneLogUpload
}
func (uploader *LogUploader) UploadStoredOutput(ctx context.Context) error {
logClient, err := InitializeLogSaveClient(ctx, uploader.taskIdentification, uploader.commandName, true)
if err != nil {
return err
}
defer logClient.CloseAndRecv()
if uploader.commandName == "test_unexpected_error_during_log_streaming" {
dataChunk := api.DataChunk{Data: []byte("Live streaming of logs failed!\n")}
logEntry := api.LogEntry_Chunk{Chunk: &dataChunk}
err = logClient.Send(&api.LogEntry{Value: &logEntry})
if err != nil {
return err
}
}
uploader.storedOutput.Seek(0, io.SeekStart)
readBufferSize := int(1024 * 1024)
readBuffer := make([]byte, readBufferSize)
bufferedReader := bufio.NewReaderSize(uploader.storedOutput, readBufferSize)
for {
n, err := bufferedReader.Read(readBuffer)
if n > 0 {
dataChunk := api.DataChunk{Data: readBuffer[:n]}
logEntry := api.LogEntry_Chunk{Chunk: &dataChunk}
err = logClient.Send(&api.LogEntry{Value: &logEntry})
}
if err == io.EOF || n == 0 {
break
}
if err != nil {
return err
}
}
return nil
}
func InitializeLogStreamClient(ctx context.Context, taskIdentification *api.TaskIdentification, commandName string, raw bool) (api.CirrusCIService_StreamLogsClient, error) {
var streamLogClient api.CirrusCIService_StreamLogsClient
var err error
err = retry.Do(func() error {
streamLogClient, err = client.CirrusClient.StreamLogs(ctx, grpc.UseCompressor(gzip.Name))
return err
}, retry.Delay(5*time.Second), retry.Attempts(3), retry.Context(ctx))
if err != nil {
log.Printf("Failed to start streaming logs for %s! %s", commandName, err.Error())
request := api.ReportAgentProblemRequest{
TaskIdentification: taskIdentification,
Message: fmt.Sprintf("Failed to start streaming logs for command %v: %v", commandName, err),
}
client.CirrusClient.ReportAgentWarning(ctx, &request)
return nil, err
}
logEntryKey := api.LogEntry_LogKey{TaskIdentification: taskIdentification, CommandName: commandName, Raw: raw}
logEntry := api.LogEntry_Key{Key: &logEntryKey}
streamLogClient.Send(&api.LogEntry{Value: &logEntry})
return streamLogClient, nil
}
func InitializeLogSaveClient(
ctx context.Context,
taskIdentification *api.TaskIdentification,
commandName string,
raw bool,
) (api.CirrusCIService_SaveLogsClient, error) {
var streamLogClient api.CirrusCIService_StreamLogsClient
var err error
err = retry.Do(
func() error {
streamLogClient, err = client.CirrusClient.SaveLogs(ctx, grpc.UseCompressor(gzip.Name))
return err
},
retry.Delay(5*time.Second),
retry.Attempts(3),
)
if err != nil {
log.Printf("Failed to start saving logs for %s! %s", commandName, err.Error())
request := api.ReportAgentProblemRequest{
TaskIdentification: taskIdentification,
Message: fmt.Sprintf("Failed to start saving logs for command %v: %v", commandName, err),
}
client.CirrusClient.ReportAgentWarning(ctx, &request)
return nil, err
}
logEntryKey := api.LogEntry_LogKey{TaskIdentification: taskIdentification, CommandName: commandName, Raw: raw}
logEntry := api.LogEntry_Key{Key: &logEntryKey}
streamLogClient.Send(&api.LogEntry{Value: &logEntry})
return streamLogClient, nil
}