forked from trpc-ecosystem/go-metrics-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
117 lines (105 loc) · 3.41 KB
/
filter.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 prometheus
import (
"context"
"fmt"
"strings"
"time"
"trpc.group/trpc-go/trpc-go"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/errs"
"trpc.group/trpc-go/trpc-go/filter"
"trpc.group/trpc-go/trpc-go/metrics"
)
// Set the time consumption interval.
// 1 10 20 40 80 160 1000 2000 3000
var clientBounds = metrics.NewValueBounds(1.0, 10.0, 20.0, 40.0, 80.0, 160.0, 1000.0, 2000.0, 3000.0)
var serverBounds = clientBounds
// SetClientBounds set client filter buckets.
func SetClientBounds(b metrics.BucketBounds) {
clientBounds = b
}
// SetServerBounds set server filter buckets.
func SetServerBounds(b metrics.BucketBounds) {
serverBounds = b
}
func getLabels(msg codec.Msg, err error) []*metrics.Dimension {
code := fmt.Sprintf("%d", errs.RetOK)
if err != nil {
e, ok := err.(*errs.Error)
if ok && e != nil {
if e.Desc != "" {
code = fmt.Sprintf("%s_%d", e.Desc, e.Code)
} else {
code = fmt.Sprintf("%d", e.Code)
}
} else {
code = fmt.Sprintf("%d", errs.RetUnknown)
}
}
var remoteAddr, localAddr string
if msg.RemoteAddr() != nil {
remoteAddr = getAddr(msg.RemoteAddr().String())
}
if msg.LocalAddr() != nil {
localAddr = getAddr(msg.LocalAddr().String())
}
return []*metrics.Dimension{
{Name: "CallerApp", Value: msg.CallerApp()},
{Name: "CallerServer", Value: msg.CallerServer()},
{Name: "CallerService", Value: msg.CallerService()},
{Name: "CallerMethod", Value: msg.CallerMethod()},
{Name: "CalleeApp", Value: msg.CalleeApp()},
{Name: "CalleeServer", Value: msg.CalleeServer()},
{Name: "CalleeService", Value: msg.CalleeService()},
{Name: "CalleeMethod", Value: msg.CalleeMethod()},
{Name: "CalleeContainerName", Value: msg.CalleeContainerName()},
{Name: "CalleeSetName", Value: msg.CalleeSetName()},
{Name: "RemoteAddr", Value: remoteAddr},
{Name: "LocalAddr", Value: localAddr},
{Name: "Code", Value: code},
}
}
// ClientFilter client filter for prome.
func ClientFilter(ctx context.Context, req, rsp interface{}, handler filter.ClientHandleFunc) error {
begin := time.Now()
hErr := handler(ctx, req, rsp)
msg := trpc.Message(ctx)
if msg.CallerApp() == "gorm" {
return nil
}
labels := getLabels(msg, hErr)
ms := make([]*metrics.Metrics, 0)
t := float64(time.Since(begin)) / float64(time.Millisecond)
ms = append(ms,
metrics.NewMetrics("time", t, metrics.PolicyHistogram),
metrics.NewMetrics("requests", 1.0, metrics.PolicySUM))
metrics.Histogram("ClientFilter_time", clientBounds)
r := metrics.NewMultiDimensionMetricsX("ClientFilter", labels, ms)
_ = GetDefaultPrometheusSink().Report(r)
return hErr
}
// ServerFilter server filter for prome.
func ServerFilter(ctx context.Context, req interface{}, handler filter.ServerHandleFunc) (rsp interface{}, err error) {
begin := time.Now()
rsp, err = handler(ctx, req)
msg := trpc.Message(ctx)
labels := getLabels(msg, err)
ms := make([]*metrics.Metrics, 0)
t := float64(time.Since(begin)) / float64(time.Millisecond)
ms = append(ms,
metrics.NewMetrics("time", t, metrics.PolicyHistogram),
metrics.NewMetrics("requests", 1.0, metrics.PolicySUM))
metrics.Histogram("ServerFilter_time", serverBounds)
r := metrics.NewMultiDimensionMetricsX("ServerFilter", labels, ms)
_ = GetDefaultPrometheusSink().Report(r)
return rsp, err
}
// getAddr obtains IP, excluding ports.
func getAddr(add string) string {
var addr string
s := strings.Split(add, ":")
if len(s) > 0 {
addr = s[0]
}
return addr
}