Skip to content

Commit

Permalink
[chore] remove multierr use in mdatagen (#10080)
Browse files Browse the repository at this point in the history
Use errors.Join instead.

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
codeboten committed May 3, 2024
1 parent aeb0cf0 commit 8e7be1c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
go.opentelemetry.io/otel/metric v1.26.0
go.opentelemetry.io/otel/trace v1.26.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/text v0.14.0
)
Expand Down Expand Up @@ -48,6 +47,7 @@ require (
go.opentelemetry.io/otel/exporters/prometheus v0.48.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
Expand Down
46 changes: 22 additions & 24 deletions cmd/mdatagen/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ import (
"fmt"
"regexp"

"go.uber.org/multierr"

"go.opentelemetry.io/collector/pdata/pcommon"
)

func (md *metadata) Validate() error {
var errs error
if err := md.validateType(); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
if err := md.validateStatus(); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
if err := md.validateResourceAttributes(); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
if err := md.validateMetrics(); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
return errs
}
Expand Down Expand Up @@ -64,11 +62,11 @@ func (md *metadata) validateStatus() error {
return errors.New("missing status")
}
if err := md.Status.validateClass(); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
if md.Parent == "" {
if err := md.Status.validateStability(); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
}
return errs
Expand All @@ -91,7 +89,7 @@ func (s *Status) validateStability() error {
}
for stability, component := range s.Stability {
if len(component) == 0 {
errs = multierr.Append(errs, fmt.Errorf("missing component for stability: %v", stability))
errs = errors.Join(errs, fmt.Errorf("missing component for stability: %v", stability))
}
for _, c := range component {
if c != "metrics" &&
Expand All @@ -107,7 +105,7 @@ func (s *Status) validateStability() error {
c != "logs_to_metrics" &&
c != "logs_to_logs" &&
c != "extension" {
errs = multierr.Append(errs, fmt.Errorf("invalid component: %v", c))
errs = errors.Join(errs, fmt.Errorf("invalid component: %v", c))
}
}
}
Expand All @@ -118,11 +116,11 @@ func (md *metadata) validateResourceAttributes() error {
var errs error
for name, attr := range md.ResourceAttributes {
if attr.Description == "" {
errs = multierr.Append(errs, fmt.Errorf("empty description for resource attribute: %v", name))
errs = errors.Join(errs, fmt.Errorf("empty description for resource attribute: %v", name))
}
empty := ValueType{ValueType: pcommon.ValueTypeEmpty}
if attr.Type == empty {
errs = multierr.Append(errs, fmt.Errorf("empty type for resource attribute: %v", name))
errs = errors.Join(errs, fmt.Errorf("empty type for resource attribute: %v", name))
}
}
return errs
Expand All @@ -133,17 +131,17 @@ func (md *metadata) validateMetrics() error {
usedAttrs := map[attributeName]bool{}
for mn, m := range md.Metrics {
if m.Sum == nil && m.Gauge == nil {
errs = multierr.Append(errs, fmt.Errorf("metric %v doesn't have a metric type key, "+
errs = errors.Join(errs, fmt.Errorf("metric %v doesn't have a metric type key, "+
"one of the following has to be specified: sum, gauge", mn))
continue
}
if m.Sum != nil && m.Gauge != nil {
errs = multierr.Append(errs, fmt.Errorf("metric %v has more than one metric type keys, "+
errs = errors.Join(errs, fmt.Errorf("metric %v has more than one metric type keys, "+
"only one of the following has to be specified: sum, gauge", mn))
continue
}
if err := m.validate(); err != nil {
errs = multierr.Append(errs, fmt.Errorf(`metric "%v": %w`, mn, err))
errs = errors.Join(errs, fmt.Errorf(`metric "%v": %w`, mn, err))
continue
}
unknownAttrs := make([]attributeName, 0, len(m.Attributes))
Expand All @@ -155,26 +153,26 @@ func (md *metadata) validateMetrics() error {
}
}
if len(unknownAttrs) > 0 {
errs = multierr.Append(errs, fmt.Errorf(`metric "%v" refers to undefined attributes: %v`, mn, unknownAttrs))
errs = errors.Join(errs, fmt.Errorf(`metric "%v" refers to undefined attributes: %v`, mn, unknownAttrs))
}
}
errs = multierr.Append(errs, md.validateAttributes(usedAttrs))
errs = errors.Join(errs, md.validateAttributes(usedAttrs))
return errs
}

func (m *metric) validate() error {
var errs error
if m.Description == "" {
errs = multierr.Append(errs, errors.New(`missing metric description`))
errs = errors.Join(errs, errors.New(`missing metric description`))
}
if m.Unit == nil {
errs = multierr.Append(errs, errors.New(`missing metric unit`))
errs = errors.Join(errs, errors.New(`missing metric unit`))
}
if m.Sum != nil {
errs = multierr.Append(errs, m.Sum.Validate())
errs = errors.Join(errs, m.Sum.Validate())
}
if m.Gauge != nil {
errs = multierr.Append(errs, m.Gauge.Validate())
errs = errors.Join(errs, m.Gauge.Validate())
}
return errs
}
Expand All @@ -191,18 +189,18 @@ func (md *metadata) validateAttributes(usedAttrs map[attributeName]bool) error {
unusedAttrs := make([]attributeName, 0, len(md.Attributes))
for attrName, attr := range md.Attributes {
if attr.Description == "" {
errs = multierr.Append(errs, fmt.Errorf(`missing attribute description for: %v`, attrName))
errs = errors.Join(errs, fmt.Errorf(`missing attribute description for: %v`, attrName))
}
empty := ValueType{ValueType: pcommon.ValueTypeEmpty}
if attr.Type == empty {
errs = multierr.Append(errs, fmt.Errorf("empty type for attribute: %v", attrName))
errs = errors.Join(errs, fmt.Errorf("empty type for attribute: %v", attrName))
}
if !usedAttrs[attrName] {
unusedAttrs = append(unusedAttrs, attrName)
}
}
if len(unusedAttrs) > 0 {
errs = multierr.Append(errs, fmt.Errorf("unused attributes: %v", unusedAttrs))
errs = errors.Join(errs, fmt.Errorf("unused attributes: %v", unusedAttrs))
}
return errs
}

0 comments on commit 8e7be1c

Please sign in to comment.