-
Notifications
You must be signed in to change notification settings - Fork 330
/
search.go
293 lines (250 loc) · 7.89 KB
/
search.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
package search
import (
"context"
"sort"
"strings"
"time"
"github.com/didi/sharingan/replayer-agent/common/handlers/conf"
"github.com/didi/sharingan/replayer-agent/common/handlers/httpclient"
"github.com/didi/sharingan/replayer-agent/common/handlers/tlog"
"github.com/didi/sharingan/replayer-agent/idl"
"github.com/didi/sharingan/replayer-agent/model/esmodel"
"github.com/didi/sharingan/replayer-agent/model/nuwaplt"
"github.com/didi/sharingan/replayer-agent/utils/helper"
jsoniter "github.com/json-iterator/go"
)
type SearchRecords []*SearchRecord
//Len()
func (s SearchRecords) Len() int {
return len(s)
}
//Less():成绩将有低到高排序
func (s SearchRecords) Less(i, j int) bool {
return s[i].Timestamp > s[j].Timestamp
}
//Swap()
func (s SearchRecords) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
/**
* 前端查询ES数据接口(各种查询条件),超时10s
* @Return 多条session,封装成前端展示数据结构
*/
func Search(ctx context.Context, req *idl.SearchReq) []*SearchRecord {
req = SearchPreHandle(req)
// read from es
if conf.Handler.GetString("es_url.default") != "" {
return SearchFromES(ctx, req)
}
// read from local
stat, contents := GetTrafficFromLocal(ctx, req)
if stat != 0 {
return nil
}
return SearchHandleLocal(contents, req)
}
// SearchPreHandle 处理Search接口参数
func SearchPreHandle(req *idl.SearchReq) *idl.SearchReq {
if len(req.Date) == 1 {
req.Start = req.Date[0]
req.End = time.Now().Format("2006-01-02")
} else if len(req.Date) == 2 {
req.Start = req.Date[0]
req.End = req.Date[1]
}
return req
}
// SearchFromES 从es读取数据,并处理按Search接口返回
func SearchFromES(ctx context.Context, req *idl.SearchReq) []*SearchRecord {
flowList := make([]*SearchRecord, 0)
body, qErr := Query(ctx, req, 0)
if qErr != nil {
return flowList
}
if queryForID(req.Field) {
return retrieveSessionIds(ctx, body, flowList)
}
return retrieveSessions(ctx, req.Project, body, flowList)
}
// SearchHandleLocal 处理本地流量符合Search接口
func SearchHandleLocal(contents []string, req *idl.SearchReq) []*SearchRecord {
flowList := make([]*SearchRecord, 0)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
//原始流量格式
for _, flow := range contents {
traffic := &esmodel.Session{}
err := json.Unmarshal([]byte(flow), traffic)
if err != nil {
continue
}
flowList = AppendFlowList(flowList, req, *traffic)
sort.Sort(SearchRecords(flowList))
}
return flowList
}
// GetTrafficFromLocal 从本地读取流量
func GetTrafficFromLocal(ctx context.Context, req *idl.SearchReq) (int, []string) {
//读取配置文件conf/traffic/{project}
contents, err := helper.ReadLines(conf.Root + "/conf/traffic/" + req.Project)
if err != nil {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "Failed to read /conf/traffic/"+req.Project+", err="+err.Error())
return 1, nil
}
return 0, contents
}
//AppendFlowList 筛选流量并格式化,并追加到flowList数组
func AppendFlowList(flowList []*SearchRecord, req *idl.SearchReq, session esmodel.Session) []*SearchRecord {
if valide, data := filterTraffic(req, session); valide {
if queryForID(req.Field) {
flowList = append(flowList, &SearchRecord{SessionId: data.SessionId})
} else {
flowList = append(flowList, &SearchRecord{Project: data.Project, SessionId: data.SessionId, Timestamp: data.Timestamp, Req: data.Req, Res: data.Res, Actions: data.Actions})
}
}
return flowList
}
//filterTraffic 过滤配置文件conf/traffic/{project}下的流量
func filterTraffic(req *idl.SearchReq, session esmodel.Session) (bool, *SearchRecord) {
traffic := handleOneSessionRaw(context.TODO(), session, req.Project)
if traffic == nil {
return false, nil
}
// 线下回放不过滤时间
// t := strings.Split(traffic.Timestamp, "T")
// if req.Start > t[0] || req.End < t[0] {
// return false, nil
// }
if req.InboundRequest != "" {
if !strings.Contains(traffic.Req, req.InboundRequest) {
return false, nil
}
}
if req.InboundResponse != "" {
if !strings.Contains(traffic.Res, req.InboundResponse) {
return false, nil
}
}
if req.OutboundRequest != "" || req.OutboundResponse != "" || req.Apollo != "" {
signReq, signRes, signApollo := true, true, true
if req.OutboundRequest != "" {
signReq = false
}
if req.OutboundResponse != "" {
signRes = false
}
if req.Apollo != "" {
signApollo = false
}
for _, subReq := range traffic.Actions {
if req.OutboundRequest != "" && strings.Contains(subReq["req"], req.OutboundRequest) {
signReq = true
}
if req.OutboundResponse != "" && strings.Contains(subReq["res"], req.OutboundResponse) {
signRes = true
}
if req.Apollo != "" && strings.Contains(subReq["apollo"], req.Apollo) {
signApollo = true
}
if signReq && signRes && signApollo {
break
}
}
if !signRes || !signReq || !signApollo {
return false, nil
}
}
if req.SessionId != "" {
if !strings.Contains(traffic.SessionId, req.SessionId) {
return false, nil
}
}
return true, traffic
}
/**
* 回放某个具体session时,后端查询ES数据接口(根据SessionId查询),超时3s
* @Return 一个session,原始数据格式
*/
func GetRawSessions(ctx context.Context, req *idl.SearchReq) *esmodel.Session {
//优先读取es地址
if conf.Handler.GetString("es_url.default") != "" {
return GetRawSessionFromES(ctx, req)
}
// read from local
stat, contents := GetTrafficFromLocal(ctx, req)
if stat != 0 {
return nil
}
return GetRawSessionHandleLocal(ctx, contents, req)
}
func GetRawSessionFromES(ctx context.Context, req *idl.SearchReq) *esmodel.Session {
body, err := Query(ctx, req, 3*time.Second)
if err != nil {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "errmsg= GetRawSessions failed from es ||err=%s", err.Error())
return nil
}
sessions, err := esmodel.RetrieveSessions(body)
if err != nil {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "errmsg= GetRawSessions failed at retrieve session from es ||err=%s", err.Error())
return nil
} else if len(sessions) == 0 {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "errmsg= GetRawSessions got empty session from es!")
return nil
}
return &sessions[0]
}
// GetRawSessionHandleLocal 处理本地流量符合GetRawSessions接口
func GetRawSessionHandleLocal(ctx context.Context, contents []string, req *idl.SearchReq) *esmodel.Session {
session := new(esmodel.Session)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
for _, flow := range contents {
traffic := &esmodel.Session{}
err := json.Unmarshal([]byte(flow), traffic)
if err != nil {
tlog.Handler.Warnf(ctx, tlog.DLTagUndefined, "errmsg= GetRawSessions failed at unmarshal from conf with origin struct ||err=%s", err.Error())
continue
}
if traffic.SessionId == req.SessionId {
session = traffic
break
}
}
if session == nil {
tlog.Handler.Warnf(ctx, tlog.DLTagUndefined, "errmsg=receive zero session from es or conf")
}
return session
}
func Query(ctx context.Context, req *idl.SearchReq, timeout time.Duration) ([]byte, error) {
dsl, err := toDSL(req)
if err != nil {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "errmsg=generate dsl failed||err=%s", err)
return nil, err
}
return doQuery(ctx, dsl, timeout)
}
func doQuery(ctx context.Context, data []byte, timeout time.Duration) (body []byte, err error) {
headers := map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "1",
}
if timeout <= 0 {
timeout = 10 * time.Second
}
for i := 0; i < 3; i++ {
_, body, err = httpclient.Handler.Post(ctx, resolveURL(ctx), data, timeout, headers)
if err == nil && len(body) > 180 {
return
}
}
return
}
func resolveURL(ctx context.Context) string {
url := conf.Handler.GetString("es_url.default")
depart, ok := ctx.Value(nuwaplt.KDepartment).(string)
if ok && depart != nuwaplt.DefaultDepartment {
otherUrl := conf.Handler.GetString("es_url." + depart)
if otherUrl != "" {
url = otherUrl
}
}
return url
}