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

[internal/sqlquery] use errors.Join instead of multierr #31768

Merged
merged 3 commits into from
Mar 15, 2024
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
37 changes: 18 additions & 19 deletions internal/sqlquery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
)

type Config struct {
Expand Down Expand Up @@ -48,36 +47,36 @@ type Query struct {
}

func (q Query) Validate() error {
var errs error
var errs []error
if q.SQL == "" {
errs = multierr.Append(errs, errors.New("'query.sql' cannot be empty"))
errs = append(errs, errors.New("'query.sql' cannot be empty"))
}
if len(q.Logs) == 0 && len(q.Metrics) == 0 {
errs = multierr.Append(errs, errors.New("at least one of 'query.logs' and 'query.metrics' must not be empty"))
errs = append(errs, errors.New("at least one of 'query.logs' and 'query.metrics' must not be empty"))
}
for _, logs := range q.Logs {
if err := logs.Validate(); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
}
for _, metric := range q.Metrics {
if err := metric.Validate(); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
}
return errs
return errors.Join(errs...)
}

type LogsCfg struct {
BodyColumn string `mapstructure:"body_column"`
}

func (config LogsCfg) Validate() error {
var errs error
var errs []error
if config.BodyColumn == "" {
errs = multierr.Append(errs, errors.New("'body_column' must not be empty"))
errs = append(errs, errors.New("'body_column' must not be empty"))
}
return errs
return errors.Join(errs...)
}

type MetricCfg struct {
Expand All @@ -96,29 +95,29 @@ type MetricCfg struct {
}

func (c MetricCfg) Validate() error {
var errs error
var errs []error
if c.MetricName == "" {
errs = multierr.Append(errs, errors.New("'metric_name' cannot be empty"))
errs = append(errs, errors.New("'metric_name' cannot be empty"))
}
if c.ValueColumn == "" {
errs = multierr.Append(errs, errors.New("'value_column' cannot be empty"))
errs = append(errs, errors.New("'value_column' cannot be empty"))
}
if err := c.ValueType.Validate(); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
if err := c.DataType.Validate(); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
if err := c.Aggregation.Validate(); err != nil {
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
if c.DataType == MetricTypeGauge && c.Aggregation != "" {
errs = multierr.Append(errs, fmt.Errorf("aggregation=%s but data_type=%s does not support aggregation", c.Aggregation, c.DataType))
errs = append(errs, fmt.Errorf("aggregation=%s but data_type=%s does not support aggregation", c.Aggregation, c.DataType))
}
if errs != nil && c.MetricName != "" {
errs = multierr.Append(fmt.Errorf("invalid metric config with metric_name '%s'", c.MetricName), errs)
errs = append(errs, fmt.Errorf("invalid metric config with metric_name '%s'", c.MetricName))
}
return errs
return errors.Join(errs...)
}

type MetricType string
Expand Down
8 changes: 4 additions & 4 deletions internal/sqlquery/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sqlquery // import "github.com/open-telemetry/opentelemetry-collector-co

import (
"context"
"errors"

// register Db drivers
_ "github.com/SAP/go-hdb/driver"
Expand All @@ -14,7 +15,6 @@ import (
_ "github.com/microsoft/go-mssqldb/integratedauth/krb5"
_ "github.com/sijms/go-ora/v2"
_ "github.com/snowflakedb/gosnowflake"
"go.uber.org/multierr"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -52,19 +52,19 @@ func (cl DbSQLClient) QueryRows(ctx context.Context, args ...any) ([]StringMap,
return nil, err
}
scanner := newRowScanner(colTypes)
var warnings error
var warnings []error
for sqlRows.Next() {
err = scanner.scan(sqlRows)
if err != nil {
return nil, err
}
sm, scanErr := scanner.toStringMap()
if scanErr != nil {
warnings = multierr.Append(warnings, scanErr)
warnings = append(warnings, scanErr)
}
out = append(out, sm)
}
return out, warnings
return out, errors.Join(warnings...)
}

func (cl DbSQLClient) prepareQueryFields(sql string, args []any) []zap.Field {
Expand Down
14 changes: 9 additions & 5 deletions internal/sqlquery/db_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -90,11 +89,16 @@ func TestDBSQLClient_Nulls_MultiRow(t *testing.T) {
}
rows, err := cl.QueryRows(context.Background())
assert.Error(t, err)
errs := multierr.Errors(err)
for _, err := range errs {
assert.True(t, errors.Is(err, errNullValueWarning))

var e interface{ Unwrap() []error }
if errors.As(err, &e) {
uw := e.Unwrap()
assert.Len(t, uw, 2)

for _, err := range uw {
assert.True(t, errors.Is(err, errNullValueWarning))
}
}
assert.Len(t, errs, 2)
assert.Len(t, rows, 2)
assert.EqualValues(t, map[string]string{
"col_0": "42",
Expand Down
7 changes: 3 additions & 4 deletions internal/sqlquery/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/scrapererror"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -84,17 +83,17 @@ func (s *Scraper) Scrape(ctx context.Context) (pmetric.Metrics, error) {
sms := rm.ScopeMetrics()
sm := sms.AppendEmpty()
ms := sm.Metrics()
var errs error
var errs []error
for _, metricCfg := range s.Query.Metrics {
for i, row := range rows {
if err = rowToMetric(row, metricCfg, ms.AppendEmpty(), s.StartTime, ts, s.ScrapeCfg); err != nil {
err = fmt.Errorf("row %d: %w", i, err)
errs = multierr.Append(errs, err)
errs = append(errs, err)
}
}
}
if errs != nil {
return out, scrapererror.NewPartialScrapeError(errs, len(multierr.Errors(errs)))
return out, scrapererror.NewPartialScrapeError(errors.Join(errs...), len(errs))
}
return out, nil
}
Expand Down