feat: add performance logging middleware and enhance performance tracking across services

- Introduced RequestPerfMiddleware to log request performance metrics, including slow request thresholds.
- Updated various services and retrievers to utilize the new performance logging utility for better tracking of execution times.
- Enhanced existing methods with detailed performance logs for operations such as embedding, searching, and indexing.
- Removed deprecated logging setup in stream_new_chat and replaced it with the new performance logger.
This commit is contained in:
DESKTOP-RTLN3BA\$punk 2026-02-27 16:32:30 -08:00
parent 68bb196d45
commit 664c43ca13
11 changed files with 430 additions and 36 deletions

View file

@ -1,5 +1,8 @@
import time
from datetime import datetime
from app.utils.perf import get_perf_logger
class DocumentHybridSearchRetriever:
def __init__(self, db_session):
@ -38,6 +41,9 @@ class DocumentHybridSearchRetriever:
from app.config import config
from app.db import Document
perf = get_perf_logger()
t0 = time.perf_counter()
# Get embedding for the query
embedding_model = config.embedding_model_instance
query_embedding = embedding_model.embed(query_text)
@ -63,6 +69,10 @@ class DocumentHybridSearchRetriever:
# Execute the query
result = await self.db_session.execute(query)
documents = result.scalars().all()
perf.info(
"[doc_search] vector_search in %.3fs results=%d space=%d",
time.perf_counter() - t0, len(documents), search_space_id,
)
return documents
@ -92,6 +102,9 @@ class DocumentHybridSearchRetriever:
from app.db import Document
perf = get_perf_logger()
t0 = time.perf_counter()
# Create tsvector and tsquery for PostgreSQL full-text search
tsvector = func.to_tsvector("english", Document.content)
tsquery = func.plainto_tsquery("english", query_text)
@ -118,6 +131,10 @@ class DocumentHybridSearchRetriever:
# Execute the query
result = await self.db_session.execute(query)
documents = result.scalars().all()
perf.info(
"[doc_search] full_text_search in %.3fs results=%d space=%d",
time.perf_counter() - t0, len(documents), search_space_id,
)
return documents
@ -151,6 +168,9 @@ class DocumentHybridSearchRetriever:
from app.config import config
from app.db import Chunk, Document, DocumentType
perf = get_perf_logger()
t0 = time.perf_counter()
# Get embedding for the query
embedding_model = config.embedding_model_instance
query_embedding = embedding_model.embed(query_text)
@ -303,4 +323,8 @@ class DocumentHybridSearchRetriever:
)
final_docs.append(entry)
perf.info(
"[doc_search] hybrid_search TOTAL in %.3fs docs=%d space=%d type=%s",
time.perf_counter() - t0, len(final_docs), search_space_id, document_type,
)
return final_docs