Skip to content

Commit 24c046e

Browse files
Add snapshot commands (#96)
Add commands to start and perform snapshot test cases to ease manual/CI usage. The alternative would be to curl and try to parse the results which isn't trivial to do. Co-authored-by: Brett Langdon <me@brett.is>
1 parent b75aac7 commit 24c046e

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

ddapm_test_agent/cmd.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import argparse
2+
import os
3+
import sys
4+
5+
import requests
6+
import yarl
7+
8+
9+
def _add_token_arg(parser: argparse.ArgumentParser) -> None:
10+
"""Add the test session token argument to the parser."""
11+
parser.add_argument(
12+
"--test-session-token",
13+
type=str,
14+
default=os.environ.get("TEST_SESSION_TOKEN"),
15+
help="Test session token to query with.",
16+
)
17+
18+
19+
def _add_agent_url_arg(parser: argparse.ArgumentParser) -> None:
20+
"""Add the agent url argument to the given parser."""
21+
parser.add_argument(
22+
"--agent-url",
23+
type=str,
24+
default=os.environ.get(
25+
"DD_TRACE_AGENT_URL",
26+
os.environ.get("DD_AGENT_URL", "http://localhost:8126"),
27+
),
28+
help=("Test agent URL. Default is http://localhost:8126"),
29+
)
30+
31+
32+
def main_session_start() -> None:
33+
"""Entrypoint for the start-session command"""
34+
parser = argparse.ArgumentParser(
35+
description=(
36+
"Start a test agent session with a given token. "
37+
"All data submitted with this token will be associated with this session."
38+
),
39+
prog="ddapm-test-agent-start-session",
40+
)
41+
_add_agent_url_arg(parser)
42+
_add_token_arg(parser)
43+
parsed_args = parser.parse_args(sys.argv[1:])
44+
url = yarl.URL(parsed_args.agent_url).with_path("/test/session/start")
45+
resp = requests.get(
46+
str(url), params={"test_session_token": parsed_args.test_session_token}
47+
)
48+
if resp.status_code != 200:
49+
print(resp.text)
50+
sys.exit(1)
51+
print(resp.text)
52+
sys.exit(0)
53+
54+
55+
def main_snapshot() -> None:
56+
"""Entrypoint for the snapshot command"""
57+
parser = argparse.ArgumentParser(
58+
description=("Perform a snapshot test for the data received in the session."),
59+
prog="ddapm-test-agent-snapshot",
60+
)
61+
_add_agent_url_arg(parser)
62+
_add_token_arg(parser)
63+
parsed_args = parser.parse_args(sys.argv[1:])
64+
if not parsed_args.test_session_token:
65+
print(
66+
"Error: a test token is required! Please specify one with --test-session-token"
67+
" command line argument or the TEST_SESSION_TOKEN environment variable."
68+
)
69+
sys.exit(1)
70+
url = yarl.URL(parsed_args.agent_url).with_path("/test/session/snapshot")
71+
resp = requests.get(
72+
str(url), params={"test_session_token": parsed_args.test_session_token}
73+
)
74+
if resp.status_code != 200:
75+
print(resp.text)
76+
sys.exit(1)
77+
print(resp.text)
78+
sys.exit(0)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add ``ddapm-test-agent-session-start`` and ``ddapm-test-agent-snapshot`` commands
5+
to provide a more convenient way to perform snapshots manually or in CI.

riotfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
pkgs={
5858
"mypy": latest,
5959
"pytest": latest,
60-
"types-setuptools": latest,
6160
"types-protobuf": latest,
61+
"types-requests": latest,
62+
"types-setuptools": latest,
6263
},
6364
),
6465
Venv(

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"aiohttp",
3131
"ddsketch",
3232
"msgpack",
33+
"requests",
3334
"typing_extensions",
35+
"yarl",
3436
],
3537
tests_require=testing_deps,
3638
setup_requires=["setuptools_scm"],
@@ -39,6 +41,8 @@
3941
"console_scripts": [
4042
"ddapm-test-agent=ddapm_test_agent.agent:main",
4143
"ddapm-test-agent-fmt=ddapm_test_agent.fmt:main",
44+
"ddapm-test-agent-session-start=ddapm_test_agent.cmd:main_session_start",
45+
"ddapm-test-agent-snapshot=ddapm_test_agent.cmd:main_snapshot",
4246
]
4347
},
4448
extras_require={

tests/test_snapshot_integration.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,41 @@ async def test_tracestats(
321321
assert resp.status == 400
322322
else:
323323
assert resp.status == 200
324+
325+
326+
async def test_cmd(testagent: aiohttp.ClientSession, tracer: Tracer) -> None:
327+
"""Test the commands provided with the library.
328+
329+
Note that this test reuses the trace/snapshot from test_single_trace above.
330+
"""
331+
env = os.environ.copy()
332+
p = subprocess.run(
333+
["ddapm-test-agent-session-start", "--test-session-token=test_single_trace"],
334+
env=env,
335+
)
336+
assert p.returncode == 0
337+
with tracer.trace(
338+
"root", service="custom_service", resource="/url/endpoint", span_type="web"
339+
):
340+
pass
341+
tracer.flush()
342+
p = subprocess.run(
343+
["ddapm-test-agent-snapshot", "--test-session-token=test_single_trace"], env=env
344+
)
345+
assert p.returncode == 0
346+
347+
# Ensure failing snapshots work.
348+
p = subprocess.run(
349+
["ddapm-test-agent-session-start", "--test-session-token=test_single_trace"],
350+
env=env,
351+
)
352+
assert p.returncode == 0
353+
with tracer.trace(
354+
"root1234", service="custom_service", resource="/url/endpoint", span_type="web"
355+
):
356+
pass
357+
tracer.shutdown()
358+
p = subprocess.run(
359+
["ddapm-test-agent-snapshot", "--test-session-token=test_single_trace"], env=env
360+
)
361+
assert p.returncode == 1

0 commit comments

Comments
 (0)