Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ory/x
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 13f1993b6ccecc12c8776c80ba0367cee9168428
Choose a base ref
..
head repository: ory/x
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f949dbf9dc5e0ac81d820c1fca25b7a03ea95a38
Choose a head ref
Showing with 66 additions and 7 deletions.
  1. +1 −1 go.mod
  2. +2 −2 go.sum
  3. +46 −2 otelx/attribute.go
  4. +17 −2 otelx/semconv/events.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -189,7 +189,7 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/runc v1.1.13 // indirect
github.com/opencontainers/runc v1.1.14 // indirect
github.com/openzipkin/zipkin-go v0.4.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -635,8 +635,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/opencontainers/runc v1.1.13 h1:98S2srgG9vw0zWcDpFMn5TRrh8kLxa/5OFUstuUhmRs=
github.com/opencontainers/runc v1.1.13/go.mod h1:R016aXacfp/gwQBYw2FDGa9m+n6atbLWrYY8hNMT/sA=
github.com/opencontainers/runc v1.1.14 h1:rgSuzbmgz5DUJjeSnw337TxDbRuqjs6iqQck/2weR6w=
github.com/opencontainers/runc v1.1.14/go.mod h1:E4C2z+7BxR7GHXp0hAY53mek+x49X1LjPNeMTfRGvOA=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/openzipkin/zipkin-go v0.4.2 h1:zjqfqHjUpPmB3c1GlCvvgsM1G4LkvqQbBDueDOCg/jA=
github.com/openzipkin/zipkin-go v0.4.2/go.mod h1:ZeVkFjuuBiSy13y8vpSDCjMi9GoI3hPpCJSBx/EYFhY=
48 changes: 46 additions & 2 deletions otelx/attribute.go
Original file line number Diff line number Diff line change
@@ -3,12 +3,56 @@

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{}
s := make([]attribute.KeyValue, 0, len(attrs))
for k, v := range attrs {
s = append(s, attribute.String(k, v))
}
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)
}
19 changes: 17 additions & 2 deletions otelx/semconv/events.go
Original file line number Diff line number Diff line change
@@ -33,10 +33,11 @@ const (
AttributeKeyWorkspace AttributeKey = "WorkspaceID"
AttributeKeySubscriptionID AttributeKey = "SubscriptionID"
AttributeKeyProjectEnvironment AttributeKey = "ProjectEnvironment"
AttributeKeyAPIKeyID AttributeKey = "APIKeyID"
)

func AttrIdentityID(val uuid.UUID) otelattr.KeyValue {
return otelattr.String(AttributeKeyIdentityID.String(), val.String())
func AttrIdentityID[V string | uuid.UUID](val V) otelattr.KeyValue {
return otelattr.String(AttributeKeyIdentityID.String(), uuidOrString(val))
}

func AttrNID(val uuid.UUID) otelattr.KeyValue {
@@ -74,3 +75,17 @@ func AttrGeoLocation(val httpx.GeoLocation) []otelattr.KeyValue {

return geoLocationAttributes
}

func AttrAPIKeyID[V string | uuid.UUID](val V) otelattr.KeyValue {
return otelattr.String(AttributeKeyAPIKeyID.String(), uuidOrString(val))
}

func uuidOrString[V string | uuid.UUID](val V) string {
switch val := any(val).(type) {
case string:
return val
case uuid.UUID:
return val.String()
}
panic("unreachable")
}