Skip to content

Commit bd3a363

Browse files
committed
refac
1 parent 1dcbfd4 commit bd3a363

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

backend/open_webui/utils/auth.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from pytz import UTC
2020
from typing import Optional, Union, List, Dict
2121

22-
from opentelemetry import trace
2322

2423

2524
from open_webui.utils.access_control import has_permission
@@ -30,6 +29,7 @@
3029
from open_webui.constants import ERROR_MESSAGES
3130

3231
from open_webui.env import (
32+
ENABLE_OTEL,
3333
ENABLE_PASSWORD_VALIDATION,
3434
OFFLINE_MODE,
3535
LICENSE_BLOB,
@@ -327,12 +327,15 @@ async def get_current_user(
327327
user = get_current_user_by_api_key(request, token)
328328

329329
# Add user info to current span
330-
current_span = trace.get_current_span()
331-
if current_span:
332-
current_span.set_attribute('client.user.id', user.id)
333-
current_span.set_attribute('client.user.email', user.email)
334-
current_span.set_attribute('client.user.role', user.role)
335-
current_span.set_attribute('client.auth.type', 'api_key')
330+
if ENABLE_OTEL:
331+
from opentelemetry import trace
332+
333+
current_span = trace.get_current_span()
334+
if current_span:
335+
current_span.set_attribute('client.user.id', user.id)
336+
current_span.set_attribute('client.user.email', user.email)
337+
current_span.set_attribute('client.user.role', user.role)
338+
current_span.set_attribute('client.auth.type', 'api_key')
336339

337340
return user
338341

@@ -369,12 +372,15 @@ async def get_current_user(
369372
)
370373

371374
# Add user info to current span
372-
current_span = trace.get_current_span()
373-
if current_span:
374-
current_span.set_attribute('client.user.id', user.id)
375-
current_span.set_attribute('client.user.email', user.email)
376-
current_span.set_attribute('client.user.role', user.role)
377-
current_span.set_attribute('client.auth.type', 'jwt')
375+
if ENABLE_OTEL:
376+
from opentelemetry import trace
377+
378+
current_span = trace.get_current_span()
379+
if current_span:
380+
current_span.set_attribute('client.user.id', user.id)
381+
current_span.set_attribute('client.user.email', user.email)
382+
current_span.set_attribute('client.user.role', user.role)
383+
current_span.set_attribute('client.auth.type', 'jwt')
378384

379385
# Refresh the user's last active timestamp asynchronously
380386
# to prevent blocking the request
@@ -422,12 +428,15 @@ def get_current_user_by_api_key(request, api_key: str):
422428
raise HTTPException(status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED)
423429

424430
# Add user info to current span
425-
current_span = trace.get_current_span()
426-
if current_span:
427-
current_span.set_attribute('client.user.id', user.id)
428-
current_span.set_attribute('client.user.email', user.email)
429-
current_span.set_attribute('client.user.role', user.role)
430-
current_span.set_attribute('client.auth.type', 'api_key')
431+
if ENABLE_OTEL:
432+
from opentelemetry import trace
433+
434+
current_span = trace.get_current_span()
435+
if current_span:
436+
current_span.set_attribute('client.user.id', user.id)
437+
current_span.set_attribute('client.user.email', user.email)
438+
current_span.set_attribute('client.user.role', user.role)
439+
current_span.set_attribute('client.auth.type', 'api_key')
431440

432441
Users.update_last_active_by_id(user.id)
433442
return user

backend/open_webui/utils/logger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import TYPE_CHECKING
55

66
from loguru import logger
7-
from opentelemetry import trace
7+
88
from open_webui.env import (
99
ENABLE_AUDIT_STDOUT,
1010
ENABLE_AUDIT_LOGS_FILE,
@@ -100,6 +100,8 @@ def _get_extras(self):
100100
if not ENABLE_OTEL:
101101
return {}
102102

103+
from opentelemetry import trace
104+
103105
extras = {}
104106
context = trace.get_current_span().get_span_context()
105107
if context.is_valid:

src/lib/components/chat/Chat.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,10 +1245,12 @@
12451245
}
12461246
}
12471247
1248-
if (query) {
1249-
messageInput?.setText(query);
1248+
if (query || eventFiles?.length) {
1249+
if (query) {
1250+
messageInput?.setText(query);
1251+
}
12501252
await tick();
1251-
submitHandler(query);
1253+
submitHandler(query || '');
12521254
}
12531255
} else if ($page.url.searchParams.get('q')) {
12541256
const q = $page.url.searchParams.get('q') ?? '';

0 commit comments

Comments
 (0)