1
0
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:
Bəxtiyar
2025-07-10 10:45:44 +03:00
committed by GitHub
parent 73b2cd39b0
commit 687c3f5fb5
5 changed files with 181 additions and 0 deletions

View File

@@ -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

View 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)
}

View 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)
}
})
}
}

View 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)
}

View 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)
}
})
}
}