Skip to content

Commit

Permalink
Move CombineErrors to consumererror package (open-telemetry#2442)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Feb 12, 2021
1 parent 9de6dd7 commit 5cc0707
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 70 deletions.
27 changes: 2 additions & 25 deletions component/componenterror/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package componenterror

import (
"errors"
"fmt"
"strings"

"go.opentelemetry.io/collector/consumer/consumererror"
)
Expand All @@ -36,28 +34,7 @@ var (
)

// CombineErrors converts a list of errors into one error.
// Deprecated: use consumererror.CombineErrors instead.
func CombineErrors(errs []error) error {
numErrors := len(errs)
if numErrors == 0 {
// No errors
return nil
}

if numErrors == 1 {
return errs[0]
}

errMsgs := make([]string, 0, numErrors)
permanent := false
for _, err := range errs {
if !permanent && consumererror.IsPermanent(err) {
permanent = true
}
errMsgs = append(errMsgs, err.Error())
}
err := fmt.Errorf("[%s]", strings.Join(errMsgs, "; "))
if permanent {
err = consumererror.Permanent(err)
}
return err
return consumererror.CombineErrors(errs)
}
6 changes: 3 additions & 3 deletions config/configcheck/configcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"strings"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer/consumererror"
)

// The regular expression for valid config field tag.
Expand Down Expand Up @@ -56,7 +56,7 @@ func ValidateConfigFromFactories(factories component.Factories) error {
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

// ValidateConfig enforces that given configuration object is following the patterns
Expand Down Expand Up @@ -109,7 +109,7 @@ func validateConfigDataType(t reflect.Type) error {
// reflect.UnsafePointer.
}

if err := componenterror.CombineErrors(errs); err != nil {
if err := consumererror.CombineErrors(errs); err != nil {
return fmt.Errorf(
"type %q from package %q has invalid config settings: %v",
t.Name(),
Expand Down
47 changes: 47 additions & 0 deletions consumer/consumererror/combineerrors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 consumererror

import (
"fmt"
"strings"
)

// CombineErrors converts a list of errors into one error.
func CombineErrors(errs []error) error {
numErrors := len(errs)
if numErrors == 0 {
// No errors
return nil
}

if numErrors == 1 {
return errs[0]
}

errMsgs := make([]string, 0, numErrors)
permanent := false
for _, err := range errs {
if !permanent && IsPermanent(err) {
permanent = true
}
errMsgs = append(errMsgs, err.Error())
}
err := fmt.Errorf("[%s]", strings.Join(errMsgs, "; "))
if permanent {
err = Permanent(err)
}
return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package componenterror_test
package consumererror

import (
"fmt"
"testing"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer/consumererror"
)

func TestCombineErrors(t *testing.T) {
Expand Down Expand Up @@ -50,20 +47,20 @@ func TestCombineErrors(t *testing.T) {
errors: []error{
fmt.Errorf("foo"),
fmt.Errorf("bar"),
consumererror.Permanent(fmt.Errorf("permanent"))},
Permanent(fmt.Errorf("permanent"))},
expected: "Permanent error: [foo; bar; Permanent error: permanent]",
},
}

for _, tc := range testCases {
got := componenterror.CombineErrors(tc.errors)
got := CombineErrors(tc.errors)
if (got == nil) != tc.expectNil {
t.Errorf("CombineErrors(%v) == nil? Got: %t. Want: %t", tc.errors, got == nil, tc.expectNil)
}
if got != nil && tc.expected != got.Error() {
t.Errorf("CombineErrors(%v) = %q. Want: %q", tc.errors, got, tc.expected)
}
if tc.expectedPermanent && !consumererror.IsPermanent(got) {
if tc.expectedPermanent && !IsPermanent(got) {
t.Errorf("CombineErrors(%v) = %q. Want: consumererror.permanent", tc.errors, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/kafkaexporter/jaeger_marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/gogo/protobuf/jsonpb"
jaegerproto "github.com/jaegertracing/jaeger/model"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
jaegertranslator "go.opentelemetry.io/collector/translator/trace/jaeger"
)
Expand Down Expand Up @@ -50,7 +50,7 @@ func (j jaegerMarshaller) Marshal(traces pdata.Traces) ([]Message, error) {
messages = append(messages, Message{Value: bts})
}
}
return messages, componenterror.CombineErrors(errs)
return messages, consumererror.CombineErrors(errs)
}

func (j jaegerMarshaller) Encoding() string {
Expand Down
3 changes: 1 addition & 2 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/golang/snappy"
"github.com/prometheus/prometheus/prompb"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
otlp "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1"
Expand Down Expand Up @@ -155,7 +154,7 @@ func (prwe *PrwExporter) PushMetrics(ctx context.Context, md pdata.Metrics) (int
}

if dropped != 0 {
return dropped, componenterror.CombineErrors(errs)
return dropped, consumererror.CombineErrors(errs)
}

return 0, nil
Expand Down
8 changes: 4 additions & 4 deletions processor/cloningfanoutconnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package processor
import (
"context"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func (mfc metricsCloningFanOutConnector) ConsumeMetrics(ctx context.Context, md
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

// NewTracesCloningFanOutConnector wraps multiple traces consumers in a single one and clones the data
Expand Down Expand Up @@ -98,7 +98,7 @@ func (tfc tracesCloningFanOutConnector) ConsumeTraces(ctx context.Context, td pd
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

// NewLogsCloningFanOutConnector wraps multiple trace consumers in a single one.
Expand Down Expand Up @@ -134,5 +134,5 @@ func (lfc logsCloningFanOutConnector) ConsumeLogs(ctx context.Context, ld pdata.
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}
8 changes: 4 additions & 4 deletions processor/fanoutconnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package processor
import (
"context"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
)

Expand Down Expand Up @@ -46,7 +46,7 @@ func (mfc metricsFanOutConnector) ConsumeMetrics(ctx context.Context, md pdata.M
errs = append(errs, err)
}
}
return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

// NewTracesFanOutConnector wraps multiple trace consumers in a single one.
Expand All @@ -70,7 +70,7 @@ func (tfc traceFanOutConnector) ConsumeTraces(ctx context.Context, td pdata.Trac
errs = append(errs, err)
}
}
return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

// NewLogsFanOutConnector wraps multiple log consumers in a single one.
Expand All @@ -94,5 +94,5 @@ func (fc logsFanOutConnector) ConsumeLogs(ctx context.Context, ld pdata.Logs) er
errs = append(errs, err)
}
}
return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}
3 changes: 2 additions & 1 deletion receiver/jaegerreceiver/trace_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/obsreport"
jaegertranslator "go.opentelemetry.io/collector/translator/trace/jaeger"
)
Expand Down Expand Up @@ -233,7 +234,7 @@ func (jr *jReceiver) Shutdown(context.Context) error {
jr.grpc.Stop()
jr.grpc = nil
}
err = componenterror.CombineErrors(errs)
err = consumererror.CombineErrors(errs)
})

return err
Expand Down
3 changes: 1 addition & 2 deletions receiver/scraperhelper/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"strings"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer/consumererror"
)

