|
19 | 19 | from pytz import UTC |
20 | 20 | from typing import Optional, Union, List, Dict |
21 | 21 |
|
22 | | -from opentelemetry import trace |
23 | 22 |
|
24 | 23 |
|
25 | 24 | from open_webui.utils.access_control import has_permission |
|
30 | 29 | from open_webui.constants import ERROR_MESSAGES |
31 | 30 |
|
32 | 31 | from open_webui.env import ( |
| 32 | + ENABLE_OTEL, |
33 | 33 | ENABLE_PASSWORD_VALIDATION, |
34 | 34 | OFFLINE_MODE, |
35 | 35 | LICENSE_BLOB, |
@@ -327,12 +327,15 @@ async def get_current_user( |
327 | 327 | user = get_current_user_by_api_key(request, token) |
328 | 328 |
|
329 | 329 | # 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') |
336 | 339 |
|
337 | 340 | return user |
338 | 341 |
|
@@ -369,12 +372,15 @@ async def get_current_user( |
369 | 372 | ) |
370 | 373 |
|
371 | 374 | # 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') |
378 | 384 |
|
379 | 385 | # Refresh the user's last active timestamp asynchronously |
380 | 386 | # to prevent blocking the request |
@@ -422,12 +428,15 @@ def get_current_user_by_api_key(request, api_key: str): |
422 | 428 | raise HTTPException(status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED) |
423 | 429 |
|
424 | 430 | # 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') |
431 | 440 |
|
432 | 441 | Users.update_last_active_by_id(user.id) |
433 | 442 | return user |
|
0 commit comments