From cc2ce16c0ce446fe9f9ce52b7c44fc0a9b606ef7 Mon Sep 17 00:00:00 2001 From: Joonsoo Park Date: Thu, 2 Nov 2023 01:57:18 +0900 Subject: [PATCH] [chore][exporter/sumologicexporter] use errors.Join instead of go.uber.org/multierr (#28614) **Description:** use errors.Join instead of go.uber.org/multierr **Link to tracking Issue:** #25121 --------- Co-authored-by: Andrzej Stencel --- exporter/sumologicexporter/exporter.go | 22 ++++++++-------- exporter/sumologicexporter/go.mod | 2 +- exporter/sumologicexporter/sender.go | 31 +++++++++++------------ exporter/sumologicexporter/sender_test.go | 6 ++--- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/exporter/sumologicexporter/exporter.go b/exporter/sumologicexporter/exporter.go index 398ed8a02a6a..bbc4f454a9f7 100644 --- a/exporter/sumologicexporter/exporter.go +++ b/exporter/sumologicexporter/exporter.go @@ -5,6 +5,7 @@ package sumologicexporter // import "github.com/open-telemetry/opentelemetry-col import ( "context" + "errors" "fmt" "net/http" @@ -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 { @@ -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 ) @@ -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() @@ -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) } } } @@ -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 { @@ -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 @@ -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 ) @@ -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() @@ -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) } } } @@ -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 { @@ -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 diff --git a/exporter/sumologicexporter/go.mod b/exporter/sumologicexporter/go.mod index 8ee9638512d1..a6a1257d06d6 100644 --- a/exporter/sumologicexporter/go.mod +++ b/exporter/sumologicexporter/go.mod @@ -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 ( @@ -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 diff --git a/exporter/sumologicexporter/sender.go b/exporter/sumologicexporter/sender.go index 0f3f1513c44d..08dfec87a2aa 100644 --- a/exporter/sumologicexporter/sender.go +++ b/exporter/sumologicexporter/sender.go @@ -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 { @@ -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 ) @@ -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...) } @@ -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 ) @@ -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...) } @@ -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 @@ -311,19 +310,19 @@ 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 } } @@ -331,12 +330,12 @@ func (s *sender) appendAndSend( 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 diff --git a/exporter/sumologicexporter/sender_test.go b/exporter/sumologicexporter/sender_test.go index 71e71a3b4b06..68c6bb47efb7 100644 --- a/exporter/sumologicexporter/sender_test.go +++ b/exporter/sumologicexporter/sender_test.go @@ -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) } @@ -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) } @@ -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) }