-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathslowops.go
232 lines (219 loc) · 6.67 KB
/
slowops.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
// Copyright 2022-present Kuei-chun Chen. All rights reserved.
package hatchet
import (
"encoding/json"
"errors"
"regexp"
"strings"
"github.com/simagix/gox"
"go.mongodb.org/mongo-driver/bson"
)
const (
cmdCollstats = "collstats"
cmdCreateIndexes = "createIndexes"
cmdInsert = "insert"
cmdDistinct = "distinct"
cmdAggregate = "aggregate"
cmdCount = "count"
cmdDelete = "delete"
cmdFind = "find"
cmdFindAndModify = "findandmodify"
cmdGetMore = "getMore"
cmdRemove = "remove"
cmdUpdate = "update"
)
// AnalyzeLog analyzes slow op log
func AnalyzeLog(str string) (*OpStat, error) {
doc := Logv2Info{}
if err := bson.UnmarshalExtJSON([]byte(str), false, &doc); err != nil {
return nil, err
}
return AnalyzeSlowOp(&doc)
}
// AnalyzeSlowOp analyzes slow ops
func AnalyzeSlowOp(doc *Logv2Info) (*OpStat, error) {
var err error
stat := &OpStat{}
c := doc.Component
if c != "COMMAND" && c != "QUERY" && c != "WRITE" {
return stat, errors.New("unsupported command")
}
b, _ := bson.Marshal(doc.Attr)
bson.Unmarshal(b, &doc.Attributes)
stat.TotalMilli = doc.Attributes.Milli
stat.Namespace = doc.Attributes.NS
if stat.Namespace == "" {
return stat, errors.New("no namespace found")
} else if strings.HasSuffix(stat.Namespace, ".$cmd") {
if commands, ok := BsonD2M(doc.Attr)["command"].(bson.D); ok {
if len(commands) > 0 {
elem := commands[0]
stat.Op = elem.Key
if doc.Attributes.NS, ok = elem.Value.(string); !ok {
doc.Attributes.NS = stat.Namespace
}
if !strings.Contains(doc.Attributes.NS, ".") {
doc.Attributes.NS = strings.ReplaceAll(stat.Namespace, "$cmd", doc.Attributes.NS)
}
}
}
if doc.Attributes.ErrMsg != "" {
stat.Index = "ErrMsg: " + doc.Attributes.ErrMsg
return stat, errors.New("unrecognized collection, ignored")
}
}
if doc.Attributes.PlanSummary != "" { // not insert
plan := doc.Attributes.PlanSummary
if strings.HasPrefix(plan, "IXSCAN") {
stat.Index = plan[len("IXSCAN")+1:]
stat.Index = strings.ReplaceAll(stat.Index, ": ", ":")
} else {
stat.Index = plan
}
} else if doc.Attributes.ErrMsg != "" {
stat.Index = "ErrMsg: " + doc.Attributes.ErrMsg
}
stat.Reslen = doc.Attributes.Reslen
if doc.Attributes.Command == nil {
return stat, errors.New("no command found")
}
command := doc.Attributes.Command
stat.Op = doc.Attributes.Type
if stat.Op == "command" || stat.Op == "none" {
stat.Op = getOp(command)
}
var isGetMore bool
if stat.Op == cmdGetMore {
isGetMore = true
command = doc.Attributes.OriginatingCommand
stat.Op = getOp(command)
}
if stat.Op == cmdInsert || stat.Op == cmdDistinct ||
stat.Op == cmdCreateIndexes || stat.Op == cmdCollstats {
stat.QueryPattern = ""
} else if stat.QueryPattern == "" &&
(stat.Op == cmdFind || stat.Op == cmdUpdate || stat.Op == cmdRemove || stat.Op == cmdDelete) {
var query interface{}
if command["q"] != nil {
query = command["q"]
} else if command["query"] != nil {
query = command["query"]
} else if command["filter"] != nil {
query = command["filter"]
}
if query != nil {
walker := gox.NewMapWalker(cb)
doc := walker.Walk(query.(map[string]interface{}))
if buf, err := json.Marshal(doc); err == nil {
stat.QueryPattern = string(buf)
} else {
stat.QueryPattern = "{}"
}
} else {
stat.QueryPattern = "{}"
}
} else if stat.Op == cmdAggregate {
pipeline, ok := command["pipeline"].(bson.A)
if !ok || len(pipeline) == 0 {
return stat, errors.New("pipeline not found")
}
var stage interface{}
for _, v := range pipeline {
stage = v
break
}
fmap := stage.(map[string]interface{})
if !isRegex(fmap) {
walker := gox.NewMapWalker(cb)
doc := walker.Walk(fmap)
if buf, err := json.Marshal(doc); err == nil {
stat.QueryPattern = string(buf)
} else {
stat.QueryPattern = "{}"
}
if strings.Contains(stat.QueryPattern, "$changeStream") {
if len(pipeline) > 1 {
buf, _ := json.Marshal(pipeline[1])
stat.QueryPattern = string(buf)
} else {
stat.QueryPattern = "{}"
}
} else if !strings.Contains(stat.QueryPattern, "$match") && !strings.Contains(stat.QueryPattern, "$sort") &&
!strings.Contains(stat.QueryPattern, "$facet") && !strings.Contains(stat.QueryPattern, "$indexStats") {
stat.QueryPattern = "{}"
}
} else {
buf, _ := json.Marshal(fmap)
str := string(buf)
re := regexp.MustCompile(`{(.*):{"\$regularExpression":{"options":"(\S+)?","pattern":"(\^)?(\S+)"}}}`)
stat.QueryPattern = re.ReplaceAllString(str, "{$1:/$3.../$2}")
}
} else {
var fmap map[string]interface{}
if command["filter"] != nil {
fmap = command["filter"].(map[string]interface{})
} else if command["query"] != nil {
fmap = command["query"].(map[string]interface{})
} else if command["q"] != nil {
fmap = command["q"].(map[string]interface{})
} else {
stat.QueryPattern = "{}"
}
if !isRegex(fmap) {
walker := gox.NewMapWalker(cb)
doc := walker.Walk(fmap)
var data []byte
if data, err = json.Marshal(doc); err != nil {
return stat, err
}
stat.QueryPattern = string(data)
if stat.QueryPattern == `{"":null}` {
stat.QueryPattern = "{}"
}
} else {
buf, _ := json.Marshal(fmap)
str := string(buf)
re := regexp.MustCompile(`{(.*):{"\$regularExpression":{"options":"(\S+)?","pattern":"(\^)?(\S+)"}}}`)
stat.QueryPattern = re.ReplaceAllString(str, "{$1:/$3.../$2}")
}
}
if stat.Op == "" {
return stat, nil
}
re := regexp.MustCompile(`^{("\$match"|"\$sort"):(\S+)}$`)
stat.QueryPattern = re.ReplaceAllString(stat.QueryPattern, `$2`)
re = regexp.MustCompile(`^{("(\$facet")):\S+}$`)
stat.QueryPattern = re.ReplaceAllString(stat.QueryPattern, `{$1:...}`)
re = regexp.MustCompile(`{"\$oid":1}`)
stat.QueryPattern = re.ReplaceAllString(stat.QueryPattern, `1`)
re = regexp.MustCompile(`("\$n?in"):\[\S+(,\s?\S+)*\]`)
stat.QueryPattern = re.ReplaceAllString(stat.QueryPattern, `$1:[...]`)
re = regexp.MustCompile(`"(\$?\w+)":`)
stat.QueryPattern = re.ReplaceAllString(stat.QueryPattern, ` $1:`)
stat.QueryPattern = strings.ReplaceAll(stat.QueryPattern, "}", " }")
if isGetMore {
stat.Op = cmdGetMore
}
return stat, nil
}
func isRegex(doc map[string]interface{}) bool {
if buf, err := json.Marshal(doc); err != nil {
return false
} else if strings.Contains(string(buf), "$regularExpression") {
return true
}
return false
}
func getOp(command map[string]interface{}) string {
ops := []string{cmdAggregate, cmdCollstats, cmdCount, cmdCreateIndexes, cmdDelete, cmdDistinct,
cmdFind, cmdFindAndModify, cmdGetMore, cmdInsert, cmdUpdate}
for _, v := range ops {
if command[v] != nil {
return v
}
}
return ""
}
func cb(value interface{}) interface{} {
return 1
}