Skip to content

Commit be71c94

Browse files
committed
Update test_asgi_middleware.py
1 parent 755f41d commit be71c94

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -895,15 +895,18 @@ async def test_user_agent_synthetic_bot_detection(self):
895895
b"bingbot/1.0",
896896
]
897897

898-
for user_agent in test_cases:
898+
# Test each user agent case separately to avoid span accumulation
899+
for i, user_agent in enumerate(test_cases):
899900
with self.subTest(user_agent=user_agent):
900901
# Clear headers first
901902
self.scope["headers"] = []
902903

903-
def update_expected_synthetic_bot(expected, ua=user_agent):
904+
def update_expected_synthetic_bot(expected):
904905
expected[3]["attributes"].update(
905906
{
906-
SpanAttributes.HTTP_USER_AGENT: ua.decode("utf8"),
907+
SpanAttributes.HTTP_USER_AGENT: user_agent.decode(
908+
"utf8"
909+
),
907910
USER_AGENT_SYNTHETIC_TYPE: "bot",
908911
}
909912
)
@@ -918,6 +921,9 @@ def update_expected_synthetic_bot(expected, ua=user_agent):
918921
outputs, modifiers=[update_expected_synthetic_bot]
919922
)
920923

924+
# Clear spans after each test case to prevent accumulation
925+
self.memory_exporter.clear()
926+
921927
async def test_user_agent_synthetic_test_detection(self):
922928
"""Test that test user agents are detected as synthetic with type 'test'"""
923929
test_cases = [
@@ -926,15 +932,18 @@ async def test_user_agent_synthetic_test_detection(self):
926932
b"test-alwayson-client",
927933
]
928934

929-
for user_agent in test_cases:
935+
# Test each user agent case separately to avoid span accumulation
936+
for i, user_agent in enumerate(test_cases):
930937
with self.subTest(user_agent=user_agent):
931938
# Clear headers first
932939
self.scope["headers"] = []
933940

934-
def update_expected_synthetic_test(expected, ua=user_agent):
941+
def update_expected_synthetic_test(expected):
935942
expected[3]["attributes"].update(
936943
{
937-
SpanAttributes.HTTP_USER_AGENT: ua.decode("utf8"),
944+
SpanAttributes.HTTP_USER_AGENT: user_agent.decode(
945+
"utf8"
946+
),
938947
USER_AGENT_SYNTHETIC_TYPE: "test",
939948
}
940949
)
@@ -949,6 +958,9 @@ def update_expected_synthetic_test(expected, ua=user_agent):
949958
outputs, modifiers=[update_expected_synthetic_test]
950959
)
951960

961+
# Clear spans after each test case to prevent accumulation
962+
self.memory_exporter.clear()
963+
952964
async def test_user_agent_non_synthetic(self):
953965
"""Test that normal user agents are not marked as synthetic"""
954966
test_cases = [
@@ -958,16 +970,19 @@ async def test_user_agent_non_synthetic(self):
958970
b"curl/7.68.0",
959971
]
960972

961-
for user_agent in test_cases:
973+
# Test each user agent case separately to avoid span accumulation
974+
for i, user_agent in enumerate(test_cases):
962975
with self.subTest(user_agent=user_agent):
963976
# Clear headers first
964977
self.scope["headers"] = []
965978

966-
def update_expected_non_synthetic(expected, ua=user_agent):
979+
def update_expected_non_synthetic(expected):
967980
# Should only have the user agent, not synthetic type
968981
expected[3]["attributes"].update(
969982
{
970-
SpanAttributes.HTTP_USER_AGENT: ua.decode("utf8"),
983+
SpanAttributes.HTTP_USER_AGENT: user_agent.decode(
984+
"utf8"
985+
),
971986
}
972987
)
973988
return expected
@@ -981,6 +996,9 @@ def update_expected_non_synthetic(expected, ua=user_agent):
981996
outputs, modifiers=[update_expected_non_synthetic]
982997
)
983998

999+
# Clear spans after each test case to prevent accumulation
1000+
self.memory_exporter.clear()
1001+
9841002
async def test_user_agent_synthetic_new_semconv(self):
9851003
"""Test synthetic user agent detection with new semantic conventions"""
9861004
user_agent = b"Mozilla/5.0 (compatible; Googlebot/2.1)"

0 commit comments

Comments
 (0)