Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] migrate all the files under connector to use errors.Join #25184

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions connector/countconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package countconnector // import "github.com/open-telemetry/opentelemetry-collec

import (
"context"
"errors"
"fmt"

"go.opentelemetry.io/collector/component"
Expand All @@ -13,7 +14,6 @@ import (
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
Expand Down Expand Up @@ -43,7 +43,7 @@ func (c *count) Capabilities() consumer.Capabilities {
}

func (c *count) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
var errors error
var multiError error
countMetrics := pmetric.NewMetrics()
countMetrics.ResourceMetrics().EnsureCapacity(td.ResourceSpans().Len())
for i := 0; i < td.ResourceSpans().Len(); i++ {
Expand All @@ -57,12 +57,12 @@ func (c *count) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
for k := 0; k < scopeSpan.Spans().Len(); k++ {
span := scopeSpan.Spans().At(k)
sCtx := ottlspan.NewTransformContext(span, scopeSpan.Scope(), resourceSpan.Resource())
errors = multierr.Append(errors, spansCounter.update(ctx, span.Attributes(), sCtx))
multiError = errors.Join(multiError, spansCounter.update(ctx, span.Attributes(), sCtx))

for l := 0; l < span.Events().Len(); l++ {
event := span.Events().At(l)
eCtx := ottlspanevent.NewTransformContext(event, span, scopeSpan.Scope(), resourceSpan.Resource())
errors = multierr.Append(errors, spanEventsCounter.update(ctx, event.Attributes(), eCtx))
multiError = errors.Join(multiError, spanEventsCounter.update(ctx, event.Attributes(), eCtx))
}
}
}
Expand All @@ -81,14 +81,14 @@ func (c *count) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
spansCounter.appendMetricsTo(countScope.Metrics())
spanEventsCounter.appendMetricsTo(countScope.Metrics())
}
if errors != nil {
return errors
if multiError != nil {
return multiError
}
return c.metricsConsumer.ConsumeMetrics(ctx, countMetrics)
}

func (c *count) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
var errors error
var multiError error
countMetrics := pmetric.NewMetrics()
countMetrics.ResourceMetrics().EnsureCapacity(md.ResourceMetrics().Len())
for i := 0; i < md.ResourceMetrics().Len(); i++ {
Expand All @@ -102,42 +102,42 @@ func (c *count) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
for k := 0; k < scopeMetrics.Metrics().Len(); k++ {
metric := scopeMetrics.Metrics().At(k)
mCtx := ottlmetric.NewTransformContext(metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource())
errors = multierr.Append(errors, metricsCounter.update(ctx, pcommon.NewMap(), mCtx))
multiError = errors.Join(multiError, metricsCounter.update(ctx, pcommon.NewMap(), mCtx))

//exhaustive:enforce
switch metric.Type() {
case pmetric.MetricTypeGauge:
dps := metric.Gauge().DataPoints()
for i := 0; i < dps.Len(); i++ {
dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource())
errors = multierr.Append(errors, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
multiError = errors.Join(multiError, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
}
case pmetric.MetricTypeSum:
dps := metric.Sum().DataPoints()
for i := 0; i < dps.Len(); i++ {
dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource())
errors = multierr.Append(errors, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
multiError = errors.Join(multiError, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
}
case pmetric.MetricTypeSummary:
dps := metric.Summary().DataPoints()
for i := 0; i < dps.Len(); i++ {
dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource())
errors = multierr.Append(errors, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
multiError = errors.Join(multiError, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
}
case pmetric.MetricTypeHistogram:
dps := metric.Histogram().DataPoints()
for i := 0; i < dps.Len(); i++ {
dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource())
errors = multierr.Append(errors, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
multiError = errors.Join(multiError, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
}
case pmetric.MetricTypeExponentialHistogram:
dps := metric.ExponentialHistogram().DataPoints()
for i := 0; i < dps.Len(); i++ {
dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource())
errors = multierr.Append(errors, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
multiError = errors.Join(multiError, dataPointsCounter.update(ctx, dps.At(i).Attributes(), dCtx))
}
case pmetric.MetricTypeEmpty:
errors = multierr.Append(errors, fmt.Errorf("metric %q: invalid metric type: %v", metric.Name(), metric.Type()))
multiError = errors.Join(multiError, fmt.Errorf("metric %q: invalid metric type: %v", metric.Name(), metric.Type()))
}
}
}
Expand All @@ -156,14 +156,14 @@ func (c *count) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
metricsCounter.appendMetricsTo(countScope.Metrics())
dataPointsCounter.appendMetricsTo(countScope.Metrics())
}
if errors != nil {
return errors
if multiError != nil {
return multiError
}
return c.metricsConsumer.ConsumeMetrics(ctx, countMetrics)
}

func (c *count) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
var errors error
var multiError error
countMetrics := pmetric.NewMetrics()
countMetrics.ResourceMetrics().EnsureCapacity(ld.ResourceLogs().Len())
for i := 0; i < ld.ResourceLogs().Len(); i++ {
Expand All @@ -177,7 +177,7 @@ func (c *count) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
logRecord := scopeLogs.LogRecords().At(k)

lCtx := ottllog.NewTransformContext(logRecord, scopeLogs.Scope(), resourceLog.Resource())
errors = multierr.Append(errors, counter.update(ctx, logRecord.Attributes(), lCtx))
multiError = errors.Join(multiError, counter.update(ctx, logRecord.Attributes(), lCtx))
}
}

Expand All @@ -194,8 +194,8 @@ func (c *count) ConsumeLogs(ctx context.Context, ld plog.Logs) error {

counter.appendMetricsTo(countScope.Metrics())
}
if errors != nil {
return errors
if multiError != nil {
return multiError
}
return c.metricsConsumer.ConsumeMetrics(ctx, countMetrics)
}
12 changes: 6 additions & 6 deletions connector/countconnector/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package countconnector // import "github.com/open-telemetry/opentelemetry-collec

import (
"context"
"errors"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
)
Expand All @@ -36,7 +36,7 @@ type attrCounter struct {
}

func (c *counter[K]) update(ctx context.Context, attrs pcommon.Map, tCtx K) error {
var errors error
var multiError error
for name, md := range c.metricDefs {
countAttrs := pcommon.NewMap()
for _, attr := range md.attrs {
Expand All @@ -54,17 +54,17 @@ func (c *counter[K]) update(ctx context.Context, attrs pcommon.Map, tCtx K) erro

// No conditions, so match all.
if md.condition == nil {
errors = multierr.Append(errors, c.increment(name, countAttrs))
multiError = errors.Join(multiError, c.increment(name, countAttrs))
continue
}

if match, err := md.condition.Eval(ctx, tCtx); err != nil {
errors = multierr.Append(errors, err)
multiError = errors.Join(multiError, err)
} else if match {
errors = multierr.Append(errors, c.increment(name, countAttrs))
multiError = errors.Join(multiError, c.increment(name, countAttrs))
}
}
return errors
return multiError
}

func (c *counter[K]) increment(metricName string, attrs pcommon.Map) error {
Expand Down
2 changes: 1 addition & 1 deletion connector/countconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
go.opentelemetry.io/collector/connector v0.82.0
go.opentelemetry.io/collector/consumer v0.82.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0014
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.25.0
)

Expand Down Expand Up @@ -43,6 +42,7 @@ require (
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion connector/routingconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
go.opentelemetry.io/collector/connector v0.82.0
go.opentelemetry.io/collector/consumer v0.82.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0014
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.25.0
)

Expand Down Expand Up @@ -38,6 +37,7 @@ require (
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions connector/routingconnector/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package routingconnector // import "github.com/open-telemetry/opentelemetry-coll

import (
"context"
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c *logsConnector) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
}
}
for consumer, group := range groups {
errs = multierr.Append(errs, consumer.ConsumeLogs(ctx, group))
errs = errors.Join(errs, consumer.ConsumeLogs(ctx, group))
}
return errs
}
Expand Down
4 changes: 2 additions & 2 deletions connector/routingconnector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package routingconnector // import "github.com/open-telemetry/opentelemetry-coll

import (
"context"
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand Down Expand Up @@ -95,7 +95,7 @@ func (c *metricsConnector) ConsumeMetrics(ctx context.Context, md pmetric.Metric
}

for consumer, group := range groups {
errs = multierr.Append(errs, consumer.ConsumeMetrics(ctx, group))
errs = errors.Join(errs, consumer.ConsumeMetrics(ctx, group))
}
return errs
}
Expand Down
4 changes: 2 additions & 2 deletions connector/routingconnector/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package routingconnector // import "github.com/open-telemetry/opentelemetry-coll

import (
"context"
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c *tracesConnector) ConsumeTraces(ctx context.Context, t ptrace.Traces) er
}

for consumer, group := range groups {
errs = multierr.Append(errs, consumer.ConsumeTraces(ctx, group))
errs = errors.Join(errs, consumer.ConsumeTraces(ctx, group))
}
return errs
}
Expand Down
4 changes: 2 additions & 2 deletions connector/spanmetricsconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package spanmetricsconnector

import (
"errors"
"path/filepath"
"testing"
"time"
Expand All @@ -13,7 +14,6 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector/internal/metrics"
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestLoadConfig(t *testing.T) {
err = component.UnmarshalConfig(sub, cfg)

if tt.expected == nil {
err = multierr.Append(err, component.ValidateConfig(cfg))
err = errors.Join(err, component.ValidateConfig(cfg))
assert.ErrorContains(t, err, tt.errorMessage)
return
}
Expand Down
2 changes: 1 addition & 1 deletion connector/spanmetricsconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
go.opentelemetry.io/collector/consumer v0.82.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0014
go.opentelemetry.io/collector/semconv v0.82.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.25.0
google.golang.org/grpc v1.57.0
)
Expand All @@ -42,6 +41,7 @@ require (
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
Expand Down