From f03cb994f7b4cf36847d26eda31b082a5650e692 Mon Sep 17 00:00:00 2001 From: Patrik Date: Mon, 16 Sep 2024 19:45:39 +0200 Subject: [PATCH] feat(otelx): add API key attribute key (#810) --- otelx/attribute.go | 2 +- otelx/semconv/events.go | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/otelx/attribute.go b/otelx/attribute.go index 81db5b46..7f76687a 100644 --- a/otelx/attribute.go +++ b/otelx/attribute.go @@ -13,7 +13,7 @@ import ( const nullString = "" 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)) } diff --git a/otelx/semconv/events.go b/otelx/semconv/events.go index 0663e3f9..80f04641 100644 --- a/otelx/semconv/events.go +++ b/otelx/semconv/events.go @@ -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") +}