/
metrics.go
145 lines (127 loc) · 2.95 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
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
package grpcmetrics
import (
"fmt"
"math"
"strings"
"sync"
"github.com/VictoriaMetrics/metrics"
"google.golang.org/grpc/codes"
)
type set struct {
*metrics.Set
}
func (s *set) counter(name string) any {
if s.Set != nil {
return s.Set.NewCounter(name)
}
return metrics.NewCounter(name)
}
func (s *set) histogram(name string) any {
if s.Set != nil {
return s.Set.NewHistogram(name)
}
return metrics.NewHistogram(name)
}
func newCounter(name string) *counter {
return &counter{newMetric(name)}
}
type counter struct {
*metric
}
func (c *counter) with(s *set, typ, method string, code codes.Code) *metrics.Counter {
return c.metric.with(typ, method, code, s.counter).(*metrics.Counter)
}
func newHistogram(name string) *histogram {
return &histogram{newMetric(name)}
}
type histogram struct {
*metric
}
func (h *histogram) with(s *set, typ, method string) *metrics.Histogram {
return h.metric.with(typ, method, noCode, s.histogram).(*metrics.Histogram)
}
func newMetric(name string) *metric {
return &metric{
name: name,
methods: map[string]map[codes.Code]any{},
}
}
type metric struct {
mu sync.RWMutex
name string
methods map[string]map[codes.Code]any // TODO: use metrics.Metric when it's exported
}
func (m *metric) with(
typ, method string, code codes.Code, new func(name string) any,
) any {
m.mu.RLock() // try read lock first and promote to write lock if needed
var upgraded bool
defer func() {
if upgraded {
m.mu.Unlock()
} else {
m.mu.RUnlock()
}
}()
methods, ok := m.methods[method]
if !ok {
upgraded = true
m.mu.RUnlock()
m.mu.Lock()
// r to w mutex upgrading is not atomic,
// so we need to check that another routine hasn't got here first
if m.methods[method] == nil {
m.methods[method] = map[codes.Code]any{}
}
methods = m.methods[method]
}
metric, ok := methods[code]
if !ok {
wasUpgraded := upgraded
if !upgraded {
upgraded = true
m.mu.RUnlock()
m.mu.Lock()
}
if wasUpgraded || methods[code] == nil {
service, method := splitMethodName(method)
var b strings.Builder
b.Grow(1024) // should be enough for almost all metric names
b.WriteString(m.name)
b.WriteString(`{grpc_type="`)
b.WriteString(typ)
b.WriteString(`",grpc_service="`)
b.WriteString(service)
b.WriteString(`",grpc_method="`)
b.WriteString(method)
if code != noCode {
b.WriteString(`",grpc_code="`)
b.WriteString(code.String())
}
b.WriteString(`"}`)
methods[code] = new(b.String())
}
metric = methods[code]
}
return metric
}
const noCode = math.MaxUint32
func splitMethodName(s string) (string, string) {
if len(s) == 0 || s[0] != '/' {
panic(fmt.Sprintf("malformed full method: %s", s))
}
i := strings.IndexByte(s[1:], '/')
return s[1 : i+1], s[i+2:]
}
func streamType(server, client bool) string {
switch {
case server && client:
return "bidi_stream"
case server:
return "server_stream"
case client:
return "client_stream"
default:
return unary
}
}