...
1
16
17
18
19 package metrics
20
21 import (
22 "context"
23 "net/url"
24 "sync"
25 "time"
26 )
27
28 var registerMetrics sync.Once
29
30
31 type DurationMetric interface {
32 Observe(duration time.Duration)
33 }
34
35
36 type ExpiryMetric interface {
37 Set(expiry *time.Time)
38 }
39
40
41 type LatencyMetric interface {
42 Observe(ctx context.Context, verb string, u url.URL, latency time.Duration)
43 }
44
45 type ResolverLatencyMetric interface {
46 Observe(ctx context.Context, host string, latency time.Duration)
47 }
48
49
50 type SizeMetric interface {
51 Observe(ctx context.Context, verb string, host string, size float64)
52 }
53
54
55 type ResultMetric interface {
56 Increment(ctx context.Context, code string, method string, host string)
57 }
58
59
60 type CallsMetric interface {
61
62 Increment(exitCode int, callStatus string)
63 }
64
65
66
67 type RetryMetric interface {
68 IncrementRetry(ctx context.Context, code string, method string, host string)
69 }
70
71
72 type TransportCacheMetric interface {
73 Observe(value int)
74 }
75
76
77
78 type TransportCreateCallsMetric interface {
79 Increment(result string)
80 }
81
82 var (
83
84 ClientCertExpiry ExpiryMetric = noopExpiry{}
85
86 ClientCertRotationAge DurationMetric = noopDuration{}
87
88 RequestLatency LatencyMetric = noopLatency{}
89
90 ResolverLatency ResolverLatencyMetric = noopResolverLatency{}
91
92 RequestSize SizeMetric = noopSize{}
93
94 ResponseSize SizeMetric = noopSize{}
95
96 RateLimiterLatency LatencyMetric = noopLatency{}
97
98 RequestResult ResultMetric = noopResult{}
99
100
101 ExecPluginCalls CallsMetric = noopCalls{}
102
103
104 RequestRetry RetryMetric = noopRetry{}
105
106
107 TransportCacheEntries TransportCacheMetric = noopTransportCache{}
108
109
110 TransportCreateCalls TransportCreateCallsMetric = noopTransportCreateCalls{}
111 )
112
113
114 type RegisterOpts struct {
115 ClientCertExpiry ExpiryMetric
116 ClientCertRotationAge DurationMetric
117 RequestLatency LatencyMetric
118 ResolverLatency ResolverLatencyMetric
119 RequestSize SizeMetric
120 ResponseSize SizeMetric
121 RateLimiterLatency LatencyMetric
122 RequestResult ResultMetric
123 ExecPluginCalls CallsMetric
124 RequestRetry RetryMetric
125 TransportCacheEntries TransportCacheMetric
126 TransportCreateCalls TransportCreateCallsMetric
127 }
128
129
130
131 func Register(opts RegisterOpts) {
132 registerMetrics.Do(func() {
133 if opts.ClientCertExpiry != nil {
134 ClientCertExpiry = opts.ClientCertExpiry
135 }
136 if opts.ClientCertRotationAge != nil {
137 ClientCertRotationAge = opts.ClientCertRotationAge
138 }
139 if opts.RequestLatency != nil {
140 RequestLatency = opts.RequestLatency
141 }
142 if opts.ResolverLatency != nil {
143 ResolverLatency = opts.ResolverLatency
144 }
145 if opts.RequestSize != nil {
146 RequestSize = opts.RequestSize
147 }
148 if opts.ResponseSize != nil {
149 ResponseSize = opts.ResponseSize
150 }
151 if opts.RateLimiterLatency != nil {
152 RateLimiterLatency = opts.RateLimiterLatency
153 }
154 if opts.RequestResult != nil {
155 RequestResult = opts.RequestResult
156 }
157 if opts.ExecPluginCalls != nil {
158 ExecPluginCalls = opts.ExecPluginCalls
159 }
160 if opts.RequestRetry != nil {
161 RequestRetry = opts.RequestRetry
162 }
163 if opts.TransportCacheEntries != nil {
164 TransportCacheEntries = opts.TransportCacheEntries
165 }
166 if opts.TransportCreateCalls != nil {
167 TransportCreateCalls = opts.TransportCreateCalls
168 }
169 })
170 }
171
172 type noopDuration struct{}
173
174 func (noopDuration) Observe(time.Duration) {}
175
176 type noopExpiry struct{}
177
178 func (noopExpiry) Set(*time.Time) {}
179
180 type noopLatency struct{}
181
182 func (noopLatency) Observe(context.Context, string, url.URL, time.Duration) {}
183
184 type noopResolverLatency struct{}
185
186 func (n noopResolverLatency) Observe(ctx context.Context, host string, latency time.Duration) {
187 }
188
189 type noopSize struct{}
190
191 func (noopSize) Observe(context.Context, string, string, float64) {}
192
193 type noopResult struct{}
194
195 func (noopResult) Increment(context.Context, string, string, string) {}
196
197 type noopCalls struct{}
198
199 func (noopCalls) Increment(int, string) {}
200
201 type noopRetry struct{}
202
203 func (noopRetry) IncrementRetry(context.Context, string, string, string) {}
204
205 type noopTransportCache struct{}
206
207 func (noopTransportCache) Observe(int) {}
208
209 type noopTransportCreateCalls struct{}
210
211 func (noopTransportCreateCalls) Increment(string) {}
212
View as plain text