-
Notifications
You must be signed in to change notification settings - Fork 0
/
qs.go
328 lines (291 loc) · 7.96 KB
/
qs.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"sync"
"github.com/itchyny/gojq"
)
const slowQueryFilter string = `select(.msg == "Slow query" and .c == "COMMAND" and .attr.command != "unrecognized" and (.attr.ninserted == 0 | not))`
const logToQuery string = `
(
if .attr.command.q then
.attr.command.q
elif .attr.command.query then
.attr.command.query
elif .attr.command.filter then
.attr.command.filter
elif (.attr.command.updates | type) == "array" and
(.attr.command.updates | length) > 0 and
(.attr.command.updates[0] | type) == "object" then
.attr.command.updates[0].q
elif (.attr.command.deletes | type) == "array" and
(.attr.command.deletes | length) > 0 and
(.attr.command.deletes[0] | type) == "object" then
.attr.command.deletes[0].q
else
.attr.command.query
end
)
`
const queryToShape string = `
walk(
if type == "array" then
sort | unique
elif type == "object" then
if (.["$oid"] or .["$symbol"] or .["$numberInt"] or .["$numberLong"] or .["$numberDouble"] or .["$numberDecimal"]) then
1
elif (.["$binary"] or .["$code"] or .["$timestamp"] or .["$regularExpression"] or .["$dbPointer"] or .["$date"]) then
1
elif (.["$ref"] or .["$minKey"] or .["$maxKey"]) then
1
elif .["$lt"] then
.["$gt"] = .["$lt"] | del(.["$lt"])
elif .["$lte"] then
.["$gt"] = .["$lte"] | del(.["$lte"])
elif .["$gte"] then
.["$gt"] = .["$gte"] | del(.["$gte"])
else
.
end
elif (type == "string" or type == "number" or type == "boolean") then
1
else
.
end
)
`
const logToNs string = `
(
if (.attr.ns != null) and (.attr.ns | test("\\$cmd$")) then
if .attr.command.find then
"\(.attr.ns | split(".")[0]).\(.attr.command.find)"
elif .attr.command.findAndModify then
"\(.attr.ns | split(".")[0]).\(.attr.command.findAndModify)"
elif .attr.command.update then
"\(.attr.ns | split(".")[0]).\(.attr.command.update)"
elif .attr.command.insert then
"\(.attr.ns | split(".")[0]).\(.attr.command.insert)"
elif .attr.command.delete then
"\(.attr.ns | split(".")[0]).\(.attr.command.delete)"
else
.attr.ns
end
else
.attr.ns
end
)
`
const logToAction string = `
(
if .attr.type == "update" then
"update"
elif .attr.type == "remove" then
"remove"
elif .attr.command.find then
"find"
elif .attr.command.findAndModify then
"findAndModify"
elif .attr.command.update then
"update"
elif .attr.command.insert then
"insert"
elif .attr.command.delete then
"delete"
else
"other"
end
)
`
const logToDurMs string = `.attr.durationMillis`
const logToShape string = logToQuery + ` | ` + queryToShape
const logToNsActionShapeDurMsObject string = `
{
"ns": ` + logToNs + `,
"action": ` + logToAction + `,
"shape": ` + logToShape + `,
"durMS": ` + logToDurMs + "}"
const reduceWithDurMs string = `
reduce inputs as $j
({};
$j.ns as $ns
| $j.action as $action
| $j.shape as $shape
| $j.durMS as $durMS
| .[$ns] as $curNsObj
| "\($shape)" as $shapestr
| $curNsObj[$shapestr] as $curShapeObj
| ($curNsObj["countWithQuery"] + (if $shapestr == "null" or $shapestr == "{}" then 0 else 1 end)) as $newCountWithQuery
| ($curNsObj["countWithoutQuery"] + (if $shapestr == "null" or $shapestr == "{}" then 1 else 0 end)) as $newCountWithoutQuery
| $curShapeObj["actions"] as $curActionsObj
| $curActionsObj[$action] as $curActionObj
| {
"count": ($curActionObj["count"] + 1),
"durMSes": (if $curActionObj and $curActionObj["durMSes"] then $curActionObj["durMSes"] + [$durMS] else [$durMS] end)
} as $setActionFields
| ($curActionObj + $setActionFields) as $newActionObj
| {
($action): $newActionObj
} as $setActionsFields
| ($curActionsObj + $setActionsFields) as $newActionsObj
| {
"count": ($curShapeObj["count"] + 1),
"actions": $newActionsObj,
"shape": $shape
}
as $setShapeFields
| ($curShapeObj + $setShapeFields) as $newShapeObj
| {
($shapestr): $newShapeObj,
"countWithQuery": $newCountWithQuery,
"countWithoutQuery": $newCountWithoutQuery
}
as $setNsFields
| ($curNsObj + $setNsFields) as $newNsObj
| . + { ($ns): ($newNsObj) }
)
`
const transformShapesToArray string = `
to_entries
| map(
{
"key": .key,
"value": {
"countWithQuery": .value.countWithQuery,
"countWithoutQuery": .value.countWithoutQuery,
"queryShapes": (
.value
| to_entries
| map(.value | objects)
)
}
}
)
| from_entries
`
const addStats string = `
def ceil: if . | floor == . then . else . + 1.0 | floor end;
def perc($p; $arr): $arr | sort as $arr | ($arr | length) as $len | ($p / 100.0 * $len) | ceil as $rank | $arr[$rank - 1];
walk(
if type == "object" and has("durMSes") then
(
.durationMillis.p50 = perc(50; .durMSes)
| .durationMillis.p95 = perc(95; .durMSes)
| .durationMillis.max = (.durMSes | max)
| del(.durMSes)
)
else .
end
)
`
const sortShapesByCountDesc string = `
walk(
if type == "array" and (. | first | type == "object") and (. | first | has("count")) then
sort_by(.count * -1)
else
.
end)
`
const simplifiedLogs string = slowQueryFilter + ` | ` + logToNsActionShapeDurMsObject
const reduceAndStats string = reduceWithDurMs + ` | ` + transformShapesToArray + ` | ` + addStats + ` | ` + sortShapesByCountDesc
// channelIter struct
type channelIter struct {
ch <-chan interface{}
}
// Next method to implement the Iter interface for channelIter
func (c *channelIter) Next() (interface{}, bool) {
value, ok := <-c.ch
return value, ok
}
func main() {
// Create buffered channels with capacity 1000
channel1 := make(chan interface{}, 1000)
channel2 := make(chan interface{}, 1000)
// Create a WaitGroup to wait for goroutines to complete
var wg sync.WaitGroup
// Increment the WaitGroup counter for each goroutine
wg.Add(2)
// Goroutine to read from channel1, do first gojq operation, and write to channel2
go func() {
query, err := gojq.Parse(simplifiedLogs)
if err != nil {
log.Fatalln(err)
}
code, err := gojq.Compile(query)
if err != nil {
log.Fatalln(err)
}
for message := range channel1 {
//fmt.Fprintln(os.Stdout, "read from channel1: ", message)
var jmap map[string]interface{}
json.Unmarshal([]byte(message.(string)), &jmap)
iter := code.Run(jmap)
count := 0
for {
v, ok := iter.Next()
if !ok {
break
}
count++
if count > 1 {
fmt.Fprintln(os.Stderr, "Error: there should only be one result from code.Run")
}
if err, ok := v.(error); ok {
fmt.Fprintln(os.Stderr, "error:", err)
}
channel2 <- v
}
}
close(channel2)
wg.Done()
}()
// Goroutine to read from channel2, do second gojq operation (reduce) and write to stdout
go func() {
query, err := gojq.Parse(reduceAndStats)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to parse jq query:", err)
os.Exit(1)
}
chanIter := &channelIter{ch: channel2}
code, err := gojq.Compile(query, gojq.WithInputIter(chanIter))
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to compile jq query:", err)
os.Exit(1)
}
// Create an iterator
iter := code.Run(nil)
// Execute the gojq query
v, ok := iter.Next()
if !ok {
fmt.Fprintln(os.Stderr, "Error executing jq query.")
os.Exit(1)
}
if err, ok := v.(error); ok {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
encoder := json.NewEncoder(os.Stdout)
// Print the final state containing statistics
if err := encoder.Encode(v); err != nil {
fmt.Fprintln(os.Stderr, "Error encoding final state:", err)
}
wg.Done()
}()
// Read from stdin and write to channel1
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
//fmt.Fprintln(os.Stdout, "read from stdin: ", line)
channel1 <- line
}
// Close channel1 to signal that no more data will be sent on it
close(channel1)
// Check for errors in stdin reading
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
// Wait for all goroutines to complete
wg.Wait()
}