Skip to content

Introduce Mocked Backend V2 - #7668

Draft
cbeauchesne wants to merge 18 commits into
mainfrom
cbeauchesne/backend-v2
Draft

Introduce Mocked Backend V2#7668
cbeauchesne wants to merge 18 commits into
mainfrom
cbeauchesne/backend-v2

Conversation

@cbeauchesne

Copy link
Copy Markdown
Collaborator

Motivation

Unify how data are collected by datadog, by adding a Mocked backend that works in all situations (agentless, SSI, classic end-to-end).

Changes

  • utils/mocked_backend/backend_v2.py: new in-process mock HTTP server standing in for the Datadog backend. Logs every request to logs/interfaces/backend_v2, decodes Content-Encoding (gzip/deflate/br/zstd), and reuses utils.proxy._deserializer.deserialize() unchanged to parse bodies.
  • utils/interfaces/_backend_v2.py: new _BackendV2InterfaceValidator (subclasses ProxyBasedInterfaceValidator), owns the mock server lifecycle, exposes get_traces/get_spans/get_spans_list mirroring AgentInterfaceValidator.
  • endtoend.py: new mocked_backend_v2 flag — points the Agent container's DD_DD_URL/DD_APM_DD_URL at the mock server; raises if combined with use_proxy_for_agent=True.
  • _scenarios/init.py: apm_tracing_e2e_single_span now uses mocked_backend_v2=True instead of require_api_key=True.
  • test_single_span.py: assertions switched to interfaces.backend_v2.get_spans_list(...).
  • Supporting: AgentContainer gets extra_hosts for host.docker.internal; requirements.txt adds requests-toolbelt, Brotli, zstandard.

And apply it to APM_TRACING_E2E_SINGLE_SPAN scenario

Workflow

  1. ⚠️ Create your PR as draft ⚠️
  2. Work on you PR until the CI passes
  3. Mark it as ready for review
    • Test logic is modified? -> Get a review from RFC owner.
    • Framework is modified, or non obvious usage of it -> get a review from R&P team

🚀 Once your PR is reviewed and the CI green, you can merge it!

🛟 #apm-shared-testing 🛟

Reviewer checklist

  • Anything but tests/ or manifests/ is modified ? I have the approval from R&P team
  • A docker base image is modified?
    • the relevant build-XXX-image label is present
  • A scenario is added, removed or renamed?

@cbeauchesne
cbeauchesne requested a lite review from Copilot September 7, 2026 15:44
@cbeauchesne

Copy link
Copy Markdown
Collaborator Author

@codex review

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

CODEOWNERS have been resolved as:

tests/test_the_test/test_mock_backend_v2.py                             @DataDog/system-tests-core
utils/interfaces/_backend_v2.py                                         @DataDog/system-tests-core
utils/mocked_backend/backend_v2.py                                      @DataDog/system-tests-core
.github/workflows/run-end-to-end.yml                                    @DataDog/system-tests-core
.gitlab-ci.yml                                                          @DataDog/system-tests-core
manifests/java.yml                                                      @DataDog/system-tests-reviewers
requirements.txt                                                        @DataDog/system-tests-core
tests/apm_tracing_e2e/test_single_span.py                               @DataDog/system-tests-core
tests/schemas/test_schemas.py                                           @DataDog/system-tests-core
utils/_context/_scenarios/__init__.py                                   @DataDog/system-tests-core
utils/_context/_scenarios/endtoend.py                                   @DataDog/system-tests-core
utils/_context/containers.py                                            @DataDog/system-tests-core
utils/ci/gitlab/main.yml                                                @DataDog/system-tests-core
utils/interfaces/__init__.py                                            @DataDog/system-tests-core
utils/interfaces/_core.py                                               @DataDog/system-tests-core
utils/proxy/_deserializer.py                                            @DataDog/system-tests-core
utils/proxy/core.py                                                     @DataDog/system-tests-core
utils/scripts/replay_scenarios.sh                                       @DataDog/system-tests-core
utils/scripts/update_mirror_images.py                                   @DataDog/system-tests-core

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

