From df853246f3301030af929265c59ff0476393d0bf Mon Sep 17 00:00:00 2001 From: Jarrod Lowe Date: Sat, 17 May 2025 09:11:26 +1200 Subject: [PATCH] Add user details to otel span --- backend/open_webui/utils/auth.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/utils/auth.py b/backend/open_webui/utils/auth.py index 118ac049e..2db0da7e5 100644 --- a/backend/open_webui/utils/auth.py +++ b/backend/open_webui/utils/auth.py @@ -13,6 +13,8 @@ import pytz from pytz import UTC from typing import Optional, Union, List, Dict +from opentelemetry import trace + from open_webui.models.users import Users from open_webui.constants import ERROR_MESSAGES @@ -194,7 +196,17 @@ def get_current_user( status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED ) - return get_current_user_by_api_key(token) + user = get_current_user_by_api_key(token) + + # Add user info to current span + current_span = trace.get_current_span() + if current_span: + current_span.set_attribute("client.user.id", user.id) + current_span.set_attribute("client.user.email", user.email) + current_span.set_attribute("client.user.role", user.role) + current_span.set_attribute("client.auth.type", "api_key") + + return user # auth by jwt token try: @@ -213,6 +225,14 @@ def get_current_user( detail=ERROR_MESSAGES.INVALID_TOKEN, ) else: + # Add user info to current span + current_span = trace.get_current_span() + if current_span: + current_span.set_attribute("client.user.id", user.id) + current_span.set_attribute("client.user.email", user.email) + current_span.set_attribute("client.user.role", user.role) + current_span.set_attribute("client.auth.type", "jwt") + # Refresh the user's last active timestamp asynchronously # to prevent blocking the request if background_tasks: @@ -234,6 +254,14 @@ def get_current_user_by_api_key(api_key: str): detail=ERROR_MESSAGES.INVALID_TOKEN, ) else: + # Add user info to current span + current_span = trace.get_current_span() + if current_span: + current_span.set_attribute("client.user.id", user.id) + current_span.set_attribute("client.user.email", user.email) + current_span.set_attribute("client.user.role", user.role) + current_span.set_attribute("client.auth.type", "api_key") + Users.update_user_last_active_by_id(user.id) return user