Skip to content

Commit 7184154

Browse files
mergify[bot]Kyle-Verhoogbrettlangdon
authored
fix(ci): wait for traces to be sent in snapshot tests (#4374) (#4384)
Add a wait_for_traces argument to snapshots to poll the test agent until the correct number of traces are available. In particular httpx has been flaky. (cherry picked from commit 4e710ca) Co-authored-by: Kyle Verhoog <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent c2d7ab4 commit 7184154

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

tests/contrib/httpx/test_httpx.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ def test_configure_service_name_env():
169169

170170
async def test():
171171
token = "tests.contrib.httpx.test_httpx.test_configure_service_name_env"
172-
with snapshot_context(token=token):
172+
with snapshot_context(wait_for_num_traces=1, token=token):
173173
DEFAULT_HEADERS = {
174174
"User-Agent": "python-httpx/x.xx.x",
175175
}
176176
httpx.get(url, headers=DEFAULT_HEADERS)
177177

178-
with snapshot_context(token=token):
178+
with snapshot_context(wait_for_num_traces=1, token=token):
179179
async with httpx.AsyncClient() as client:
180180
DEFAULT_HEADERS = {
181181
"User-Agent": "python-httpx/x.xx.x",
@@ -209,13 +209,13 @@ def test_configure_global_service_name_env():
209209

210210
async def test():
211211
token = "tests.contrib.httpx.test_httpx.test_configure_global_service_name_env"
212-
with snapshot_context(token=token):
212+
with snapshot_context(wait_for_num_traces=1, token=token):
213213
DEFAULT_HEADERS = {
214214
"User-Agent": "python-httpx/x.xx.x",
215215
}
216216
httpx.get(url, headers=DEFAULT_HEADERS)
217217

218-
with snapshot_context(token=token):
218+
with snapshot_context(wait_for_num_traces=1, token=token):
219219
async with httpx.AsyncClient() as client:
220220
await client.get(url, headers=DEFAULT_HEADERS)
221221

@@ -232,11 +232,11 @@ async def test_get_500(snapshot_context):
232232
We mark the span as an error
233233
"""
234234
url = get_url("/status/500")
235-
with snapshot_context():
235+
with snapshot_context(wait_for_num_traces=1):
236236
resp = httpx.get(url, headers=DEFAULT_HEADERS)
237237
assert resp.status_code == 500
238238

239-
with snapshot_context():
239+
with snapshot_context(wait_for_num_traces=1):
240240
async with httpx.AsyncClient() as client:
241241
resp = await client.get(url, headers=DEFAULT_HEADERS)
242242
assert resp.status_code == 500
@@ -245,17 +245,17 @@ async def test_get_500(snapshot_context):
245245
@pytest.mark.asyncio
246246
async def test_split_by_domain(snapshot_context):
247247
"""
248-
When split_by_domain is configure
248+
When split_by_domain is configured
249249
We set the service name to the <host>:<port>
250250
"""
251251
url = get_url("/status/200")
252252

253253
with override_config("httpx", {"split_by_domain": True}):
254-
with snapshot_context():
254+
with snapshot_context(wait_for_num_traces=1):
255255
resp = httpx.get(url, headers=DEFAULT_HEADERS)
256256
assert resp.status_code == 200
257257

258-
with snapshot_context():
258+
with snapshot_context(wait_for_num_traces=1):
259259
async with httpx.AsyncClient() as client:
260260
resp = await client.get(url, headers=DEFAULT_HEADERS)
261261
assert resp.status_code == 200
@@ -275,11 +275,11 @@ async def test_trace_query_string(snapshot_context):
275275
"User-Agent": "python-httpx/x.xx.x",
276276
}
277277
with override_http_config("httpx", {"trace_query_string": True}):
278-
with snapshot_context():
278+
with snapshot_context(wait_for_num_traces=1):
279279
resp = httpx.get(url, headers=headers)
280280
assert resp.status_code == 200
281281

282-
with snapshot_context():
282+
with snapshot_context(wait_for_num_traces=1):
283283
async with httpx.AsyncClient() as client:
284284
resp = await client.get(url, headers=headers)
285285
assert resp.status_code == 200
@@ -300,11 +300,11 @@ async def test_request_headers(snapshot_context):
300300

301301
try:
302302
config.httpx.http.trace_headers(["Some-Request-Header", "Some-Response-Header"])
303-
with snapshot_context():
303+
with snapshot_context(wait_for_num_traces=1):
304304
resp = httpx.get(url, headers=headers)
305305
assert resp.status_code == 200
306306

307-
with snapshot_context():
307+
with snapshot_context(wait_for_num_traces=1):
308308
async with httpx.AsyncClient() as client:
309309
resp = await client.get(url, headers=headers)
310310
assert resp.status_code == 200

tests/utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import contextlib
22
from contextlib import contextmanager
33
import inspect
4+
import json
45
import os
56
import subprocess
67
import sys
8+
import time
79
from typing import List
810

911
import attr
@@ -854,7 +856,7 @@ def clear(self):
854856

855857

856858
@contextmanager
857-
def snapshot_context(token, ignores=None, tracer=None, async_mode=True, variants=None):
859+
def snapshot_context(token, ignores=None, tracer=None, async_mode=True, variants=None, wait_for_num_traces=None):
858860
# Use variant that applies to update test token. One must apply. If none
859861
# apply, the test should have been marked as skipped.
860862
if variants:
@@ -910,6 +912,27 @@ def snapshot_context(token, ignores=None, tracer=None, async_mode=True, variants
910912
del tracer._writer._headers["X-Datadog-Test-Session-Token"]
911913
del os.environ["_DD_TRACE_WRITER_ADDITIONAL_HEADERS"]
912914

915+
conn = httplib.HTTPConnection(parsed.hostname, parsed.port)
916+
917+
# Wait for the traces to be available
918+
if wait_for_num_traces is not None:
919+
traces = []
920+
for i in range(20):
921+
try:
922+
conn.request("GET", "/test/session/traces?test_session_token=%s" % token)
923+
r = conn.getresponse()
924+
if r.status == 200:
925+
traces = json.loads(r.read())
926+
if len(traces) == wait_for_num_traces:
927+
break
928+
except Exception:
929+
pass
930+
time.sleep(0.1)
931+
else:
932+
pytest.fail(
933+
"Expected %r trace(s), got %r:\n%s" % (wait_for_num_traces, len(traces), traces), pytrace=False
934+
)
935+
913936
# Query for the results of the test.
914937
conn = httplib.HTTPConnection(parsed.hostname, parsed.port)
915938
conn.request("GET", "/test/session/snapshot?ignores=%s&test_session_token=%s" % (",".join(ignores), token))

0 commit comments

Comments
 (0)