Skip to content

Commit c4bdd87

Browse files
MrAliaspellared
andauthored
Support custom error type semantics (#7442)
Allow instrumentation to provide domain-specific error type values (i.e. HTTP or gRPC status codes) for the "error.type" semantic attribute. This is accomplished by passing an error value that implements the `interface{ ErrorType string }` interface. --------- Co-authored-by: Robert Pająk <[email protected]>
1 parent 931a5bd commit c4bdd87

File tree

5 files changed

+137
-108
lines changed

5 files changed

+137
-108
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4242
- `WithInstrumentationAttributes` in `go.opentelemetry.io/otel/log` synchronously de-duplicates the passed attributes instead of delegating it to the returned `LoggerOption`. (#7266)
4343
- `Distinct` in `go.opentelemetry.io/otel/attribute` is no longer guaranteed to uniquely identify an attribute set. Collisions between `Distinct` values for different Sets are possible with extremely high cardinality (billions of series per instrument), but are highly unlikely. (#7175)
4444
- The default `TranslationStrategy` in `go.opentelemetry.io/exporters/prometheus` is changed from `otlptranslator.NoUTF8EscapingWithSuffixes` to `otlptranslator.UnderscoreEscapingWithSuffixes`. (#7421)
45+
- The `ErrorType` function in `go.opentelemetry.io/otel/semconv/v1.37.0` now handles custom error types.
46+
If an error implements an `ErrorType() string` method, the return value of that method will be used as the error type. (#7442)
4547

4648
<!-- Released section -->
4749
<!-- Don't change this section unless doing release -->

internal/tools/semconvkit/templates/error_type.go.tmpl

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,54 @@
44
package semconv // import "go.opentelemetry.io/otel/semconv/{{.TagVer}}"
55

66
import (
7-
"fmt"
87
"reflect"
98

109
"go.opentelemetry.io/otel/attribute"
1110
)
1211

1312
// ErrorType returns an [attribute.KeyValue] identifying the error type of err.
13+
//
14+
// If err is nil, the returned attribute has the default value
15+
// [ErrorTypeOther].
16+
//
17+
// If err's type has the method
18+
//
19+
// ErrorType() string
20+
//
21+
//
22+
// then the returned attribute has the value of err.ErrorType(). Otherwise, the
23+
// returned attribute has a value derived from the concrete type of err.
24+
//
25+
// The key of the returned attribute is [ErrorTypeKey].
1426
func ErrorType(err error) attribute.KeyValue {
1527
if err == nil {
1628
return ErrorTypeOther
1729
}
18-
t := reflect.TypeOf(err)
19-
var value string
20-
if t.PkgPath() == "" && t.Name() == "" {
21-
// Likely a builtin type.
22-
value = t.String()
23-
} else {
24-
value = fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
30+
31+
return ErrorTypeKey.String(errorType(err))
32+
}
33+
34+
func errorType(err error) string {
35+
var s string
36+
if et, ok := err.(interface{ ErrorType() string }); ok {
37+
// Prioritize the ErrorType method if available.
38+
s = et.ErrorType()
2539
}
40+
if s == "" {
41+
// Fallback to reflection if the ErrorType method is not supported or
42+
// returns an empty value.
2643

27-
if value == "" {
28-
return ErrorTypeOther
44+
t := reflect.TypeOf(err)
45+
pkg, name := t.PkgPath(), t.Name()
46+
if pkg != "" && name != "" {
47+
s = pkg + "." + name
48+
} else {
49+
// The type has no package path or name (predeclared, not-defined,
50+
// or alias for a not-defined type).
51+
//
52+
// This is not guaranteed to be unique, but is a best effort.
53+
s = t.String()
54+
}
2955
}
30-
return ErrorTypeKey.String(value)
56+
return s
3157
}

internal/tools/semconvkit/templates/error_type_test.go.tmpl

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,41 @@ package semconv // import "go.opentelemetry.io/otel/semconv/{{.TagVer}}"
55

66
import (
77
"errors"
8-
"fmt"
9-
"reflect"
108
"testing"
11-
12-
"go.opentelemetry.io/otel/attribute"
139
)
1410

15-
type CustomError struct{}
11+
const pkg = "go.opentelemetry.io/otel/semconv/{{.TagVer}}"
1612

17-
func (CustomError) Error() string {
18-
return "custom error"
13+
func TestErrorType(t *testing.T) {
14+
check(t, nil, ErrorTypeOther.Value.AsString())
15+
check(t, errors.New("msg"), "*errors.errorString")
16+
check(t, custom("aborted"), "aborted")
17+
check(t, custom(""), pkg+".ErrCustomType") // empty ErrorType, use concrete type.
1918
}
2019

21-
func TestErrorType(t *testing.T) {
22-
customErr := CustomError{}
23-
builtinErr := errors.New("something went wrong")
24-
var nilErr error
25-
26-
wantCustomType := reflect.TypeOf(customErr)
27-
wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name())
28-
29-
tests := []struct {
30-
name string
31-
err error
32-
want attribute.KeyValue
33-
}{
34-
{
35-
name: "BuiltinError",
36-
err: builtinErr,
37-
want: attribute.String("error.type", "*errors.errorString"),
38-
},
39-
{
40-
name: "CustomError",
41-
err: customErr,
42-
want: attribute.String("error.type", wantCustomStr),
43-
},
44-
{
45-
name: "NilError",
46-
err: nilErr,
47-
want: ErrorTypeOther,
48-
},
20+
func check(t *testing.T, err error, want string) {
21+
t.Helper()
22+
got := ErrorType(err)
23+
if got.Key != ErrorTypeKey {
24+
t.Errorf("ErrorType(%v) key = %v, want %v", err, got.Key, ErrorTypeKey)
4925
}
50-
51-
for _, tt := range tests {
52-
t.Run(tt.name, func(t *testing.T) {
53-
got := ErrorType(tt.err)
54-
if got != tt.want {
55-
t.Errorf("ErrorType(%v) = %v, want %v", tt.err, got, tt.want)
56-
}
57-
})
26+
if got.Value.AsString() != want {
27+
t.Errorf("ErrorType(%v) value = %v, want %v", err, got.Value.AsString(), want)
5828
}
5929
}
30+
31+
func custom(typ string) error {
32+
return ErrCustomType{Type: typ}
33+
}
34+
35+
type ErrCustomType struct {
36+
Type string
37+
}
38+
39+
func (e ErrCustomType) Error() string {
40+
return "custom: " + e.Type
41+
}
42+
43+
func (e ErrCustomType) ErrorType() string {
44+
return e.Type
45+
}

semconv/v1.37.0/error_type.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,57 @@
44
package semconv // import "go.opentelemetry.io/otel/semconv/v1.37.0"
55

66
import (
7-
"fmt"
87
"reflect"
98

109
"go.opentelemetry.io/otel/attribute"
1110
)
1211

1312
// ErrorType returns an [attribute.KeyValue] identifying the error type of err.
13+
//
14+
// If err is nil, the returned attribute has the default value
15+
// [ErrorTypeOther].
16+
//
17+
// If err implements the interface
18+
//
19+
// // ErrorTyper is an error that provides a specific type definition for the
20+
// // error it represents.
21+
// type ErrorTyper interface {
22+
// ErrorType() string
23+
// }
24+
//
25+
// then the returned attribute has the value of err.ErrorType(). Otherwise, the
26+
// returned attribute has a value derived from the concrete type of err.
27+
//
28+
// The key of the returned attribute is [ErrorTypeKey].
1429
func ErrorType(err error) attribute.KeyValue {
1530
if err == nil {
1631
return ErrorTypeOther
1732
}
18-
t := reflect.TypeOf(err)
19-
var value string
20-
if t.PkgPath() == "" && t.Name() == "" {
21-
// Likely a builtin type.
22-
value = t.String()
23-
} else {
24-
value = fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
33+
34+
return ErrorTypeKey.String(errorType(err))
35+
}
36+
37+
func errorType(err error) string {
38+
var s string
39+
if et, ok := err.(interface{ ErrorType() string }); ok {
40+
// Prioritize the ErrorType method if available.
41+
s = et.ErrorType()
2542
}
43+
if s == "" {
44+
// Fallback to reflection if the ErrorType method is not supported or
45+
// returns an empty value.
2646

27-
if value == "" {
28-
return ErrorTypeOther
47+
t := reflect.TypeOf(err)
48+
pkg, name := t.PkgPath(), t.Name()
49+
if pkg != "" && name != "" {
50+
s = pkg + "." + name
51+
} else {
52+
// The type has no package path or name (predeclared, not-defined,
53+
// or alias for a not-defined type).
54+
//
55+
// This is not guaranteed to be unique, but is a best effort.
56+
s = t.String()
57+
}
2958
}
30-
return ErrorTypeKey.String(value)
59+
return s
3160
}

semconv/v1.37.0/error_type_test.go

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,41 @@ package semconv // import "go.opentelemetry.io/otel/semconv/v1.37.0"
55

66
import (
77
"errors"
8-
"fmt"
9-
"reflect"
108
"testing"
11-
12-
"go.opentelemetry.io/otel/attribute"
139
)
1410

15-
type CustomError struct{}
11+
const pkg = "go.opentelemetry.io/otel/semconv/v1.37.0"
1612

17-
func (CustomError) Error() string {
18-
return "custom error"
13+
func TestErrorType(t *testing.T) {
14+
check(t, nil, ErrorTypeOther.Value.AsString())
15+
check(t, errors.New("msg"), "*errors.errorString")
16+
check(t, custom("aborted"), "aborted")
17+
check(t, custom(""), pkg+".ErrCustomType") // empty ErrorType, use concrete type.
1918
}
2019

21-
func TestErrorType(t *testing.T) {
22-
customErr := CustomError{}
23-
builtinErr := errors.New("something went wrong")
24-
var nilErr error
25-
26-
wantCustomType := reflect.TypeOf(customErr)
27-
wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name())
28-
29-
tests := []struct {
30-
name string
31-
err error
32-
want attribute.KeyValue
33-
}{
34-
{
35-
name: "BuiltinError",
36-
err: builtinErr,
37-
want: attribute.String("error.type", "*errors.errorString"),
38-
},
39-
{
40-
name: "CustomError",
41-
err: customErr,
42-
want: attribute.String("error.type", wantCustomStr),
43-
},
44-
{
45-
name: "NilError",
46-
err: nilErr,
47-
want: ErrorTypeOther,
48-
},
20+
func check(t *testing.T, err error, want string) {
21+
t.Helper()
22+
got := ErrorType(err)
23+
if got.Key != ErrorTypeKey {
24+
t.Errorf("ErrorType(%v) key = %v, want %v", err, got.Key, ErrorTypeKey)
4925
}
50-
51-
for _, tt := range tests {
52-
t.Run(tt.name, func(t *testing.T) {
53-
got := ErrorType(tt.err)
54-
if got != tt.want {
55-
t.Errorf("ErrorType(%v) = %v, want %v", tt.err, got, tt.want)
56-
}
57-
})
26+
if got.Value.AsString() != want {
27+
t.Errorf("ErrorType(%v) value = %v, want %v", err, got.Value.AsString(), want)
5828
}
5929
}
30+
31+
func custom(typ string) error {
32+
return ErrCustomType{Type: typ}
33+
}
34+
35+
type ErrCustomType struct {
36+
Type string
37+
}
38+
39+
func (e ErrCustomType) Error() string {
40+
return "custom: " + e.Type
41+
}
42+
43+
func (e ErrCustomType) ErrorType() string {
44+
return e.Type
45+
}

0 commit comments

Comments
 (0)