mirror of
https://git.mirrors.martin98.com/https://github.com/open-webui/open-webui
synced 2025-08-18 07:36:00 +08:00
Add user details to otel span
This commit is contained in:
parent
4ce1e88750
commit
df853246f3
@ -13,6 +13,8 @@ import pytz
|
|||||||
from pytz import UTC
|
from pytz import UTC
|
||||||
from typing import Optional, Union, List, Dict
|
from typing import Optional, Union, List, Dict
|
||||||
|
|
||||||
|
from opentelemetry import trace
|
||||||
|
|
||||||
from open_webui.models.users import Users
|
from open_webui.models.users import Users
|
||||||
|
|
||||||
from open_webui.constants import ERROR_MESSAGES
|
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
|
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
|
# auth by jwt token
|
||||||
try:
|
try:
|
||||||
@ -213,6 +225,14 @@ def get_current_user(
|
|||||||
detail=ERROR_MESSAGES.INVALID_TOKEN,
|
detail=ERROR_MESSAGES.INVALID_TOKEN,
|
||||||
)
|
)
|
||||||
else:
|
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
|
# Refresh the user's last active timestamp asynchronously
|
||||||
# to prevent blocking the request
|
# to prevent blocking the request
|
||||||
if background_tasks:
|
if background_tasks:
|
||||||
@ -234,6 +254,14 @@ def get_current_user_by_api_key(api_key: str):
|
|||||||
detail=ERROR_MESSAGES.INVALID_TOKEN,
|
detail=ERROR_MESSAGES.INVALID_TOKEN,
|
||||||
)
|
)
|
||||||
else:
|
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)
|
Users.update_user_last_active_by_id(user.id)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
Loading…
x
Reference in New Issue
Block a user