/
stats.go
566 lines (489 loc) · 14 KB
/
stats.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package service
import (
"fmt"
"strings"
"github.com/imdevlab/tracing/collector/misc"
"github.com/imdevlab/tracing/pkg/constant"
"github.com/imdevlab/tracing/pkg/stats"
"github.com/imdevlab/tracing/pkg/pinpoint/thrift/trace"
)
// Stats 计算结果
type Stats struct {
httpCodes map[int32]struct{} // http标准code
api *stats.ApiStore // api统计
sql *stats.SQLS // sql语句计算统计
method *stats.Methods // 接口计算统计
exception *stats.Exceptions // 异常计算统计
apiMap *stats.ApiMap // 接口拓扑图
srvMap *stats.SrvMap // 应用拓扑图
}
// NewStats ....
func NewStats(httpCodes, customCodes map[int32]struct{}) *Stats {
stats := &Stats{
httpCodes: make(map[int32]struct{}),
api: stats.NewApiStore(),
sql: stats.NewSQLS(),
method: stats.NewMethods(),
exception: stats.NewExceptions(),
apiMap: stats.NewApiMap(),
srvMap: stats.NewSrvMap(),
}
// http codes默认给200
stats.httpCodes[200] = struct{}{}
// 添加策略
for code := range httpCodes {
stats.httpCodes[code] = struct{}{}
}
return stats
}
// 如果该应用为父节点,那么要统计自己的api,目标为自己,请求者为unknow
func (s *Stats) selfApiCounter(span *trace.TSpan) {
// 查找应用
app, ok := s.api.Apps[span.GetApplicationName()]
if !ok {
app = stats.NewApp()
s.api.Apps[span.GetApplicationName()] = app
}
url, ok := app.Urls[span.GetRPC()]
if !ok {
url = stats.NewUrl()
app.Urls[span.GetRPC()] = url
}
url.Duration += span.GetElapsed()
url.AccessCount++
if span.GetElapsed() > url.MaxDuration {
url.MaxDuration = span.GetElapsed()
}
if url.MinDuration == 0 || url.MinDuration > span.GetElapsed() {
url.MinDuration = span.GetElapsed()
}
cacheCode := false
var code int32
// Annotations
for _, annotation := range span.GetAnnotations() {
if annotation.GetKey() == constant.HTTP_STATUS_CODE {
cacheCode = true
code = annotation.GetValue().GetIntValue()
break
}
}
// 检查code是否正确, 如果在策略里面没找到发现code,那么说明这次http请求失败
if cacheCode {
if _, ok := s.httpCodes[code]; !ok {
url.AccessCodeErrCount++
}
}
// 耗时小于满意时间满意次数加1
if span.GetElapsed() < misc.Conf.Stats.SatisfactionTime {
url.SatisfactionCount++
// 耗时小于可容忍时间,可容忍次数加一, 其他都为沮丧次数
} else if span.GetElapsed() < misc.Conf.Stats.TolerateTime {
url.TolerateCount++
}
}
// SpanCounter 计算
func (s *Stats) SpanCounter(span *trace.TSpan) error {
// 计算API信息
for _, event := range span.GetSpanEventList() {
s.eventCounter(span.GetApplicationName(), span.GetRPC(), event)
}
// 如果该应用为父节点,那么要统计自己的api,目标为自己,请求者为unknow
if span.GetParentSpanId() == -1 {
s.selfApiCounter(span)
}
// 计算API被哪些服务调用
{
s.apiMapCounter(span)
}
// 计算服务拓扑图
{
s.parentMapCounter(span)
}
return nil
}
// SpanChunkCounter counter 计算
func (s *Stats) SpanChunkCounter(spanChunk *trace.TSpanChunk) error {
for _, event := range spanChunk.GetSpanEventList() {
s.eventCounter(spanChunk.GetApplicationName(), "", event)
}
return nil
}
// sqlCount 计算sql
func (s *Stats) sqlCount(event *trace.TSpanEvent) {
var sqlID int32
cacheSQLID := false
for _, annotation := range event.GetAnnotations() {
if annotation.GetKey() == constant.SQL_ID {
sqlID = annotation.Value.GetIntStringStringValue().GetIntValue()
cacheSQLID = true
break
}
}
// 没有sqlID 直接返回
if !cacheSQLID {
logger.Warn("unfind sql id")
return
}
sql, ok := s.sql.Get(sqlID)
if !ok {
sql = stats.NewSQL()
s.sql.Store(sqlID, sql)
}
sql.Duration += event.GetEndElapsed()
sql.Count++
if event.GetEndElapsed() > sql.MaxDuration {
sql.MaxDuration = event.GetEndElapsed()
}
if sql.MinDuration == 0 || sql.MinDuration > event.GetEndElapsed() {
sql.MinDuration = event.GetEndElapsed()
}
// 是否有异常抛出
if event.GetExceptionInfo() != nil {
sql.ErrCount++
}
}
// apiMapCounter 接口被哪些服务调用计算
func (s *Stats) apiMapCounter(span *trace.TSpan) {
apiStr := span.GetRPC()
if len(apiStr) <= 0 {
return
}
api, ok := s.apiMap.Apis[apiStr] //.APIS[]
if !ok {
api = stats.NewApi()
s.apiMap.Apis[apiStr] = api
}
var parentName string
var parentType int16
// spanID 为-1的情况该服务就是父节点,查不到被谁调用,这里可以考虑能不能抓到请求者到IP
if span.ParentSpanId == -1 {
parentName = "UNKNOWN"
parentType = constant.SERVERTYPE_UNKNOWN
} else {
parentName = span.GetParentApplicationName()
parentType = span.GetParentApplicationType()
}
parents, ok := api.Parents[parentName]
if !ok {
parents = stats.NewParent()
parents.Type = parentType
api.Parents[parentName] = parents
}
parents.AccessCount++
parents.AccessDuration += span.Elapsed
if span.GetErr() != 0 {
parents.ExceptionCount++
}
}
// parentMapCounter 计算服务拓扑图
func (s *Stats) parentMapCounter(span *trace.TSpan) {
// spanID 为-1的情况该服务就是父节点,请求者应该是没接入监控
if span.ParentSpanId == -1 {
s.srvMap.UnknowParent.AccessCount++
s.srvMap.UnknowParent.AccessDuration += span.GetElapsed()
return
}
s.srvMap.AppType = span.GetServiceType()
parent, ok := s.srvMap.Parents[span.GetParentApplicationName()]
if !ok {
parent = stats.NewSrvParent()
parent.Type = span.GetParentApplicationType()
s.srvMap.Parents[span.GetParentApplicationName()] = parent
}
parent.TargetCount++
if span.GetErr() != 0 {
parent.TargetErrCount++
}
}
// isDubbo 检查event类型是否为rpc接口
func isDubbo(eventType int16) bool {
if eventType == constant.DUBBO_PROVIDER ||
eventType == constant.DUBBO_CONSUMER ||
eventType == constant.DUBBO {
return true
}
return false
}
// isHttp 检查event类型是否为http接口
func isHttp(eventType int16) bool {
if eventType == constant.HTTP_CLIENT_3 ||
eventType == constant.HTTP_CLIENT_3_INTERNAL ||
eventType == constant.HTTP_CLIENT_4 ||
eventType == constant.HTTP_CLIENT_4_INTERNAL {
return true
}
return false
}
// isDB 检查event类型是否为数据库操作
func isDB(eventType int16) bool {
if eventType == constant.MYSQL_EXECUTE_QUERY ||
eventType == constant.ORACLE_EXECUTE_QUERY ||
eventType == constant.MARIADB_EXECUTE_QUERY {
return true
}
return false
}
func getip(destinationID string) (string, error) {
strs := strings.Split(destinationID, ":")
if len(strs) != 2 {
return "", fmt.Errorf("unknow addr")
}
if len(strs[0]) == 0 {
return "", fmt.Errorf("error ip")
}
return strs[0], nil
}
func gethost(destinationID string) (string, error) {
strs := strings.Split(destinationID, ":")
if len(strs) != 2 {
return "", fmt.Errorf("unknow addr")
}
if len(strs[0]) == 0 {
return "", fmt.Errorf("error ip")
}
return strs[0], nil
}
func (s *Stats) urlCounter(event *trace.TSpanEvent) {
// 是否已经缓存api
cacheApi := false
cacheCode := false
var code int32
var urlStr string
// Annotations
for _, annotation := range event.GetAnnotations() {
if annotation.GetKey() == constant.HTTP_URL {
cacheApi = true
urlStr = annotation.GetValue().GetStringValue()
continue
}
if annotation.GetKey() == constant.HTTP_STATUS_CODE {
cacheCode = true
code = annotation.GetValue().GetIntValue()
}
if cacheApi && cacheCode {
break
}
}
if cacheApi {
// 获取app信息
destinationID := event.GetDestinationId()
if len(destinationID) <= 0 {
return
}
// 通过IP获取app name
ip, err := getip(destinationID)
if err == nil {
appName, ok := gCollector.apps.getNameByIP(ip)
if ok {
destinationID = appName
}
} else {
// 如果不是IP尝试切一下-,如果还是找不到那么就使用destinationID
destinationID = cutVip(destinationID)
}
// 查找应用
app, ok := s.api.Apps[destinationID]
if !ok {
app = stats.NewApp()
s.api.Apps[destinationID] = app
}
url, ok := app.Urls[urlStr]
if !ok {
url = stats.NewUrl()
app.Urls[urlStr] = url
}
url.Duration += event.GetEndElapsed()
url.AccessCount++
if event.GetEndElapsed() > url.MaxDuration {
url.MaxDuration = event.GetEndElapsed()
}
if url.MinDuration == 0 || url.MinDuration > event.GetEndElapsed() {
url.MinDuration = event.GetEndElapsed()
}
// 检查code是否正确, 如果在策略里面没找到发现code,那么说明这次http请求失败
if cacheCode {
if _, ok := s.httpCodes[code]; !ok {
url.AccessCodeErrCount++
}
}
// 检查网络错误
// AccessNetErrCount int // 访问网络错误数
// 耗时小于满意时间满意次数加1
if event.GetEndElapsed() < misc.Conf.Stats.SatisfactionTime {
url.SatisfactionCount++
// 耗时小于可容忍时间,可容忍次数加一, 其他都为沮丧次数
} else if event.GetEndElapsed() < misc.Conf.Stats.TolerateTime {
url.TolerateCount++
}
}
}
func (s *Stats) dubboCounter(appName string, event *trace.TSpanEvent) {
for _, annotation := range event.GetAnnotations() {
if annotation.GetKey() == constant.DUBBO_RPC {
app, ok := s.api.Apps[appName]
if !ok {
app = stats.NewApp()
s.api.Apps[appName] = app
}
dubbo, ok := app.Dubbos[annotation.GetValue().GetStringValue()]
if !ok {
dubbo = stats.NewDubbo()
app.Dubbos[annotation.GetValue().GetStringValue()] = dubbo
}
dubbo.AccessCount++
dubbo.Duration += event.GetEndElapsed()
if dubbo.MinDuration == 0 || dubbo.MinDuration > event.GetEndElapsed() {
dubbo.MinDuration = event.GetEndElapsed()
}
if event.GetEndElapsed() > dubbo.MaxDuration {
dubbo.MaxDuration = event.GetEndElapsed()
}
if event.GetExceptionInfo() == nil {
dubbo.AccessExErrCount++
}
break
}
}
}
// eventCounter event数据统计 计算api信息, sql、method、异常等
func (s *Stats) eventCounter(appName, apiStr string, event *trace.TSpanEvent) {
if isDubbo(event.GetServiceType()) {
// dubbo
s.dubboCounter(appName, event)
} else if isHttp(event.GetServiceType()) {
// http 统计
s.urlCounter(event)
} else if isDB(event.GetServiceType()) {
// 数据库统计
s.sqlCount(event)
}
// method统计
s.methodCount(apiStr, event)
// exception
s.exceptionCount(event)
// app后续服务拓扑图计算
s.targetMapCounter(event)
}
// exceptionCount 异常统计
func (s *Stats) exceptionCount(event *trace.TSpanEvent) {
// 参看是否存在异常,不存在直接返回
exInfo := event.GetExceptionInfo()
if exInfo == nil {
return
}
method, ok := s.exception.Get(event.GetApiId())
if !ok {
method = stats.NewExMethod()
s.exception.Store(event.GetApiId(), method)
}
ex, ok := method.Get(exInfo.GetIntValue())
if !ok {
ex = stats.NewException()
method.Store(exInfo.GetIntValue(), ex)
}
ex.Duration += event.GetEndElapsed()
ex.Type = int(event.GetServiceType())
ex.Count++
if event.GetEndElapsed() > ex.MaxDuration {
ex.MaxDuration = event.GetEndElapsed()
}
if ex.MinDuration == 0 || ex.MinDuration > event.GetEndElapsed() {
ex.MinDuration = event.GetEndElapsed()
}
}
func (s *Stats) methodCount(apiStr string, event *trace.TSpanEvent) {
// 保存api到methods
if len(apiStr) > 0 {
if len(s.method.ApiStr) == 0 {
s.method.ApiStr = apiStr
}
}
method, ok := s.method.Get(event.GetApiId())
if !ok {
method = stats.NewMethod(event.GetServiceType())
s.method.Store(event.GetApiId(), method)
}
method.Duration += event.GetEndElapsed()
method.Count++
if event.GetEndElapsed() > method.MaxDuration {
method.MaxDuration = event.GetEndElapsed()
}
if method.MinDuration == 0 || method.MinDuration > event.GetEndElapsed() {
method.MinDuration = event.GetEndElapsed()
}
// 是否有异常抛出
if event.GetExceptionInfo() != nil {
method.ErrCount++
}
}
func cutVip(destination string) string {
var name string
names := strings.Split(destination, "-")
if len(names) == 1 {
name = destination
} else if len(names) == 3 {
name = names[1]
} else if len(names) == 4 {
name = names[1] + names[2]
}
return name
}
// 计算child拓扑图
func (s *Stats) targetMapCounter(event *trace.TSpanEvent) {
if event.ServiceType == constant.DUBBO_CONSUMER ||
event.ServiceType == constant.HTTP_CLIENT_3 ||
event.ServiceType == constant.HTTP_CLIENT_3_INTERNAL ||
event.ServiceType == constant.HTTP_CLIENT_4 ||
event.ServiceType == constant.HTTP_CLIENT_4_INTERNAL ||
event.ServiceType == constant.MYSQL_EXECUTE_QUERY ||
event.ServiceType == constant.REDIS ||
event.ServiceType == constant.ORACLE_EXECUTE_QUERY ||
event.ServiceType == constant.MARIADB_EXECUTE_QUERY {
destinationID := event.GetDestinationId()
if len(destinationID) <= 0 {
return
}
targets, ok := s.srvMap.Targets[event.ServiceType]
if !ok {
targets = make(map[string]*stats.Target)
s.srvMap.Targets[event.ServiceType] = targets
}
// http&&dubbo做特殊处理
if event.ServiceType == constant.HTTP_CLIENT_3 ||
event.ServiceType == constant.HTTP_CLIENT_3_INTERNAL ||
event.ServiceType == constant.HTTP_CLIENT_4 ||
event.ServiceType == constant.HTTP_CLIENT_4_INTERNAL ||
event.ServiceType == constant.DUBBO_CONSUMER {
ip, err := getip(destinationID)
if err == nil {
appName, ok := gCollector.apps.getNameByIP(ip)
if ok {
destinationID = appName
}
} else {
// 如果不是IP尝试切一下-,如果还是找不到那么就使用destinationID
destinationID = cutVip(destinationID)
}
}
target, ok := targets[destinationID]
if !ok {
target = stats.NewTarget()
targets[destinationID] = target
}
// http code统计
if event.ServiceType == constant.HTTP_CLIENT_3 ||
event.ServiceType == constant.HTTP_CLIENT_3_INTERNAL ||
event.ServiceType == constant.HTTP_CLIENT_4 ||
event.ServiceType == constant.HTTP_CLIENT_4_INTERNAL {
for _, annotation := range event.GetAnnotations() {
if annotation.GetKey() == constant.HTTP_STATUS_CODE {
if _, ok := s.httpCodes[annotation.Value.GetIntValue()]; !ok {
target.AccessErrCount++
}
}
}
}
target.AccessCount++
target.AccessDuration += event.EndElapsed
}
}