-
Notifications
You must be signed in to change notification settings - Fork 28
/
stats.go
178 lines (155 loc) · 4.18 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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"bytes"
"fmt"
"sort"
"sync"
"time"
"v.io/x/ref/lib/stats"
"v.io/x/ref/lib/stats/counter"
"v.io/x/ref/lib/stats/histogram"
"v.io/v23/naming"
)
type outstandingCall struct {
remote naming.Endpoint
method string
when time.Time
}
type outstandingCalls []*outstandingCall
func (oc outstandingCalls) Less(i, j int) bool {
return oc[i].when.Before(oc[j].when)
}
func (oc outstandingCalls) Swap(i, j int) {
oc[i], oc[j] = oc[j], oc[i]
}
func (oc outstandingCalls) Len() int {
return len(oc)
}
type outstandingStats struct {
prefix string
mu sync.Mutex
outstanding map[*outstandingCall]bool
}
func newOutstandingStats(prefix string) *outstandingStats {
o := &outstandingStats{
prefix: prefix,
outstanding: make(map[*outstandingCall]bool),
}
stats.NewStringFunc(prefix, o.String)
return o
}
func (o *outstandingStats) String() string {
defer o.mu.Unlock()
o.mu.Lock()
if len(o.outstanding) == 0 {
return "No outstanding calls."
}
calls := make(outstandingCalls, 0, len(o.outstanding))
for o := range o.outstanding {
calls = append(calls, o)
}
sort.Sort(calls)
now := time.Now()
buf := &bytes.Buffer{}
for _, o := range calls {
fmt.Fprintf(buf, "%s age:%v from:%v\n", o.method, now.Sub(o.when), o.remote)
}
return buf.String()
}
func (o *outstandingStats) close() {
stats.Delete(o.prefix) //nolint:errcheck
}
func (o *outstandingStats) start(method string, remote naming.Endpoint) func() {
o.mu.Lock()
nw := &outstandingCall{
method: method,
remote: remote,
when: time.Now(),
}
o.outstanding[nw] = true
o.mu.Unlock()
return func() {
o.mu.Lock()
delete(o.outstanding, nw)
o.mu.Unlock()
}
}
type rpcStats struct {
mu sync.RWMutex
prefix string
methods map[string]*perMethodStats
blessingsCacheStats *blessingsCacheStats
}
func newRPCStats(prefix string) *rpcStats {
return &rpcStats{
prefix: prefix,
methods: make(map[string]*perMethodStats),
blessingsCacheStats: newBlessingsCacheStats(prefix),
}
}
type perMethodStats struct {
latency *histogram.Histogram
}
func (s *rpcStats) stop() {
stats.Delete(s.prefix) //nolint:errcheck
}
func (s *rpcStats) record(method string, latency time.Duration) {
// Try first with a read lock. This will succeed in the most common
// case. If it fails, try again with a write lock and create the stats
// objects if they are still not there.
s.mu.RLock()
m, ok := s.methods[method]
s.mu.RUnlock()
if !ok {
m = s.newPerMethodStats(method)
}
m.latency.Add(int64(latency / time.Millisecond)) //nolint:errcheck
}
//nolint:unused
func (s *rpcStats) recordBlessingCache(hit bool) {
s.blessingsCacheStats.incr(hit)
}
// newPerMethodStats creates a new perMethodStats object if one doesn't exist
// already. It returns the newly created object, or the already existing one.
func (s *rpcStats) newPerMethodStats(method string) *perMethodStats {
s.mu.Lock()
defer s.mu.Unlock()
m, ok := s.methods[method]
if !ok {
name := naming.Join(s.prefix, "methods", method, "latency-ms")
s.methods[method] = &perMethodStats{
latency: stats.NewHistogram(name, histogram.Options{
NumBuckets: 25,
GrowthFactor: 1,
SmallestBucketSize: 1,
MinValue: 0,
}),
}
m = s.methods[method]
}
return m
}
// blessingsCacheStats keeps blessing cache hits and total calls received to determine
// how often the blessingCache is being used.
type blessingsCacheStats struct {
callsReceived, cacheHits *counter.Counter
}
func newBlessingsCacheStats(prefix string) *blessingsCacheStats {
cachePrefix := naming.Join(prefix, "security", "blessings", "cache")
return &blessingsCacheStats{
callsReceived: stats.NewCounter(naming.Join(cachePrefix, "attempts")),
cacheHits: stats.NewCounter(naming.Join(cachePrefix, "hits")),
}
}
// Incr increments the cache attempt counter and the cache hit counter if hit is true.
//
//nolint:unused
func (s *blessingsCacheStats) incr(hit bool) {
s.callsReceived.Incr(1)
if hit {
s.cacheHits.Incr(1)
}
}