Skip to content

Commit

Permalink
chore: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Sep 12, 2024
1 parent 06974cc commit 15b0ff4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions otelx/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"go.opentelemetry.io/otel/attribute"
)

const nullString = "<null>"

func StringAttrs(attrs map[string]string) []attribute.KeyValue {
s := []attribute.KeyValue{}
for k, v := range attrs {
Expand All @@ -27,16 +29,17 @@ func Nullable[V any, VN *V | sql.Null[V], A func(string, V) attribute.KeyValue](
switch v := any(v).(type) {
case *V:
if v == nil {
return attribute.String(k, "<nil>")
return attribute.String(k, nullString)
}
return a(k, *v)
case sql.Null[V]:
if !v.Valid {
return attribute.String(k, "<nil>")
return attribute.String(k, nullString)
}
return a(k, v.V)
}
return attribute.String(k, "unsupported type")
// This should never happen, as the type switch above is exhaustive to the generic type VN.
return attribute.String(k, fmt.Sprintf("<got unsupported type %T>", v))
}

func NullString[V *string | sql.Null[string]](k string, v V) attribute.KeyValue {
Expand All @@ -45,7 +48,7 @@ func NullString[V *string | sql.Null[string]](k string, v V) attribute.KeyValue

func NullStringer(k string, v fmt.Stringer) attribute.KeyValue {
if v == nil {
return attribute.String(k, "<nil>")
return attribute.String(k, nullString)
}
return attribute.String(k, v.String())
}
Expand Down

0 comments on commit 15b0ff4

Please sign in to comment.