You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-08-10 22:31:50 +02:00
semconv: add ErrorType attribute function (#6962)
Closes #6904 This PR adds a helper method to semconv package to set ErrorType attribute easily. --------- Co-authored-by: Damien Mathieu <42@dmathieu.com> Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
@@ -39,6 +39,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
- `RPCConnectRPCResponseMetadata`
|
- `RPCConnectRPCResponseMetadata`
|
||||||
- `RPCGRPCRequestMetadata`
|
- `RPCGRPCRequestMetadata`
|
||||||
- `RPCGRPCResponseMetadata`
|
- `RPCGRPCResponseMetadata`
|
||||||
|
- Add `ErrorType` attribute helper function to the `go.opentelmetry.io/otel/semconv/v1.34.0` package. (#6962)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
31
internal/tools/semconvkit/templates/error_type.go.tmpl
Normal file
31
internal/tools/semconvkit/templates/error_type.go.tmpl
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// Copyright The OpenTelemetry Authors
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package semconv // import "go.opentelemetry.io/otel/semconv/{{.TagVer}}"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrorType returns an [attribute.KeyValue] identifying the error type of err.
|
||||||
|
func ErrorType(err error) attribute.KeyValue {
|
||||||
|
if err == nil {
|
||||||
|
return ErrorTypeOther
|
||||||
|
}
|
||||||
|
t := reflect.TypeOf(err)
|
||||||
|
var value string
|
||||||
|
if t.PkgPath() == "" && t.Name() == "" {
|
||||||
|
// Likely a builtin type.
|
||||||
|
value = t.String()
|
||||||
|
} else {
|
||||||
|
value = fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
if value == "" {
|
||||||
|
return ErrorTypeOther
|
||||||
|
}
|
||||||
|
return ErrorTypeKey.String(value)
|
||||||
|
}
|
59
internal/tools/semconvkit/templates/error_type_test.go.tmpl
Normal file
59
internal/tools/semconvkit/templates/error_type_test.go.tmpl
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
// Copyright The OpenTelemetry Authors
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package semconv // import "go.opentelemetry.io/otel/semconv/{{.TagVer}}"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CustomError struct{}
|
||||||
|
|
||||||
|
func (CustomError) Error() string {
|
||||||
|
return "custom error"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorType(t *testing.T) {
|
||||||
|
customErr := CustomError{}
|
||||||
|
builtinErr := errors.New("something went wrong")
|
||||||
|
var nilErr error
|
||||||
|
|
||||||
|
wantCustomType := reflect.TypeOf(customErr)
|
||||||
|
wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name())
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
want attribute.KeyValue
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "BuiltinError",
|
||||||
|
err: builtinErr,
|
||||||
|
want: attribute.String("error.type", "*errors.errorString"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CustomError",
|
||||||
|
err: customErr,
|
||||||
|
want: attribute.String("error.type", wantCustomStr),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "NilError",
|
||||||
|
err: nilErr,
|
||||||
|
want: ErrorTypeOther,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := ErrorType(tt.err)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("ErrorType(%v) = %v, want %v", tt.err, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
31
semconv/v1.34.0/error_type.go
Normal file
31
semconv/v1.34.0/error_type.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// Copyright The OpenTelemetry Authors
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package semconv // import "go.opentelemetry.io/otel/semconv/v1.34.0"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrorType returns an [attribute.KeyValue] identifying the error type of err.
|
||||||
|
func ErrorType(err error) attribute.KeyValue {
|
||||||
|
if err == nil {
|
||||||
|
return ErrorTypeOther
|
||||||
|
}
|
||||||
|
t := reflect.TypeOf(err)
|
||||||
|
var value string
|
||||||
|
if t.PkgPath() == "" && t.Name() == "" {
|
||||||
|
// Likely a builtin type.
|
||||||
|
value = t.String()
|
||||||
|
} else {
|
||||||
|
value = fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
if value == "" {
|
||||||
|
return ErrorTypeOther
|
||||||
|
}
|
||||||
|
return ErrorTypeKey.String(value)
|
||||||
|
}
|
59
semconv/v1.34.0/error_type_test.go
Normal file
59
semconv/v1.34.0/error_type_test.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
// Copyright The OpenTelemetry Authors
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package semconv // import "go.opentelemetry.io/otel/semconv/v1.34.0"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CustomError struct{}
|
||||||
|
|
||||||
|
func (CustomError) Error() string {
|
||||||
|
return "custom error"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorType(t *testing.T) {
|
||||||
|
customErr := CustomError{}
|
||||||
|
builtinErr := errors.New("something went wrong")
|
||||||
|
var nilErr error
|
||||||
|
|
||||||
|
wantCustomType := reflect.TypeOf(customErr)
|
||||||
|
wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name())
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
want attribute.KeyValue
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "BuiltinError",
|
||||||
|
err: builtinErr,
|
||||||
|
want: attribute.String("error.type", "*errors.errorString"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CustomError",
|
||||||
|
err: customErr,
|
||||||
|
want: attribute.String("error.type", wantCustomStr),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "NilError",
|
||||||
|
err: nilErr,
|
||||||
|
want: ErrorTypeOther,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := ErrorType(tt.err)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("ErrorType(%v) = %v, want %v", tt.err, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user