-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathlogv2.go
More file actions
607 lines (573 loc) · 17.3 KB
/
Copy pathlogv2.go
File metadata and controls
607 lines (573 loc) · 17.3 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* logv2.go
*/
package hatchet
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/simagix/gox"
"go.mongodb.org/mongo-driver/bson"
)
const (
COLLSCAN = "COLLSCAN"
DOLLAR_CMD = "$cmd"
LIMIT = 100
TOP_N = 23
)
var instance *Logv2
// GetLogv2 returns Logv2 instance
func GetLogv2() *Logv2 {
if instance == nil {
instance = &Logv2{url: SQLITE3_FILE}
}
return instance
}
// Logv2 keeps Logv2 object
type Logv2 struct {
buildInfo map[string]interface{}
cacheSize int
from time.Time
logname string
legacy bool
hatchetName string
isDigest bool
merge bool
s3client *S3Client
testing bool //test mode
to time.Time
totalLines int
url string // connection string
user string
verbose bool
version string
}
// Logv2Info stores logv2 struct
type Logv2Info struct {
Attr bson.D `json:"attr" bson:"attr"`
Component string `json:"c" bson:"c"`
Context string `json:"ctx" bson:"ctx"`
ID int `json:"id" bson:"id"`
Msg string `json:"msg" bson:"msg"`
Severity string `json:"s" bson:"s"`
Timestamp time.Time `json:"t" bson:"t"`
Attributes Attributes
Message string // remaining legacy message
Client *RemoteClient
Marker int
}
type Attributes struct {
AppName string `json:"appName" bson:"appName"`
Command map[string]interface{} `json:"command" bson:"command"`
ErrMsg string `json:"errMsg" bson:"errMsg"`
Milli int `json:"durationMillis" bson:"durationMillis"`
NS string `json:"ns" bson:"ns"`
OriginatingCommand map[string]interface{} `json:"originatingCommand" bson:"originatingCommand"`
PlanSummary string `json:"planSummary" bson:"planSummary"`
Reslen int `json:"reslen" bson:"reslen"`
Type string `json:"type" bson:"type"`
}
type RemoteClient struct {
Accepted int `json:"accepted" bson:"accepted"`
Conns int `json:"conns" bson:"conns"`
Ended int `json:"ended" bson:"ended"`
IP string `json:"value" bson:"ip"`
Port string `json:"port" bson:"port"`
Driver string `bsno:"driver"` // driver name
Version string `bsno:"version"` // driver version
}
// OpStat stores performance data
type OpStat struct {
AvgMilli float64 `json:"avg_ms" bson:"avg_ms"` // avg millisecond
Count int `json:"count" bson:"count"` // number of ops
Index string `json:"index" bson:"index"` // index used
MaxMilli int `json:"max_ms" bson:"max_ms"` // max millisecond
Namespace string `json:"ns" bson:"ns"` // database.collectin
Op string `json:"op" bson:"op"` // count, delete, find, remove, and update
QueryPattern string `json:"query_pattern" bson:"query_pattern"` // query pattern
Reslen int `json:"total_reslen" bson:"total_reslen"` // total reslen
TotalMilli int `json:"total_ms" bson:"total_ms"` // total milliseconds
Marker int
}
type LegacyLog struct {
Timestamp string `json:"date" bson:"date"`
Severity string `json:"severity" bson:"severity"`
Component string `json:"component" bson:"component"`
Context string `json:"context" bson:"context"`
Marker int
Message string `json:"message" bson:"message"` // remaining legacy message
}
type HatchetInfo struct {
Arch string `bson:"arch"`
End string `bson:"end"`
Merge bool `bson:"merge"`
Module string `bson:"module"`
Name string `bson:"name"`
OS string `bson:"os"`
Start string `bson:"start"`
Version string `bson:"version"`
Drivers []map[string]string
Provider string `bson:"region"`
Region string `bson:"provider"`
}
func (ptr *Logv2) GetDBType() int {
if strings.HasPrefix(ptr.url, "mongodb://") || strings.HasPrefix(ptr.url, "mongodb+srv://") {
return Mongo
}
return SQLite3
}
// isMongoDBLog checks if a file is a MongoDB log by reading and validating the first line
func isMongoDBLog(filename string) bool {
file, err := os.Open(filename)
if err != nil {
return false
}
defer file.Close()
reader, err := gox.NewReader(file)
if err != nil {
return false
}
// Read first non-empty line
for {
line, _, err := reader.ReadLine()
if err != nil {
return false
}
if len(line) == 0 {
continue
}
// Try to parse as MongoDB logv2 JSON
doc := Logv2Info{}
if err := bson.UnmarshalExtJSON(line, false, &doc); err != nil {
return false
}
// Check for required logv2 fields: timestamp and severity
return !doc.Timestamp.IsZero() && doc.Severity != ""
}
}
// Analyze analyzes logs from a file or directory (1 level only)
func (ptr *Logv2) Analyze(logname string, marker int) error {
// Check if input is a directory
fileInfo, err := os.Stat(logname)
if err != nil {
return err
}
if fileInfo.IsDir() {
// Process directory (1 level only, no recursion)
entries, err := os.ReadDir(logname)
if err != nil {
return err
}
// Get existing names once, then track new names locally to avoid DB query timing issues
existingNames, _ := GetExistingHatchetNames()
processedNames := make([]string, 0)
fileCount := 0
for _, entry := range entries {
if entry.IsDir() {
continue // skip subdirectories
}
name := entry.Name()
if strings.HasPrefix(name, ".") {
continue // skip hidden files
}
fullPath := filepath.Join(logname, name)
// Check if file is a MongoDB log by reading first line
if !isMongoDBLog(fullPath) {
log.Printf("skipping %s (not a MongoDB log)", name)
continue
}
fileCount++
// Generate unique name using combined existing + processed names
allNames := append(existingNames, processedNames...)
hatchetName := getUniqueHatchetName(fullPath, allNames)
processedNames = append(processedNames, hatchetName)
// Create a new Logv2 instance for each file to avoid state issues
fileLogv2 := &Logv2{
hatchetName: hatchetName,
url: ptr.url,
legacy: ptr.legacy,
from: ptr.from,
to: ptr.to,
}
if err := fileLogv2.Analyze(fullPath, 0); err != nil { // marker=0 to skip name regeneration
log.Printf("error processing %s: %v", fullPath, err)
// continue with other files
}
// Print summary for each file
if !fileLogv2.legacy {
fileLogv2.PrintSummary()
}
}
if fileCount == 0 {
log.Printf("no MongoDB log files found in directory %s", logname)
}
// Clear hatchetName so caller knows not to print summary again
ptr.hatchetName = ""
return nil
}
var buf []byte
var file *os.File
var reader *bufio.Reader
ptr.logname = logname
// Generate unique hatchet name for each file when not merging
// marker=0: name pre-set by directory handler or upload handler, skip regeneration
// marker>=1: command line files, generate name for each
if !ptr.merge && marker > 0 {
existingNames, _ := GetExistingHatchetNames()
ptr.hatchetName = getUniqueHatchetName(ptr.logname, existingNames)
}
if !ptr.legacy {
log.Println("processing", logname)
log.Println("hatchet name is", ptr.hatchetName)
}
if ptr.s3client != nil {
var buf []byte
if buf, err = ptr.s3client.GetObject(logname); err != nil {
return err
}
if reader, err = GetBufioReader(buf); err != nil {
return err
}
} else if strings.HasPrefix(logname, "http://") || strings.HasPrefix(logname, "https://") {
var username, password string
if ptr.user != "" {
toks := strings.Split(ptr.user, ":")
if len(toks) == 2 {
username = toks[0]
password = toks[1]
}
}
if ptr.isDigest {
if reader, err = GetHTTPDigestContent(logname, username, password); err != nil {
return err
}
} else {
if reader, err = GetHTTPContent(logname, username, password); err != nil {
return err
}
}
} else {
if file, err = os.Open(logname); err != nil {
return err
}
defer file.Close()
if reader, err = gox.NewReader(file); err != nil {
// EOF from NewReader is expected for empty files, so don't treat it as an error
if errors.Is(err, io.EOF) {
// For empty files, create a simple reader and continue
if _, err = file.Seek(0, 0); err != nil {
return err
}
reader = bufio.NewReader(file)
ptr.totalLines = 0
} else {
return err
}
} else if !ptr.legacy {
log.Println("fast counting", logname, "...")
ptr.totalLines, _ = gox.CountLines(reader)
log.Println("counted", ptr.totalLines, "lines")
if _, err = file.Seek(0, 0); err != nil {
return err
}
if reader, err = gox.NewReader(file); err != nil {
// EOF from NewReader is expected for empty files
if errors.Is(err, io.EOF) {
if _, err = file.Seek(0, 0); err != nil {
return err
}
reader = bufio.NewReader(file)
} else {
return err
}
}
}
}
var isPrefix bool
index := 0
var start, end string
var dbase Database
var mu sync.Mutex // protects buildInfo, start, and end
var dbMu sync.Mutex // protects database operations (SQLite prepared statements aren't thread-safe)
if !ptr.legacy {
if dbase, err = GetDatabase(ptr.hatchetName); err != nil {
return err
}
defer dbase.Close()
if err = dbase.Begin(); err != nil {
return err
}
}
threads := runtime.NumCPU() - 1
if threads == 0 {
threads = 1
}
log.Printf("using %v threads\n", threads)
failedMap := FailedMessages{counters: map[string]int{}}
var wg = gox.NewWaitGroup(threads)
var lineBuf bytes.Buffer // Reusable buffer for multi-line entries
for {
if !ptr.testing && !ptr.legacy && index%50 == 0 && ptr.totalLines > 0 {
fmt.Fprintf(os.Stderr, "\r%3d%% \r", (100*index)/ptr.totalLines)
}
if buf, isPrefix, err = reader.ReadLine(); err != nil { // 0x0A separator = newline
break
}
index++
if len(buf) == 0 {
log.Println("line", index, "is blank.")
continue
}
var str string
if !isPrefix {
// Fast path: single line, no allocation needed beyond the string conversion
str = string(buf)
} else {
// Multi-line entry: use buffer to avoid repeated string concatenation
lineBuf.Reset()
lineBuf.Write(buf)
for isPrefix {
var bbuf []byte
if bbuf, isPrefix, err = reader.ReadLine(); err != nil {
// EOF in the inner loop means incomplete line, which is an error
if errors.Is(err, io.EOF) {
err = fmt.Errorf("unexpected EOF while reading multi-line prefix")
}
break
}
lineBuf.Write(bbuf)
}
str = lineBuf.String()
}
// If we got an error in the inner loop, break from outer loop
if err != nil {
break
}
wg.Add(1)
go func(index int, instr string, marker int) {
defer wg.Done()
var localErr error // Use local error variable to avoid race condition
doc := Logv2Info{}
if localErr = bson.UnmarshalExtJSON([]byte(instr), false, &doc); localErr != nil {
log.Println("error UnmarshalExtJSON line", index, localErr)
return
}
doc.Marker = marker
if localErr = SetRawJSONMessage(&doc, instr); localErr != nil {
log.Println("error SetRawJSONMessage line", index, localErr)
return
}
// Protect buildInfo access with mutex
mu.Lock()
if ptr.buildInfo == nil && doc.Msg == "Build Info" {
attrMap := BsonD2M(doc.Attr)
ptr.buildInfo = attrMap["buildInfo"].(bson.M)
}
buildInfoCopy := ptr.buildInfo
mu.Unlock()
if buildInfoCopy != nil && (doc.Timestamp.Before(ptr.from) || doc.Timestamp.After(ptr.to)) {
return
}
if ptr.legacy {
dt := getDateTimeStr(doc.Timestamp)
logstr := fmt.Sprintf("%v %-2s %-8s [%v] %v", dt,
doc.Severity, doc.Component, doc.Context, doc.Message)
if !ptr.testing {
fmt.Println(logstr)
}
return
}
stat, _ := AnalyzeSlowOp(&doc)
docEnd := getDateTimeStr(doc.Timestamp)
// Protect start and end access with mutex
mu.Lock()
if start == "" {
start = docEnd
}
end = docEnd
mu.Unlock()
// Serialize database operations - SQLite prepared statements aren't thread-safe
if localErr = insertLogData(dbase, &dbMu, index, docEnd, &doc, stat); localErr != nil {
log.Println("error inserting log data line", index, localErr)
return
}
failed := " failed"
if strings.Contains(doc.Message, failed) {
n := strings.Index(doc.Message, failed) + len(failed)
failedMap.inc(doc.Message[:n])
}
}(index, str, marker)
}
// EOF is expected when we've finished reading the file, so don't treat it as an error
if errors.Is(err, io.EOF) {
err = nil
} else if err != nil {
return err
}
wg.Wait()
log.Println("completed parsing logs")
if !ptr.testing && !ptr.legacy {
fmt.Fprintf(os.Stderr, "\r \r")
}
if ptr.legacy {
return nil
}
if err = dbase.Commit(); err != nil {
log.Println("error commit", err)
return err
}
if err = dbase.InsertFailedMessages(&failedMap); err != nil {
log.Println("error insert failed messages", err)
return err
}
info := HatchetInfo{Start: start, End: end, Merge: ptr.merge}
if ptr.buildInfo != nil {
if ptr.buildInfo["environment"] != nil {
env := ptr.buildInfo["environment"].(bson.M)
info.Arch, _ = env["distarch"].(string)
info.OS, _ = env["distmod"].(string)
}
if modules, ok := ptr.buildInfo["modules"].(bson.A); ok {
if len(modules) > 0 {
info.Module, _ = modules[0].(string)
}
}
info.Version, _ = ptr.buildInfo["version"].(string)
}
if err = dbase.UpdateHatchetInfo(info); err != nil {
log.Println("error update Hatchet info", err)
return err
}
return nil
}
// insertLogData serializes database operations with a mutex.
// SQLite prepared statements within a transaction aren't thread-safe.
func insertLogData(dbase Database, dbMu *sync.Mutex, index int, docEnd string, doc *Logv2Info, stat *OpStat) error {
dbMu.Lock()
defer dbMu.Unlock()
if err := dbase.InsertLog(index, docEnd, doc, stat); err != nil {
return err
}
if doc.Client != nil {
if (doc.Client.Accepted + doc.Client.Ended) > 0 { // record connections
if err := dbase.InsertClientConn(index, doc); err != nil {
return err
}
} else if doc.Client.Driver != "" {
if isAppDriver(doc.Client) {
if err := dbase.InsertDriver(index, doc); err != nil {
return err
}
}
}
}
return nil
}
func (ptr *Logv2) PrintSummary() error {
// Skip if no hatchet name (e.g., after directory processing)
if ptr.hatchetName == "" {
return nil
}
dbase, err := GetDatabase(ptr.hatchetName)
if err != nil {
return err
}
if err = dbase.CreateMetaData(); err != nil {
log.Println("error create metadata", err)
return err
}
log.Println(GetHatchetSummary(dbase.GetHatchetInfo()))
summaries := []string{}
var buffer bytes.Buffer
buffer.WriteString("\r+----------+--------+------+------+---------------------------------+----------------------------------------------------+\n")
buffer.WriteString(fmt.Sprintf("| Command |COLLSCAN|avg ms| Count| %-32s| %-50s |\n", "Namespace", "Query Pattern"))
buffer.WriteString("|----------+--------+------+------+---------------------------------+----------------------------------------------------|\n")
var ops []OpStat
if ops, err = dbase.GetSlowOps("avg_ms", "DESC", false); err != nil {
return err
}
lines := 5
for count, value := range ops {
if count > lines {
break
}
str := value.QueryPattern
if len(value.Op) > 10 {
value.Op = value.Op[:10]
}
if len(value.Namespace) > 33 {
length := len(value.Namespace)
value.Namespace = value.Namespace[:1] + "*" + value.Namespace[(length-31):]
}
if len(str) > 60 {
str = value.QueryPattern[:50]
idx := strings.LastIndex(str, " ")
if idx > 0 {
str = value.QueryPattern[:idx]
}
}
output := ""
collscan := ""
if value.Index == COLLSCAN {
collscan = COLLSCAN
}
output = fmt.Sprintf("|%-10s %8s %6d %6d %-33s %-52s|\n", value.Op, collscan,
int(value.AvgMilli), value.Count, value.Namespace, str)
buffer.WriteString(output)
if len(value.QueryPattern) > 60 {
remaining := value.QueryPattern[len(str):]
for i := 0; i < len(remaining); i += 60 {
epos := i + 60
var pstr string
if epos > len(remaining) {
epos = len(remaining)
pstr = remaining[i:epos]
} else {
str = strings.Trim(remaining[i:epos], " ")
idx := strings.LastIndex(str, " ")
if idx >= 0 {
pstr = str[:idx]
i -= (60 - idx)
} else {
pstr = str
i -= (60 - len(str))
}
}
output = fmt.Sprintf("|%74s %-52s|\n", " ", pstr)
buffer.WriteString(output)
}
}
if value.Index != "" && value.Index != COLLSCAN {
output = fmt.Sprintf("|...index: %-110s|\n", value.Index)
buffer.WriteString(output)
}
}
buffer.WriteString("+----------+--------+------+------+---------------------------------+----------------------------------------------------+")
summaries = append(summaries, buffer.String())
if lines < len(ops) {
summaries = append(summaries,
fmt.Sprintf(`+ %v: slowest %d of %d ops displayed`, ptr.hatchetName, lines+1, len(ops)))
}
fmt.Println(strings.Join(summaries, "\n"))
return err
}
func isAppDriver(client *RemoteClient) bool {
driver := client.Driver
version := client.Version
if strings.HasPrefix(driver, "NetworkInterfaceTL") || driver == "MongoDB Internal Client" {
return false
} else if driver == "mongo-go-driver" && strings.HasSuffix(version, "-cloud") {
return false
}
return true
}