diff --git a/internal/sqlquery/config.go b/internal/sqlquery/config.go index d45fbf4a9960..ceb2b4556cfd 100644 --- a/internal/sqlquery/config.go +++ b/internal/sqlquery/config.go @@ -9,7 +9,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/receiver/scraperhelper" - "go.uber.org/multierr" ) type Config struct { @@ -48,24 +47,24 @@ 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 { @@ -73,11 +72,11 @@ type LogsCfg struct { } 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 { @@ -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 diff --git a/internal/sqlquery/db_client.go b/internal/sqlquery/db_client.go index 9f7d00908f0f..e702a6b82923 100644 --- a/internal/sqlquery/db_client.go +++ b/internal/sqlquery/db_client.go @@ -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" @@ -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" ) @@ -52,7 +52,7 @@ 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 { @@ -60,11 +60,11 @@ func (cl DbSQLClient) QueryRows(ctx context.Context, args ...any) ([]StringMap, } 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 { diff --git a/internal/sqlquery/db_client_test.go b/internal/sqlquery/db_client_test.go index a40ebd02e31f..4546886b0ec0 100644 --- a/internal/sqlquery/db_client_test.go +++ b/internal/sqlquery/db_client_test.go @@ -11,7 +11,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.uber.org/multierr" "go.uber.org/zap" ) @@ -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", diff --git a/internal/sqlquery/scraper.go b/internal/sqlquery/scraper.go index e43edf4aea34..84be8370911e 100644 --- a/internal/sqlquery/scraper.go +++ b/internal/sqlquery/scraper.go @@ -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" ) @@ -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 }