-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
284 lines (246 loc) · 7.55 KB
/
main.go
File metadata and controls
284 lines (246 loc) · 7.55 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
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"os"
"path"
"regexp"
"runtime/pprof"
"strings"
"github.com/newrelic/node-log-viewer/internal/database"
log "github.com/newrelic/node-log-viewer/internal/log"
"github.com/newrelic/node-log-viewer/internal/misc"
"github.com/newrelic/node-log-viewer/internal/tui"
v0 "github.com/newrelic/node-log-viewer/internal/v0"
"github.com/spf13/afero"
flag "github.com/spf13/pflag"
)
var exitStatus int
var fs = afero.NewOsFs()
var logger *log.Logger
var db *database.LogsDatabase
func main() {
exitStatus = 0
err := run(os.Args)
if err != nil {
fmt.Printf("app error: %v\n", err)
exitStatus = 1
}
os.Exit(exitStatus)
}
func run(args []string) error {
err := createAndParseFlags(args)
if err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if flags.Version == true {
misc.Version()
return nil
}
if flags.CpuProfile != "" {
cpuProfileOut, err := fs.Create(flags.CpuProfile)
if err != nil {
return fmt.Errorf("could not create cpu profile out file `%s`: %w", flags.CpuProfile, err)
}
pprof.StartCPUProfile(cpuProfileOut)
defer pprof.StopCPUProfile()
}
logLevel := slog.LevelInfo
if flags.LogLevel.String() != "" {
logLevel = flags.LogLevel.ToLeveler().Level()
}
logger, _ = log.New(log.WithLevel(logLevel))
logger.Debug("app info", "flags", flags.String(), "pid", os.Getpid())
db, err = initializeDatabase(logger)
logger.Info("cache file created", "cache-file", db.DatabaseFile)
defer shutdownDatabase(db, logger)
// TODO: load input file in a gofunc so we can render a progress bar
hasCachedLogs, err := db.HasCachedLogs()
if err != nil {
logger.Error("could not verify cache", "error", err)
return err
}
if hasCachedLogs == false {
logger.Trace("no cached logs found")
var inputFile io.ReadCloser
switch {
case flags.InputFile != "":
logger.Debug("attempting to parse log file (-f)", "log-file", flags.InputFile)
inputFile, err = openLogFile(flags.InputFile, logger)
case len(flags.PositionalArgs) == 1:
logger.Debug("attempting to parse log file (positional)", "log-file", flags.PositionalArgs[0])
inputFile, err = openLogFile(flags.PositionalArgs[0], logger)
}
if err != nil {
logger.Error("could not open log file", "error", err)
return err
}
defer inputFile.Close()
err = parseLogFile(inputFile, db, logger)
if err != nil {
logger.Debug("could not parse log file", "error", err)
return err
}
}
if flags.DumpRemotePayloads == true {
logger.Debug("dumping remote payloads")
err = dumpRemotePayloads(db, logger, os.Stdout)
if err != nil {
logger.Error("error dumping remote payloads", "error", err)
}
return nil
}
logger.Debug("starting tui")
ui := tui.NewTUI(db, logger)
err = ui.App.Run()
if err != nil {
logger.Error("tui application error", "error", err)
return err
}
return nil
}
// dbFile creates a cache file and returns the path to it.
func dbFile() (string, error) {
tmpDir := os.TempDir()
return path.Join(tmpDir, "newrelic_agent.sqlite"), nil
}
// initializeDatabase either loads an existing cache file, or creates a new
// cache file, and returns an initialized sqlite connection targeting that
// cache file.
func initializeDatabase(logger *log.Logger) (*database.LogsDatabase, error) {
var databaseFile string
if flags.CacheFile == "" {
databaseFile, _ = dbFile()
} else {
databaseFile = flags.CacheFile
}
d, err := database.New(database.DbParams{
DatabaseFilePath: databaseFile,
Logger: logger,
DoMigration: true,
})
if err != nil {
return nil, err
}
return d, nil
}
func shutdownDatabase(db *database.LogsDatabase, logger *log.Logger) {
db.Close()
if flags.KeepCacheFile == true {
return
}
err := os.Remove(db.DatabaseFile)
if err != nil {
logger.Error("failed to remove cache file", "cache-file", db.DatabaseFile, "error", err)
exitStatus = 1
}
}
func openLogFile(filePath string, logger *log.Logger) (io.ReadCloser, error) {
if filePath == "" {
return nil, fmt.Errorf("no input log file provided")
}
logger.Debug("opening log file", "file", filePath)
return fs.Open(filePath)
}
var matchLeadingK8sTimestamp = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3,}\s+?`)
// parseLogFile reads through a theoretical agent NDJSON log file, validates
// each line, and stores each validated line in the cache database.
func parseLogFile(logFile io.Reader, db *database.LogsDatabase, logger *log.Logger) error {
logger.Trace("starting to parse provided log file")
defer func() {
logger.Trace("finished parsing provided log file")
}()
scanBuffer := make([]byte, 0, 64*1_024)
scanner := bufio.NewScanner(logFile)
scanner.Buffer(scanBuffer, 1_024*1_024) // Scan up to 1MB.
bufferLimit := 1_000
parsedLinesBuffer := make([]database.InsertTuple, 0)
// TODO: the scanner only scans up to a maximum number of bytes before it
// gives up looking for the token (`\n` in our case) and generates a
// `bufio.ErrTooLong` error. We should instead use `bufio.Reader.ReadString`
// to parse line by line. But that will require a bit of refactoring of this
// loop. For now, 2025-05-28, it's simpler to increase the maximum byte limit.
// See:
// + https://stackoverflow.com/a/37455465
// + https://stackoverflow.com/a/6143530
for scanner.Scan() {
err := scanner.Err()
if err != nil {
logger.Error("failed to scan input", "error", err)
return err
}
// TODO: when we have a v1 line type, we need to do some text inspection
// to determine the type of log line to unmarshal. This may mean we need
// different viewers, as well.
var envelope *v0.LineEnvelope
sourceBytes := scanner.Bytes()
sourceString := string(sourceBytes)
if len(sourceString) == 0 {
// Skip empty lines in the source file.
continue
}
if matchLeadingK8sTimestamp.MatchString(sourceString) == true {
// Looks like the line starts with a k8s style timestamp. So we
// trim it off.
idx := strings.Index(sourceString, "{")
if idx == -1 {
logger.Warn("line with leading timestamp does not seem to be a log line", "line", sourceString)
continue
}
logger.Debug("trimming leading timestamp", "line", sourceString)
sourceString = sourceString[idx:]
}
if sourceString[0:1] != "{" || sourceString[len(sourceString)-1:] != "}" {
// Skip lines that do not look like NDJSON.
logger.Warn("skipping parsing of malformed line", "line", sourceString)
continue
}
err = json.Unmarshal([]byte(sourceString), &envelope)
if err != nil {
logger.Warn("failed to parse line", "error", err, "line", sourceString)
continue
}
if len(parsedLinesBuffer) < bufferLimit {
parsedLinesBuffer = append(parsedLinesBuffer, database.InsertTuple{
ParsedLog: envelope,
Source: sourceString,
})
} else {
err = db.BatchInsert(parsedLinesBuffer)
if err != nil {
return err
}
parsedLinesBuffer = []database.InsertTuple{{
ParsedLog: envelope,
Source: sourceString,
}}
}
}
if len(parsedLinesBuffer) > 0 {
err := db.BatchInsert(parsedLinesBuffer)
if err != nil {
return err
}
}
logger.Debug("finished reading log lines from input")
return nil
}
func dumpRemotePayloads(db *database.LogsDatabase, logger *log.Logger, writer io.Writer) error {
// TODO: if we implement a search by "component", utilize that here instead
query := database.SearchQuery("remote_method", db, logger)
rows, err := query.AllResults()
if err != nil {
return fmt.Errorf("failed to search logs for remote payloads: %w", err)
}
for _, row := range rows {
io.WriteString(writer, row.Original+"\n")
}
return nil
}