forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
866 lines (733 loc) · 24.7 KB
/
sender.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// 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 (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"strings"
"time"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/model/otlp"
"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/exporter/sumologicexporter/internal/observability"
)
var (
tracesMarshaler = otlp.NewProtobufTracesMarshaler()
metricsMarshaler = otlp.NewProtobufMetricsMarshaler()
logsMarshaler = otlp.NewProtobufLogsMarshaler()
)
// metricPair represents information required to send one metric to the Sumo Logic
type metricPair struct {
attributes pcommon.Map
metric pmetric.Metric
}
// countingReader keeps number of records related to reader
type countingReader struct {
counter int64
reader io.Reader
}
// newCountingReader creates countingReader with given number of records
func newCountingReader(records int) *countingReader {
return &countingReader{
counter: int64(records),
}
}
// withBytes sets up reader to read from bytes data
func (c *countingReader) withBytes(data []byte) *countingReader {
c.reader = bytes.NewReader(data)
return c
}
// withString sets up reader to read from string data
func (c *countingReader) withString(data string) *countingReader {
c.reader = strings.NewReader(data)
return c
}
// bodyBuilder keeps information about number of records related to data it keeps
type bodyBuilder struct {
builder strings.Builder
counter int
}
// newBodyBuilder returns empty bodyBuilder
func newBodyBuilder() bodyBuilder {
return bodyBuilder{}
}
// Reset resets both counter and builder content
func (b *bodyBuilder) Reset() {
b.counter = 0
b.builder.Reset()
}
// addLine adds line to builder and increments counter
func (b *bodyBuilder) addLine(line string) {
b.builder.WriteString(line) // WriteString can't actually return an error
b.counter += 1
}
// addLine adds multiple lines to builder and increments counter
func (b *bodyBuilder) addLines(lines []string) {
if len(lines) == 0 {
return
}
// add the first line separately to avoid a conditional in the loop
b.builder.WriteString(lines[0])
for _, line := range lines[1:] {
b.builder.WriteByte('\n')
b.builder.WriteString(line) // WriteString can't actually return an error
}
b.counter += len(lines)
}
// addNewLine adds newline to builder
func (b *bodyBuilder) addNewLine() {
b.builder.WriteByte('\n') // WriteByte can't actually return an error
}
// Len returns builder content length
func (b *bodyBuilder) Len() int {
return b.builder.Len()
}
// toCountingReader converts bodyBuilder to countingReader
func (b *bodyBuilder) toCountingReader() *countingReader {
return newCountingReader(b.counter).withString(b.builder.String())
}
type sender struct {
logger *zap.Logger
metricBuffer []metricPair
config *Config
client *http.Client
sources sourceFormats
compressor compressor
prometheusFormatter prometheusFormatter
jsonLogsConfig JSONLogs
dataUrlMetrics string
dataUrlLogs string
dataUrlTraces string
}
const (
// maxBufferSize defines size of the logBuffer (maximum number of plog.LogRecord entries)
maxBufferSize int = 1024 * 1024
headerContentType string = "Content-Type"
headerContentEncoding string = "Content-Encoding"
headerClient string = "X-Sumo-Client"
headerHost string = "X-Sumo-Host"
headerName string = "X-Sumo-Name"
headerCategory string = "X-Sumo-Category"
headerFields string = "X-Sumo-Fields"
attributeKeySourceHost = "_sourceHost"
attributeKeySourceName = "_sourceName"
attributeKeySourceCategory = "_sourceCategory"
contentTypeLogs string = "application/x-www-form-urlencoded"
contentTypePrometheus string = "application/vnd.sumologic.prometheus"
contentTypeOTLP string = "application/x-protobuf"
contentEncodingGzip string = "gzip"
contentEncodingDeflate string = "deflate"
)
func newSender(
logger *zap.Logger,
cfg *Config,
cl *http.Client,
s sourceFormats,
c compressor,
pf prometheusFormatter,
metricsUrl string,
logsUrl string,
tracesUrl string,
) *sender {
return &sender{
logger: logger,
config: cfg,
client: cl,
sources: s,
compressor: c,
prometheusFormatter: pf,
jsonLogsConfig: cfg.JSONLogs,
dataUrlMetrics: metricsUrl,
dataUrlLogs: logsUrl,
dataUrlTraces: tracesUrl,
}
}
var errUnauthorized = errors.New("unauthorized")
// send sends data to sumologic
func (s *sender) send(ctx context.Context, pipeline PipelineType, reader *countingReader, flds fields) error {
data, err := s.compressor.compress(reader.reader)
if err != nil {
return err
}
req, err := s.createRequest(ctx, pipeline, data)
if err != nil {
return err
}
if err := s.addRequestHeaders(req, pipeline, flds); err != nil {
return err
}
s.logger.Debug("Sending data",
zap.String("pipeline", string(pipeline)),
zap.Any("headers", req.Header),
)
start := time.Now()
resp, err := s.client.Do(req)
if err != nil {
s.recordMetrics(time.Since(start), reader.counter, req, nil, pipeline)
return err
}
defer resp.Body.Close()
s.recordMetrics(time.Since(start), reader.counter, req, resp, pipeline)
return s.handleReceiverResponse(resp)
}
func (s *sender) handleReceiverResponse(resp *http.Response) error {
// API responds with a 200 or 204 with ConentLength set to 0 when all data
// has been successfully ingested.
if resp.ContentLength == 0 && (resp.StatusCode == 200 || resp.StatusCode == 204) {
return nil
}
type ReceiverResponseCore struct {
Status int `json:"status,omitempty"`
ID string `json:"id,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
// API responds with a 200 or 204 with a JSON body describing what issues
// were encountered when processing the sent data.
switch resp.StatusCode {
case 200, 204:
var rResponse ReceiverResponseCore
var (
b = bytes.NewBuffer(make([]byte, 0, resp.ContentLength))
tr = io.TeeReader(resp.Body, b)
)
if err := json.NewDecoder(tr).Decode(&rResponse); err != nil {
s.logger.Warn("Error decoding receiver response", zap.ByteString("body", b.Bytes()))
return nil
}
l := s.logger.With(zap.String("status", resp.Status))
if len(rResponse.ID) > 0 {
l = l.With(zap.String("id", rResponse.ID))
}
if len(rResponse.Code) > 0 {
l = l.With(zap.String("code", rResponse.Code))
}
if len(rResponse.Message) > 0 {
l = l.With(zap.String("message", rResponse.Message))
}
l.Warn("There was an issue sending data")
return nil
case 401:
return errUnauthorized
default:
type ReceiverErrorResponse struct {
ReceiverResponseCore
Errors []struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"errors,omitempty"`
}
var rResponse ReceiverErrorResponse
if resp.ContentLength > 0 {
var (
b = bytes.NewBuffer(make([]byte, 0, resp.ContentLength))
tr = io.TeeReader(resp.Body, b)
)
if err := json.NewDecoder(tr).Decode(&rResponse); err != nil {
return fmt.Errorf("failed to decode API response (status: %s): %s",
resp.Status, b.String(),
)
}
}
errMsgs := []string{
fmt.Sprintf("status: %s", resp.Status),
}
if len(rResponse.ID) > 0 {
errMsgs = append(errMsgs, fmt.Sprintf("id: %s", rResponse.ID))
}
if len(rResponse.Code) > 0 {
errMsgs = append(errMsgs, fmt.Sprintf("code: %s", rResponse.Code))
}
if len(rResponse.Message) > 0 {
errMsgs = append(errMsgs, fmt.Sprintf("message: %s", rResponse.Message))
}
if len(rResponse.Errors) > 0 {
errMsgs = append(errMsgs, fmt.Sprintf("errors: %+v", rResponse.Errors))
}
err := fmt.Errorf("failed sending data: %s", strings.Join(errMsgs, ", "))
if resp.StatusCode == http.StatusBadRequest {
// Report the failure as permanent if the server thinks the request is malformed.
return consumererror.NewPermanent(err)
}
return err
}
}
func (s *sender) createRequest(ctx context.Context, pipeline PipelineType, data io.Reader) (*http.Request, error) {
var url string
if s.config.HTTPClientSettings.Endpoint == "" {
switch pipeline {
case MetricsPipeline:
url = s.dataUrlMetrics
case LogsPipeline:
url = s.dataUrlLogs
case TracesPipeline:
url = s.dataUrlTraces
default:
return nil, fmt.Errorf("unknown pipeline type: %s", pipeline)
}
} else {
url = s.config.HTTPClientSettings.Endpoint
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, data)
if err != nil {
return req, err
}
return req, err
}
// logToText converts LogRecord to a plain text line, returns it and error eventually
func (s *sender) logToText(record plog.LogRecord) string {
return record.Body().AsString()
}
// logToJSON converts LogRecord to a json line, returns it and error eventually
func (s *sender) logToJSON(record plog.LogRecord) (string, error) {
if s.jsonLogsConfig.AddTimestamp {
addJSONTimestamp(record.Attributes(), s.jsonLogsConfig.TimestampKey, record.Timestamp())
}
// Only append the body when it's not empty to prevent sending 'null' log.
if body := record.Body(); !isEmptyAttributeValue(body) {
if s.jsonLogsConfig.FlattenBody && body.Type() == pcommon.ValueTypeMap {
// Cannot use CopyTo, as it overrides data.orig's values
body.MapVal().Range(func(k string, v pcommon.Value) bool {
record.Attributes().Insert(k, v)
return true
})
} else {
record.Attributes().Upsert(s.jsonLogsConfig.LogKey, body)
}
}
nextLine, err := json.Marshal(record.Attributes().AsRaw())
if err != nil {
return "", err
}
return bytes.NewBuffer(nextLine).String(), nil
}
var timeZeroUTC = time.Unix(0, 0).UTC()
// addJSONTimestamp adds a timestamp field to record attribtues before sending
// out the logs as JSON.
// If the attached timestamp is equal to 0 (millisecond based UNIX timestamp)
// then send out current time formatted as milliseconds since January 1, 1970.
func addJSONTimestamp(attrs pcommon.Map, timestampKey string, pt pcommon.Timestamp) {
t := pt.AsTime()
if t == timeZeroUTC {
attrs.InsertInt(timestampKey, time.Now().UnixMilli())
} else {
attrs.InsertInt(timestampKey, t.UnixMilli())
}
}
func isEmptyAttributeValue(att pcommon.Value) bool {
t := att.Type()
return !(t == pcommon.ValueTypeString && len(att.StringVal()) > 0 ||
t == pcommon.ValueTypeSlice && att.SliceVal().Len() > 0 ||
t == pcommon.ValueTypeMap && att.MapVal().Len() > 0 ||
t == pcommon.ValueTypeBytes && att.BytesVal().Len() > 0)
}
// sendNonOTLPLogs sends log records from the logBuffer formatted according
// to configured LogFormat and as the result of execution
// returns array of records which has not been sent correctly and error
func (s *sender) sendNonOTLPLogs(ctx context.Context, rl plog.ResourceLogs, flds fields) ([]plog.LogRecord, error) {
if s.config.LogFormat == OTLPLogFormat {
return nil, fmt.Errorf("Attempting to send OTLP logs as non-OTLP data")
}
var (
body bodyBuilder = newBodyBuilder()
errs []error
droppedRecords []plog.LogRecord
currentRecords []plog.LogRecord
)
slgs := rl.ScopeLogs()
for i := 0; i < slgs.Len(); i++ {
slg := slgs.At(i)
for j := 0; j < slg.LogRecords().Len(); j++ {
lr := slg.LogRecords().At(j)
formattedLine, err := s.formatLogLine(lr)
if err != nil {
droppedRecords = append(droppedRecords, lr)
errs = append(errs, err)
continue
}
sent, err := s.appendAndMaybeSend(ctx, []string{formattedLine}, LogsPipeline, &body, flds)
if err != nil {
errs = append(errs, err)
droppedRecords = append(droppedRecords, currentRecords...)
}
// If data was sent and either failed or succeeded, cleanup the currentRecords slice
if sent {
currentRecords = currentRecords[:0]
}
currentRecords = append(currentRecords, lr)
}
}
if body.Len() > 0 {
if err := s.send(ctx, LogsPipeline, body.toCountingReader(), flds); err != nil {
errs = append(errs, err)
droppedRecords = append(droppedRecords, currentRecords...)
}
}
return droppedRecords, multierr.Combine(errs...)
}
func (s *sender) formatLogLine(lr plog.LogRecord) (string, error) {
var formattedLine string
var err error
switch s.config.LogFormat {
case TextFormat:
formattedLine = s.logToText(lr)
case JSONFormat:
formattedLine, err = s.logToJSON(lr)
default:
err = errors.New("unexpected log format")
}
return formattedLine, err
}
// TODO: add support for HTTP limits
func (s *sender) sendOTLPLogs(ctx context.Context, ld plog.Logs) error {
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
rl := rls.At(i)
if s.config.TranslateAttributes {
translateAttributes(rl.Resource().Attributes()).
CopyTo(rl.Resource().Attributes())
}
// Clear timestamps if required
if s.config.ClearLogsTimestamp {
slgs := rl.ScopeLogs()
for j := 0; j < slgs.Len(); j++ {
log := slgs.At(j)
for k := 0; k < log.LogRecords().Len(); k++ {
log.LogRecords().At(k).SetTimestamp(0)
}
}
}
s.addSourceResourceAttributes(rl.Resource().Attributes())
}
body, err := logsMarshaler.MarshalLogs(ld)
if err != nil {
return err
}
return s.send(ctx, LogsPipeline, newCountingReader(ld.LogRecordCount()).withBytes(body), fields{})
}
// sendNonOTLPMetrics sends metrics in right format basing on the s.config.MetricFormat
func (s *sender) sendNonOTLPMetrics(ctx context.Context, md pmetric.Metrics) (pmetric.Metrics, []error) {
if s.config.MetricFormat == OTLPMetricFormat {
return md, []error{fmt.Errorf("Attempting to send OTLP metrics as non-OTLP data")}
}
var (
body bodyBuilder = newBodyBuilder()
errs []error
currentResources []pmetric.ResourceMetrics
flds fields
)
rms := md.ResourceMetrics()
droppedMetrics := pmetric.NewMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
flds = newFields(rm.Resource().Attributes())
sms := rm.ScopeMetrics()
// generally speaking, it's fine to send multiple ResourceMetrics in a single request
// the only exception is if the computed source headers are different, as those as unique per-request
// so we check if the headers are different here and send what we have if they are
if i > 0 {
currentSourceHeaders := getSourcesHeaders(s.sources, flds)
previousFields := newFields(rms.At(i - 1).Resource().Attributes())
previousSourceHeaders := getSourcesHeaders(s.sources, previousFields)
if !reflect.DeepEqual(previousSourceHeaders, currentSourceHeaders) && body.Len() > 0 {
if err := s.send(ctx, MetricsPipeline, body.toCountingReader(), previousFields); err != nil {
errs = append(errs, err)
for _, resource := range currentResources {
resource.MoveTo(droppedMetrics.ResourceMetrics().AppendEmpty())
}
}
body.Reset()
currentResources = currentResources[:0]
}
}
// transform the metrics into formatted lines ready to be sent
var formattedLines []string
var err error
for i := 0; i < sms.Len(); i++ {
sm := sms.At(i)
for j := 0; j < sm.Metrics().Len(); j++ {
m := sm.Metrics().At(j)
var formattedLine string
switch s.config.MetricFormat {
case PrometheusFormat:
formattedLine = s.prometheusFormatter.metric2String(m, rm.Resource().Attributes())
default:
return md, []error{fmt.Errorf("unexpected metric format: %s", s.config.MetricFormat)}
}
formattedLines = append(formattedLines, formattedLine)
}
}
sent, err := s.appendAndMaybeSend(ctx, formattedLines, MetricsPipeline, &body, flds)
if err != nil {
errs = append(errs, err)
if sent {
// failed at sending, add the resource to the dropped metrics
// move instead of copy here to avoid duplicating data in memory on failure
for _, resource := range currentResources {
resource.MoveTo(droppedMetrics.ResourceMetrics().AppendEmpty())
}
}
}
// If data was sent, cleanup the currentResources slice
if sent {
currentResources = currentResources[:0]
}
currentResources = append(currentResources, rm)
}
if body.Len() > 0 {
if err := s.send(ctx, MetricsPipeline, body.toCountingReader(), flds); err != nil {
errs = append(errs, err)
for _, resource := range currentResources {
resource.MoveTo(droppedMetrics.ResourceMetrics().AppendEmpty())
}
}
}
return droppedMetrics, errs
}
func (s *sender) sendOTLPMetrics(ctx context.Context, md pmetric.Metrics) error {
rms := md.ResourceMetrics()
if rms.Len() == 0 {
s.logger.Debug("there are no metrics to send, moving on")
return nil
}
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
s.addSourceResourceAttributes(rm.Resource().Attributes())
}
body, err := metricsMarshaler.MarshalMetrics(md)
if err != nil {
return err
}
return s.send(ctx, MetricsPipeline, newCountingReader(md.DataPointCount()).withBytes(body), fields{})
}
// appendAndMaybeSend appends line to the request body that will be sent and sends
// the accumulated data if the internal logBuffer has been filled (with config.MaxRequestBodySize bytes).
// It returns a boolean indicating if the data was sent and an error
func (s *sender) appendAndMaybeSend(
ctx context.Context,
lines []string,
pipeline PipelineType,
body *bodyBuilder,
flds fields,
) (sent bool, err error) {
linesTotalLength := 0
for _, line := range lines {
linesTotalLength += len(line) + 1 // count the newline as well
}
if body.Len() > 0 && body.Len()+linesTotalLength >= s.config.MaxRequestBodySize {
sent = true
err = s.send(ctx, pipeline, body.toCountingReader(), flds)
body.Reset()
}
if body.Len() > 0 {
// Do not add newline if the body is empty
body.addNewLine()
}
body.addLines(lines)
return sent, err
}
// sendTraces sends traces in right format basing on the s.config.TraceFormat
func (s *sender) sendTraces(ctx context.Context, td ptrace.Traces) error {
if s.config.TraceFormat == OTLPTraceFormat {
return s.sendOTLPTraces(ctx, td)
}
return nil
}
// sendOTLPTraces sends trace records in OTLP format
func (s *sender) sendOTLPTraces(ctx context.Context, td ptrace.Traces) error {
if td.ResourceSpans().Len() == 0 {
s.logger.Debug("there are no traces to send, moving on")
return nil
}
capacity := td.SpanCount()
for i := 0; i < td.ResourceSpans().Len(); i++ {
s.addSourceResourceAttributes(td.ResourceSpans().At(i).Resource().Attributes())
}
body, err := tracesMarshaler.MarshalTraces(td)
if err != nil {
return err
}
if err := s.send(ctx, TracesPipeline, newCountingReader(capacity).withBytes(body), fields{}); err != nil {
return err
}
return nil
}
// cleanMetricBuffer zeroes metricBuffer
func (s *sender) cleanMetricBuffer() {
s.metricBuffer = (s.metricBuffer)[:0]
}
// countMetrics returns number of metrics in metricBuffer
func (s *sender) countMetrics() int {
return len(s.metricBuffer)
}
func addCompressHeader(req *http.Request, enc CompressEncodingType) error {
switch enc {
case GZIPCompression:
req.Header.Set(headerContentEncoding, contentEncodingGzip)
case DeflateCompression:
req.Header.Set(headerContentEncoding, contentEncodingDeflate)
case NoCompression:
default:
return fmt.Errorf("invalid content encoding: %s", enc)
}
return nil
}
func addSourcesHeaders(req *http.Request, sources sourceFormats, flds fields) {
sourceHeaderValues := getSourcesHeaders(sources, flds)
for headerName, headerValue := range sourceHeaderValues {
req.Header.Add(headerName, headerValue)
}
}
func getSourcesHeaders(sources sourceFormats, flds fields) map[string]string {
sourceHeaderValues := map[string]string{}
if !flds.isInitialized() {
return sourceHeaderValues
}
if sources.host.isSet() {
sourceHeaderValues[headerHost] = sources.host.format(flds)
}
if sources.name.isSet() {
sourceHeaderValues[headerName] = sources.name.format(flds)
}
if sources.category.isSet() {
sourceHeaderValues[headerCategory] = sources.category.format(flds)
}
return sourceHeaderValues
}
func addLogsHeaders(req *http.Request, lf LogFormatType, flds fields) {
switch lf {
case OTLPLogFormat:
req.Header.Add(headerContentType, contentTypeOTLP)
default:
req.Header.Add(headerContentType, contentTypeLogs)
}
if fieldsStr := flds.string(); fieldsStr != "" {
req.Header.Add(headerFields, fieldsStr)
}
}
func addMetricsHeaders(req *http.Request, mf MetricFormatType) error {
switch mf {
case PrometheusFormat:
req.Header.Add(headerContentType, contentTypePrometheus)
case OTLPMetricFormat:
req.Header.Add(headerContentType, contentTypeOTLP)
default:
return fmt.Errorf("unsupported metrics format: %s", mf)
}
return nil
}
func addTracesHeaders(req *http.Request, tf TraceFormatType) error {
switch tf {
case OTLPTraceFormat:
req.Header.Add(headerContentType, contentTypeOTLP)
default:
return fmt.Errorf("unsupported traces format: %s", tf)
}
return nil
}
func (s *sender) addRequestHeaders(req *http.Request, pipeline PipelineType, flds fields) error {
req.Header.Add(headerClient, s.config.Client)
if err := addCompressHeader(req, s.config.CompressEncoding); err != nil {
return err
}
addSourcesHeaders(req, s.sources, flds)
switch pipeline {
case LogsPipeline:
addLogsHeaders(req, s.config.LogFormat, flds)
case MetricsPipeline:
if err := addMetricsHeaders(req, s.config.MetricFormat); err != nil {
return err
}
case TracesPipeline:
if err := addTracesHeaders(req, s.config.TraceFormat); err != nil {
return err
}
default:
return fmt.Errorf("unexpected pipeline: %v", pipeline)
}
return nil
}
// addSourceResourceAttributes adds source related attributes:
// * source category
// * source host
// * source name
// to the provided attribute map using the provided fields as values source and using
// the source templates for formatting.
func (s *sender) addSourceRelatedResourceAttributesFromFields(attrs pcommon.Map, flds fields) {
if s.sources.host.isSet() {
attrs.InsertString(attributeKeySourceHost, s.sources.host.format(flds))
}
if s.sources.name.isSet() {
attrs.InsertString(attributeKeySourceName, s.sources.name.format(flds))
}
if s.sources.category.isSet() {
attrs.InsertString(attributeKeySourceCategory, s.sources.category.format(flds))
}
}
// addSourceResourceAttributes adds source related attributes:
// * source category
// * source host
// * source name
// to the provided attribute map, according to the corresponding templates.
//
// When those attributes are already in the attribute map then nothing is
// changed since attributes that are provided with data have precedence over
// exporter configuration.
func (s *sender) addSourceResourceAttributes(attrs pcommon.Map) {
if s.sources.host.isSet() {
if _, ok := attrs.Get(attributeKeySourceHost); !ok {
attrs.InsertString(attributeKeySourceHost, s.sources.host.formatPdataMap(attrs))
}
}
if s.sources.name.isSet() {
if _, ok := attrs.Get(attributeKeySourceName); !ok {
attrs.InsertString(attributeKeySourceName, s.sources.name.formatPdataMap(attrs))
}
}
if s.sources.category.isSet() {
if _, ok := attrs.Get(attributeKeySourceCategory); !ok {
attrs.InsertString(attributeKeySourceCategory, s.sources.category.formatPdataMap(attrs))
}
}
}
func (s *sender) recordMetrics(duration time.Duration, count int64, req *http.Request, resp *http.Response, pipeline PipelineType) {
statusCode := 0
if resp != nil {
statusCode = resp.StatusCode
}
id := s.config.ID().String()
if err := observability.RecordRequestsDuration(duration, statusCode, req.URL.String(), string(pipeline), id); err != nil {
s.logger.Debug("error for recording metric for request duration", zap.Error(err))
}
if err := observability.RecordRequestsBytes(req.ContentLength, statusCode, req.URL.String(), string(pipeline), id); err != nil {
s.logger.Debug("error for recording metric for sent bytes", zap.Error(err))
}
if err := observability.RecordRequestsRecords(count, statusCode, req.URL.String(), string(pipeline), id); err != nil {
s.logger.Debug("error for recording metric for sent records", zap.Error(err))
}
if err := observability.RecordRequestsSent(statusCode, req.URL.String(), string(pipeline), id); err != nil {
s.logger.Debug("error for recording metric for sent request", zap.Error(err))
}
}