perf: build debug log messages lazily so disabled debug logs cost nothing (#27834)

GLOBAL_LOG_LEVEL defaults to INFO, so every log.debug(...) in the backend is discarded, but the message is built first: 187 call sites interpolate their payload into an f-string before the logging call runs, so the work happens on every request and the result is thrown away. The worst one sits in process_chat_payload and stringifies the whole request body, full conversation history included, once per chat completion.

That one line with DEBUG disabled, CPython 3.12:

| conversation | payload | before   | after   |
| ------------ | ------- | -------- | ------- |
| 4 messages   | 1.2 kB  | 3.4 us   | 0.07 us |
| 20 messages  | 17 kB   | 24.8 us  | 0.07 us |
| 60 messages  | 123 kB  | 216.6 us | 0.07 us |

The lazy form log.debug('form_data: %s', form_data) hands the payload to record.getMessage(), which the InterceptHandler only reaches once a record has passed the level check. With DEBUG enabled the emitted lines are byte-identical, f'{x=}' sites included: those map to %r. MistralLoader._debug_log callers get the same treatment, since that wrapper already forwards *args.
This commit is contained in:
Classic298 authored and GitHub committed 2026-07-31 19:09:01 -05:00
1 parent b67804f2b6
commit 52cfb02c72
40 files changed
+198 -190

No files matched your search

+5 -5
View File
@@ -613,7 +613,7 @@ async def get_all_models_responses(request: Request, user: UserModel) -> list:
if provider:
model['provider'] = provider
log.debug(f'get_all_models:responses() {responses}')
log.debug('get_all_models:responses() %s', responses)
return responses
@@ -669,7 +669,7 @@ async def get_all_models(request: Request, user: UserModel) -> dict[str, list]:
return not any(name in model_id for name in _UNSUPPORTED_OPENAI_MODEL_KEYWORDS)
def get_merged_models(model_lists):
log.debug(f'merge_models_lists {model_lists}')
log.debug('merge_models_lists %s', model_lists)
models = {}
for idx, model_list in enumerate(model_lists):
@@ -710,7 +710,7 @@ async def get_all_models(request: Request, user: UserModel) -> dict[str, list]:
return models
models = get_merged_models(map(extract_data, responses))
log.debug(f'models: {models}')
log.debug('models: %s', models)
request.app.state.OPENAI_MODELS = models
return {'data': list(models.values())}
@@ -923,7 +923,7 @@ def get_azure_allowed_params(api_version: str) -> set[str]:
if api_version >= '2024-09-01-preview':
allowed_params.add('stream_options')
except ValueError:
log.debug(f'Invalid API version {api_version} for Azure OpenAI. Defaulting to allowed parameters.')
log.debug('Invalid API version %s for Azure OpenAI. Defaulting to allowed parameters.', api_version)
return allowed_params
@@ -971,7 +971,7 @@ def convert_to_azure_payload(url, payload: dict, api_version: str):
# Remove temperature if not 1 for o-series models
if 'temperature' in payload and payload['temperature'] != 1:
log.debug(
f'Removing temperature parameter for o-series model {model} as only default value (1) is supported'
'Removing temperature parameter for o-series model %s as only default value (1) is supported', model
)
del payload['temperature']