There are correctness/robustness issues in scenario replay handling and a couple of codebase-convention/guarding problems that should be fixed before merging.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR introduces a new in-process “mocked backend v2” HTTP server + interface so end-to-end scenarios can capture what the Agent submits without requiring real backend connectivity (and applies it to the APM_TRACING_E2E_SINGLE_SPAN scenario).

Changes:

  • Add MockBackendV2Server (local HTTP server) and interfaces.backend_v2 interface to log + deserialize incoming backend requests.
  • Wire a new mocked_backend_v2 scenario flag to point Agent backend URLs to the mock server and start/stop it with the scenario lifecycle.
  • Switch the single-span E2E scenario/test to validate spans via interfaces.backend_v2, and add dependencies for content decoding.
File summaries
File Description
utils/mocked_backend/backend_v2.py New local HTTP server that records and deserializes backend intake requests.
utils/interfaces/_backend_v2.py New interface validator owning the mock server lifecycle and span/trace accessors.
utils/interfaces/_core.py Adds “configured” guarding to interfaces (with a new runtime check in get_data).
utils/interfaces/init.py Exposes the new backend_v2 interface singleton.
utils/_context/containers.py Ensures Agent containers can resolve host.docker.internal when needed.
utils/_context/_scenarios/endtoend.py Adds mocked_backend_v2 flag, wires Agent env to mock URL, and starts/stops the server.
utils/_context/_scenarios/init.py Enables mocked backend v2 for the single-span E2E scenario (and disables proxy-for-agent).
tests/apm_tracing_e2e/test_single_span.py Switches assertions to read spans from interfaces.backend_v2.
requirements.txt Adds Brotli/zstandard/requests-toolbelt for content decoding and deserialization support.
manifests/java.yml Adjusts the single-span test manifest entry (YAML key syntax).
Review details
  • Files reviewed: 9/10 changed files
  • Comments generated: 4
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread utils/_context/_scenarios/endtoend.py Outdated
Comment thread utils/interfaces/_core.py Outdated
Comment thread manifests/java.yml
Comment thread utils/interfaces/_core.py
@datadog-official

datadog-official Bot commented Sep 7, 2026

Copy link
Copy Markdown

Pipelines  Tests

⚠️ Warnings

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 2 Pipeline jobs failed

DataDog/system-tests | K8S_LIB_INJECTION_NO_AC: [dd-lib-python-init-test-django-unsupported-package-force, ${PRIVATE_DOCKER_REGISTRY}/system-tests/dd-lib-python-init-test-django-unsupported-package-force, 235494822917.dkr.ecr.us-east-1.amazonaws.com/ssi/dd-lib-python-init:latest] — 🔄 Retry may pass, looks flaky

View more details · View in GitLab

Testing the test | all-jobs-are-green

View more details · View in GitHub Actions

Check run failed for 'dd-lib-python-init-test-django-unsupported-package-force'. Status: failure.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 087dabd | Docs | View more details | Give us feedback!

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b42ff6faa8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread utils/mocked_backend/backend_v2.py Outdated
Comment thread utils/_context/_scenarios/__init__.py
cbeauchesne and others added 6 commits September 7, 2026 18:10
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@cbeauchesne

cbeauchesne commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The mocked backend v2 wiring needs xdist-safe port selection (worker-specific) and more robust teardown/error checking to avoid flaky or cross-worker interference.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (5)

Previously missed (4) — in code that hasn't changed since the last review.

utils/_context/_scenarios/endtoend.py:274

  • When running tests with pytest-xdist, each worker would currently point the agent at the default mocked-backend URL (worker_id="master"), which can cause port collisions and cross-talk between workers. Use the current xdist worker id when computing the mocked backend URL.
    utils/interfaces/_backend_v2.py:26
  • The mocked backend server is started with the default worker_id ("master"), which can bind all xdist workers to the same host port. Pass the current xdist worker id through to MockBackendV2Server so each worker gets a deterministic, non-conflicting port via get_host_port().
    utils/interfaces/_backend_v2.py:47
  • This docstring claims a third return value (trace_format), but the generator yields only (data, trace). Please update the docstring to match the actual return type to avoid confusing test authors.

