forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
517 lines (449 loc) · 14.1 KB
/
exporter.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
// Copyright 2020 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 sumologicexporter
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"sync"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
"go.uber.org/zap"
"github.com/SumoLogic/sumologic-otel-collector/pkg/extension/sumologicextension"
)
const (
logsDataUrl = "/api/v1/collector/logs"
metricsDataUrl = "/api/v1/collector/metrics"
tracesDataUrl = "/api/v1/collector/traces"
)
type sumologicexporter struct {
config *Config
host component.Host
logger *zap.Logger
clientLock sync.RWMutex
client *http.Client
compressorPool sync.Pool
prometheusFormatter prometheusFormatter
// Lock around data URLs is needed because the reconfiguration of the exporter
// can happen asynchronously whenever the exporter is re registering.
dataUrlsLock sync.RWMutex
dataUrlMetrics string
dataUrlLogs string
dataUrlTraces string
id component.ID
}
func initExporter(cfg *Config, createSettings exporter.CreateSettings) (*sumologicexporter, error) {
pf, err := newPrometheusFormatter()
if err != nil {
return nil, err
}
se := &sumologicexporter{
config: cfg,
logger: createSettings.Logger,
compressorPool: sync.Pool{
New: func() any {
c, err := newCompressor(cfg.CompressEncoding)
if err != nil {
return fmt.Errorf("failed to initialize compressor: %w", err)
}
return &c
},
},
// NOTE: client is now set in start()
prometheusFormatter: pf,
id: createSettings.ID,
}
se.logger.Info(
"Sumo Logic Exporter configured",
zap.String("log_format", string(cfg.LogFormat)),
zap.String("metric_format", string(cfg.MetricFormat)),
zap.String("trace_format", string(cfg.TraceFormat)),
)
return se, nil
}
func newLogsExporter(
ctx context.Context,
params exporter.CreateSettings,
cfg *Config,
) (exporter.Logs, error) {
se, err := initExporter(cfg, params)
if err != nil {
return nil, fmt.Errorf("failed to initialize the logs exporter: %w", err)
}
return exporterhelper.NewLogsExporter(
ctx,
params,
cfg,
se.pushLogsData,
// Disable exporterhelper Timeout, since we are using a custom mechanism
// within exporter itself
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(cfg.RetrySettings),
exporterhelper.WithQueue(cfg.QueueSettings),
exporterhelper.WithStart(se.start),
exporterhelper.WithShutdown(se.shutdown),
)
}
func newMetricsExporter(
ctx context.Context,
params exporter.CreateSettings,
cfg *Config,
) (exporter.Metrics, error) {
se, err := initExporter(cfg, params)
if err != nil {
return nil, err
}
return exporterhelper.NewMetricsExporter(
ctx,
params,
cfg,
se.pushMetricsData,
// Disable exporterhelper Timeout, since we are using a custom mechanism
// within exporter itself
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(cfg.RetrySettings),
exporterhelper.WithQueue(cfg.QueueSettings),
exporterhelper.WithStart(se.start),
exporterhelper.WithShutdown(se.shutdown),
)
}
func newTracesExporter(
ctx context.Context,
params exporter.CreateSettings,
cfg *Config,
) (exporter.Traces, error) {
se, err := initExporter(cfg, params)
if err != nil {
return nil, err
}
return exporterhelper.NewTracesExporter(
ctx,
params,
cfg,
se.pushTracesData,
// Disable exporterhelper Timeout, since we are using a custom mechanism
// within exporter itself
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(cfg.RetrySettings),
exporterhelper.WithQueue(cfg.QueueSettings),
exporterhelper.WithStart(se.start),
exporterhelper.WithShutdown(se.shutdown),
)
}
// pushLogsData groups data with common metadata and sends them as separate batched requests.
// It returns the number of unsent logs and an error which contains a list of dropped records
// so they can be handled by OTC retry mechanism
func (se *sumologicexporter) pushLogsData(ctx context.Context, ld plog.Logs) error {
compr, err := se.getCompressor()
if err != nil {
return consumererror.NewLogs(err, ld)
}
defer se.compressorPool.Put(compr)
logsUrl, metricsUrl, tracesUrl := se.getDataURLs()
sdr := newSender(
se.logger,
se.config,
se.getHTTPClient(),
compr,
se.prometheusFormatter,
metricsUrl,
logsUrl,
tracesUrl,
se.id,
)
// Follow different execution path for OTLP format
if sdr.config.LogFormat == OTLPLogFormat {
if err := sdr.sendOTLPLogs(ctx, ld); err != nil {
se.handleUnauthorizedErrors(ctx, err)
return consumererror.NewLogs(err, ld)
}
return nil
}
type droppedResourceRecords struct {
resource pcommon.Resource
records []plog.LogRecord
}
var (
errs []error
dropped []droppedResourceRecords
)
// Iterate over ResourceLogs
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
rl := rls.At(i)
se.dropRoutingAttribute(rl.Resource().Attributes())
currentMetadata := newFields(rl.Resource().Attributes())
if droppedRecords, err := sdr.sendNonOTLPLogs(ctx, rl, currentMetadata); err != nil {
dropped = append(dropped, droppedResourceRecords{
resource: rl.Resource(),
records: droppedRecords,
})
errs = append(errs, err)
}
}
if len(dropped) > 0 {
ld = plog.NewLogs()
// Move all dropped records to Logs
// NOTE: we only copy resource and log records here.
// Scope is not handled properly but it never was.
for i := range dropped {
rls := ld.ResourceLogs().AppendEmpty()
dropped[i].resource.MoveTo(rls.Resource())
for j := 0; j < len(dropped[i].records); j++ {
dropped[i].records[j].MoveTo(
rls.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty(),
)
}
}
errs = deduplicateErrors(errs)
se.handleUnauthorizedErrors(ctx, errs...)
return consumererror.NewLogs(multierr.Combine(errs...), ld)
}
return nil
}
// pushMetricsData groups data with common metadata and send them as separate batched requests
// it returns number of unsent metrics and error which contains list of dropped records
// so they can be handle by the OTC retry mechanism
func (se *sumologicexporter) pushMetricsData(ctx context.Context, md pmetric.Metrics) error {
compr, err := se.getCompressor()
if err != nil {
return consumererror.NewMetrics(err, md)
}
defer se.compressorPool.Put(compr)
logsUrl, metricsUrl, tracesUrl := se.getDataURLs()
sdr := newSender(
se.logger,
se.config,
se.getHTTPClient(),
compr,
se.prometheusFormatter,
metricsUrl,
logsUrl,
tracesUrl,
se.id,
)
// Transform metrics metadata
// this includes dropping the routing attribute and translating attributes
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
se.dropRoutingAttribute(rm.Resource().Attributes())
}
var droppedMetrics pmetric.Metrics
var errs []error
if sdr.config.MetricFormat == OTLPMetricFormat {
if err := sdr.sendOTLPMetrics(ctx, md); err != nil {
droppedMetrics = md
errs = []error{err}
}
} else {
droppedMetrics, errs = sdr.sendNonOTLPMetrics(ctx, md)
}
if len(errs) > 0 {
se.handleUnauthorizedErrors(ctx, errs...)
return consumererror.NewMetrics(multierr.Combine(errs...), droppedMetrics)
}
return nil
}
// handleUnauthorizedErrors checks if any of the provided errors is an unauthorized error.
// In which case it triggers exporter reconfiguration which in turn takes the credentials
// from sumologicextension which at this point should already detect the problem with
// authorization (via heartbeats) and prepare new collector credentials to be available.
func (se *sumologicexporter) handleUnauthorizedErrors(ctx context.Context, errs ...error) {
for _, err := range errs {
if errors.Is(err, errUnauthorized) {
se.logger.Warn("Received unauthorized status code, triggering reconfiguration")
if errC := se.configure(ctx); errC != nil {
se.logger.Error("Error configuring the exporter with new credentials", zap.Error(err))
} else {
// It's enough to successfully reconfigure the exporter just once.
return
}
}
}
}
func (se *sumologicexporter) pushTracesData(ctx context.Context, td ptrace.Traces) error {
compr, err := se.getCompressor()
if err != nil {
return consumererror.NewTraces(err, td)
}
defer se.compressorPool.Put(compr)
logsUrl, metricsUrl, tracesUrl := se.getDataURLs()
sdr := newSender(
se.logger,
se.config,
se.getHTTPClient(),
compr,
se.prometheusFormatter,
metricsUrl,
logsUrl,
tracesUrl,
se.id,
)
// Drop routing attribute from ResourceSpans
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
se.dropRoutingAttribute(rss.At(i).Resource().Attributes())
}
err = sdr.sendTraces(ctx, td)
se.handleUnauthorizedErrors(ctx, err)
return err
}
func (se *sumologicexporter) getCompressor() (*compressor, error) {
switch c := se.compressorPool.Get().(type) {
case error:
return &compressor{}, fmt.Errorf("%v", c)
case *compressor:
return c, nil
default:
return &compressor{}, fmt.Errorf("unknown compressor type: %T", c)
}
}
func (se *sumologicexporter) start(ctx context.Context, host component.Host) error {
se.host = host
return se.configure(ctx)
}
func (se *sumologicexporter) configure(ctx context.Context) error {
var (
ext *sumologicextension.SumologicExtension
foundSumoExt bool
)
httpSettings := se.config.HTTPClientSettings
for _, e := range se.host.GetExtensions() {
v, ok := e.(*sumologicextension.SumologicExtension)
if ok && httpSettings.Auth.AuthenticatorID == v.ComponentID() {
ext = v
foundSumoExt = true
break
}
}
if httpSettings.Endpoint == "" && httpSettings.Auth != nil &&
string(httpSettings.Auth.AuthenticatorID.Type()) == "sumologic" {
// If user specified using sumologicextension as auth but none was
// found then return an error.
if !foundSumoExt {
return fmt.Errorf(
"sumologic was specified as auth extension (named: %q) but "+
"a matching extension was not found in the config, "+
"please re-check the config and/or define the sumologicextension",
httpSettings.Auth.AuthenticatorID.String(),
)
}
// If we're using sumologicextension as authentication extension and
// endpoint was not set then send data on a collector generic ingest URL
// with authentication set by sumologicextension.
u, err := url.Parse(ext.BaseUrl())
if err != nil {
return fmt.Errorf("failed to parse API base URL from sumologicextension: %w", err)
}
logsUrl := *u
logsUrl.Path = logsDataUrl
metricsUrl := *u
metricsUrl.Path = metricsDataUrl
tracesUrl := *u
tracesUrl.Path = tracesDataUrl
se.setDataURLs(logsUrl.String(), metricsUrl.String(), tracesUrl.String())
} else if httpSettings.Endpoint != "" {
logsUrl, err := getSignalURL(se.config, httpSettings.Endpoint, component.DataTypeLogs)
if err != nil {
return err
}
metricsUrl, err := getSignalURL(se.config, httpSettings.Endpoint, component.DataTypeMetrics)
if err != nil {
return err
}
tracesUrl, err := getSignalURL(se.config, httpSettings.Endpoint, component.DataTypeTraces)
if err != nil {
return err
}
se.setDataURLs(logsUrl, metricsUrl, tracesUrl)
// Clean authenticator if set to sumologic.
// Setting to null in configuration doesn't work, so we have to force it that way.
if httpSettings.Auth != nil && string(httpSettings.Auth.AuthenticatorID.Type()) == "sumologic" {
httpSettings.Auth = nil
}
} else {
return fmt.Errorf("no auth extension and no endpoint specified")
}
client, err := httpSettings.ToClient(se.host, component.TelemetrySettings{})
if err != nil {
return fmt.Errorf("failed to create HTTP Client: %w", err)
}
se.setHTTPClient(client)
return nil
}
func (se *sumologicexporter) setHTTPClient(client *http.Client) {
se.clientLock.Lock()
se.client = client
se.clientLock.Unlock()
}
func (se *sumologicexporter) getHTTPClient() *http.Client {
se.clientLock.RLock()
defer se.clientLock.RUnlock()
return se.client
}
func (se *sumologicexporter) setDataURLs(logs, metrics, traces string) {
se.dataUrlsLock.Lock()
se.logger.Info("setting data urls", zap.String("logs_url", logs), zap.String("metrics_url", metrics), zap.String("traces_url", traces))
se.dataUrlLogs, se.dataUrlMetrics, se.dataUrlTraces = logs, metrics, traces
se.dataUrlsLock.Unlock()
}
func (se *sumologicexporter) getDataURLs() (logs, metrics, traces string) {
se.dataUrlsLock.RLock()
defer se.dataUrlsLock.RUnlock()
return se.dataUrlLogs, se.dataUrlMetrics, se.dataUrlTraces
}
func (se *sumologicexporter) shutdown(context.Context) error {
return nil
}
func (se *sumologicexporter) dropRoutingAttribute(attr pcommon.Map) {
attr.Remove(se.config.DropRoutingAttribute)
}
// get the destination url for a given signal type
// this mostly adds signal-specific suffixes if the format is otlp
func getSignalURL(oCfg *Config, endpointUrl string, signal component.DataType) (string, error) {
url, err := url.Parse(endpointUrl)
if err != nil {
return "", err
}
switch signal {
case component.DataTypeLogs:
if oCfg.LogFormat != "otlp" {
return url.String(), nil
}
case component.DataTypeMetrics:
if oCfg.MetricFormat != "otlp" {
return url.String(), nil
}
case component.DataTypeTraces:
default:
return "", fmt.Errorf("unknown signal type: %s", signal)
}
signalUrlSuffix := fmt.Sprintf("/v1/%s", signal)
if !strings.HasSuffix(url.Path, signalUrlSuffix) {
url.Path = path.Join(url.Path, signalUrlSuffix)
}
return url.String(), nil
}