-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
124 lines (99 loc) · 4.37 KB
/
config.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
package spancheck
import (
"flag"
"fmt"
"log"
"regexp"
"strings"
)
var (
ignoreSetStatusCheckSignatures = "ignore-set-status-check-signatures"
ignoreRecordErrorCheckSignatures = "ignore-record-error-check-signatures"
)
// Config is a configuration for the spancheck analyzer.
type Config struct {
fs flag.FlagSet
// EnableAll enables all checks and takes precedence over other fields like
// DisableEndCheck. Ignore*CheckSignatures still apply.
EnableAll bool
// DisableEndCheck enables the check for calling span.End().
DisableEndCheck bool
// EnableSetStatusCheck enables the check for calling span.SetStatus.
EnableSetStatusCheck bool
// ignoreSetStatusCheckSignatures is a regex that, if matched, disables the
// SetStatus check for a particular error.
ignoreSetStatusCheckSignatures *regexp.Regexp
// IgnoreSetStatusCheckSignaturesSlice is a slice of strings that are turned into
// the IgnoreSetStatusCheckSignatures regex.
IgnoreSetStatusCheckSignaturesSlice []string
// EnableRecordErrorCheck enables the check for calling span.RecordError.
// By default, this check is disabled.
EnableRecordErrorCheck bool
// ignoreRecordErrorCheckSignatures is a regex that, if matched, disables the
// RecordError check for a particular error.
ignoreRecordErrorCheckSignatures *regexp.Regexp
// IgnoreRecordErrorCheckSignaturesSlice is a slice of strings that are turned into
// the IgnoreRecordErrorCheckSignatures regex.
IgnoreRecordErrorCheckSignaturesSlice []string
}
// NewConfigFromFlags returns a new Config with default values and flags for CLI usage.
func NewConfigFromFlags() *Config {
cfg := newDefaultConfig()
cfg.fs = flag.FlagSet{}
cfg.fs.BoolVar(&cfg.DisableEndCheck, "disable-end-check", cfg.DisableEndCheck, "disable the check for calling span.End() after span creation")
cfg.fs.BoolVar(&cfg.EnableAll, "enable-all", cfg.EnableAll, "enable all checks, overriding individual check flags")
cfg.fs.BoolVar(&cfg.EnableSetStatusCheck, "enable-set-status-check", cfg.EnableSetStatusCheck, "enable check for a span.SetStatus(codes.Error, msg) call when returning an error")
ignoreSetStatusCheckSignatures := flag.String(ignoreSetStatusCheckSignatures, "", "comma-separated list of regex for function signature that disable the span.SetStatus(codes.Error, msg) check on errors")
flag.BoolVar(&cfg.EnableRecordErrorCheck, "enable-record-error-check", cfg.EnableRecordErrorCheck, "enable check for a span.RecordError(err) call when returning an error")
ignoreRecordErrorCheckSignatures := flag.String(ignoreRecordErrorCheckSignatures, "", "comma-separated list of regex for function signature that disable the span.RecordError(err) check on errors")
cfg.ignoreSetStatusCheckSignatures = parseSignatures(*ignoreSetStatusCheckSignatures)
cfg.ignoreRecordErrorCheckSignatures = parseSignatures(*ignoreRecordErrorCheckSignatures)
return cfg
}
func newDefaultConfig() *Config {
return &Config{
DisableEndCheck: false,
EnableAll: false,
EnableRecordErrorCheck: false,
EnableSetStatusCheck: false,
ignoreRecordErrorCheckSignatures: nil,
IgnoreRecordErrorCheckSignaturesSlice: nil,
ignoreSetStatusCheckSignatures: nil,
IgnoreSetStatusCheckSignaturesSlice: nil,
}
}
// parseSignatures sets the Ignore*CheckSignatures regex from the string slices.
func (c *Config) parseSignatures() {
if c.ignoreRecordErrorCheckSignatures == nil && len(c.IgnoreRecordErrorCheckSignaturesSlice) > 0 {
c.ignoreRecordErrorCheckSignatures = createRegex(c.IgnoreRecordErrorCheckSignaturesSlice)
}
if c.ignoreSetStatusCheckSignatures == nil && len(c.IgnoreSetStatusCheckSignaturesSlice) > 0 {
c.ignoreSetStatusCheckSignatures = createRegex(c.IgnoreSetStatusCheckSignaturesSlice)
}
}
func parseSignatures(sigFlag string) *regexp.Regexp {
if sigFlag == "" {
return nil
}
sigs := []string{}
for _, sig := range strings.Split(sigFlag, ",") {
sig = strings.TrimSpace(sig)
if sig == "" {
continue
}
sigs = append(sigs, sig)
}
return createRegex(sigs)
}
func createRegex(sigs []string) *regexp.Regexp {
if len(sigs) == 0 {
return nil
}
regex := fmt.Sprintf("(%s)", strings.Join(sigs, "|"))
regexCompiled, err := regexp.Compile(regex)
if err != nil {
log.Default().Print("[WARN] failed to compile regex from signature flag", "regex", regex, "err", err)
return nil
}
return regexCompiled
}