Skip to content

API Response Test: direct unit tests for app/services/datadog/client.py#1104

Merged
muddlebee merged 5 commits intoTracer-Cloud:mainfrom
SB2318:client_service_api_response_test
May 1, 2026
Merged

API Response Test: direct unit tests for app/services/datadog/client.py#1104
muddlebee merged 5 commits intoTracer-Cloud:mainfrom
SB2318:client_service_api_response_test

Conversation

@SB2318
Copy link
Copy Markdown
Contributor

@SB2318 SB2318 commented Apr 30, 2026

Fixes #878

Describe the changes you have made in this PR -

Added a dedicated service-layer test suite for the DatadogClient integration to ensure direct coverage of core Datadog API interactions without relying on tool-layer tests.

Key changes:

  • Introduced tests/services/test_datadog_client.py
  • Added unit tests for:
    • search_logs() (success, HTTP failure, empty response)
    • list_monitors() (success, HTTP failure, empty response)
    • get_events() (success, HTTP failure, empty response)
    • is_configured validation
  • Mocked httpx.Client to ensure no real Datadog API calls are made
  • Ensured service-layer isolation so tool tests no longer implicitly validate client logic
  • Verified consistent response normalization across logs, monitors, and events APIs

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?

  • 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:

This PR introduces service-level testing for the Datadog integration to improve test isolation, reliability, and maintainability.

Previously, Datadog client behavior was only indirectly validated through tool-layer tests. This created tight coupling between layers and made failures harder to trace and debug.


What this solves:

  • Ensures Datadog API logic is directly tested at the service layer
  • Removes dependency on tool-layer tests for validating core client behavior
  • Improves test clarity, isolation, and maintainability
  • Makes failures easier to debug by localizing responsibility to the service layer

Implementation approach:

  • Used httpx.Client mocking to simulate Datadog API responses without making real network calls
  • Covered both success and failure scenarios for each endpoint:
    • logs (search_logs)
    • monitors (list_monitors)
    • events (get_events)
  • Verified response normalization logic (ensuring consistent structure for logs, monitors, and events)
  • Added coverage for configuration state validation via is_configured

Key design choice:

I chose service-layer mocking instead of integration testing to ensure:

  • Deterministic and stable test execution in CI
  • No dependency on external Datadog credentials or network availability
  • Faster test execution and improved developer feedback loop
  • Clear separation between service logic and tool-layer orchestration

This approach ensures that the Datadog client is independently verifiable and does not rely on higher-level tool tests for correctness.

This helps reviewers understand your thought process and ensures you understand the code.
-->


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
  • If it is a core feature, I have added thorough tests
  • My code follows the project's style guidelines and conventions

Note: Please check Allow edits from maintainers if you would like us to assist in the PR.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Greptile Summary

This PR adds dedicated service-layer unit tests for DatadogClient (sync) and DatadogAsyncClient, covering search_logs, list_monitors, get_events, get_pods_on_node, and is_configured across success, HTTP error, and generic exception paths. The mock wiring (httpx.Client patching for sync, async context manager mocking for async) is correct and no real network calls are made. All remaining findings are P2 style/completeness suggestions.

Confidence Score: 5/5

Safe to merge — all findings are P2 style and completeness suggestions with no blocking correctness issues.

The mock wiring is technically correct for both sync and async clients, tests cover success/error/empty paths and is_configured edge cases, and no production code is changed. All feedback is P2 (missing raise_for_status assertions, incomplete error leg coverage in one async test, leftover debug comments).

No files require special attention; improvements are optional polish.

Important Files Changed

Filename Overview
tests/services/test_datadog_client.py Adds unit tests for DatadogClient (sync); mock wiring is correct but success tests don't assert raise_for_status was called, and HTTP verb/URL is never verified.
tests/services/test_datadog_async_client.py Adds unit tests for DatadogAsyncClient; async context manager mocking is correct, but http_error test only asserts on the logs leg and leftover FIXED comments should be removed.

Sequence Diagram

sequenceDiagram
    participant Test
    participant DatadogClient
    participant _get_client
    participant httpx_Client_mock

    Note over Test,httpx_Client_mock: Synchronous client tests
    Test->>DatadogClient: search_logs("error")
    DatadogClient->>_get_client: _get_client()
    _get_client->>httpx_Client_mock: httpx.Client(base_url, headers, timeout)
    httpx_Client_mock-->>_get_client: mock_instance (cached)
    _get_client-->>DatadogClient: mock_instance
    DatadogClient->>httpx_Client_mock: mock_instance.post("/api/v2/logs/events/search")
    httpx_Client_mock-->>DatadogClient: mock_response
    DatadogClient->>DatadogClient: resp.raise_for_status()
    DatadogClient->>DatadogClient: data = resp.json()
    DatadogClient-->>Test: {success: True, logs: [...], total: N}

    Note over Test,httpx_Client_mock: Async client tests (fetch_all)
    Test->>DatadogAsyncClient: fetch_all(logs_query, monitor_query, events_query)
    DatadogAsyncClient->>httpx_Client_mock: async with httpx.AsyncClient(...) as client
    par asyncio.gather
        DatadogAsyncClient->>httpx_Client_mock: client.post("/api/v2/logs/events/search")
        DatadogAsyncClient->>httpx_Client_mock: client.get("/api/v1/monitor")
        DatadogAsyncClient->>httpx_Client_mock: client.post("/api/v2/events/search")
    end
    DatadogAsyncClient-->>Test: {logs: {...}, monitors: {...}, events: {...}}
Loading

Reviews (1): Last reviewed commit: "api test" | Re-trigger Greptile

Comment thread tests/services/test_datadog_client.py
Comment thread tests/services/test_datadog_async_client.py
Comment thread tests/services/test_datadog_async_client.py Outdated
Comment thread tests/services/test_datadog_client.py
@SB2318
Copy link
Copy Markdown
Contributor Author

SB2318 commented Apr 30, 2026

@yashksaini-coder please review

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

github-actions Bot commented May 1, 2026

🏄 Some PRs rot in review for six weeks. @SB2318's said "not today" and merged like it owned the place. 🌊


👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome.

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.

Add direct unit tests for app/services/datadog/client.py

2 participants