| 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] identifying the error type of err. |
| 14 |
func ErrorType(err error) attribute.KeyValue { |
| 15 |
if err == nil { |
| 16 |
return ErrorTypeOther |
| 17 |
} |
| 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()) |
| 25 |
} |
| 26 |
|
| 27 |
if value == "" { |
| 28 |
return ErrorTypeOther |
| 29 |
} |
| 30 |
return ErrorTypeKey.String(value) |
| 31 |
} |