Skip to content

Commit

Permalink
[chore][exporter/sumologicexporter] use errors.Join instead of go.ube…
Browse files Browse the repository at this point in the history
…r.org/multierr (open-telemetry#28614)

**Description:** use errors.Join instead of go.uber.org/multierr

**Link to tracking Issue:** open-telemetry#25121 

---------

Co-authored-by: Andrzej Stencel <astencel@sumologic.com>
  • Loading branch information
2 people authored and RoryCrispin committed Nov 24, 2023
1 parent 6bd3993 commit cc2ce16
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
22 changes: 11 additions & 11 deletions exporter/sumologicexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sumologicexporter // import "github.com/open-telemetry/opentelemetry-col

import (
"context"
"errors"
"fmt"
"net/http"

Expand All @@ -15,7 +16,6 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"
)

type sumologicexporter struct {
Expand Down Expand Up @@ -142,7 +142,7 @@ func (se *sumologicexporter) pushLogsData(ctx context.Context, ld plog.Logs) err
var (
currentMetadata fields
previousMetadata = newFields(pcommon.NewMap())
errs error
errs []error
droppedRecords []plog.LogRecord
err error
)
Expand Down Expand Up @@ -183,7 +183,7 @@ func (se *sumologicexporter) pushLogsData(ctx context.Context, ld plog.Logs) err
var dropped []plog.LogRecord
dropped, err = sdr.sendLogs(ctx, previousMetadata)
if err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
droppedRecords = append(droppedRecords, dropped...)
}
sdr.cleanLogsBuffer()
Expand All @@ -197,7 +197,7 @@ func (se *sumologicexporter) pushLogsData(ctx context.Context, ld plog.Logs) err
dropped, err = sdr.batchLog(ctx, log, previousMetadata)
if err != nil {
droppedRecords = append(droppedRecords, dropped...)
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
}
}
Expand All @@ -207,7 +207,7 @@ func (se *sumologicexporter) pushLogsData(ctx context.Context, ld plog.Logs) err
dropped, err := sdr.sendLogs(ctx, previousMetadata)
if err != nil {
droppedRecords = append(droppedRecords, dropped...)
errs = multierr.Append(errs, err)
errs = append(errs, err)
}

if len(droppedRecords) > 0 {
Expand All @@ -222,7 +222,7 @@ func (se *sumologicexporter) pushLogsData(ctx context.Context, ld plog.Logs) err
log.CopyTo(tgt)
}

return consumererror.NewLogs(errs, droppedLogs)
return consumererror.NewLogs(errors.Join(errs...), droppedLogs)
}

