Skip to content

Commit

Permalink
Update session logic
Browse files Browse the repository at this point in the history
Does the following:

* Clearing a session now clears traces only, instead of also deleting
  the session
* Requesting traces for a esssion will return a 404 Session Not found if
  no traces or session match a non-None session token
* Sessions now return session-less requests only up to the latest
  session marker
  • Loading branch information
tabgok committed Feb 10, 2024
1 parent 4fab7a1 commit 37782de
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var/
# Common virtual environments
.venv/
venv/
.python-version

# Editors
.idea/
Expand Down
251 changes: 143 additions & 108 deletions ddapm_test_agent/agent.py

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions ddapm_test_agent/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ def has_fails(self) -> bool:

def get_results(self, results: Dict[str, Dict[str, int]]) -> Dict[str, Dict[str, int]]:
"""
results = {
check.name: {
"Passed_Checks": int
"Failed_Checks": int
"Skipped_Checks": int
Return the following format
.. code-block:: python
results = {
check.name: {
"Passed_Checks": int,
"Failed_Checks": int,
"Skipped_Checks": int
}
...
}
...
}
"""
for c in self._checks:
Expand Down
2 changes: 1 addition & 1 deletion ddapm_test_agent/trace_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
log = logging.getLogger(__name__)


DEFAULT_SNAPSHOT_IGNORES = "span_id,trace_id,parent_id,duration,start,metrics.system.pid,metrics.system.process_id,metrics.process_id,meta.runtime-id"
DEFAULT_SNAPSHOT_IGNORES = "span_id,trace_id,parent_id,duration,start,metrics.system.pid,metrics.system.process_id,metrics.process_id,metrics._dd.tracer_kr,meta.runtime-id"


def _key_match(d1: Dict[str, Any], d2: Dict[str, Any], key: str) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
prelude: >
Replace this text with content to appear at the top of the section for this
release. All of the prelude content is merged together and then rendered
separately from the items listed in other parts of the file, so the text
needs to be worded so that both the prelude and the other items make sense
when read independently. This may mean repeating some details. Not every
release note requires a prelude. Usually only notes describing major
features or adding release theme details should have a prelude.
features:
- |
List new features here, or remove this section. All of the list items in
this section are combined when the release notes are rendered, so the text
needs to be worded so that it does not depend on any information only
available in another section, such as the prelude. This may mean repeating
some details.
issues:
- |
List known issues here, or remove this section. All of the list items in
this section are combined when the release notes are rendered, so the text
needs to be worded so that it does not depend on any information only
available in another section, such as the prelude. This may mean repeating
some details.
upgrade:
- |
List upgrade notes here, or remove this section. All of the list items in
this section are combined when the release notes are rendered, so the text
needs to be worded so that it does not depend on any information only
available in another section, such as the prelude. This may mean repeating
some details.
deprecations:
- |
List deprecations notes here, or remove this section. All of the list
items in this section are combined when the release notes are rendered, so
the text needs to be worded so that it does not depend on any information
only available in another section, such as the prelude. This may mean
repeating some details.
critical:
- |
Add critical notes here, or remove this section. All of the list items in
this section are combined when the release notes are rendered, so the text
needs to be worded so that it does not depend on any information only
available in another section, such as the prelude. This may mean repeating
some details.
security:
- |
Add security notes here, or remove this section. All of the list items in
this section are combined when the release notes are rendered, so the text
needs to be worded so that it does not depend on any information only
available in another section, such as the prelude. This may mean repeating
some details.
fixes:
- |
Add normal bug fixes here, or remove this section. All of the list items
in this section are combined when the release notes are rendered, so the
text needs to be worded so that it does not depend on any information only
available in another section, such as the prelude. This may mean repeating
some details.
other:
- |
Add other notes here, or remove this section. All of the list items in
this section are combined when the release notes are rendered, so the text
needs to be worded so that it does not depend on any information only
available in another section, such as the prelude. This may mean repeating
some details.
21 changes: 21 additions & 0 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,24 @@ async def test_apmtelemetry(
)
assert resp.status == 200
assert await resp.text() == "[]"


async def test_trace_new_session(
agent,
v04_reference_http_trace_payload_headers,
v04_reference_http_trace_payload_data,
):
resp = await agent.put(
"/v0.4/traces",
params={"test_session_token": "1"},
headers=v04_reference_http_trace_payload_headers,
data=v04_reference_http_trace_payload_data,
)
assert resp.status == 200, await resp.text()

resp = await agent.get("/test/session/clear", params={"test_session_token": "1"})
assert resp.status == 200

