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

+9 -9
View File
@@ -202,7 +202,7 @@ async def register_oauth_client(
'oauth_client_info': encrypt_data(oauth_client_info.model_dump(mode='json')),
}
except Exception as e:
log.debug(f'Failed to register OAuth client: {e}')
log.debug('Failed to register OAuth client: %s', e)
raise HTTPException(
status_code=400,
detail=f'Failed to register OAuth client: {e}',
@@ -279,7 +279,7 @@ async def set_tool_servers_config(
OAuthClientInformationFull(**oauth_client_info),
)
except Exception as e:
log.debug(f'Failed to add OAuth client for MCP tool server: {e}')
log.debug('Failed to add OAuth client for MCP tool server: %s', e)
continue
await publish_event(
@@ -390,7 +390,7 @@ async def verify_terminal_server_connection(
pass
except Exception as e:
log.debug(f'Failed to connect to the terminal server: {e}')
log.debug('Failed to connect to the terminal server: %s', e)
raise HTTPException(status_code=400, detail='Failed to connect to the terminal server')
@@ -454,7 +454,7 @@ async def put_terminal_server_policy(
except HTTPException:
raise
except Exception as e:
log.debug(f'Failed to access policy on terminal server: {e}')
log.debug('Failed to access policy on terminal server: %s', e)
raise HTTPException(status_code=400, detail='Failed to access policy on terminal server')
@@ -491,7 +491,7 @@ async def put_terminal_server_lifecycle(
except HTTPException:
raise
except Exception as e:
log.debug(f'Failed to access lifecycle on terminal server: {e}')
log.debug('Failed to access lifecycle on terminal server: %s', e)
raise HTTPException(status_code=400, detail='Failed to access lifecycle on terminal server')
@@ -538,7 +538,7 @@ async def refresh_terminal_server_terminals(
except HTTPException:
raise
except Exception as e:
log.debug(f'Failed to refresh terminals: {e}')
log.debug('Failed to refresh terminals: %s', e)
raise HTTPException(status_code=400, detail='Failed to refresh terminals')
@@ -557,7 +557,7 @@ async def verify_tool_servers_config(request: Request, form_data: ToolServerConn
)
discovery_urls = await get_discovery_urls(oauth_server_url)
for discovery_url in discovery_urls:
log.debug(f'Trying to fetch OAuth 2.1 discovery document from {discovery_url}')
log.debug('Trying to fetch OAuth 2.1 discovery document from %s', discovery_url)
async with aiohttp.ClientSession(
trust_env=True,
timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT),
@@ -624,7 +624,7 @@ async def verify_tool_servers_config(request: Request, form_data: ToolServerConn
'specs': specs,
}
except Exception as e:
log.debug(f'Failed to create MCP client: {e}')
log.debug('Failed to create MCP client: %s', e)
raise HTTPException(
status_code=400,
detail=f'Failed to create MCP client',
@@ -667,7 +667,7 @@ async def verify_tool_servers_config(request: Request, form_data: ToolServerConn
except HTTPException as e:
raise e
except Exception as e:
log.debug(f'Failed to connect to the tool server: {e}')
log.debug('Failed to connect to the tool server: %s', e)
raise HTTPException(
status_code=400,
detail=f'Failed to connect to the tool server',