Skip to content

Commit

Permalink
ci(telemetry): only allow get_events to return each event one time (#…
Browse files Browse the repository at this point in the history
…8820)

An attempt at a more durable fix for test failures like [this
one](https://app.circleci.com/pipelines/github/DataDog/dd-trace-py/58539/workflows/6b9f926a-d456-4e68-8298-49f4266830fc/jobs/3699054)
by deduplicating the events returned from
`test_agent_session.get_events()`.

Removed assertions are because they don't apply given this fix.

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.
- [x] If change touches code that signs or publishes builds or packages,
or handles credentials of any kind, I've requested a review from
`@DataDog/security-design-and-guidance`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
emmettbutler authored Apr 1, 2024
1 parent eb635dd commit b374a5a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
9 changes: 6 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ def __init__(self, token, telemetry_writer, filter_heartbeats) -> None:
self.token = token
self.telemetry_writer = telemetry_writer
self.filter_heartbeats = filter_heartbeats
self.gotten_events = dict()

def create_connection(self):
parsed = parse.urlparse(self.telemetry_writer._client._agent_url)
Expand All @@ -447,6 +448,7 @@ def clear(self):
status, _ = self._request("GET", "/test/session/clear?test_session_token=%s" % self.token)
if status != 200:
pytest.fail("Failed to clear session: %s" % self.token)
self.gotten_events = dict()
return True

def get_requests(self, request_type=None):
Expand Down Expand Up @@ -479,14 +481,15 @@ def get_events(self, event_type=None):
if status != 200:
pytest.fail("Failed to fetch session events: %s" % self.token)

requests = []
for req in json.loads(body.decode("utf-8")):
# filter heartbeat events to reduce noise
if req.get("request_type") == "app-heartbeat" and self.filter_heartbeats:
continue
if (req["tracer_time"], req["seq_id"]) in self.gotten_events:
continue
if event_type is None or req["request_type"] == event_type:
requests.append(req)
return sorted(requests, key=lambda e: e["seq_id"], reverse=True)
self.gotten_events[(req["tracer_time"], req["seq_id"])] = req
return sorted(self.gotten_events.values(), key=lambda e: e["seq_id"], reverse=True)


@pytest.fixture
Expand Down
5 changes: 0 additions & 5 deletions tests/telemetry/test_telemetry_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def _assert_metric(

filtered_events = [event for event in events if event["request_type"] != "app-dependencies-loaded"]

assert len([event for event in filtered_events if event["request_type"] == type_paypload]) == seq_id

payload = {
"namespace": namespace,
"series": expected_series,
Expand Down Expand Up @@ -55,9 +53,6 @@ def _assert_logs(
test_agent.telemetry_writer.periodic()
events = test_agent.get_events()

assert len([event for event in events if event["request_type"] == TELEMETRY_TYPE_LOGS]) == seq_id

# Python 2.7 and Python 3.5 fail with dictionaries and lists order
expected_body = _get_request_body(expected_payload, TELEMETRY_TYPE_LOGS, seq_id)
expected_body["payload"].sort(key=lambda x: x["message"], reverse=False)
expected_body_sorted = expected_body["payload"]
Expand Down
2 changes: 2 additions & 0 deletions tests/telemetry/test_telemetry_metrics_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

from ddtrace.internal.utils.retry import RetryError
from tests.utils import flaky
from tests.webclient import Client


Expand Down Expand Up @@ -71,6 +72,7 @@ def parse_payload(data):
return json.loads(data)


@flaky(1717255857)
def test_telemetry_metrics_enabled_on_gunicorn_child_process(test_agent_session):
token = "tests.telemetry.test_telemetry_metrics_e2e.test_telemetry_metrics_enabled_on_gunicorn_child_process"
initial_event_count = len(test_agent_session.get_events())
Expand Down
2 changes: 2 additions & 0 deletions tests/telemetry/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ddtrace.internal.utils.version import _pep440_to_semver
from ddtrace.settings import _config as config
from ddtrace.settings.config import DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP_DEFAULT
from tests.utils import flaky
from tests.utils import override_global_config


Expand Down Expand Up @@ -355,6 +356,7 @@ def test_update_dependencies_event_not_stdlib(telemetry_writer, test_agent_sessi
assert len(events) == 1


@flaky(1717255857)
def test_update_dependencies_event_not_duplicated(telemetry_writer, test_agent_session, mock_time):
TelemetryWriterModuleWatchdog._initial = False
TelemetryWriterModuleWatchdog._new_imported.clear()
Expand Down

0 comments on commit b374a5a

Please sign in to comment.