| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/otlpmetric/transform/error.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package transform // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/transform" |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"strings" |
| 13 |
|
| 14 |
mpb "go.opentelemetry.io/proto/otlp/metrics/v1" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
errUnknownAggregation = errors.New("unknown aggregation") |
| 19 |
errUnknownTemporality = errors.New("unknown temporality") |
| 20 |
) |
| 21 |
|
| 22 |
type errMetric struct { |
| 23 |
m *mpb.Metric |
| 24 |
err error |
| 25 |
} |
| 26 |
|
| 27 |
func (e errMetric) Unwrap() error { |
| 28 |
return e.err |
| 29 |
} |
| 30 |
|
| 31 |
func (e errMetric) Error() string { |
| 32 |
format := "invalid metric (name: %q, description: %q, unit: %q): %s" |
| 33 |
return fmt.Sprintf(format, e.m.Name, e.m.Description, e.m.Unit, e.err) |
| 34 |
} |
| 35 |
|
| 36 |
func (e errMetric) Is(target error) bool { |
| 37 |
return errors.Is(e.err, target) |
| 38 |
} |
| 39 |
|
| 40 |
// multiErr is used by the data-type transform functions to wrap multiple |
| 41 |
// errors into a single return value. The error message will show all errors |
| 42 |
// as a list and scope them by the datatype name that is returning them. |
| 43 |
type multiErr struct { |
| 44 |
datatype string |
| 45 |
errs []error |
| 46 |
} |
| 47 |
|
| 48 |
// errOrNil returns nil if e contains no errors, otherwise it returns e. |
| 49 |
func (e *multiErr) errOrNil() error { |
| 50 |
if len(e.errs) == 0 { |
| 51 |
return nil |
| 52 |
} |
| 53 |
return e |
| 54 |
} |
| 55 |
|
| 56 |
// append adds err to e. If err is a multiErr, its errs are flattened into e. |
| 57 |
func (e *multiErr) append(err error) { |
| 58 |
// Do not use errors.As here, this should only be flattened one layer. If |
| 59 |
// there is a *multiErr several steps down the chain, all the errors above |
| 60 |
// it will be discarded if errors.As is used instead. |
| 61 |
switch other := err.(type) { //nolint:errorlint |
| 62 |
case *multiErr: |
| 63 |
// Flatten err errors into e. |
| 64 |
e.errs = append(e.errs, other.errs...) |
| 65 |
default: |
| 66 |
e.errs = append(e.errs, err) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func (e *multiErr) Error() string { |
| 71 |
es := make([]string, len(e.errs)) |
| 72 |
for i, err := range e.errs { |
| 73 |
es[i] = fmt.Sprintf("* %s", err) |
| 74 |
} |
| 75 |
|
| 76 |
format := "%d errors occurred transforming %s:\n\t%s" |
| 77 |
return fmt.Sprintf(format, len(es), e.datatype, strings.Join(es, "\n\t")) |
| 78 |
} |
| 79 |
|
| 80 |
func (e *multiErr) Unwrap() error { |
| 81 |
switch len(e.errs) { |
| 82 |
case 0: |
| 83 |
return nil |
| 84 |
case 1: |
| 85 |
return e.errs[0] |
| 86 |
} |
| 87 |
|
| 88 |
// Return a multiErr without the leading error. |
| 89 |
cp := &multiErr{ |
| 90 |
datatype: e.datatype, |
| 91 |
errs: make([]error, len(e.errs)-1), |
| 92 |
} |
| 93 |
copy(cp.errs, e.errs[1:]) |
| 94 |
return cp |
| 95 |
} |
| 96 |
|
| 97 |
func (e *multiErr) Is(target error) bool { |
| 98 |
if len(e.errs) == 0 { |
| 99 |
return false |
| 100 |
} |
| 101 |
// Check if the first error is target. |
| 102 |
return errors.Is(e.errs[0], target) |
| 103 |
} |