forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
396 lines (352 loc) · 12.4 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Originally copied from https://github.com/signalfx/signalfx-agent/blob/fbc24b0fdd3884bd0bbfbd69fe3c83f49d4c0b77/pkg/apm/correlations/client.go
package correlations // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/correlations"
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/log"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/requests"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/requests/requestcounter"
)
var ErrChFull = errors.New("request channel full")
var errRetryChFull = errors.New("retry channel full")
var errMaxAttempts = errors.New("maximum attempts exceeded")
var errRequestCancelled = errors.New("request cancelled")
// ErrMaxEntries is an error returned when the correlation endpoint returns a 418 http status
// code indicating that the set of services or environments is too large to add another value
type ErrMaxEntries struct {
MaxEntries int64 `json:"max,omitempty"`
}
func (m *ErrMaxEntries) Error() string {
return fmt.Sprintf("max entries %d", m.MaxEntries)
}
var _ error = (*ErrMaxEntries)(nil)
// CorrelationClient is an interface for correlations.Client
type CorrelationClient interface {
Correlate(*Correlation, CorrelateCB)
Delete(*Correlation, SuccessfulDeleteCB)
Get(dimName string, dimValue string, cb SuccessfulGetCB)
Start()
Shutdown()
}
type request struct {
*Correlation
ctx context.Context
cancel context.CancelFunc
operation string
callback func(body []byte, statuscode int, err error)
sendAt time.Time
}
// Client is a client for making dimensional correlations
type Client struct {
sync.RWMutex
log log.Logger
ctx context.Context
wg sync.WaitGroup
Token string
APIURL *url.URL
client *http.Client
requestSender *requests.ReqSender
requestChan chan *request
retryChan chan *request
dedup *deduplicator
// For easier unit testing
now func() time.Time
logUpdates bool
retryDelay time.Duration
maxAttempts uint32
TotalClientError4xxResponses int64
TotalRetriedUpdates int64
TotalInvalidDimensions int64
dedupCleanupInterval time.Duration
}
// Config defines configuration for correlation settings.
type Config struct {
MaxRequests uint `mapstructure:"max_requests"`
MaxBuffered uint `mapstructure:"max_buffered"`
MaxRetries uint `mapstructure:"max_retries"`
LogUpdates bool `mapstructure:"log_updates"`
RetryDelay time.Duration `mapstructure:"retry_delay"`
CleanupInterval time.Duration `mapstructure:"cleanup_interval"`
}
// ClientConfig for correlation client.
type ClientConfig struct {
Config
AccessToken string
URL *url.URL
}
// NewCorrelationClient returns a new Client
func NewCorrelationClient(ctx context.Context, log log.Logger, client *http.Client, conf ClientConfig) (CorrelationClient, error) {
sender := requests.NewReqSender(ctx, client, conf.MaxRequests, "correlation")
return &Client{
log: log,
ctx: ctx,
Token: conf.AccessToken,
APIURL: conf.URL,
requestSender: sender,
client: client,
now: time.Now,
logUpdates: conf.LogUpdates,
requestChan: make(chan *request, conf.MaxBuffered),
retryChan: make(chan *request, conf.MaxBuffered),
dedup: newDeduplicator(int(conf.MaxBuffered)),
retryDelay: conf.RetryDelay,
maxAttempts: uint32(conf.MaxRetries) + 1,
dedupCleanupInterval: conf.CleanupInterval,
}, nil
}
func (cc *Client) putRequestOnChan(r *request) error {
// prevent requests against empty dimension names and values
if r.DimName == "" || r.DimValue == "" {
// logging this as debug because this means there's no actual dimension to correlate with
// and because this isn't being taken off on the request sender and subject to retries, this could
// potentially spam the logs
atomic.AddInt64(&cc.TotalInvalidDimensions, int64(1))
r.Logger(cc.log).WithFields(log.Fields{"method": r.operation}).Debug("No dimension key or value to correlate to")
return nil
}
r.ctx, r.cancel = context.WithCancel(requestcounter.ContextWithRequestCounter(context.Background()))
var err error
select {
case cc.requestChan <- r:
case <-cc.ctx.Done():
err = context.DeadlineExceeded
default:
err = ErrChFull
}
return err
}
func (cc *Client) putRequestOnRetryChan(r *request) error {
// handle request counter
if requestcounter.GetRequestCount(r.ctx) >= cc.maxAttempts {
return errMaxAttempts
}
requestcounter.IncrementRequestCount(r.ctx)
// set the time to retry
r.sendAt = cc.now().Add(cc.retryDelay)
if r.ctx.Err() != nil {
return errRequestCancelled
}
var err error
select {
case <-r.ctx.Done():
err = errRequestCancelled
case cc.retryChan <- r:
case <-cc.ctx.Done():
err = context.DeadlineExceeded
default:
err = errRetryChFull
}
return err
}
// CorrelateCB is a call back invoked with Correlate requests
// it is not invoked if the reqeust is deduplicated, cancelled, or the client context is cancelled
type CorrelateCB func(cor *Correlation, err error)
// Correlate
func (cc *Client) Correlate(cor *Correlation, cb CorrelateCB) {
err := cc.putRequestOnChan(&request{
Correlation: cor,
operation: http.MethodPut,
callback: func(body []byte, statuscode int, err error) {
switch statuscode {
case http.StatusOK:
if cc.logUpdates {
cor.Logger(cc.log).WithFields(log.Fields{"method": http.MethodPut}).Info("Updated dimension")
}
case http.StatusTeapot:
max := &ErrMaxEntries{}
err = json.Unmarshal(body, max)
if err == nil {
err = max
}
}
if err != nil {
cor.Logger(cc.log).WithError(err).WithFields(log.Fields{"method": http.MethodPut}).Error("Unable to update dimension, not retrying")
}
cb(cor, err)
}})
if err != nil {
cor.Logger(cc.log).WithError(err).WithFields(log.Fields{"method": http.MethodPut}).Debug("Unable to update dimension, not retrying")
}
}
// SuccessfulDeleteCB is a call back that is only invoked on successful Deletion operations
type SuccessfulDeleteCB func(cor *Correlation)
// Delete removes a correlation
func (cc *Client) Delete(cor *Correlation, callback SuccessfulDeleteCB) {
err := cc.putRequestOnChan(&request{
Correlation: cor,
operation: http.MethodDelete,
callback: func(_ []byte, statuscode int, err error) {
switch statuscode {
case http.StatusOK:
callback(cor)
if cc.logUpdates {
cor.Logger(cc.log).WithFields(log.Fields{"method": http.MethodDelete}).Info("Updated dimension")
}
default:
cc.log.WithError(err).Error("Unable to update dimension, not retrying")
}
}})
if err != nil {
cor.Logger(cc.log).WithError(err).WithFields(log.Fields{"method": http.MethodDelete}).Debug("Unable to update dimension, not retrying")
}
}
// SuccessfulGetCB
type SuccessfulGetCB func(map[string][]string)
// Get
func (cc *Client) Get(dimName string, dimValue string, callback SuccessfulGetCB) {
err := cc.putRequestOnChan(&request{
Correlation: &Correlation{
DimName: dimName,
DimValue: dimValue,
},
operation: http.MethodGet,
callback: func(body []byte, statuscode int, err error) {
switch statuscode {
case http.StatusOK:
var response = map[string][]string{}
err = json.Unmarshal(body, &response)
if err != nil {
cc.log.WithError(err).WithFields(log.Fields{"dim": dimName, "value": dimValue}).Error("Unable to unmarshall correlations for dimension")
return
}
callback(response)
case http.StatusNotFound:
// only log this as debug because we do a blanket fetch of correlations on the backend
// and if the backend fails to find anything this isn't really an error for us
cc.log.WithError(err).Debug("Unable to update dimension, not retrying")
default:
cc.log.WithError(err).Error("Unable to update dimension, not retrying")
}
},
})
if err != nil {
cc.log.WithError(err).WithFields(log.Fields{"dimensionName": dimName, "dimensionValue": dimValue}).Debug("Unable to retrieve correlations for dimension, not retrying")
}
}
func (cc *Client) makeRequest(r *request) {
var (
req *http.Request
err error
)
// build endpoint url
endpoint := fmt.Sprintf("%s/v2/apm/correlate/%s/%s", cc.APIURL, url.PathEscape(r.DimName), url.PathEscape(r.DimValue))
switch r.operation {
case http.MethodGet:
req, err = http.NewRequest(r.operation, endpoint, nil)
case http.MethodPut:
// TODO: pool the reader
endpoint = fmt.Sprintf("%s/%s", endpoint, r.Type)
req, err = http.NewRequest(r.operation, endpoint, strings.NewReader(r.Value))
req.Header.Add("Content-Type", "text/plain")
case http.MethodDelete:
endpoint = fmt.Sprintf("%s/%s/%s", endpoint, r.Type, url.PathEscape(r.Value))
req, err = http.NewRequest(r.operation, endpoint, nil)
default:
err = fmt.Errorf("unknown operation")
}
if err != nil {
// logging this as debug because this means there's something fundamentally wrong with the request
// and because this isn't being taken off on the request sender and subject to retries, this could
// potentially spam the logs long term. This would be a really good candidate for a throttled error logger
r.Correlation.Logger(cc.log).WithError(err).WithFields(log.Fields{"method": r.operation}).Debug("Unable to make request, not retrying")
r.cancel()
return
}
req.Header.Add("X-SF-TOKEN", cc.Token)
req = req.WithContext(
context.WithValue(req.Context(), requests.RequestFailedCallbackKey, requests.RequestFailedCallback(func(body []byte, statusCode int, err error) {
// retry if the http status code is not 4XX. A 4xx or http client error implies
// an error that is not going to be remedied by retrying.
if statusCode < 400 || statusCode >= 500 {
// The retry (for non 400 errors) is meant to provide some measure of robustness against
// temporary API failures. If the API is down for significant
// periods of time, correlation updates will probably eventually back
// up beyond conf.MaxBuffered and start dropping.
retryErr := cc.putRequestOnRetryChan(r)
if retryErr == nil {
r.Correlation.Logger(cc.log).WithError(err).WithFields(log.Fields{"method": req.Method}).Debug("Unable to update dimension, retrying")
return
}
} else {
atomic.AddInt64(&cc.TotalClientError4xxResponses, int64(1))
}
// invoke the callback
r.callback(body, statusCode, err)
// cancel the request context
r.cancel()
})))
req = req.WithContext(
context.WithValue(req.Context(), requests.RequestSuccessCallbackKey, requests.RequestSuccessCallback(func(body []byte) {
r.callback(body, http.StatusOK, nil)
// close the request context
r.cancel()
})))
// This will block if we don't have enough requests
cc.requestSender.Send(req)
}
// routines
// processChan processes incoming requests, drops duplicates, and cancels conflicting requests
func (cc *Client) processChan() {
defer cc.wg.Done()
purgeDeduper := time.NewTimer(cc.dedupCleanupInterval)
defer purgeDeduper.Stop()
for {
select {
case <-cc.ctx.Done():
return
case <-purgeDeduper.C:
cc.dedup.purge()
purgeDeduper.Reset(cc.dedupCleanupInterval)
case r := <-cc.requestChan:
if cc.dedup.isDup(r) {
r.cancel()
continue
}
cc.makeRequest(r)
}
}
}
// processRetryChan is a routine that drains the retry channel and waits until the appropriate time to retry the request
func (cc *Client) processRetryChan() {
defer cc.wg.Done()
for {
select {
case <-cc.ctx.Done(): // client is shutdown
return
case r := <-cc.retryChan:
if r.ctx.Err() != nil {
continue
}
select {
case <-time.After(time.Until(r.sendAt)): // wait and resend the request
atomic.AddInt64(&cc.TotalRetriedUpdates, int64(1))
cc.makeRequest(r)
case <-r.ctx.Done(): // request is cancelled
continue
case <-cc.ctx.Done(): // client is shutdown
return
}
}
}
}
// Start the client's processing queue
func (cc *Client) Start() {
cc.wg.Add(2)
go cc.processChan()
go cc.processRetryChan()
}
// Shutdown the client. This will block until the context's cancel
// function is complete.
func (cc *Client) Shutdown() {
cc.wg.Wait()
}