return nil
Expand All @@ -235,7 +235,7 @@ func (se *sumologicexporter) pushMetricsData(ctx context.Context, md pmetric.Met
var (
currentMetadata fields
previousMetadata = newFields(pcommon.NewMap())
errs error
errs []error
droppedRecords []metricPair
attributes pcommon.Map
)
Expand Down Expand Up @@ -282,7 +282,7 @@ func (se *sumologicexporter) pushMetricsData(ctx context.Context, md pmetric.Met
var dropped []metricPair
dropped, err = sdr.sendMetrics(ctx, previousMetadata)
if err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
droppedRecords = append(droppedRecords, dropped...)
}
sdr.cleanMetricBuffer()
Expand All @@ -295,7 +295,7 @@ func (se *sumologicexporter) pushMetricsData(ctx context.Context, md pmetric.Met
dropped, err = sdr.batchMetric(ctx, mp, currentMetadata)
if err != nil {
droppedRecords = append(droppedRecords, dropped...)
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
}
}
Expand All @@ -305,7 +305,7 @@ func (se *sumologicexporter) pushMetricsData(ctx context.Context, md pmetric.Met
dropped, err := sdr.sendMetrics(ctx, previousMetadata)
if err != nil {
droppedRecords = append(droppedRecords, dropped...)
errs = multierr.Append(errs, err)
errs = append(errs, err)
}

if len(droppedRecords) > 0 {
Expand All @@ -321,7 +321,7 @@ func (se *sumologicexporter) pushMetricsData(ctx context.Context, md pmetric.Met
record.metric.CopyTo(ilms.AppendEmpty().Metrics().AppendEmpty())
}

return consumererror.NewMetrics(errs, droppedMetrics)
return consumererror.NewMetrics(errors.Join(errs...), droppedMetrics)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion exporter/sumologicexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
go.opentelemetry.io/collector/consumer v0.88.1-0.20231026220224-6405e152a2d9
go.opentelemetry.io/collector/exporter v0.88.1-0.20231026220224-6405e152a2d9
go.opentelemetry.io/collector/pdata v1.0.0-rcv0017.0.20231026220224-6405e152a2d9
go.uber.org/multierr v1.11.0
)

require (
Expand Down Expand Up @@ -50,6 +49,7 @@ require (
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
Expand Down
31 changes: 15 additions & 16 deletions exporter/sumologicexporter/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"
)

type appendResponse struct {
Expand Down Expand Up @@ -181,7 +180,7 @@ func (s *sender) logToJSON(record plog.LogRecord) (string, error) {
func (s *sender) sendLogs(ctx context.Context, flds fields) ([]plog.LogRecord, error) {
var (
body strings.Builder
errs error
errs []error
droppedRecords []plog.LogRecord
currentRecords []plog.LogRecord
)
Expand All @@ -201,13 +200,13 @@ func (s *sender) sendLogs(ctx context.Context, flds fields) ([]plog.LogRecord, e

if err != nil {
droppedRecords = append(droppedRecords, record)
errs = multierr.Append(errs, err)
errs = append(errs, err)
continue
}

ar, err := s.appendAndSend(ctx, formattedLine, LogsPipeline, &body, flds)
if err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
if ar.sent {
droppedRecords = append(droppedRecords, currentRecords...)
}
Expand All @@ -230,19 +229,19 @@ func (s *sender) sendLogs(ctx context.Context, flds fields) ([]plog.LogRecord, e

if body.Len() > 0 {
if err := s.send(ctx, LogsPipeline, strings.NewReader(body.String()), flds); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
droppedRecords = append(droppedRecords, currentRecords...)
}
}

return droppedRecords, errs
return droppedRecords, errors.Join(errs...)
}

// sendMetrics sends metrics in right format basing on the s.config.MetricFormat
func (s *sender) sendMetrics(ctx context.Context, flds fields) ([]metricPair, error) {
var (
body strings.Builder
errs error
errs []error
droppedRecords []metricPair
currentRecords []metricPair
)
Expand All @@ -264,13 +263,13 @@ func (s *sender) sendMetrics(ctx context.Context, flds fields) ([]metricPair, er

if err != nil {
droppedRecords = append(droppedRecords, record)
errs = multierr.Append(errs, err)
errs = append(errs, err)
continue
}

ar, err := s.appendAndSend(ctx, formattedLine, MetricsPipeline, &body, flds)
if err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
if ar.sent {
droppedRecords = append(droppedRecords, currentRecords...)
}
Expand All @@ -293,12 +292,12 @@ func (s *sender) sendMetrics(ctx context.Context, flds fields) ([]metricPair, er

if body.Len() > 0 {
if err := s.send(ctx, MetricsPipeline, strings.NewReader(body.String()), flds); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
droppedRecords = append(droppedRecords, currentRecords...)
}
}

return droppedRecords, errs
return droppedRecords, errors.Join(errs...)
}

// appendAndSend appends line to the request body that will be sent and sends
Expand All @@ -311,32 +310,32 @@ func (s *sender) appendAndSend(
body *strings.Builder,
flds fields,
) (appendResponse, error) {
var errs error
var errs []error
ar := newAppendResponse()

if body.Len() > 0 && body.Len()+len(line) >= s.config.MaxRequestBodySize {
ar.sent = true
errs = multierr.Append(errs, s.send(ctx, pipeline, strings.NewReader(body.String()), flds))
errs = append(errs, s.send(ctx, pipeline, strings.NewReader(body.String()), flds))
body.Reset()
}

if body.Len() > 0 {
// Do not add newline if the body is empty
if _, err := body.WriteString("\n"); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
ar.appended = false
}
}

if ar.appended {
// Do not append new line if separator was not appended
if _, err := body.WriteString(line); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
ar.appended = false
}
}

return ar, errs
return ar, errors.Join(errs...)
}

// cleanLogsBuffer zeroes logBuffer
Expand Down
6 changes: 3 additions & 3 deletions exporter/sumologicexporter/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func TestSendLogsSplitFailedAll(t *testing.T) {
assert.EqualError(
t,
err,
"error during sending data: 500 Internal Server Error; error during sending data: 404 Not Found",
"error during sending data: 500 Internal Server Error\nerror during sending data: 404 Not Found",
)
assert.Equal(t, test.s.logBuffer[0:2], dropped)
}
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestSendLogsJsonSplitFailedAll(t *testing.T) {
assert.EqualError(
t,
err,
"error during sending data: 500 Internal Server Error; error during sending data: 404 Not Found",
"error during sending data: 500 Internal Server Error\nerror during sending data: 404 Not Found",
)
assert.Equal(t, test.s.logBuffer[0:2], dropped)
}
Expand Down Expand Up @@ -712,7 +712,7 @@ gauge_metric_name{foo="bar",remote_name="156955",url="http://another_url"} 245 1
assert.EqualError(
t,
err,
"error during sending data: 500 Internal Server Error; error during sending data: 404 Not Found",
"error during sending data: 500 Internal Server Error\nerror during sending data: 404 Not Found",
)
assert.Equal(t, test.s.metricBuffer[0:2], dropped)
}
Expand Down

0 comments on commit cc2ce16

Please sign in to comment.