/
client.go
570 lines (494 loc) · 15.6 KB
/
client.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
Package tsclient provides a common client for sending metrics to the DO timeseries system.
The timeseries system is a push-based system where metrics are submitted in batches
via the SendMetrics method at fixed time intervals. Metrics are submitted to the wharf
server.
Wharf responds with a rate-limit value which the client must wait that many seconds
or longer before submitting the next batch of metrics -- this is exposed via the WaitDuration()
method.
*/
package tsclient
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"sync/atomic"
"time"
"github.com/golang/snappy"
"github.com/pkg/errors"
"github.com/digitalocean/do-agent/internal/log"
"github.com/digitalocean/do-agent/pkg/clients/tsclient/structuredstream"
)
const (
binaryContentType = "application/timeseries-binary-0"
jsonContentType = "application/json"
userAgentHeader = "User-Agent"
authKeyHeader = "X-Auth-Key"
contentTypeHeader = "Content-Type"
internalProxyURL = "http://169.254.169.254"
defaultWaitIntervalSeconds = 60
defaultMaxBatchSize = 1000
defaultMaxMetricLength = 512
maxWaitInterval = 10 * time.Minute
)
// Client is an interface for sending batches of metrics
type Client interface {
AddMetric(def *Definition, value float64, labels ...string) error
AddMetricWithTime(def *Definition, t time.Time, value float64, labels ...string) error
Flush() error
WaitDuration() time.Duration
MaxBatchSize() int
MaxMetricLength() int
ResetWaitTimer()
}
// HTTPClient is used to send metrics via http
type HTTPClient struct {
httpClient *http.Client
userAgent string
metadataEndpoint string
radarEndpoint string
wharfEndpoints []string
wharfEndpointSSLHostname string
lastFlushAttempt time.Time
lastFlushConnection time.Time
waitIntervalSeconds int32
maxBatchSize int32
maxMetricLength int32
numConsecutiveFailures int
bootstrapRequired bool
trusted bool
lastSend map[string]int64
isZeroTime bool
// variables only used when trusted
appName string
appKey string
// variables only used when non-trusted
dropletID string
region string
buf *bytes.Buffer
w *snappy.Writer
}
// ClientOptions are client options
type ClientOptions struct {
UserAgent string
WharfEndpoints []string
WharfEndpointSSLHostname string
AppName string
AppKey string
MetadataEndpoint string
RadarEndpoint string
Timeout time.Duration
IsTrusted bool
MaxBatchSize int
MaxMetricLength int
}
// ClientOptFn allows for overriding options
type ClientOptFn func(*ClientOptions)
// WithWharfEndpoint overrides the default wharf endpoint, this option must be set when WithTrustedAppKey is used.
func WithWharfEndpoint(endpoint string) ClientOptFn {
return WithWharfEndpoints([]string{endpoint})
}
// WithWharfEndpoints overrides the default wharf endpoint, this option must be set when WithTrustedAppKey is used.
func WithWharfEndpoints(endpoints []string) ClientOptFn {
return func(o *ClientOptions) {
o.WharfEndpoints = endpoints
}
}
// WithWharfEndpointSSLHostname overrides the default wharf endpoint, this option must be set when WithTrustedAppKey is used.
func WithWharfEndpointSSLHostname(hostname string) ClientOptFn {
return func(o *ClientOptions) {
o.WharfEndpointSSLHostname = hostname
}
}
// WithMetadataEndpoint overrides the default metadata endpoint, this option is only applicable to non-trusted clients (i.e. running on a customer droplet).
func WithMetadataEndpoint(endpoint string) ClientOptFn {
return func(o *ClientOptions) {
o.MetadataEndpoint = endpoint
}
}
// WithRadarEndpoint overrides the default radar endpoint, this option is only applicable to non-trusted clients (i.e. running on a customer droplet).
func WithRadarEndpoint(endpoint string) ClientOptFn {
return func(o *ClientOptions) {
o.RadarEndpoint = endpoint
}
}
// WithTimeout overrides the default wharf endpoint
func WithTimeout(timeout time.Duration) ClientOptFn {
return func(o *ClientOptions) {
o.Timeout = timeout
}
}
// WithUserAgent overrides the http user agent
func WithUserAgent(s string) ClientOptFn {
return func(o *ClientOptions) {
o.UserAgent = s
}
}
// WithTrustedAppKey disables metadata authentication; trusted apps can override the host_id and user_id tags.
func WithTrustedAppKey(appName, appKey string) ClientOptFn {
return func(o *ClientOptions) {
o.AppName = appName
o.AppKey = appKey
o.IsTrusted = true
}
}
// WithDefaultLimits set default metric limits. These will always be overridden by the server after first write
func WithDefaultLimits(maxBatchSize, maxMetricLength int) ClientOptFn {
return func(o *ClientOptions) {
o.MaxBatchSize = maxBatchSize
o.MaxMetricLength = maxMetricLength
}
}
// New creates a new client
func New(opts ...ClientOptFn) Client {
opt := &ClientOptions{
UserAgent: "tsclient-unknown",
Timeout: 10 * time.Second,
MetadataEndpoint: fmt.Sprintf("%s/metadata", internalProxyURL),
RadarEndpoint: internalProxyURL,
}
for _, fn := range opts {
fn(opt)
}
if opt.MaxMetricLength == 0 {
opt.MaxMetricLength = defaultMaxMetricLength
}
if opt.MaxBatchSize == 0 {
opt.MaxBatchSize = defaultMaxBatchSize
}
var tlsConfig tls.Config
if opt.WharfEndpointSSLHostname != "" {
tlsConfig.ServerName = opt.WharfEndpointSSLHostname
}
httpClient := &http.Client{
Timeout: opt.Timeout,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: opt.Timeout,
}).Dial,
TLSHandshakeTimeout: opt.Timeout,
ResponseHeaderTimeout: opt.Timeout,
DisableKeepAlives: true,
TLSClientConfig: &tlsConfig,
},
}
if opt.IsTrusted {
if len(opt.WharfEndpoints) == 0 {
panic("WithWharfEndpoint() must be used WithTrustedAppKey")
}
if opt.AppName == "" {
panic("appname must be set")
}
}
return &HTTPClient{
userAgent: opt.UserAgent,
metadataEndpoint: opt.MetadataEndpoint,
radarEndpoint: opt.RadarEndpoint,
wharfEndpoints: opt.WharfEndpoints,
wharfEndpointSSLHostname: opt.WharfEndpointSSLHostname,
appName: opt.AppName,
appKey: opt.AppKey,
httpClient: httpClient,
waitIntervalSeconds: defaultWaitIntervalSeconds,
maxBatchSize: int32(opt.MaxBatchSize),
maxMetricLength: int32(opt.MaxMetricLength),
bootstrapRequired: true,
trusted: opt.IsTrusted,
lastSend: map[string]int64{},
}
}
func (c *HTTPClient) bootstrapFromMetadata() error {
var err error
if c.trusted {
return nil
}
c.dropletID, err = c.GetDropletID()
if err != nil {
return err
}
log.Debug("droplet ID: %s", c.dropletID)
c.region, err = c.GetRegion()
if err != nil {
return err
}
log.Debug("region: %s", c.region)
authToken, err := c.GetAuthToken()
if err != nil {
return err
}
log.Debug("auth token: %s", truncate(authToken, 5))
appKey, err := c.GetAppKey(authToken)
if err != nil {
return err
}
c.appKey = appKey
log.Debug("appkey: %s", truncate(c.appKey, 5))
return nil
}
// url returns a potentially randomized endpoint to send data to
// the url must constantly be randomized; otherwise the cache across all wharf endpoints
// will be skewed (i.e. only a single node will know about the droplet -> user ID lookups)
// and when a restart/failure finally happens, then a different wharf endpoint will be picked,
// and it wont have anything in its cache.
func (c *HTTPClient) url() string {
if c.trusted {
if len(c.wharfEndpoints) == 0 {
panic("trusted app with no wharf endpoints; shouldnt happen")
}
if c.appName == "" {
panic("appname not defined; shouldnt happen")
}
endpoint := c.wharfEndpoints[rand.Intn(len(c.wharfEndpoints))]
return fmt.Sprintf("%s/v1/metrics/trusted/%s", endpoint, c.appName)
}
endpoint := internalProxyURL
if len(c.wharfEndpoints) > 0 {
endpoint = c.wharfEndpoints[rand.Intn(len(c.wharfEndpoints))]
}
return fmt.Sprintf("%s/v1/metrics/droplet_id/%s", endpoint, c.dropletID)
}
// WaitDuration returns the duration before the next batch of metrics will be accepted
func (c *HTTPClient) WaitDuration() time.Duration {
d := time.Since(c.lastFlushAttempt)
wi := time.Second * time.Duration(atomic.LoadInt32(&c.waitIntervalSeconds))
if d < wi {
return wi - d
}
return 0
}
// MaxBatchSize returns the maximum amount of metrics that may be sent per batch
func (c *HTTPClient) MaxBatchSize() int {
return int(atomic.LoadInt32(&c.maxBatchSize))
}
// MaxMetricLength is the maximum length of a metric that may be sent (all labels and values combined)
func (c *HTTPClient) MaxMetricLength() int {
return int(atomic.LoadInt32(&c.maxMetricLength))
}
// AddMetric adds a metric to the batch
func (c *HTTPClient) AddMetric(def *Definition, value float64, labels ...string) error {
return c.addMetricWithMSEpochTime(def, 0, value, labels...)
}
// AddMetricWithTime adds a metric to the batch
func (c *HTTPClient) AddMetricWithTime(def *Definition, t time.Time, value float64, labels ...string) error {
ms := t.UTC().UnixNano() / int64(time.Millisecond)
return c.addMetricWithMSEpochTime(def, ms, value, labels...)
}
func (c *HTTPClient) addMetricWithMSEpochTime(def *Definition, ms int64, value float64, labels ...string) error {
isZeroTime := bool(ms == 0)
if c.buf == nil {
c.buf = new(bytes.Buffer)
c.w = snappy.NewBufferedWriter(c.buf)
c.lastSend = map[string]int64{}
c.isZeroTime = isZeroTime
} else {
if isZeroTime != c.isZeroTime {
panic("client support for AddMetrics and AddMetricWithTime is mutually exclusive")
}
}
lfm, err := GetLFM(def, labels)
if err != nil {
return errors.Wrap(err, "failed to get LFM")
}
if !isZeroTime {
// ensure sufficient time between reported metric values
if lastSend, ok := c.lastSend[lfm]; ok && (time.Duration(ms-lastSend)*time.Millisecond) < c.WaitDuration() {
return errors.WithStack(ErrSendTooFrequent)
}
c.lastSend[lfm] = ms
}
writer := structuredstream.NewWriter(c.w)
writer.WriteUint16PrefixedString(lfm)
writer.Write(ms)
writer.Write(value)
if err := writer.Error(); err != nil {
log.Error("failed to write: %+v", err)
return errors.WithStack(ErrWriteFailure)
}
return nil
}
func (c *HTTPClient) clearBufferedMetrics() {
c.buf = nil
// clean lastSend (potential memory leak otherwise)
nowMS := time.Now().UTC().UnixNano() / int64(time.Millisecond)
for lfm, t := range c.lastSend {
if (nowMS - t) > 60*60*1000 {
delete(c.lastSend, lfm)
}
}
}
// ResetWaitTimer causes the wait duration timer to reset
func (c *HTTPClient) ResetWaitTimer() {
c.lastFlushAttempt = time.Now()
}
// Flush sends the batch of metrics to wharf
func (c *HTTPClient) Flush() error {
now := time.Now()
if now.Sub(c.lastFlushAttempt) < c.WaitDuration() {
return ErrFlushTooFrequent
}
c.lastFlushAttempt = now
if c.numConsecutiveFailures > 3 {
timeSinceLastConnection := now.Sub(c.lastFlushConnection)
requiredWait := time.Minute * time.Duration(c.numConsecutiveFailures+rand.Intn(3))
if requiredWait > maxWaitInterval {
requiredWait = maxWaitInterval
}
if timeSinceLastConnection < requiredWait {
return ErrCircuitBreaker
}
}
if c.buf == nil {
return nil
}
c.lastFlushConnection = now
if c.bootstrapRequired || c.numConsecutiveFailures > 60 {
if err := c.bootstrapFromMetadata(); err != nil {
c.numConsecutiveFailures++
return err
}
c.bootstrapRequired = false
}
err := c.w.Flush()
if err != nil {
return err
}
url := c.url()
log.Debug("sending metrics to %s", url)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(c.buf.Bytes()))
if err != nil {
c.numConsecutiveFailures++
if c.isZeroTime {
c.clearBufferedMetrics()
}
return err
}
req.Header.Add(userAgentHeader, c.userAgent)
if c.wharfEndpointSSLHostname != "" {
req.Host = c.wharfEndpointSSLHostname
}
req.Header.Set(contentTypeHeader, binaryContentType)
req.Header.Add(authKeyHeader, c.appKey)
resp, err := c.httpClient.Do(req.WithContext(context.Background()))
if err != nil {
c.numConsecutiveFailures++
if c.isZeroTime {
c.clearBufferedMetrics()
}
return err
}
contentType := resp.Header.Get(contentTypeHeader)
if contentType == jsonContentType {
defer c.handleSonarResponse(resp.Body)
}
if resp.StatusCode != http.StatusAccepted {
c.numConsecutiveFailures++
if c.isZeroTime {
c.clearBufferedMetrics()
}
return &UnexpectedHTTPStatusError{StatusCode: resp.StatusCode}
}
c.numConsecutiveFailures = 0
c.clearBufferedMetrics()
return nil
}
// handleSonarResponse reads sonar response messages and parses limits, setting them for future usages
func (c *HTTPClient) handleSonarResponse(r io.ReadCloser) {
defer r.Close()
res, err := readBody(r)
if err != nil {
log.Error("failed to read response body of sonar message: +%v", err)
} else {
if res.FrequencySeconds != 0 {
atomic.StoreInt32(&c.waitIntervalSeconds, res.FrequencySeconds)
}
if res.MaxMetricLength != 0 {
atomic.StoreInt32(&c.maxMetricLength, res.MaxMetricLength)
}
if res.MaxBatchSize != 0 {
atomic.StoreInt32(&c.maxBatchSize, res.MaxBatchSize)
}
}
}
// GetWaitInterval returns the wait interval between metrics
func (c *HTTPClient) GetWaitInterval() time.Duration {
return time.Second * time.Duration(atomic.LoadInt32(&c.waitIntervalSeconds))
}
// GetDropletID returns the droplet ID
func (c *HTTPClient) GetDropletID() (string, error) {
return c.httpGet(fmt.Sprintf("%s/v1/id", c.metadataEndpoint), "")
}
// GetRegion returns the region
func (c *HTTPClient) GetRegion() (string, error) {
return c.httpGet(fmt.Sprintf("%s/v1/region", c.metadataEndpoint), "")
}
// GetAuthToken returns an auth token
func (c *HTTPClient) GetAuthToken() (string, error) {
return c.httpGet(fmt.Sprintf("%s/v1/auth-token", c.metadataEndpoint), "")
}
// GetAppKey returns the appkey
func (c *HTTPClient) GetAppKey(authToken string) (string, error) {
body, err := c.httpGet(fmt.Sprintf("%s/v1/appkey/droplet-auth-token", c.radarEndpoint), authToken)
if err != nil {
return "", err
}
var appKey string
err = json.Unmarshal([]byte(body), &appKey)
if err != nil {
return "", err
}
return appKey, nil
}
func truncate(str string, num int) string {
s := str
if len(str) > num {
if num > 3 {
num -= 3
}
s = str[0:num] + "*******"
}
return s
}
func (c *HTTPClient) httpGet(url, authToken string) (string, error) {
log.Debug("HTTP GET %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
if authToken != "" {
authValue := "DOMETADATA " + authToken
req.Header.Add("Authorization", authValue)
log.Debug("Authorization: %s", truncate(authValue, 15))
}
resp, err := c.httpClient.Do(req.WithContext(context.Background()))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
log.Debug("got status code %d while fetching %s (auth token: %s)", resp.StatusCode, url, truncate(authToken, 5))
return "", &UnexpectedHTTPStatusError{StatusCode: resp.StatusCode}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// {"success":true,"frequency":60,"max_metrics":1000,"max_lfm":512}
type sonarResponse struct {
Success bool `json:"success"`
FrequencySeconds int32 `json:"frequency"`
MaxBatchSize int32 `json:"max_metrics"`
MaxMetricLength int32 `json:"max_lfm"`
}
func readBody(r io.Reader) (sonarResponse, error) {
var res sonarResponse
return res, json.NewDecoder(r).Decode(&res)
}