forked from weibocom/motan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
117 lines (101 loc) · 2.82 KB
/
metrics.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
package filter
import (
"time"
motan "github.com/weibocom/motan-go/core"
"github.com/weibocom/motan-go/metrics"
"github.com/weibocom/motan-go/protocol"
)
const (
MetricsTotalCountSuffix = ".total_count"
MetricsTotalCountSuffixLen = len(MetricsTotalCountSuffix)
MetricsBizErrorCountSuffix = ".biz_error_count"
MetricsOtherErrorCountSuffix = ".other_error_count"
MetricsSlowCountSuffix = ".slow_count"
)
type MetricsFilter struct {
next motan.EndPointFilter
}
func (m *MetricsFilter) GetIndex() int {
return 2
}
func (m *MetricsFilter) NewFilter(url *motan.URL) motan.Filter {
return &MetricsFilter{}
}
func (m *MetricsFilter) GetName() string {
return Metrics
}
func (m *MetricsFilter) HasNext() bool {
if m.next != nil {
return true
}
return false
}
func (m *MetricsFilter) GetType() int32 {
return motan.EndPointFilterType
}
func (m *MetricsFilter) SetNext(e motan.EndPointFilter) {
m.next = e
return
}
func (m *MetricsFilter) GetNext() motan.EndPointFilter {
if m.next != nil {
return m.next
}
return nil
}
func (m *MetricsFilter) Filter(caller motan.Caller, request motan.Request) motan.Response {
start := time.Now()
response := m.GetNext().Filter(caller, request)
proxy := false
provider := false
ctx := request.GetRPCContext(false)
if ctx != nil {
proxy = ctx.Proxy
}
// get role
role := "motan-client"
switch caller.(type) {
case motan.Provider:
provider = true
if proxy {
role = "motan-server-agent"
} else {
role = "motan-server"
}
case motan.EndPoint:
if proxy {
role = "motan-client-agent"
}
}
//get application
application := request.GetAttachment(protocol.MSource)
if provider {
application = caller.GetURL().GetParam(motan.ApplicationKey, "")
}
key := metrics.Escape(role) +
":" + metrics.Escape(application) +
":" + metrics.Escape(request.GetMethod())
addMetric(metrics.Escape(request.GetAttachment(protocol.MGroup)),
metrics.Escape(request.GetAttachment(protocol.MPath)),
key, time.Since(start).Nanoseconds()/1e6, response)
return response
}
func addMetric(group string, service string, key string, cost int64, response motan.Response) {
metrics.AddCounter(group, service, key+MetricsTotalCountSuffix, 1) //total_count
if response.GetException() != nil { //err_count
exception := response.GetException()
if exception.ErrType == motan.BizException {
metrics.AddCounter(group, service, key+MetricsBizErrorCountSuffix, 1)
} else {
metrics.AddCounter(group, service, key+MetricsOtherErrorCountSuffix, 1)
}
}
metrics.AddCounter(group, service, key+metrics.ElapseTimeSuffix(cost), 1)
if cost > 200 {
metrics.AddCounter(group, service, key+MetricsSlowCountSuffix, 1)
}
metrics.AddHistograms(group, service, key, cost)
}
func (m *MetricsFilter) SetContext(context *motan.Context) {
metrics.StartReporter(context)
}