Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(otelx): nullable attribute helpers #809

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion otelx/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

package otelx

import "go.opentelemetry.io/otel/attribute"
import (
"database/sql"
"fmt"

"go.opentelemetry.io/otel/attribute"
)

const nullString = "<null>"

func StringAttrs(attrs map[string]string) []attribute.KeyValue {
s := []attribute.KeyValue{}
Expand All @@ -12,3 +19,40 @@ func StringAttrs(attrs map[string]string) []attribute.KeyValue {
}
return s
}

func AutoInt[I int | int32 | int64](k string, v I) attribute.KeyValue {
// Internally, the OpenTelemetry SDK uses int64 for all integer values anyway.
return attribute.Int64(k, int64(v))
}

func Nullable[V any, VN *V | sql.Null[V], A func(string, V) attribute.KeyValue](a A, k string, v VN) attribute.KeyValue {
switch v := any(v).(type) {
case *V:
if v == nil {
return attribute.String(k, nullString)
}
return a(k, *v)
case sql.Null[V]:
if !v.Valid {
return attribute.String(k, nullString)
}
return a(k, v.V)
}
// 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 {
return Nullable(attribute.String, k, v)
}

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

func NullInt[I int | int32 | int64, V *I | sql.Null[I]](k string, v V) attribute.KeyValue {
return Nullable[I](AutoInt, k, v)
}
Loading