Skip to content

fix: move stdlib imports to module level in hot-path functions#1216

Merged
muddlebee merged 2 commits intoTracer-Cloud:mainfrom
Jai0401:fix/stdlib-imports-hotpath
May 2, 2026
Merged

fix: move stdlib imports to module level in hot-path functions#1216
muddlebee merged 2 commits intoTracer-Cloud:mainfrom
Jai0401:fix/stdlib-imports-hotpath

Conversation

@Jai0401
Copy link
Copy Markdown
Contributor

@Jai0401 Jai0401 commented May 2, 2026

Fixes #1209

Describe the changes you have made in this PR -

Moves import copy and import datetime from inside hot-path functions to module top level, eliminating per-call import overhead.

Files changed

  • app/nodes/chat.py — moved import copy to module top (was inside _apply_guardrails_to_messages())
  • app/nodes/root_cause_diagnosis/prompt_builder.py — moved import datetime to module top (was inside _format_tags_with_ts())

Demo/Screenshot for feature changes and bug fixes -

# Before (import on every call):
def _apply_guardrails_to_messages(msgs):
    import copy  # executes every invocation
    ...

# After (import once at module load):
import copy  # module top level
def _apply_guardrails_to_messages(msgs):
    ...  # no import overhead

Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • No, I wrote all the code myself
  • Yes, I used AI assistance (continue below)

If you used AI assistance:

  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions

Explain your implementation approach:

The fix moves two stdlib imports that were inside hot-path functions to module top level:

  1. app/nodes/chat.py: import copy moved from inside _apply_guardrails_to_messages() to module top
  2. app/nodes/root_cause_diagnosis/prompt_builder.py: import datetime moved from inside _format_tags_with_ts() to module top

This 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

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • My code follows the project's style guidelines and conventions
  • All checks pass: ruff lint, ruff format-check, pytest (29 tests passing)

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-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 2, 2026

Greptile Summary

This PR moves import copy and import datetime from inside hot-path functions to module level, eliminating per-call import overhead. The changes are minimal, correct, and stdlib-safe with no circular-import risk.

Confidence Score: 5/5

Safe 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

Filename Overview
app/nodes/chat.py Moves import copy from inside _apply_guardrails_to_messages to module level; a blank line in the docstring was incidentally dropped.
app/nodes/root_cause_diagnosis/prompt_builder.py Moves import datetime from inside _format_datadog_log_entry (not _format_tags_with_ts as stated in the PR description) to module level; no functional issues.

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"]
Loading

Reviews (1): Last reviewed commit: "fix: move stdlib imports to module level..." | Re-trigger Greptile

Comment thread app/nodes/chat.py
Comment on lines 186 to 189
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.
"""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Suggested change
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.
"""

@muddlebee
Copy link
Copy Markdown
Collaborator

@Jai0401 thank you looks good. I will wait for CI checks.

@muddlebee muddlebee merged commit 2cd1065 into Tracer-Cloud:main May 2, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

🏆 @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.

VaibhavUpreti pushed a commit that referenced this pull request May 3, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Stdlib imports placed inside hot-path functions — copy/datetime imported per-call instead of module-top

2 participants