resp = await agent.get("/test/traces", params={"trace_ids": "123456"})
assert resp.status == 200
assert await resp.text() == "[[]]"
6 changes: 3 additions & 3 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ async def test_concurrent_session(
for token in ["test_case", "test_case2"]:
resp = await agent.get("/test/session/traces", params={"test_session_token": token})
assert resp.status == 200
assert await resp.json() == v04_reference_http_trace_payload_data_raw
result = await resp.json()
assert result == v04_reference_http_trace_payload_data_raw, result

resp = await agent.get("/test/session/traces")
assert resp.status == 200
Expand All @@ -69,8 +70,7 @@ async def test_concurrent_session(
assert resp.status == 200
for token in ["test_case", "test_case2"]:
resp = await agent.get("/test/session/traces", params={"test_session_token": token})
assert resp.status == 200
assert await resp.json() == []
assert resp.status == 404


async def test_two_sessions(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ async def test_snapshot_single_trace(
When the same trace is sent again
The snapshot should pass
""" # noqa: RST301
# Start a session
resp = await agent.get("/test/session/start", params={"test_session_token": "test_case"})
assert resp.status == 200
# Send a trace
resp = await do_reference_v04_http_trace(token="test_case")
assert resp.status == 200
Expand Down Expand Up @@ -176,6 +179,8 @@ async def test_snapshot_single_trace(
],
)
async def test_snapshot_trace_differences(agent, expected_traces, actual_traces, error):
resp = await agent.get("/test/session/start", params={"test_session_token": "test"})
assert resp.status == 200
resp = await v04_trace(agent, expected_traces, token="test")
assert resp.status == 200, await resp.text()

Expand Down Expand Up @@ -363,6 +368,8 @@ def test_generate_tracestats_snapshot(buckets, expected):


async def test_snapshot_custom_dir(agent, tmp_path, do_reference_v04_http_trace):
resp = await agent.get("/test/session/start", params={"test_session_token": "test_case"})
assert resp.status == 200
resp = await do_reference_v04_http_trace(token="test_case")
assert resp.status == 200

Expand All @@ -381,6 +388,8 @@ async def test_snapshot_custom_dir(agent, tmp_path, do_reference_v04_http_trace)


async def test_snapshot_custom_file(agent, tmp_path, do_reference_v04_http_trace):
resp = await agent.get("/test/session/start", params={"test_session_token": "test_case"})
assert resp.status == 200
resp = await do_reference_v04_http_trace(token="test_case")
assert resp.status == 200

Expand All @@ -401,6 +410,8 @@ async def test_snapshot_custom_file(agent, tmp_path, do_reference_v04_http_trace

@pytest.mark.parametrize("snapshot_ci_mode", [False, True])
async def test_snapshot_tracestats(agent, tmp_path, snapshot_ci_mode, do_reference_v06_http_stats, snapshot_dir):
resp = await agent.get("/test/session/start", params={"test_session_token": "test_case"})
assert resp.status == 200
resp = await do_reference_v06_http_stats(token="test_case")
assert resp.status == 200

Expand Down Expand Up @@ -430,6 +441,8 @@ async def test_snapshot_tracestats(agent, tmp_path, snapshot_ci_mode, do_referen

@pytest.mark.parametrize("snapshot_removed_attrs", [{"start", "duration"}])
async def test_removed_attributes(agent, tmp_path, snapshot_removed_attrs, do_reference_v04_http_trace):
resp = await agent.get("/test/session/start", params={"test_session_token": "test_case"})
assert resp.status == 200
resp = await do_reference_v04_http_trace(token="test_case")
assert resp.status == 200

Expand All @@ -454,6 +467,8 @@ async def test_removed_attributes(agent, tmp_path, snapshot_removed_attrs, do_re

@pytest.mark.parametrize("snapshot_removed_attrs", [{"metrics.process_id"}])
async def test_removed_attributes_metrics(agent, tmp_path, snapshot_removed_attrs, do_reference_v04_http_trace):
resp = await agent.get("/test/session/start", params={"test_session_token": "test_case"})
assert resp.status == 200
resp = await do_reference_v04_http_trace(token="test_case")
assert resp.status == 200

Expand Down Expand Up @@ -599,6 +614,8 @@ async def test_removed_attributes_metrics(agent, tmp_path, snapshot_removed_attr
],
)
async def test_snapshot_trace_differences_removed_start(agent, expected_traces, actual_traces, error):
resp = await agent.get("/test/session/start", params={"test_session_token": "test"})
assert resp.status == 200
resp = await v04_trace(agent, expected_traces, token="test")
assert resp.status == 200, await resp.text()

Expand Down
3 changes: 3 additions & 0 deletions tests/test_snapshot_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
If snapshots need to be (re)generated, set GENERATE_SNAPSHOTS=1
and run the tests as usual.
"""

import asyncio
import os
import subprocess
Expand Down Expand Up @@ -149,6 +150,7 @@ async def test_single_trace(
span.set_metric(k, v)
tracer.shutdown()
resp = await testagent.get("http://localhost:8126/test/session/snapshot?test_session_token=test_single_trace")

assert resp.status == response_code


Expand Down Expand Up @@ -298,6 +300,7 @@ async def test_tracestats(
do_traces: Callable[[Tracer], None],
fail: bool,
) -> None:
await testagent.get("http://localhost:8126/test/session/start?test_session_token=test_trace_stats")
do_traces(stats_tracer)
stats_tracer.shutdown() # force out the stats
resp = await testagent.get("http://localhost:8126/test/session/snapshot?test_session_token=test_trace_stats")
Expand Down

0 comments on commit 37782de

Please sign in to comment.