refactor: move regexp compile to top, reduce redundant logic (#1)

This commit is contained in:
Marc 2023-04-23 20:03:29 -04:00 committed by GitHub
parent 953f365c24
commit 8df06fcefd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

100
main.go
View file

@ -6,8 +6,8 @@ import (
"log" "log"
"os" "os"
"regexp" "regexp"
"strconv"
"strings" "strings"
"strconv"
) )
type Span struct { type Span struct {
@ -44,36 +44,43 @@ type TraceData struct {
} }
type Config struct { type Config struct {
query string query string
tracePath string tracePath string
verbosity int verbosity int
depthLimit int depthLimit int
} }
var rVerboseFlag = regexp.MustCompile("^-(?P<verbosity>(v{1,2}))$")
var rDepthPattern = regexp.MustCompile("--depth=(?P<depth>([1-9][0-9]*|0))")
// Prints traces recursively with increasing ident levels. // Prints traces recursively with increasing ident levels.
// More data is printed until there are no more traces available // More data is printed until there are no more traces available
// i.e. until the ChildrenIds key yields an empty array. // i.e. until the ChildrenIds key yields an empty array.
func recursivelyPrintTraces(idToSpanMap map[string]Span, current string, config Config, depth int) { func recursivelyPrintTraces(idToSpanMap map[string]Span, current string, config Config, depth int) {
if depth > config.depthLimit { return } if depth > config.depthLimit {
return
}
currentSpan := idToSpanMap[current] currentSpan := idToSpanMap[current]
prefix := strings.Repeat(" ", depth) prefix := strings.Repeat(" ", depth)
log.Println(fmt.Sprintf("%s\033[35m[%s]\033[0m: %fms", prefix, currentSpan.Name, currentSpan.Duration*1000)) log.Println(fmt.Sprintf("%s\033[35m[%s]\033[0m: %fms", prefix, currentSpan.Name, currentSpan.Duration*1000))
if config.verbosity > 0 { if config.verbosity > 0 {
log.Println(fmt.Sprintf("%s \033[36m> %s\033[0m", prefix, currentSpan.Resource)) log.Println(fmt.Sprintf("%s \033[36m> %s\033[0m", prefix, currentSpan.Resource))
} }
if config.verbosity > 1 { if config.verbosity > 1 {
meta, err := json.MarshalIndent(currentSpan.Meta, "", " ") meta, err := json.MarshalIndent(currentSpan.Meta, "", " ")
if err != nil { panic(err) } if err != nil {
panic(err)
}
log.Println(fmt.Sprintf("%s %s", prefix, meta)) log.Println(fmt.Sprintf("%s %s", prefix, meta))
} }
for _, childId := range currentSpan.ChildrenIds { for _, childId := range currentSpan.ChildrenIds {
recursivelyPrintTraces(idToSpanMap, childId, config, depth+1) recursivelyPrintTraces(idToSpanMap, childId, config, depth+1)
} }
} }
@ -123,53 +130,46 @@ func parseTraceJsonFromFile(path string) TraceData {
return fullTrace return fullTrace
} }
// Parses command-line arguments to extract flags and
// other useful contextual details.
func parseArgs(args []string) Config { func parseArgs(args []string) Config {
collectedArgs := map[string]bool{} rootOfInterest := os.Args[2]
tracePath := os.Args[1]
verbosity := 0
depth := 9999
for _, arg := range os.Args { for _, arg := range os.Args {
collectedArgs[arg] = true verbosity_match := rVerboseFlag.FindAllString(arg, -1)
}
verbosity := 0 if verbosity_match != nil {
verbosity = len(verbosity_match[0])
continue
}
_, verbose := collectedArgs["-v"] depthMatch := rDepthPattern.FindStringSubmatch(strings.Join(os.Args, " "))
if verbose { verbosity = 1 } if depthMatch != nil {
depthValueIndex := rDepthPattern.SubexpIndex("depth")
_, veryVerbose := collectedArgs["-vv"] depthValue := depthMatch[depthValueIndex]
if veryVerbose { verbosity = 2 } depthLimit, err := strconv.Atoi(depthValue)
depthPattern := "--depth=(?P<depth>([1-9][0-9]*|0))" if err != nil {
rDepthPattern := regexp.MustCompile(depthPattern) log.Println("Couldn't parse depth limit, ignoring")
}
depthMatch := rDepthPattern.FindStringSubmatch(strings.Join(os.Args, " ")) depth = depthLimit
continue
depth := 9999 }
}
if depthMatch != nil { return Config{rootOfInterest, tracePath, verbosity, depth}
depthValueIndex := rDepthPattern.SubexpIndex("depth")
depthValue := depthMatch[depthValueIndex]
depthLimit, err := strconv.Atoi(depthValue)
if err != nil {
log.Println("Couldn't parse depth limit, ignoring")
}
depth = depthLimit
}
rootOfInterest := os.Args[2]
tracePath := os.Args[1]
return Config{ rootOfInterest, tracePath, verbosity, depth }
} }
func main() { func main() {
config := parseArgs(os.Args) config := parseArgs(os.Args)
fullTrace := parseTraceJsonFromFile(config.tracePath) fullTrace := parseTraceJsonFromFile(config.tracePath)
spansById, spanIdsByResourceName := buildSpanIndexes(fullTrace) spansById, spanIdsByResourceName := buildSpanIndexes(fullTrace)