forked from itspage/go-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
151 lines (128 loc) · 3.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
package router
import (
"time"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/registry"
router "github.com/micro/go-os/router/proto"
"github.com/rcrowley/go-metrics"
)
type stats struct {
// the service for these stats
service *registry.Service
// time of stats creation
timestamp int64
// used for the selector stats
dropped int64
success int64
errors int64
// track slas for endpoints
endpoints map[string]*endpoint
}
// endpoint metrics
type endpoint struct {
dropped metrics.Timer
success metrics.Timer
errors metrics.Timer
}
func newEndpoint() *endpoint {
return &endpoint{
dropped: metrics.NewTimer(),
success: metrics.NewTimer(),
errors: metrics.NewTimer(),
}
}
func newStats(service *registry.Service) *stats {
return &stats{
service: service,
endpoints: make(map[string]*endpoint),
timestamp: time.Now().Unix(),
}
}
func (s *stats) Mark(service string, node *registry.Node, err error) {
if err == nil {
s.success++
return
}
// parse so we can get the error code and see if it was dropped (408)
perr, ok := err.(*errors.Error)
if !ok {
perr = &errors.Error{Id: service, Code: 500, Detail: err.Error()}
}
switch perr.Code {
case 408:
s.dropped++
default:
s.errors++
}
}
func (s *stats) Record(r Request, node *registry.Node, d time.Duration, err error) {
endpoint, ok := s.endpoints[r.Method()]
if !ok {
endpoint = newEndpoint()
s.endpoints[r.Method()] = endpoint
}
// track the success
if err == nil {
endpoint.success.Update(d)
return
}
// track the error
// parse so we can get the error code and see if it was dropped (408)
perr, ok := err.(*errors.Error)
if !ok {
perr = &errors.Error{Id: r.Service(), Code: 500, Detail: err.Error()}
}
switch perr.Code {
case 408:
endpoint.dropped.Update(d)
default:
endpoint.errors.Update(d)
}
}
func (s *stats) Reset() {
s.timestamp = time.Now().Unix()
s.dropped = 0
s.success = 0
s.errors = 0
for k, _ := range s.endpoints {
s.endpoints[k] = newEndpoint()
}
}
func (s *stats) ToProto(client *registry.Service) *router.Stats {
t := time.Now().Unix()
stats := &router.Stats{
Service: serviceToProto(s.service),
Client: serviceToProto(client),
Timestamp: t,
Duration: t - s.timestamp,
Selected: &router.Selected{
Errors: &router.Metrics{Count: s.errors},
Success: &router.Metrics{Count: s.success},
Dropped: &router.Metrics{Count: s.dropped},
},
}
for name, endpoint := range s.endpoints {
stats.EndpointStats = append(stats.EndpointStats, &router.EndpointStats{
Name: name,
Errors: &router.Metrics{
Count: endpoint.errors.Count(),
Mean: endpoint.errors.Mean(),
StdDev: endpoint.errors.StdDev(),
Upper95: endpoint.errors.Percentile(0.95),
},
Success: &router.Metrics{
Count: endpoint.success.Count(),
Mean: endpoint.success.Mean(),
StdDev: endpoint.success.StdDev(),
Upper95: endpoint.success.Percentile(0.95),
},
Dropped: &router.Metrics{
Count: endpoint.dropped.Count(),
Mean: endpoint.dropped.Mean(),
StdDev: endpoint.dropped.StdDev(),
Upper95: endpoint.dropped.Percentile(0.95),
},
})
}
return stats
}