-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
223 lines (177 loc) · 5.39 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
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
package spancheck
import (
"flag"
"fmt"
"log"
"regexp"
"strings"
)
// Check is a type of check that can be enabled or disabled.
type Check int
const (
// EndCheck if enabled, checks that span.End() is called after span creation and before the function returns.
EndCheck Check = iota
// SetStatusCheck if enabled, checks that `span.SetStatus(codes.Error, msg)` is called when returning an error.
SetStatusCheck
// RecordErrorCheck if enabled, checks that span.RecordError(err) is called when returning an error.
RecordErrorCheck
)
var (
startSpanSignatureCols = 2
defaultStartSpanSignatures = []string{
// https://github.com/open-telemetry/opentelemetry-go/blob/98b32a6c3a87fbee5d34c063b9096f416b250897/trace/trace.go#L523
`\(go.opentelemetry.io/otel/trace.Tracer\).Start:opentelemetry`,
// https://pkg.go.dev/go.opencensus.io/trace#StartSpan
`go.opencensus.io/trace.StartSpan:opencensus`,
// https://github.com/census-instrumentation/opencensus-go/blob/v0.24.0/trace/trace_api.go#L66
`go.opencensus.io/trace.StartSpanWithRemoteParent:opencensus`,
}
)
func (c Check) String() string {
switch c {
case EndCheck:
return "end"
case SetStatusCheck:
return "set-status"
case RecordErrorCheck:
return "record-error"
default:
return ""
}
}
// Checks is a list of all checks by name.
var Checks = map[string]Check{
EndCheck.String(): EndCheck,
SetStatusCheck.String(): SetStatusCheck,
RecordErrorCheck.String(): RecordErrorCheck,
}
type spanStartMatcher struct {
signature *regexp.Regexp
spanType spanType
}
// Config is a configuration for the spancheck analyzer.
type Config struct {
fs flag.FlagSet
// EnabledChecks is a list of checks to enable by name.
EnabledChecks []string
// IgnoreChecksSignaturesSlice is a slice of strings that are turned into
// the IgnoreSetStatusCheckSignatures regex.
IgnoreChecksSignaturesSlice []string
StartSpanMatchersSlice []string
endCheckEnabled bool
setStatusEnabled bool
recordErrorEnabled bool
// ignoreChecksSignatures is a regex that, if matched, disables the
// SetStatus and RecordError checks on error.
ignoreChecksSignatures *regexp.Regexp
startSpanMatchers []spanStartMatcher
startSpanMatchersCustomRegex *regexp.Regexp
}
// NewDefaultConfig returns a new Config with default values.
func NewDefaultConfig() *Config {
return &Config{
EnabledChecks: []string{EndCheck.String()},
StartSpanMatchersSlice: defaultStartSpanSignatures,
}
}
// finalize parses checks and signatures from the public string slices of Config.
func (c *Config) finalize() {
c.parseSignatures()
checks := parseChecks(c.EnabledChecks)
c.endCheckEnabled = contains(checks, EndCheck)
c.setStatusEnabled = contains(checks, SetStatusCheck)
c.recordErrorEnabled = contains(checks, RecordErrorCheck)
}
// parseSignatures sets the Ignore*CheckSignatures regex from the string slices.
func (c *Config) parseSignatures() {
c.parseIgnoreSignatures()
c.parseStartSpanSignatures()
}
func (c *Config) parseIgnoreSignatures() {
if c.ignoreChecksSignatures == nil && len(c.IgnoreChecksSignaturesSlice) > 0 {
if len(c.IgnoreChecksSignaturesSlice) == 1 && c.IgnoreChecksSignaturesSlice[0] == "" {
return
}
c.ignoreChecksSignatures = createRegex(c.IgnoreChecksSignaturesSlice)
}
}
func (c *Config) parseStartSpanSignatures() {
if c.startSpanMatchers != nil {
return
}
customMatchers := []string{}
for i, sig := range c.StartSpanMatchersSlice {
parts := strings.Split(sig, ":")
// Make sure we have both a signature and a telemetry type
if len(parts) != startSpanSignatureCols {
log.Default().Printf("[WARN] invalid start span signature \"%s\". expected regex:telemetry-type\n", sig)
continue
}
sig, sigType := parts[0], parts[1]
if len(sig) < 1 {
log.Default().Print("[WARN] invalid start span signature, empty pattern")
continue
}
spanType, ok := SpanTypes[sigType]
if !ok {
validSpanTypes := make([]string, 0, len(SpanTypes))
for k := range SpanTypes {
validSpanTypes = append(validSpanTypes, k)
}
log.Default().
Printf("[WARN] invalid start span type \"%s\". expected one of %s\n", sigType, strings.Join(validSpanTypes, ", "))
continue
}
regex, err := regexp.Compile(sig)
if err != nil {
log.Default().Printf("[WARN] failed to compile regex from signature %s: %v\n", sig, err)
continue
}
c.startSpanMatchers = append(c.startSpanMatchers, spanStartMatcher{
signature: regex,
spanType: spanType,
})
if i >= len(defaultStartSpanSignatures) {
customMatchers = append(customMatchers, sig)
}
}
c.startSpanMatchersCustomRegex = createRegex(customMatchers)
}
func parseChecks(checksSlice []string) []Check {
if len(checksSlice) == 0 {
return nil
}
checks := []Check{}
for _, check := range checksSlice {
checkName := strings.TrimSpace(check)
if checkName == "" {
continue
}
check, ok := Checks[checkName]
if !ok {
continue
}
checks = append(checks, check)
}
return checks
}
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
}
func contains(s []Check, e Check) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}