From 0156c137a9242729e292b76cc9c867840cef0902 Mon Sep 17 00:00:00 2001 From: zepatrik Date: Mon, 16 Sep 2024 17:44:24 +0200 Subject: [PATCH] feat(otelx): add API key attribute keys --- otelx/attribute.go | 2 +- otelx/semconv/events.go | 24 ++++++++++++++++++++++-- 2 files changed, 23 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..fa6e3a97 100644 --- a/otelx/semconv/events.go +++ b/otelx/semconv/events.go @@ -33,10 +33,12 @@ const ( AttributeKeyWorkspace AttributeKey = "WorkspaceID" AttributeKeySubscriptionID AttributeKey = "SubscriptionID" AttributeKeyProjectEnvironment AttributeKey = "ProjectEnvironment" + AttributeProjectKeyAPIKeyID AttributeKey = "ProjectAPIKeyID" + AttributeWorkspaceKeyAPIKeyID AttributeKey = "WorkspaceAPIKeyID" ) -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 +76,21 @@ func AttrGeoLocation(val httpx.GeoLocation) []otelattr.KeyValue { return geoLocationAttributes } + +func AttrProjectAPIKey[V string | uuid.UUID](val V) otelattr.KeyValue { + return otelattr.String(AttributeProjectKeyAPIKeyID.String(), uuidOrString(val)) +} + +func AttrWorkspaceAPIKey[V string | uuid.UUID](val V) otelattr.KeyValue { + return otelattr.String(AttributeWorkspaceKeyAPIKeyID.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") +}