fix: move stdlib imports to module level in hot-path functions#1216
fix: move stdlib imports to module level in hot-path functions#1216muddlebee merged 2 commits intoTracer-Cloud:mainfrom
Conversation
Moves `import copy` from inside _apply_guardrails_to_messages() and `import datetime` from inside _format_tags_with_ts() to the module top level, eliminating per-call import overhead.
Greptile SummaryThis PR moves Confidence Score: 5/5Safe to merge — only stdlib import hoisting, no behavioral changes. Both changes are pure refactors moving stdlib imports to module scope. No logic, no control flow, and no new dependencies are altered. The only finding is a P2 docstring style nit. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Module load] -->|Before: per-call| B1["_apply_guardrails_to_messages()\nimport copy ← removed"]
A -->|Before: per-call| B2["_format_datadog_log_entry()\nimport datetime ← removed"]
A -->|After: once at import time| C1["import copy ✓"]
A -->|After: once at import time| C2["import datetime ✓"]
C1 --> D1["_apply_guardrails_to_messages()\nuses copy directly"]
C2 --> D2["_format_datadog_log_entry()\nuses datetime directly"]
Reviews (1): Last reviewed commit: "fix: move stdlib imports to module level..." | Re-trigger Greptile |
| def _apply_guardrails_to_messages(msgs: list[Any]) -> list[Any]: | ||
| """Return a copy of *msgs* with redacted content, leaving originals untouched. | ||
|
|
||
| Operates on copies to avoid mutating shared LangGraph state objects. | ||
| """ |
There was a problem hiding this comment.
Docstring blank line inadvertently removed
The blank line separating the one-line summary from the extended description in the docstring was dropped as a side-effect of removing the import copy statement. PEP 257 calls for that separator line in multi-line docstrings.
| def _apply_guardrails_to_messages(msgs: list[Any]) -> list[Any]: | |
| """Return a copy of *msgs* with redacted content, leaving originals untouched. | |
| Operates on copies to avoid mutating shared LangGraph state objects. | |
| """ | |
| def _apply_guardrails_to_messages(msgs: list[Any]) -> list[Any]: | |
| """Return a copy of *msgs* with redacted content, leaving originals untouched. | |
| Operates on copies to avoid mutating shared LangGraph state objects. | |
| """ |
|
@Jai0401 thank you looks good. I will wait for CI checks. |
|
🏆 @Jai0401 did not come to play. PR opened. Review survived. Merged clean. Retire the jersey. 🎽 👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome. |
…1243) Hoists `import json` from inside hot-path function bodies to module top in two services, matching the pattern from #1216 (which fixed #1209 for `copy` and `datetime` in app/nodes/). - app/services/tracer_client/tracer_integrations.py - get_all_integrations: import was inside a `for` loop iterating integration records, re-running the import statement once per iteration - get_grafana_credentials: import was per-call during integration resolution - app/services/lambda_client.py - invoke_function: import was per Lambda invocation Fixes #1242

Fixes #1209
Describe the changes you have made in this PR -
Moves
import copyandimport datetimefrom inside hot-path functions to module top level, eliminating per-call import overhead.Files changed
import copyto module top (was inside_apply_guardrails_to_messages())import datetimeto module top (was inside_format_tags_with_ts())Demo/Screenshot for feature changes and bug fixes -
Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
Explain your implementation approach:
The fix moves two stdlib imports that were inside hot-path functions to module top level:
app/nodes/chat.py:import copymoved from inside_apply_guardrails_to_messages()to module topapp/nodes/root_cause_diagnosis/prompt_builder.py:import datetimemoved from inside_format_tags_with_ts()to module topThis eliminates the overhead of re-importing these modules on every function call. The change is minimal and surgical — only 2 lines added, 4 lines removed per file. Both files pass AST parse validation, ruff lint, ruff format-check, and the full pytest suite (29 tests passing related to chat/prompt_builder).
Checklist before requesting a review