-
Notifications
You must be signed in to change notification settings - Fork 9
/
analyzer.go
462 lines (399 loc) · 11.9 KB
/
analyzer.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package analyze
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/jlewi/foyle/app/api"
"github.com/jlewi/foyle/app/pkg/docs"
"github.com/jlewi/foyle/app/pkg/logs"
"github.com/jlewi/foyle/protos/go/foyle/v1alpha1"
"github.com/jlewi/monogo/helpers"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
)
const (
// unsetExitCode is a random negative exit code so we can tell when it hasn't been set.
unsetExitCode = -2377
)
// Analyzer is responsible for analyzing logs.
type Analyzer struct {
}
// NewAnalyzer creates a new Analyzer.
func NewAnalyzer() (*Analyzer, error) {
return &Analyzer{}, nil
}
type ResultFiles struct {
BlockLogs []string
GenerateTraces []string
ExecuteTraces []string
}
// Analyze analyzes the logs.
func (a *Analyzer) Analyze(ctx context.Context, logsDir string, outDir string) (ResultFiles, error) {
log := logs.FromContext(ctx)
log.Info("Analyzing logs", "logsDir", logsDir, "outDir", outDir)
results := initResultFiles(outDir)
if _, err := os.Stat(logsDir); os.IsNotExist(err) {
// Logger won't be setup yet so we can't use it.
log.Info("Creating output directory", "dir", outDir)
err := os.MkdirAll(outDir, 0755)
if err != nil {
return results, errors.Wrapf(err, "could not create log directory %s", outDir)
}
}
jsonFiles, err := findLogFiles(ctx, logsDir)
if err != nil {
return results, err
}
traces, blocks, err := buildTraces(ctx, jsonFiles, results)
if err != nil {
return results, err
}
err = buildBlockLogs(ctx, traces, blocks, results.BlockLogs[0])
return results, err
}
// buildTraces creates a map of all the traces and initializes the blocks.
func buildTraces(ctx context.Context, jsonFiles []string, resultFiles ResultFiles) (map[string]api.Trace, map[string]*api.BlockLog, error) {
log := logs.FromContext(ctx)
// Entries is a mapping from a traceId to a list of logEntries associated with that entry.
traceEntries := make(map[string][]*api.LogEntry)
for _, p := range jsonFiles {
log.Info("Reading file", "path", p)
f, err := os.Open(p)
if err != nil {
log.Error(err, "Error opening file; file will be skipped", "path", p)
continue
}
d := json.NewDecoder(f)
for {
entry := &api.LogEntry{}
err := d.Decode(entry)
if err != nil {
if err == io.EOF {
break
}
log.Error(err, "Error decoding log entry", "path", p)
continue
}
// Ignore log entries without traces
if entry.TraceID() == "" {
continue
}
items, ok := traceEntries[entry.TraceID()]
if !ok {
items = make([]*api.LogEntry, 0, 10)
}
items = append(items, entry)
traceEntries[entry.TraceID()] = items
}
}
// Store a map of all traces
traces := make(map[string]api.Trace)
// Build a map of the blocks
blocks := make(map[string]*api.BlockLog)
// Create encoders to write the traces
genFile, err := os.Create(resultFiles.GenerateTraces[0])
if err != nil {
return nil, nil, err
}
defer helpers.DeferIgnoreError(genFile.Close)
execFile, err := os.Create(resultFiles.ExecuteTraces[0])
if err != nil {
return nil, nil, err
}
defer helpers.DeferIgnoreError(execFile.Close)
genEnc := json.NewEncoder(genFile)
execEnc := json.NewEncoder(execFile)
// Now combine all the entries for each trace
for tid, items := range traceEntries {
log.Info("Combining entries for trace", "traceId", tid, "numEntries", len(items))
trace, err := combineEntriesForTrace(ctx, items)
if err != nil {
log.Error(err, "Error combining entries for trace", "traceId", tid)
continue
}
traces[tid] = trace
var enc *json.Encoder
// Update the blocks associated with this trace
switch t := trace.(type) {
case *api.GenerateTrace:
for _, oBlock := range t.Response.GetBlocks() {
bid := oBlock.GetId()
if bid == "" {
continue
}
block, ok := blocks[bid]
if !ok {
block = &api.BlockLog{
ID: bid,
}
blocks[bid] = block
}
block.GenTraceID = tid
}
enc = genEnc
case *api.ExecuteTrace:
bid := t.Request.GetBlock().GetId()
if bid == "" {
continue
}
block, ok := blocks[bid]
if !ok {
block = &api.BlockLog{
ID: bid,
}
blocks[bid] = block
}
if block.ExecTraceIDs == nil {
block.ExecTraceIDs = make([]string, 0, 10)
}
block.ExecTraceIDs = append(block.ExecTraceIDs, tid)
enc = execEnc
default:
log.Error(fmt.Errorf("Unknown trace type"), "Unknown trace type", "trace", t)
}
if enc != nil {
if err := enc.Encode(trace); err != nil {
log.Error(err, "Error writing trace to output file")
}
}
}
return traces, blocks, nil
}
func findLogFiles(ctx context.Context, logsDir string) ([]string, error) {
log := logs.FromContext(ctx)
jsonFiles := []string{}
paths := map[string]bool{}
if _, err := os.Stat(logsDir); err != nil && os.IsNotExist(err) {
return jsonFiles, fmt.Errorf("Analyze invoked for non-existent path: %v", logsDir)
}
// Walk the directory and add all JSON files.
walkErr := filepath.Walk(logsDir,
func(path string, info os.FileInfo, walkErr error) error {
// Skip non YAML files
ext := strings.ToLower(filepath.Ext(info.Name()))
if ext != ".json" && ext != ".jsonl" {
return nil
}
p, err := filepath.EvalSymlinks(path)
if err != nil {
log.Error(err, "Failed to evaluate symlink", "path", path)
return err
}
paths[p] = true
return nil
})
if walkErr != nil {
return jsonFiles, walkErr
}
for p := range paths {
jsonFiles = append(jsonFiles, p)
}
sort.Strings(jsonFiles)
return jsonFiles, nil
}
func initResultFiles(outDir string) ResultFiles {
stamp := time.Now().Format("2006-01-02T15:04:05")
return ResultFiles{
BlockLogs: []string{filepath.Join(outDir, fmt.Sprintf("blocks.logs.%s.jsonl", stamp))},
GenerateTraces: []string{filepath.Join(outDir, fmt.Sprintf("traces.generate.%s.jsonl", stamp))},
ExecuteTraces: []string{filepath.Join(outDir, fmt.Sprintf("traces.execute.%s.jsonl", stamp))},
}
}
func buildBlockLogs(ctx context.Context, traces map[string]api.Trace, blocks map[string]*api.BlockLog, outFile string) error {
log := logs.FromContext(ctx)
oDir := filepath.Dir(outFile)
if _, err := os.Stat(oDir); os.IsNotExist(err) {
log.Info("Creating directory for block logs", "dir", oDir)
err := os.MkdirAll(oDir, 0755)
if err != nil {
return errors.Wrapf(err, "could not log directory %s", oDir)
}
}
// Now we can process each block and write the combined entries to the output file.
oFile, err := os.Create(outFile)
if err != nil {
return err
}
defer helpers.DeferIgnoreError(oFile.Close)
enc := json.NewEncoder(oFile)
for bid, blockLog := range blocks {
log.Info("Combining entries for block", "blockId", bid)
if err := buildBlockLog(ctx, blockLog, traces); err != nil {
log.Error(err, "Error combining entries for block", "blockId", bid)
continue
}
if err := enc.Encode(blockLog); err != nil {
log.Error(err, "Error writing example to output file")
}
}
return nil
}
func buildBlockLog(ctx context.Context, block *api.BlockLog, traces map[string]api.Trace) error {
log := logs.FromContext(ctx)
log = log.WithValues("blockId", block.ID)
log.Info("Building block log", "block", block)
if block.ID == "" {
return errors.New("Block ID is required")
}
if block.GenTraceID != "" {
genTrace, ok := traces[block.GenTraceID].(*api.GenerateTrace)
if !ok {
log.Error(errors.New("Missing GenerateTrace for traceId"), "Error getting generate trace", "genTraceId", block.GenTraceID)
} else {
block.Doc = genTrace.Request.GetDoc()
}
// If the block was generated as part of evaluation mode then consider it to be in evaluation mode.
if genTrace.EvalMode {
block.EvalMode = true
}
// Find the actual block
for _, b := range genTrace.Response.GetBlocks() {
if b.GetId() == block.ID {
block.GeneratedBlock = b
break
}
}
if block.GeneratedBlock == nil {
log.Error(errors.New("Failed to find generated block"), "Error finding generated block", "blockId", block.ID)
}
}
var lastTrace *api.ExecuteTrace
// Get the last execution trace
for _, tid := range block.ExecTraceIDs {
trace, ok := traces[tid].(*api.ExecuteTrace)
if !ok {
log.Error(errors.New("Missing ExecuteTrace for traceId"), "Error getting execute trace", "execTraceId", tid)
continue
}
if lastTrace == nil {
lastTrace = trace
continue
}
if lastTrace.StartTime.Before(trace.StartTime) {
lastTrace = trace
}
}
if lastTrace != nil {
// If the block was executed as part of evaluation mode then consider it to be in evaluation mode.
if lastTrace.EvalMode {
block.EvalMode = true
}
block.ExecutedBlock = lastTrace.Request.GetBlock()
block.ExitCode = unsetExitCode
for _, o := range lastTrace.Response.GetOutputs() {
exitCode, ok := docs.GetExitCode(o)
if ok {
block.ExitCode = exitCode
break
}
}
}
return nil
}
func combineEntriesForTrace(ctx context.Context, entries []*api.LogEntry) (api.Trace, error) {
// First sort the entries by timestamp.
sort.Slice(entries, func(i, j int) bool {
return entries[i].Time().Before(entries[j].Time())
})
// Loop through the entries until we identify the message that tells us what kind of trace it is.
for _, logEntry := range entries {
function := logEntry.Function()
if strings.HasSuffix(function, "agent.(*Agent).Generate") {
return combineGenerateTrace(ctx, entries)
}
if strings.HasSuffix(function, "executor.(*Executor).Execute") {
return combineExecuteTrace(ctx, entries)
}
}
return nil, errors.New("Failed to identify trace type")
}
func combineGenerateTrace(ctx context.Context, entries []*api.LogEntry) (*api.GenerateTrace, error) {
trace := &api.GenerateTrace{}
evalMode := false
for _, e := range entries {
if trace.TraceID == "" {
trace.TraceID = e.TraceID()
}
if mode, present := e.EvalMode(); present {
// If any of the entries are marked as true then we will consider the trace to be in eval mode.
// We don't want to assume that the evalMode will be set on all log entries in the trace.
// So the logic is to assume its not eval mode by default and then set it to eval mode if we find
// One entry that is marked as eval mode.
if mode {
evalMode = mode
}
}
if trace.Request == nil {
raw := e.Request()
if raw != nil {
request := &v1alpha1.GenerateRequest{}
if err := protojson.Unmarshal([]byte(raw), request); err != nil {
return nil, err
}
trace.Request = request
trace.StartTime = e.Time()
}
}
if trace.Response == nil {
raw := e.Response()
if raw != nil {
v := &v1alpha1.GenerateResponse{}
if err := protojson.Unmarshal([]byte(raw), v); err != nil {
return nil, err
}
trace.Response = v
trace.EndTime = e.Time()
}
}
}
trace.EvalMode = evalMode
return trace, nil
}
func combineExecuteTrace(ctx context.Context, entries []*api.LogEntry) (*api.ExecuteTrace, error) {
trace := &api.ExecuteTrace{}
evalMode := false
for _, e := range entries {
if trace.TraceID == "" {
trace.TraceID = e.TraceID()
}
if mode, present := e.EvalMode(); present {
// If any of the entries are marked as true then we will consider the trace to be in eval mode.
// We don't want to assume that the evalMode will be set on all log entries in the trace.
// So the logic is to assume its not eval mode by default and then set it to eval mode if we find
// One entry that is marked as eval mode.
if mode {
evalMode = mode
}
}
if trace.Request == nil {
raw := e.Request()
if raw != nil {
request := &v1alpha1.ExecuteRequest{}
if err := protojson.Unmarshal([]byte(raw), request); err != nil {
return nil, err
}
trace.Request = request
trace.StartTime = e.Time()
}
}
if trace.Response == nil {
raw := e.Response()
if raw != nil {
v := &v1alpha1.ExecuteResponse{}
if err := protojson.Unmarshal([]byte(raw), v); err != nil {
return nil, err
}
trace.Response = v
trace.EndTime = e.Time()
}
}
}
trace.EvalMode = evalMode
return trace, nil
}