forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.go
660 lines (559 loc) · 19.9 KB
/
extension.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sumologicextension
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/google/uuid"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configauth"
"go.opentelemetry.io/collector/config/confighttp"
"go.uber.org/zap"
grpccredentials "google.golang.org/grpc/credentials"
"github.com/SumoLogic/sumologic-otel-collector/pkg/extension/sumologicextension/api"
"github.com/SumoLogic/sumologic-otel-collector/pkg/extension/sumologicextension/credentials"
)
type SumologicExtension struct {
collectorName string
// The lock around baseUrl is needed because sumologicexporter is using
// it as base URL for API requests and this access has to be coordinated.
baseUrlLock sync.RWMutex
baseUrl string
host component.Host
conf *Config
origLogger *zap.Logger
logger *zap.Logger
credentialsStore credentials.Store
hashKey string
httpClient *http.Client
registrationInfo api.OpenRegisterResponsePayload
closeChan chan struct{}
closeOnce sync.Once
backOff *backoff.ExponentialBackOff
}
const (
heartbeatUrl = "/api/v1/collector/heartbeat"
registerUrl = "/api/v1/collector/register"
collectorIdField = "collector_id"
collectorNameField = "collector_name"
collectorCredentialIdField = "collector_credential_id"
)
const (
DefaultHeartbeatInterval = 15 * time.Second
)
var errGRPCNotSupported = fmt.Errorf("gRPC is not supported by sumologicextension")
// SumologicExtension implements ClientAuthenticator
var _ configauth.ClientAuthenticator = (*SumologicExtension)(nil)
func newSumologicExtension(conf *Config, logger *zap.Logger) (*SumologicExtension, error) {
if conf.Credentials.InstallToken == "" {
return nil, errors.New("access credentials not provided: need install_token")
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
credentialsStore, err := credentials.NewLocalFsStore(
credentials.WithCredentialsDirectory(conf.CollectorCredentialsDirectory),
credentials.WithLogger(logger),
)
if err != nil {
return nil, fmt.Errorf("failed to initialize credentials store: %w", err)
}
var (
collectorName string
hashKey = createHashKey(conf)
)
if conf.CollectorName == "" {
// If collector name is not set by the user, check if the collector was restarted
// and that we can reuse collector name save in credentials store.
if creds, err := credentialsStore.Get(hashKey); err != nil {
// If credentials file is not stored on filesystem generate collector name
collectorName = fmt.Sprintf("%s-%s", hostname, uuid.New())
} else {
collectorName = creds.CollectorName
}
} else {
collectorName = conf.CollectorName
}
if conf.HeartBeatInterval <= 0 {
conf.HeartBeatInterval = DefaultHeartbeatInterval
}
// Prepare ExponentialBackoff
backOff := backoff.NewExponentialBackOff()
backOff.InitialInterval = conf.BackOff.InitialInterval
backOff.MaxElapsedTime = conf.BackOff.MaxElapsedTime
backOff.MaxInterval = conf.BackOff.MaxInterval
return &SumologicExtension{
collectorName: collectorName,
baseUrl: strings.TrimSuffix(conf.ApiBaseUrl, "/"),
conf: conf,
origLogger: logger,
logger: logger,
hashKey: hashKey,
credentialsStore: credentialsStore,
closeChan: make(chan struct{}),
backOff: backOff,
}, nil
}
func createHashKey(conf *Config) string {
return fmt.Sprintf("%s%s%s",
conf.CollectorName,
conf.Credentials.InstallToken,
strings.TrimSuffix(conf.ApiBaseUrl, "/"),
)
}
func (se *SumologicExtension) Start(ctx context.Context, host component.Host) error {
se.host = host
colCreds, err := se.getCredentials(ctx)
if err != nil {
return err
}
if err = se.injectCredentials(colCreds); err != nil {
return err
}
// Add logger fields based on actual collector name and ID.
se.logger = se.origLogger.With(
zap.String(collectorNameField, colCreds.Credentials.CollectorName),
zap.String(collectorIdField, colCreds.Credentials.CollectorId),
)
go se.heartbeatLoop()
return nil
}
// Shutdown is invoked during service shutdown.
func (se *SumologicExtension) Shutdown(ctx context.Context) error {
se.closeOnce.Do(func() { close(se.closeChan) })
select {
case <-ctx.Done():
return ctx.Err()
default:
return nil
}
}
func (se *SumologicExtension) validateCredentials(
ctx context.Context,
colCreds credentials.CollectorCredentials,
) error {
se.logger.Info("Validating collector credentials...",
zap.String(collectorCredentialIdField, colCreds.Credentials.CollectorCredentialId),
zap.String(collectorIdField, colCreds.Credentials.CollectorId),
)
if err := se.injectCredentials(colCreds); err != nil {
return err
}
return se.sendHeartbeatWithHTTPClient(ctx, se.httpClient)
}
// injectCredentials injects the collector credentials:
// * into registration info that's stored in the extension and can be used by roundTripper
// * into http client and its transport so that each request is using collector
// credentials as authentication keys
func (se *SumologicExtension) injectCredentials(colCreds credentials.CollectorCredentials) error {
// Set the registration info so that it can be used in RoundTripper.
se.registrationInfo = colCreds.Credentials
httpClient, err := se.getHTTPClient(se.conf.HTTPClientSettings, colCreds.Credentials)
if err != nil {
return err
}
se.httpClient = httpClient
return nil
}
func (se *SumologicExtension) getHTTPClient(
httpClientSettings confighttp.HTTPClientSettings,
regInfo api.OpenRegisterResponsePayload,
) (*http.Client, error) {
httpClient, err := httpClientSettings.ToClientWithHost(
se.host,
component.TelemetrySettings{},
)
if err != nil {
return nil, fmt.Errorf("couldn't create HTTP client: %w", err)
}
// Set the transport so that all requests from httpClient will contain
// the collector credentials.
httpClient.Transport, err = se.RoundTripper(httpClient.Transport)
if err != nil {
return nil, fmt.Errorf("couldn't create HTTP client transport: %w", err)
}
return httpClient, nil
}
// getCredentials retrieves the credentials for the collector.
// It does so by checking the local credentials store and by validating those credentials.
// In case they are invalid or are not available through local credentials store
// then it tries to register the collector using the provided access keys.
func (se *SumologicExtension) getCredentials(ctx context.Context) (credentials.CollectorCredentials, error) {
var (
colCreds credentials.CollectorCredentials
err error
)
if !se.conf.ForceRegistration {
colCreds, err = se.getLocalCredentials(ctx)
if err == nil {
errV := se.validateCredentials(ctx, colCreds)
if errV == nil {
se.logger.Info("Found stored credentials, skipping registration",
zap.String(collectorNameField, colCreds.Credentials.CollectorName),
)
return colCreds, nil
}
// Credentials might have ended up being invalid or the collector
// might have been removed in Sumo.
// Fall back to removing the credentials and recreating them by registering
// the collector.
if err := se.credentialsStore.Delete(se.hashKey); err != nil {
se.logger.Error(
"Unable to delete old collector credentials", zap.Error(err),
)
}
se.logger.Info("Locally stored credentials invalid. Trying to re-register...",
zap.String(collectorNameField, colCreds.Credentials.CollectorName),
zap.String(collectorIdField, colCreds.Credentials.CollectorId),
zap.Error(errV),
)
} else {
se.logger.Info("Locally stored credentials not found, registering the collector")
}
}
colCreds, err = se.getCredentialsByRegistering(ctx)
if err != nil {
return credentials.CollectorCredentials{}, err
}
return colCreds, nil
}
// getCredentialsByRegistering registers the collector and returns the credentials
// obtained from the API.
func (se *SumologicExtension) getCredentialsByRegistering(ctx context.Context) (credentials.CollectorCredentials, error) {
colCreds, err := se.registerCollectorWithBackoff(ctx, se.collectorName)
if err != nil {
return credentials.CollectorCredentials{}, err
}
if err := se.credentialsStore.Store(se.hashKey, colCreds); err != nil {
se.logger.Error(
"Unable to store collector credentials, they will be used now but won't be re-used on next run",
zap.Error(err),
)
}
se.collectorName = colCreds.CollectorName
return colCreds, nil
}
// getLocalCredentials returns the credentials retrieved from local credentials
// storage in case they are available there.
func (se *SumologicExtension) getLocalCredentials(ctx context.Context) (credentials.CollectorCredentials, error) {
colCreds, err := se.credentialsStore.Get(se.hashKey)
if err != nil {
return credentials.CollectorCredentials{},
fmt.Errorf("problem finding local collector credentials (hash key: %s): %w",
se.hashKey, err,
)
}
se.collectorName = colCreds.CollectorName
if colCreds.ApiBaseUrl != "" {
se.SetBaseUrl(colCreds.ApiBaseUrl)
}
return colCreds, nil
}
// registerCollector registers the collector using registration API and returns
// the obtained collector credentials.
func (se *SumologicExtension) registerCollector(ctx context.Context, collectorName string) (credentials.CollectorCredentials, error) {
u, err := url.Parse(se.BaseUrl())
if err != nil {
return credentials.CollectorCredentials{}, err
}
u.Path = registerUrl
// TODO: just plain hostname or we want to add some custom logic when setting
// hostname in request?
hostname, err := os.Hostname()
if err != nil {
return credentials.CollectorCredentials{}, fmt.Errorf("cannot get hostname: %w", err)
}
var buff bytes.Buffer
if err = json.NewEncoder(&buff).Encode(api.OpenRegisterRequestPayload{
CollectorName: collectorName,
Description: se.conf.CollectorDescription,
Category: se.conf.CollectorCategory,
Fields: se.conf.CollectorFields,
Hostname: hostname,
Ephemeral: se.conf.Ephemeral,
Clobber: se.conf.Clobber,
TimeZone: se.conf.TimeZone,
}); err != nil {
return credentials.CollectorCredentials{}, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), &buff)
if err != nil {
return credentials.CollectorCredentials{}, err
}
addClientCredentials(req,
se.conf.Credentials,
)
addJSONHeaders(req)
se.logger.Info("Calling register API", zap.String("URL", u.String()))
client := *http.DefaultClient
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
res, err := client.Do(req)
if err != nil {
se.logger.Warn("Collector registration HTTP request failed", zap.Error(err))
return credentials.CollectorCredentials{}, fmt.Errorf("failed to register the collector: %w", err)
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 400 {
return se.handleRegistrationError(res)
} else if res.StatusCode == 301 {
// Use the URL from Location header for subsequent requests.
u := strings.TrimSuffix(res.Header.Get("Location"), "/")
se.SetBaseUrl(u)
se.logger.Info("Redirected to a different deployment",
zap.String("url", u),
)
return se.registerCollector(ctx, collectorName)
}
var resp api.OpenRegisterResponsePayload
if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
return credentials.CollectorCredentials{}, err
}
return credentials.CollectorCredentials{
CollectorName: collectorName,
Credentials: resp,
ApiBaseUrl: se.BaseUrl(),
}, nil
}
// handleRegistrationError handles the collector registration errors and returns
// appropriate error for backoff handling and logging purposes.
func (se *SumologicExtension) handleRegistrationError(res *http.Response) (credentials.CollectorCredentials, error) {
var errResponse api.ErrorResponsePayload
if err := json.NewDecoder(res.Body).Decode(&errResponse); err != nil {
var buff bytes.Buffer
if _, errCopy := io.Copy(&buff, res.Body); errCopy != nil {
return credentials.CollectorCredentials{}, fmt.Errorf(
"failed to read the collector registration response body, status code: %d, err: %w",
res.StatusCode, errCopy,
)
}
return credentials.CollectorCredentials{}, fmt.Errorf(
"failed to decode collector registration response body: %s, status code: %d, err: %w",
buff.String(), res.StatusCode, err,
)
}
se.logger.Warn("Collector registration failed",
zap.Int("status_code", res.StatusCode),
zap.String("error_id", errResponse.ID),
zap.Any("errors", errResponse.Errors),
)
// Return unrecoverable error for 4xx status codes except 429
if res.StatusCode >= 400 && res.StatusCode < 500 && res.StatusCode != 429 {
return credentials.CollectorCredentials{}, backoff.Permanent(fmt.Errorf(
"failed to register the collector, got HTTP status code: %d",
res.StatusCode,
))
}
return credentials.CollectorCredentials{}, fmt.Errorf(
"failed to register the collector, got HTTP status code: %d", res.StatusCode,
)
}
// callRegisterWithBackoff calls registration using exponential backoff algorithm
// this loosely base on backoff.Retry function
func (se *SumologicExtension) registerCollectorWithBackoff(ctx context.Context, collectorName string) (credentials.CollectorCredentials, error) {
se.backOff.Reset()
for {
creds, err := se.registerCollector(ctx, collectorName)
if err == nil {
se.logger = se.origLogger.With(
zap.String(collectorNameField, creds.Credentials.CollectorName),
zap.String(collectorIdField, creds.Credentials.CollectorId),
)
se.logger.Info("Collector registration finished successfully")
return creds, nil
}
nbo := se.backOff.NextBackOff()
// Return error if backoff reaches the limit or uncoverable error is spotted
if _, ok := err.(*backoff.PermanentError); nbo == se.backOff.Stop || ok {
return credentials.CollectorCredentials{}, fmt.Errorf("collector registration failed: %w", err)
}
t := time.NewTimer(nbo)
defer t.Stop()
select {
case <-t.C:
case <-ctx.Done():
return credentials.CollectorCredentials{}, fmt.Errorf("collector registration cancelled: %w", ctx.Err())
}
}
}
func (se *SumologicExtension) heartbeatLoop() {
if se.registrationInfo.CollectorCredentialId == "" || se.registrationInfo.CollectorCredentialKey == "" {
se.logger.Error("Collector not registered, cannot send heartbeat")
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
// When the close channel is closed ...
<-se.closeChan
// ... cancel the ongoing heartbeat request.
cancel()
}()
se.logger.Info("Heartbeat loop initialized. Starting to send hearbeat requests")
timer := time.NewTimer(se.conf.HeartBeatInterval)
for {
select {
case <-se.closeChan:
se.logger.Info("Heartbeat sender turned off")
return
default:
err := se.sendHeartbeatWithHTTPClient(ctx, se.httpClient)
if err != nil {
if errors.Is(err, errUnauthorizedHeartbeat) {
se.logger.Warn("Heartbeat request unauthorized, re-registering the collector")
colCreds, err := se.getCredentialsByRegistering(ctx)
if err != nil {
se.logger.Error("Heartbeat error, cannot register the collector", zap.Error(err))
continue
}
// Inject newly received credentials into extension's configuration.
if err = se.injectCredentials(colCreds); err != nil {
se.logger.Error("Heartbeat error, cannot inject new collector credentials", zap.Error(err))
continue
}
// Overwrite old logger fields with new collector name and ID.
se.logger = se.origLogger.With(
zap.String(collectorNameField, colCreds.Credentials.CollectorName),
zap.String(collectorIdField, colCreds.Credentials.CollectorId),
)
} else {
se.logger.Error("Heartbeat error", zap.Error(err))
}
} else {
se.logger.Debug("Heartbeat sent")
}
select {
case <-timer.C:
timer.Stop()
timer.Reset(se.conf.HeartBeatInterval)
case <-se.closeChan:
}
}
}
}
var errUnauthorizedHeartbeat = errors.New("heartbeat unauthorized")
type ErrorAPI struct {
status int
body string
}
func (e ErrorAPI) Error() string {
return fmt.Sprintf("API error (status code: %d): %s", e.status, e.body)
}
func (se *SumologicExtension) sendHeartbeatWithHTTPClient(ctx context.Context, httpClient *http.Client) error {
u, err := url.Parse(se.BaseUrl() + heartbeatUrl)
if err != nil {
return fmt.Errorf("unable to parse heartbeat URL %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), nil)
if err != nil {
return fmt.Errorf("unable to create HTTP request %w", err)
}
addJSONHeaders(req)
res, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to send HTTP request: %w", err)
}
defer res.Body.Close()
switch res.StatusCode {
default:
var buff bytes.Buffer
if _, err := io.Copy(&buff, res.Body); err != nil {
return fmt.Errorf(
"failed to copy collector heartbeat response body, status code: %d, err: %w",
res.StatusCode, err,
)
}
return fmt.Errorf("collector heartbeat request failed: %w",
ErrorAPI{
status: res.StatusCode,
body: buff.String(),
},
)
case http.StatusUnauthorized:
return errUnauthorizedHeartbeat
case http.StatusNoContent:
}
return nil
}
func (se *SumologicExtension) ComponentID() config.ComponentID {
return se.conf.ExtensionSettings.ID()
}
func (se *SumologicExtension) CollectorID() string {
return se.registrationInfo.CollectorId
}
func (se *SumologicExtension) BaseUrl() string {
se.baseUrlLock.RLock()
defer se.baseUrlLock.RUnlock()
return se.baseUrl
}
func (se *SumologicExtension) SetBaseUrl(baseUrl string) {
se.baseUrlLock.Lock()
se.baseUrl = baseUrl
se.baseUrlLock.Unlock()
}
// Implement [1] in order for this extension to be used as custom exporter
// authenticator.
//
// [1]: https://github.com/open-telemetry/opentelemetry-collector/blob/2e84285efc665798d76773b9901727e8836e9d8f/config/configauth/clientauth.go#L34-L39
func (se *SumologicExtension) RoundTripper(base http.RoundTripper) (http.RoundTripper, error) {
return roundTripper{
collectorCredentialId: se.registrationInfo.CollectorCredentialId,
collectorCredentialKey: se.registrationInfo.CollectorCredentialKey,
base: base,
}, nil
}
func (se *SumologicExtension) PerRPCCredentials() (grpccredentials.PerRPCCredentials, error) {
return nil, errGRPCNotSupported
}
type roundTripper struct {
collectorCredentialId string
collectorCredentialKey string
base http.RoundTripper
}
func (rt roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
addCollectorCredentials(req, rt.collectorCredentialId, rt.collectorCredentialKey)
return rt.base.RoundTrip(req)
}
func addCollectorCredentials(req *http.Request, collectorCredentialId string, collectorCredentialKey string) {
token := base64.StdEncoding.EncodeToString(
[]byte(collectorCredentialId + ":" + collectorCredentialKey),
)
// Delete the existing Authorization header so prevent sending both the old one
// and the new one.
req.Header.Del("Authorization")
req.Header.Add("Authorization", "Basic "+token)
}
func addClientCredentials(req *http.Request, credentials accessCredentials) {
var authHeaderValue string
if credentials.InstallToken != "" {
authHeaderValue = fmt.Sprintf("Bearer %s", credentials.InstallToken)
}
req.Header.Del("Authorization")
req.Header.Add("Authorization", authHeaderValue)
}