-
Notifications
You must be signed in to change notification settings - Fork 55
/
metrics.go
343 lines (291 loc) · 7.83 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package unleash
import (
"bytes"
"context"
"encoding/json"
"fmt"
"math"
"net/http"
"net/url"
"runtime"
"sync"
"time"
"github.com/Unleash/unleash-client-go/v4/internal/api"
)
// MetricsData represents the data sent to the unleash server.
type MetricsData struct {
// AppName is the name of the application.
AppName string `json:"appName"`
// InstanceID is the instance identifier.
InstanceID string `json:"instanceId"`
// Bucket is the payload data sent to the server.
Bucket api.Bucket `json:"bucket"`
// The runtime version of our Platform
PlatformVersion string `json:"platformVersion"`
// The runtime name of our Platform
PlatformName string `json:"platformName"`
// Which version of Yggdrasil is being used
YggdrasilVersion *string `json:"yggdrasilVersion"`
// Optional field that describes the sdk version (name:version)
SDKVersion string `json:"sdkVersion"`
// Which version of the Unleash-Client-Spec is this SDK validated against
SpecVersion string `json:"specVersion"`
}
// ClientData represents the data sent to the unleash during registration.
type ClientData struct {
// AppName is the name of the application.
AppName string `json:"appName"`
// InstanceID is the instance identifier.
InstanceID string `json:"instanceId"`
// Optional field that describes the sdk version (name:version)
SDKVersion string `json:"sdkVersion"`
// Strategies is a list of names of the strategies supported by the client.
Strategies []string `json:"strategies"`
// Started indicates the time at which the client was created.
Started time.Time `json:"started"`
// Interval specifies the time interval (in ms) that the client is using for refreshing
// feature toggles.
Interval int64 `json:"interval"`
PlatformVersion string `json:"platformVersion"`
PlatformName string `json:"platformName"`
YggdrasilVersion *string `json:"yggdrasilVersion"`
// Which version of the Unleash-Client-Spec is this SDK validated against
SpecVersion string `json:"specVersion"`
}
type metric struct {
// Name is the name of the feature toggle.
Name string
// Enabled indicates whether the feature was enabled or not.
Enabled bool
}
type metrics struct {
metricsChannels
options metricsOptions
started time.Time
bucketMu sync.Mutex
bucket api.Bucket
ticker *time.Ticker
close chan struct{}
closed chan struct{}
ctx context.Context
cancel func()
maxSkips float64
errors float64
skips float64
}
func newMetrics(options metricsOptions, channels metricsChannels) *metrics {
m := &metrics{
metricsChannels: channels,
options: options,
started: time.Now(),
close: make(chan struct{}),
closed: make(chan struct{}),
maxSkips: 10,
errors: 0,
skips: 0,
}
ctx, cancel := context.WithCancel(context.Background())
m.ctx = ctx
m.cancel = cancel
if m.options.httpClient == nil {
m.options.httpClient = http.DefaultClient
}
m.resetBucket()
if m.options.metricsInterval <= 0 {
m.options.disableMetrics = true
}
if !m.options.disableMetrics {
m.ticker = time.NewTicker(m.options.metricsInterval)
m.registerInstance()
go m.sync()
}
return m
}
func (m *metrics) Close() error {
if !m.options.disableMetrics {
m.ticker.Stop()
m.cancel()
close(m.close)
<-m.closed
}
return nil
}
func (m *metrics) sync() {
for {
select {
case <-m.ticker.C:
if m.skips == 0 {
m.sendMetrics()
} else {
m.decrementSkip()
}
case <-m.close:
close(m.closed)
return
}
}
}
func (m *metrics) registerInstance() {
u, _ := m.options.url.Parse("./client/register")
payload := m.getClientData()
resp, err := m.doPost(u, payload)
if err != nil {
m.err(err)
return
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusMultipleChoices {
m.warn(fmt.Errorf("%s return %d", u.String(), resp.StatusCode))
}
m.registered <- payload
}
func (m *metrics) backoff() {
m.errors = math.Min(m.maxSkips, m.errors+1)
m.skips = m.errors
}
func (m *metrics) configurationError() {
m.errors = m.maxSkips
m.skips = m.errors
}
func (m *metrics) successfulPost() {
m.errors = math.Max(0, m.errors-1)
m.skips = m.errors
}
func (m *metrics) decrementSkip() {
m.skips = math.Max(0, m.skips-1)
}
func (m *metrics) sendMetrics() {
m.bucketMu.Lock()
bucket := m.resetBucket()
m.bucketMu.Unlock()
if bucket.IsEmpty() {
return
}
bucket.Stop = time.Now()
payload := MetricsData{
AppName: m.options.appName,
InstanceID: m.options.instanceId,
Bucket: bucket,
SDKVersion: fmt.Sprintf("%s:%s", clientName, clientVersion),
PlatformName: "go",
PlatformVersion: runtime.Version(),
YggdrasilVersion: nil,
SpecVersion: specVersion,
}
u, _ := m.options.url.Parse("./client/metrics")
resp, err := m.doPost(u, payload)
if err != nil {
m.err(err)
return
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusMultipleChoices {
if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusNotFound {
m.configurationError()
} else if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode >= http.StatusInternalServerError {
m.backoff()
}
m.warn(fmt.Errorf("%s return %d", u.String(), resp.StatusCode))
// The post failed, re-add the metrics we attempted to send so
// they are included in the next post.
for name, tc := range bucket.Toggles {
m.add(name, true, tc.Yes)
m.add(name, false, tc.No)
}
m.bucketMu.Lock()
// Set the start time of the current bucket to the one we
// attempted to send.
m.bucket.Start = bucket.Start
m.bucketMu.Unlock()
} else {
m.successfulPost()
m.sent <- payload
}
}
func (m *metrics) doPost(url *url.URL, payload interface{}) (*http.Response, error) {
var body bytes.Buffer
enc := json.NewEncoder(&body)
if err := enc.Encode(payload); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url.String(), &body)
if err != nil {
return nil, err
}
req = req.WithContext(m.ctx)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("UNLEASH-APPNAME", m.options.appName)
req.Header.Add("UNLEASH-INSTANCEID", m.options.instanceId)
req.Header.Add("User-Agent", m.options.appName)
for k, v := range m.options.customHeaders {
req.Header[k] = v
}
return m.options.httpClient.Do(req)
}
func (m *metrics) add(name string, enabled bool, num int32) {
if m.options.disableMetrics || num == 0 {
return
}
m.bucketMu.Lock()
defer m.bucketMu.Unlock()
t, exists := m.bucket.Toggles[name]
if !exists {
t = api.ToggleCount{
Variants: map[string]int32{},
}
}
if enabled {
t.Yes += num
} else {
t.No += num
}
m.bucket.Toggles[name] = t
}
func (m *metrics) count(name string, enabled bool) {
if m.options.disableMetrics {
return
}
m.add(name, enabled, 1)
m.metricsChannels.count <- metric{Name: name, Enabled: enabled}
}
func (m *metrics) countVariants(name string, enabled bool, variantName string) {
if m.options.disableMetrics {
return
}
m.add(name, enabled, 1)
m.metricsChannels.count <- metric{Name: name, Enabled: enabled}
m.bucketMu.Lock()
defer m.bucketMu.Unlock()
t, _ := m.bucket.Toggles[name]
if len(t.Variants) == 0 {
t.Variants = make(map[string]int32)
}
if _, ok := t.Variants[variantName]; !ok {
t.Variants[variantName] = 1
} else {
t.Variants[variantName] += 1
}
m.bucket.Toggles[name] = t
}
func (m *metrics) resetBucket() api.Bucket {
prev := m.bucket
m.bucket = api.Bucket{
Start: time.Now(),
Toggles: map[string]api.ToggleCount{},
}
return prev
}
func (m *metrics) getClientData() ClientData {
return ClientData{
m.options.appName,
m.options.instanceId,
fmt.Sprintf("%s:%s", clientName, clientVersion),
m.options.strategies,
m.started,
int64(m.options.metricsInterval.Seconds()),
runtime.Version(),
"go",
nil,
specVersion,
}
}