perf: build info log messages lazily so raising the log level actually saves work (#27837)
Raising GLOBAL_LOG_LEVEL to WARNING buys quieter output but not less work: 241 INFO call sites interpolate their payload into an f-string before the logging call gets to drop it. The heaviest is get_doc, which logs every chunk id and metadata dict in a collection, so on the full-context retrieval path that is the entire knowledge base, once per chat request.
That one line at WARNING, CPython 3.12:
| knowledge base | payload | before | after |
| -------------- | ------- | -------- | ------- |
| top-k of 3 | 1.2 kB | 3.8 us | 0.07 us |
| 500 chunks | 201 kB | 583.6 us | 0.08 us |
| 5000 chunks | 2.0 MB | 5.8 ms | 0.15 us |
The lazy form log.info('query_doc:result %s %s', result.ids, result.metadatas) hands the payload to record.getMessage(), which the InterceptHandler only reaches once a record has passed the level check. Output at INFO is byte-identical. Two sites that already built their message eagerly, one str concat and one % operator, move to the same lazy form.
This commit is contained in:
1 parent
52145eede9
commit
2d18727ab8
48 files changed
+297
-247
No files matched your search
@@ -351,7 +351,7 @@ async def reindex_knowledge_files(
|
||||
failed_files = []
|
||||
start_time = time.monotonic()
|
||||
|
||||
log.info(f'Starting reindexing for {len(knowledge_bases)} knowledge bases ({total_files} files)')
|
||||
log.info('Starting reindexing for %s knowledge bases (%s files)', len(knowledge_bases), total_files)
|
||||
|
||||
for kb_idx, (knowledge_base, files) in enumerate(knowledge_base_files, start=1):
|
||||
try:
|
||||
@@ -371,8 +371,13 @@ async def reindex_knowledge_files(
|
||||
eta = f', ETA: {round(elapsed / (processed_files - 1) * remaining_files)}s'
|
||||
|
||||
log.info(
|
||||
f'Reindexing knowledge base {kb_idx}/{len(knowledge_bases)} '
|
||||
f'file {processed_files}/{total_files}{eta}: {file.filename}'
|
||||
'Reindexing knowledge base %s/%s file %s/%s%s: %s',
|
||||
kb_idx,
|
||||
len(knowledge_bases),
|
||||
processed_files,
|
||||
total_files,
|
||||
eta,
|
||||
file.filename,
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -397,7 +402,7 @@ async def reindex_knowledge_files(
|
||||
for failed in failed_files:
|
||||
log.warning(f'File ID: {failed["file_id"]}, Error: {failed["error"]}')
|
||||
|
||||
log.info(f'Reindexing completed in {round(time.monotonic() - start_time)}s.')
|
||||
log.info('Reindexing completed in %ss.', round(time.monotonic() - start_time))
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.KNOWLEDGE_REINDEXED,
|
||||
@@ -426,14 +431,14 @@ async def reindex_knowledge_base_metadata_embeddings(
|
||||
this entire operation would exhaust the connection pool.
|
||||
"""
|
||||
knowledge_bases = await Knowledges.get_knowledge_bases()
|
||||
log.info(f'Reindexing embeddings for {len(knowledge_bases)} knowledge bases')
|
||||
log.info('Reindexing embeddings for %s knowledge bases', len(knowledge_bases))
|
||||
|
||||
success_count = 0
|
||||
for kb in knowledge_bases:
|
||||
if await embed_knowledge_base_metadata(request, kb.id, kb.name, kb.description):
|
||||
success_count += 1
|
||||
|
||||
log.info(f'Embedding reindex complete: {success_count}/{len(knowledge_bases)}')
|
||||
log.info('Embedding reindex complete: %s/%s', success_count, len(knowledge_bases))
|
||||
return {'total': len(knowledge_bases), 'success': success_count}
|
||||
|
||||
|
||||
@@ -1687,11 +1692,11 @@ async def delete_knowledge_by_id(
|
||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||
)
|
||||
|
||||
log.info(f'Deleting knowledge base: {id} (name: {knowledge.name})')
|
||||
log.info('Deleting knowledge base: %s (name: %s)', id, knowledge.name)
|
||||
|
||||
# Get all models
|
||||
models = await Models.get_all_models(db=db)
|
||||
log.info(f'Found {len(models)} models to check for knowledge base {id}')
|
||||
log.info('Found %s models to check for knowledge base %s', len(models), id)
|
||||
|
||||
# Update models that reference this knowledge base
|
||||
for model in models:
|
||||
@@ -1702,7 +1707,7 @@ async def delete_knowledge_by_id(
|
||||
|
||||
# If the knowledge list changed, update the model
|
||||
if len(updated_knowledge) != len(knowledge_list):
|
||||
log.info(f'Updating model {model.id} to remove knowledge base {id}')
|
||||
log.info('Updating model %s to remove knowledge base %s', model.id, id)
|
||||
model.meta.knowledge = updated_knowledge
|
||||
model_form = ModelForm(**model.model_dump())
|
||||
await Models.update_model_by_id(model.id, model_form, db=db)
|
||||
@@ -2022,7 +2027,7 @@ async def add_files_to_knowledge_batch(
|
||||
)
|
||||
|
||||
# Batch-fetch all files to avoid N+1 queries
|
||||
log.info(f'files/batch/add - {len(form_data)} files')
|
||||
log.info('files/batch/add - %s files', len(form_data))
|
||||
file_ids = [form.file_id for form in form_data]
|
||||
files = await Files.get_files_by_ids(file_ids, db=db)
|
||||
|
||||
|
||||
Reference in new issue
Block a user