Skip to content

Commit

Permalink
feat: Add verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyo committed Jun 27, 2019
1 parent 05954de commit fc05d53
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ type CmdArgs struct {
pattern *string
dryRun *bool
noFollow *bool
verbose *bool
// maxEvents *int
fromLineNumber *int
config *string
}

var (
_isDryRun bool
_verbose bool
)

func isDryRun() bool {
return _isDryRun
}

func isVerbose() bool {
return _verbose
}

func initSentry(config *Config) {
if isDryRun() {
log.Println("Dry-run mode enabled, not initializing Sentry client")
Expand Down Expand Up @@ -69,15 +75,17 @@ func main() {
args := CmdArgs{
file: kingpin.Arg("file", "File to parse").String(),
pattern: kingpin.Flag("pattern", "Pattern to look for").String(),
dryRun: kingpin.Flag("dry-run", "Dry-run mode").Bool(),
dryRun: kingpin.Flag("dry-run", "Dry-run mode").Default("false").Bool(),
noFollow: kingpin.Flag("no-follow", "Do not wait for the new data").Bool(),
// maxEvents: kingpin.Flag("max-events", "Exit after the given number of events are processed").Int(),
fromLineNumber: kingpin.Flag("from-line", "Start reading from this line number").Default("-1").Int(),
config: kingpin.Flag("config", "Path to the configuration").String(),
config: kingpin.Flag("config", "Path to the configuration").Short('c').String(),
verbose: kingpin.Flag("verbose", "Print every match").Short('v').Default("false").Bool(),
}

kingpin.Parse()
_isDryRun = *args.dryRun
_verbose = *args.verbose

var config *Config

Expand Down
44 changes: 28 additions & 16 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const TimeStampField = "timestamp"

var wg sync.WaitGroup

var printMutex sync.Mutex

func printMap(m map[string]string) {
keys := make([]string, 0, len(m))
for k := range m {
Expand All @@ -48,24 +50,24 @@ func captureEvent(line string, values map[string]string, hub *sentry.Hub) {
// Attempt to parse the timestamp
timestamp := parseTimestamp(values[TimeStampField])

hub.WithScope(func(scope *sentry.Scope) {
for key, value := range values {
if value == "" {
continue
}
scope.SetTag(key, value)
}
scope := hub.Scope()

if timestamp != 0 {
scope.SetTag("parsed_timestamp", strconv.FormatInt(timestamp, 10))
for key, value := range values {
if value == "" {
continue
}
scope.SetTag(key, value)
}

scope.SetLevel(sentry.LevelError)
if timestamp != 0 {
scope.SetTag("parsed_timestamp", strconv.FormatInt(timestamp, 10))
}

scope.SetExtra("log_entry", line)
scope.SetLevel(sentry.LevelError)

hub.CaptureMessage(message)
})
scope.SetExtra("log_entry", line)

hub.CaptureMessage(message)
}

func parseTimestamp(str string) int64 {
Expand Down Expand Up @@ -104,8 +106,12 @@ func processLine(line string, patterns []string, g *grok.Grok, hub *sentry.Hub)

captureEvent(line, parsedValues, hub)

log.Println("Entry found:")
printMap(parsedValues)
if isVerbose() {
printMutex.Lock()
log.Println("Entry found:")
printMap(parsedValues)
printMutex.Unlock()
}
}

func initGrokProcessor() *grok.Grok {
Expand Down Expand Up @@ -199,13 +205,19 @@ func processFile(fileInput *FileInputConfig, g *grok.Grok) {
Level: sentry.LevelInfo,
}, nil)

processLine(line.Text, fileInput.Patterns, g, hub)
hub.WithScope(func(_ *sentry.Scope) {
processLine(line.Text, fileInput.Patterns, g, hub)
})
}

hub.Flush(10 * time.Second)
}

func runWithConfig(config *Config) {
if isVerbose() {
log.Println("Verbose mode enabled, printing every match")
}

g := initGrokProcessor()

// Load patterns
Expand Down

0 comments on commit fc05d53

Please sign in to comment.