Skip to content

Commit 80c646c

Browse files
committed
feat: add ErrorType for semconv package 1.34.0 (#6904)
1 parent 78f873f commit 80c646c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package semconv // import "go.opentelemetry.io/otel/semconv/{{.TagVer}}"
5+
6+
7+
// ErrorType returns an attribute KeyValue that identifies the type of the given error.
8+
// It uses reflection to determine the fully qualified type name for observability and monitoring.
9+
func ErrorType(err error) attribute.KeyValue {
10+
t := reflect.TypeOf(err)
11+
var value string
12+
if t.PkgPath() == "" && t.Name() == "" {
13+
// Likely a builtin type.
14+
value = t.String()
15+
} else {
16+
value = fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
17+
}
18+
19+
if value == "" {
20+
return ErrorTypeOther
21+
}
22+
return ErrorTypeKey.String(value)
23+
}

semconv/v1.34.0/errorType.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package semconv // import "go.opentelemetry.io/otel/semconv/v1.34.0"
5+
6+
import (
7+
"fmt"
8+
"reflect"
9+
10+
"go.opentelemetry.io/otel/attribute"
11+
)
12+
13+
// ErrorType returns an attribute KeyValue that identifies the type of the given error.
14+
// It uses reflection to determine the fully qualified type name for observability and monitoring.
15+
func ErrorType(err error) attribute.KeyValue {
16+
t := reflect.TypeOf(err)
17+
var value string
18+
if t.PkgPath() == "" && t.Name() == "" {
19+
// Likely a builtin type.
20+
value = t.String()
21+
} else {
22+
value = fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
23+
}
24+
25+
if value == "" {
26+
return ErrorTypeOther
27+
}
28+
return ErrorTypeKey.String(value)
29+
}

0 commit comments

Comments
 (0)