Skip to content

Commit 8d812e4

Browse files
committed
Add verification for omission of supportability metrics.
1 parent aeead9f commit 8d812e4

File tree

1 file changed

+59
-33
lines changed

1 file changed

+59
-33
lines changed

tests/agent_unittests/test_http_client.py

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,27 @@ def test_http_no_payload(server, method):
188188
assert foo_header == "bar"
189189

190190

191-
def test_non_ok_response(server):
191+
@pytest.mark.parametrize("client_cls", (HttpClient, ApplicationModeClient))
192+
def test_non_ok_response(client_cls, server):
192193
internal_metrics = CustomMetrics()
193194

194-
with ApplicationModeClient(
195+
with client_cls(
195196
"localhost", server.port, disable_certificate_validation=True
196197
) as client:
197198
with InternalTraceContext(internal_metrics):
198199
status, _ = client.send_request(method="PUT")
199200

200201
assert status != 200
201-
assert dict(internal_metrics.metrics()) == {
202-
"Supportability/Python/Collector/Failures": [1, 0, 0, 0, 0, 0],
203-
"Supportability/Python/Collector/Failures/direct": [1, 0, 0, 0, 0, 0],
204-
"Supportability/Python/Collector/HTTPError/%d" % status: [1, 0, 0, 0, 0, 0],
205-
}
202+
internal_metrics = dict(internal_metrics.metrics())
203+
204+
if client_cls is ApplicationModeClient:
205+
assert internal_metrics == {
206+
"Supportability/Python/Collector/Failures": [1, 0, 0, 0, 0, 0],
207+
"Supportability/Python/Collector/Failures/direct": [1, 0, 0, 0, 0, 0],
208+
"Supportability/Python/Collector/HTTPError/%d" % status: [1, 0, 0, 0, 0, 0],
209+
}
210+
else:
211+
assert not internal_metrics
206212

207213

208214
def test_http_close_connection(server):
@@ -226,14 +232,23 @@ def test_http_close_connection_in_context_manager():
226232
client.close_connection()
227233

228234

229-
@pytest.mark.parametrize("method", ("gzip", "deflate"))
230-
@pytest.mark.parametrize("threshold", (0, 100))
231-
def test_http_payload_compression(server, method, threshold):
235+
@pytest.mark.parametrize(
236+
"client_cls,method,threshold",
237+
(
238+
(HttpClient, "gzip", 0),
239+
(HttpClient, "gzip", 100),
240+
(ApplicationModeClient, "gzip", 0),
241+
(ApplicationModeClient, "gzip", 100),
242+
(ApplicationModeClient, "deflate", 0),
243+
(ApplicationModeClient, "deflate", 100),
244+
),
245+
)
246+
def test_http_payload_compression(server, client_cls, method, threshold):
232247
payload = b"*" * 20
233248

234249
internal_metrics = CustomMetrics()
235250

236-
with ApplicationModeClient(
251+
with client_cls(
237252
"localhost",
238253
server.port,
239254
disable_certificate_validation=True,
@@ -251,28 +266,39 @@ def test_http_payload_compression(server, method, threshold):
251266
payload_byte_len = len(sent_payload)
252267

253268
internal_metrics = dict(internal_metrics.metrics())
254-
assert internal_metrics["Supportability/Python/Collector/Output/Bytes/test"][
255-
:2
256-
] == [1, payload_byte_len,]
269+
if client_cls is ApplicationModeClient:
270+
assert internal_metrics["Supportability/Python/Collector/Output/Bytes/test"][
271+
:2
272+
] == [1, payload_byte_len,]
257273

258-
if threshold < 20:
259-
assert len(internal_metrics) == 3
274+
if threshold < 20:
275+
# Verify compression time is recorded
276+
assert (
277+
internal_metrics["Supportability/Python/Collector/ZLIB/Compress/test"][
278+
0
279+
]
280+
== 1
281+
)
282+
assert (
283+
internal_metrics["Supportability/Python/Collector/ZLIB/Compress/test"][
284+
1
285+
]
286+
> 0
287+
)
260288

261-
# Verify compression time is recorded
262-
assert (
263-
internal_metrics["Supportability/Python/Collector/ZLIB/Compress/test"][0]
264-
== 1
265-
)
266-
assert (
267-
internal_metrics["Supportability/Python/Collector/ZLIB/Compress/test"][1]
268-
> 0
269-
)
289+
# Verify the original payload length is recorded
290+
assert internal_metrics["Supportability/Python/Collector/ZLIB/Bytes/test"][
291+
:2
292+
] == [1, len(payload)]
270293

271-
# Verify the original payload length is recorded
272-
assert internal_metrics["Supportability/Python/Collector/ZLIB/Bytes/test"][
273-
:2
274-
] == [1, len(payload)]
294+
assert len(internal_metrics) == 3
295+
else:
296+
# Verify no ZLIB compression metrics were sent
297+
assert len(internal_metrics) == 1
298+
else:
299+
assert not internal_metrics
275300

301+
if threshold < 20:
276302
expected_content_encoding = method.encode("utf-8")
277303
assert sent_payload != payload
278304
if method == "deflate":
@@ -284,9 +310,6 @@ def test_http_payload_compression(server, method, threshold):
284310
else:
285311
expected_content_encoding = b"Identity"
286312

287-
# Verify no ZLIB compression metrics were sent
288-
assert len(internal_metrics) == 1
289-
290313
for header in data[1:-1]:
291314
if header.lower().startswith(b"content-encoding"):
292315
_, content_encoding = header.split(b":", 1)
@@ -544,6 +567,7 @@ def test_serverless_mode_client():
544567
(
545568
(HttpClient, "localhost", False),
546569
(HttpClient, None, False),
570+
(HttpClient, None, True),
547571
(ApplicationModeClient, None, True),
548572
(ApplicationModeClient, "localhost", True),
549573
(DeveloperModeClient, None, False),
@@ -581,7 +605,7 @@ def test_audit_logging(server, insecure_server, client_cls, proxy_host, exceptio
581605
exc = callable_name(type(e.args[0]))
582606

583607
internal_metrics = dict(internal_metrics.metrics())
584-
if exception:
608+
if exception and client_cls is ApplicationModeClient:
585609
if proxy_host:
586610
connection = "https-proxy"
587611
else:
@@ -592,6 +616,8 @@ def test_audit_logging(server, insecure_server, client_cls, proxy_host, exceptio
592616
% connection: [1, 0, 0, 0, 0, 0],
593617
"Supportability/Python/Collector/Exception/%s" % exc: [1, 0, 0, 0, 0, 0],
594618
}
619+
else:
620+
assert not internal_metrics
595621

596622
# Verify the audit log isn't empty
597623
assert audit_log_fp.tell()

0 commit comments

Comments
 (0)