Expand All @@ -35,7 +34,7 @@ func CombineScrapeErrors(errs []error) error {
}

if !partialScrapeErr {
return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

errMsgs := make([]string, 0, len(errs))
Expand Down
4 changes: 2 additions & 2 deletions receiver/scraperhelper/scrapercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (sc *controller) Shutdown(ctx context.Context) error {
errs = append(errs, err)
}
}
return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

// startScraping initiates a ticker that calls Scrape based on the configured
Expand Down Expand Up @@ -249,7 +249,7 @@ func (mms *multiMetricScraper) Shutdown(ctx context.Context) error {
errs = append(errs, err)
}
}
return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

func (mms *multiMetricScraper) Scrape(ctx context.Context, receiverName string) (pdata.ResourceMetricsSlice, error) {
Expand Down
3 changes: 1 addition & 2 deletions receiver/scraperhelper/scrapercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
Expand Down Expand Up @@ -327,7 +326,7 @@ func getExpectedShutdownErr(test metricsTestCase) error {
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

func assertChannelsCalled(t *testing.T, chs []chan bool, message string) {
Expand Down
8 changes: 4 additions & 4 deletions service/builder/exporters_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config/configerror"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer/consumererror"
)

// builtExporter is an exporter that is built based on a config. It can have
Expand All @@ -43,7 +43,7 @@ func (bexp *builtExporter) Start(ctx context.Context, host component.Host) error
}
}

return componenterror.CombineErrors(errors)
return consumererror.CombineErrors(errors)
}

// Shutdown the trace component and the metrics component of an exporter.
Expand All @@ -56,7 +56,7 @@ func (bexp *builtExporter) Shutdown(ctx context.Context) error {
}
}

return componenterror.CombineErrors(errors)
return consumererror.CombineErrors(errors)
}

func (bexp *builtExporter) getTraceExporter() component.TracesExporter {
Expand Down Expand Up @@ -109,7 +109,7 @@ func (exps Exporters) ShutdownAll(ctx context.Context) error {
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

func (exps Exporters) ToMapByDataType() map[configmodels.DataType]map[configmodels.Exporter]component.Exporter {
Expand Down
6 changes: 3 additions & 3 deletions service/builder/extensions_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer/consumererror"
)

// builtExporter is an exporter that is built based on a config. It can have
Expand Down Expand Up @@ -71,7 +71,7 @@ func (exts Extensions) ShutdownAll(ctx context.Context) error {
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

func (exts Extensions) NotifyPipelineReady() error {
Expand Down Expand Up @@ -99,7 +99,7 @@ func (exts Extensions) NotifyPipelineNotReady() error {
}
}

return componenterror.CombineErrors(errs)
return consumererror.CombineErrors(errs)
}

func (exts Extensions) ToMap() map[configmodels.Extension]component.ServiceExtension {
Expand Down
Loading

0 comments on commit 5cc0707

Please sign in to comment.