Skip to content

Commit 9073dd2

Browse files
authored
Add latency metrics (#4472)
* k * update
1 parent 9b6c762 commit 9073dd2

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Diff for: backend/onyx/auth/users.py

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
from onyx.utils.telemetry import create_milestone_and_report
106106
from onyx.utils.telemetry import optional_telemetry
107107
from onyx.utils.telemetry import RecordType
108+
from onyx.utils.timing import log_function_time
108109
from onyx.utils.url import add_url_params
109110
from onyx.utils.variable_functionality import fetch_ee_implementation_or_noop
110111
from onyx.utils.variable_functionality import fetch_versioned_implementation
@@ -363,6 +364,7 @@ async def validate_password(self, password: str, _: schemas.UC | models.UP) -> N
363364
)
364365
return
365366

367+
@log_function_time(print_only=True)
366368
async def oauth_callback(
367369
self,
368370
oauth_name: str,
@@ -609,6 +611,7 @@ async def on_after_request_verify(
609611
user.email, token, new_organization=user_count == 1
610612
)
611613

614+
@log_function_time(print_only=True)
612615
async def authenticate(
613616
self, credentials: OAuth2PasswordRequestForm
614617
) -> Optional[User]:
@@ -1235,6 +1238,7 @@ async def authorize(
12351238

12361239
return OAuth2AuthorizeResponse(authorization_url=authorization_url)
12371240

1241+
@log_function_time(print_only=True)
12381242
@router.get(
12391243
"/callback",
12401244
name=callback_route_name,

Diff for: backend/onyx/chat/process_message.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import traceback
23
from collections import defaultdict
34
from collections.abc import Callable
@@ -914,6 +915,7 @@ def stream_chat_message_objects(
914915
retrieval_options.filters.user_folder_ids = user_folder_ids
915916

916917
# Create override kwargs for the search tool
918+
917919
override_kwargs = SearchToolOverrideKwargs(
918920
force_no_rerank=search_for_ordering_only, # Skip reranking for ordering-only
919921
alternate_db_session=None,
@@ -1109,9 +1111,6 @@ def stream_chat_message_objects(
11091111
logger.info(
11101112
f"ORDERING: Processing search results for ordering {len(user_files)} user files"
11111113
)
1112-
import time
1113-
1114-
ordering_start = time.time()
11151114

11161115
# Extract document order from search results
11171116
doc_order = []
@@ -1147,8 +1146,6 @@ def stream_chat_message_objects(
11471146
if f_id in file_id_to_user_file
11481147
]
11491148

1150-
time.time() - ordering_start
1151-
11521149
yield UserKnowledgeFilePacket(
11531150
user_files=[
11541151
FileDescriptor(
@@ -1436,6 +1433,7 @@ def stream_chat_message(
14361433
custom_tool_additional_headers: dict[str, str] | None = None,
14371434
is_connected: Callable[[], bool] | None = None,
14381435
) -> Iterator[str]:
1436+
start_time = time.time()
14391437
with get_session_context_manager() as db_session:
14401438
objects = stream_chat_message_objects(
14411439
new_msg_req=new_msg_req,
@@ -1446,6 +1444,11 @@ def stream_chat_message(
14461444
is_connected=is_connected,
14471445
)
14481446
for obj in objects:
1447+
# Check if this is a QADocsResponse with document results
1448+
if isinstance(obj, QADocsResponse):
1449+
document_retrieval_latency = time.time() - start_time
1450+
logger.debug(f"First doc time: {document_retrieval_latency}")
1451+
14491452
yield get_json_line(obj.model_dump())
14501453

14511454

Diff for: backend/onyx/server/query_and_chat/chat_backend.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def handle_new_chat_message(
418418
"""
419419
tenant_id = get_current_tenant_id()
420420
logger.debug(f"Received new chat message: {chat_message_req.message}")
421+
time.time()
421422

422423
if (
423424
not chat_message_req.message
@@ -448,7 +449,7 @@ def stream_generator() -> Generator[str, None, None]:
448449
),
449450
is_connected=is_connected_func,
450451
):
451-
yield json.dumps(packet) if isinstance(packet, dict) else packet
452+
yield packet
452453

453454
except Exception as e:
454455
logger.exception("Error in chat message streaming")

0 commit comments

Comments
 (0)