diff --git a/quotientai/async_client.py b/quotientai/async_client.py index f0b6556..7e0b95f 100644 --- a/quotientai/async_client.py +++ b/quotientai/async_client.py @@ -30,6 +30,7 @@ def __init__(self, api_key: str): self.token = None self.token_expiry = 0 self.token_api_key = None + self._user = None self._token_path = ( token_dir / ".quotient" diff --git a/quotientai/client.py b/quotientai/client.py index 5a354eb..0248044 100644 --- a/quotientai/client.py +++ b/quotientai/client.py @@ -32,6 +32,7 @@ def __init__(self, api_key: str): self.token = None self.token_expiry = 0 self.token_api_key = None + self._user = None self._token_path = ( token_dir / ".quotient" diff --git a/quotientai/resources/auth.py b/quotientai/resources/auth.py index 6e459e8..3b94a19 100644 --- a/quotientai/resources/auth.py +++ b/quotientai/resources/auth.py @@ -8,6 +8,11 @@ def authenticate(self): A call to GET /auth/profile to initially authenticate the user. """ response = self._client._get("/auth/profile") + + # Set the user_id if successful + if response and isinstance(response, dict) and 'user_id' in response: + self._client._user = response['user_id'] + return response class AsyncAuthResource: @@ -25,6 +30,11 @@ def authenticate(self): task = loop.create_task(self._client._get("/auth/profile")) # Run the task to completion result = loop.run_until_complete(task) + + # Set the user_id if successful + if result and isinstance(result, dict) and 'user_id' in result: + self._client._user = result['user_id'] + return result finally: loop.close() diff --git a/quotientai/tracing/core.py b/quotientai/tracing/core.py index cc53e57..298a903 100644 --- a/quotientai/tracing/core.py +++ b/quotientai/tracing/core.py @@ -42,6 +42,7 @@ class QuotientAttributes(str, Enum): app_name = "app.name" environment = "app.environment" detections = "quotient.detections" + user = "quotient.user" class TracingResource: @@ -123,6 +124,17 @@ def _create_otlp_exporter(self, endpoint: str, headers: dict): """ return OTLPSpanExporter(endpoint=endpoint, headers=headers) + + def _get_user(self): + """ + Get user_id from client. + Returns the user_id or None if not found. + """ + if hasattr(self._client, '_user'): + return self._client._user + return "None" + + @functools.lru_cache() def _setup_auto_collector(self, app_name: str, environment: str, instruments: Optional[tuple] = None, detections: Optional[str] = None): """ @@ -139,6 +151,7 @@ def _setup_auto_collector(self, app_name: str, environment: str, instruments: Op resource_attributes = { QuotientAttributes.app_name: app_name, QuotientAttributes.environment: environment, + QuotientAttributes.user: self._get_user(), } if detections is not None: @@ -154,7 +167,7 @@ def _setup_auto_collector(self, app_name: str, environment: str, instruments: Op "OTEL_EXPORTER_OTLP_ENDPOINT", DEFAULT_TRACING_ENDPOINT, ) - + # Parse headers from environment or use default headers = { "Authorization": f"Bearer {self._client.api_key}",