This issue also appears on line 86 of the same file.
utils/scripts/replay_scenarios.sh:7

  • NOT_SUPPPORTED is misspelled (and the comment has a typo), which makes the script harder to maintain. Rename it to NOT_SUPPORTED and fix the comment spelling; update the reference in the loop accordingly.

utils/interfaces/_backend_v2.py:86

  • This docstring claims a third return value (trace_format), but the generator yields only (data, span). Please update the docstring to match the actual return type to avoid confusing test authors.
        Returns data, span and trace_format
  • Files reviewed: 16/17 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread utils/_context/_scenarios/endtoend.py
@cbeauchesne
cbeauchesne requested a lite review from Copilot September 7, 2026 19:36
@cbeauchesne cbeauchesne changed the title Introduce Mocked Backemd V2 Introduce Mocked Backend V2 Sep 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

There are a few concrete correctness/operability issues (notably schema tests returning instead of skipping, and backend_v2 trace parsing assuming deserialized content is always present) that should be addressed before approval.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (4)

Previously missed (3) — in code that hasn't changed since the last review.

utils/_context/_scenarios/endtoend.py:274

  • get_mocked_backend_v2_container_url() is called without a worker identifier, and MockBackendV2Server defaults to worker_id="master". If this scenario runs under pytest-xdist, multiple workers will try to bind/use the same host port (4901), causing port collisions/flaky startup. Consider plumbing the xdist worker id (e.g. via PYTEST_XDIST_WORKER or fixture-provided worker_id) through to both the server port selection and the agent env URL.
    utils/interfaces/_backend_v2.py:66
  • get_traces() assumes every backend_v2 entry has data["request"]["content"]. If request deserialization fails, the deserializer stores traceback/raw_content and omits content, which would currently raise a KeyError here and hide the real deserialization error. Add an explicit guard that raises a clearer error (or skips the entry) when content is missing.
    utils/scripts/replay_scenarios.sh:7
  • Spelling issues make this script harder to read/maintain: the comment has "backedn" and the variable is named NOT_SUPPPORTED (3 P’s). Rename it to NOT_SUPPORTED and update the reference.

tests/schemas/test_schemas.py:186

  • Same as above: return silently passes the test. Use pytest.skip(...) so CI reports an intentional skip for OTEL_COLLECTOR.
    def test_agent(self):
        if context.scenario.name in ("OTEL_COLLECTOR",):  # need to clean that point...
            return
  • Files reviewed: 18/19 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread tests/schemas/test_schemas.py Outdated
@cbeauchesne

Copy link
Copy Markdown
Collaborator Author

@codex review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

There are a couple of concrete issues in the updated scripts/server implementation (notably the replay scenario membership check and a resource-close issue) that should be addressed before approval.

Review details

Suppressed comments (3)

Previously missed (3) — in code that hasn't changed since the last review.

utils/mocked_backend/backend_v2.py:46

  • The zstd decode path creates a stream reader but never closes it, which can leak resources over many requests. Wrap the reader in a context manager so it is always closed.
    utils/scripts/replay_scenarios.sh:21
  • This membership check uses regex substring matching against the flattened array, which can yield false positives if one scenario name is a substring of another. Use a delimiter-based exact match instead.
    utils/scripts/replay_scenarios.sh:7
  • Typo in the comment ("backedn") and awkward phrasing; consider fixing to "backend" to keep the script self-explanatory.
  • Files reviewed: 18/19 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e34481f624

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


def get_mocked_backend_v2_container_url() -> str:
"""The URL a container can use to reach the mock server on the docker host."""
return f"http://{HOST_DOCKER_INTERNAL}:{get_mocked_backend_v2_port()}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Route DinD containers to the process hosting the mock

In the GitLab environment inspected, utils/ci/gitlab/templates.yml:13-18 runs against the remote DinD daemon at tcp://docker:2376, so host.docker.internal inside the Agent resolves to that daemon's host namespace, not to the separate CI job container where this Python server binds port 4901. Because this commit also removes APM_TRACING_E2E_SINGLE_SPAN from the GitLab exclusions, the Agent cannot reach this URL, no backend-v2 traces are captured, and both single-span assertions fail; expose an address reachable from nested containers or keep the scenario excluded in DinD.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants