This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
206 lines (180 loc) · 4.75 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/apex/log"
"github.com/pborman/getopt"
"github.com/prometheus/prometheus/promql/parser"
"gopkg.in/yaml.v2"
)
type Query struct {
Params *QueryParams
Stats *QueryStats
}
type QueryParams struct {
Query string
}
type QueryStats struct {
Timings *QueryStatsTimings
}
type QueryStatsTimings struct {
EvalTotalTime float64
ResultSortTime float64
QueryPreparationTime float64
InnerEvalTime float64
ExecQueueTime float64
ExecTotalTime float64
}
type queryStats struct {
query string
tookTime float64
count uint32
}
func main() {
var (
optPathToQueryLog = getopt.StringLong("query-log", 'l', "", "Path to query log file [e.g. query.log]")
optMinQueryTime = getopt.DurationLong("min-query-time", 'm', 0, "Min query duration time to include in a result")
optMinQueryCount = getopt.Uint32Long("min-query-count", 'c', 0, "Min query count it should reappear in the log to include in a result")
optGenerateRecordingRules = getopt.BoolLong("generate-recording-rules", 'r', "Generate recording rules and output")
optGenerateRollupRules = getopt.BoolLong("generate-rollup-rules", 'g', "Generate rollup rules and output")
slowQueries = make(map[string]*queryStats, 100)
)
getopt.Parse()
if *optPathToQueryLog == "" {
getopt.Usage()
os.Exit(1)
}
logFile, err := os.Open(*optPathToQueryLog)
if err != nil {
log.Fatalf("Error opening query log file: %v\n", err)
}
defer logFile.Close()
reader := bufio.NewReader(logFile)
var line string
lineNo := 1
for {
line, err = reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
log.Errorf("error reading json line %d: %v", lineNo, err)
continue
}
query := &Query{}
err = json.Unmarshal([]byte(line), query)
if err != nil {
log.Errorf("error unmarshaling json line %d: %v", lineNo, err)
continue
}
qd, err := time.ParseDuration(fmt.Sprintf("%fs", query.Stats.Timings.InnerEvalTime))
if err != nil {
log.Errorf("error parsing query duration '%f': %v", query.Stats.Timings.InnerEvalTime, err)
continue
}
if qd < *optMinQueryTime {
continue
}
log.Debugf("Query: %s, Took time: %fs\n", query.Params.Query, query.Stats.Timings.InnerEvalTime)
expr, err := parser.ParseExpr(query.Params.Query)
if err != nil {
log.Errorf("error parsing query at line %d: %v", lineNo, err)
continue
}
parser.Inspect(expr, func(node parser.Node, path []parser.Node) error {
if node == nil {
// Skip empty nodes.
return nil
}
switch n := node.(type) {
case *parser.AggregateExpr:
qs := n.String()
q, ok := slowQueries[qs]
if ok {
q.count++
// If the whole query has more operations then tookTime will be incorrect for only
// a single aggregation operation, but lets ignore it for now.
q.tookTime += query.Stats.Timings.InnerEvalTime
} else {
slowQueries[qs] = &queryStats{
query: qs,
count: 1,
tookTime: query.Stats.Timings.InnerEvalTime,
}
}
}
return nil
})
lineNo++
}
arr := make([]*queryStats, 0, len(slowQueries))
for _, v := range slowQueries {
if v.count >= *optMinQueryCount {
arr = append(arr, v)
}
}
sort.Slice(arr, func(i, j int) bool {
return arr[i].tookTime > arr[j].tookTime
})
if *optGenerateRecordingRules {
fmt.Println("groups:")
fmt.Println("- name: high_cardinality_analyzer_group")
fmt.Println(" rules:")
for _, v := range arr {
fmt.Println(" - record:", toRuleName(v.query))
fmt.Println(" expr:", v.query)
}
return
}
if *optGenerateRollupRules {
failedQueries := make([]string, 0, len(arr))
cfg := &Configuration{
Rules: &RulesConfiguration{
RollupRules: make([]RollupRuleConfiguration, 0, len(arr)),
},
}
for _, v := range arr {
r := queryToRollupRule(v.query)
if r != nil {
cfg.Rules.RollupRules = append(cfg.Rules.RollupRules, *r)
} else {
failedQueries = append(failedQueries, v.query)
}
}
y, err := yaml.Marshal(cfg)
if err != nil {
fmt.Printf("Failed to serialize rules to YAML: %v\n", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(y))
if len(failedQueries) > 0 {
fmt.Println("\nFailed to convert the following queries:")
for _, s := range failedQueries {
fmt.Println(s)
}
}
return
}
for _, v := range arr {
fmt.Printf("%s, count: %d, took total: %fs\n", v.query, v.count, v.tookTime)
}
}
var re = regexp.MustCompile(`[^\w]`)
func toRuleName(expr string) string {
s := re.ReplaceAllLiteralString(expr, ":")
s = strings.ReplaceAll(s, "::", ":")
for strings.HasPrefix(s, ":") {
s = s[1:]
}
for strings.HasSuffix(s, ":") {
s = s[0 : len(s)-1]
}
return s
}