diff --git a/src/test/components/tests/test_attr_max_size.py b/src/test/components/tests/test_attr_max_size.py index 6741b5e1..260ca702 100644 --- a/src/test/components/tests/test_attr_max_size.py +++ b/src/test/components/tests/test_attr_max_size.py @@ -1,5 +1,5 @@ -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -14,8 +14,7 @@ def test_large_span_attribute_size_max_size_env_var_was_set(self): assert body is not None - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(4, len(spans_container.spans)) diff --git a/src/test/components/tests/test_execution_tags.py b/src/test/components/tests/test_execution_tags.py index 4b8b78fc..e76df7b2 100644 --- a/src/test/components/tests/test_execution_tags.py +++ b/src/test/components/tests/test_execution_tags.py @@ -1,5 +1,5 @@ -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -14,8 +14,7 @@ def test_execution_tag(self): assert body is not None - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(4, len(spans_container.spans)) diff --git a/src/test/integration/boto3/tests/test_boto3.py b/src/test/integration/boto3/tests/test_boto3.py index 332db067..83c33f68 100644 --- a/src/test/integration/boto3/tests/test_boto3.py +++ b/src/test/integration/boto3/tests/test_boto3.py @@ -1,6 +1,6 @@ import json -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -35,8 +35,7 @@ def test_boto3_instrumentation(self): self.assertEqual(body, {"status": "ok"}) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(4, len(spans_container.spans)) diff --git a/src/test/integration/fastapi/tests/test_fastapi.py b/src/test/integration/fastapi/tests/test_fastapi.py index 0ca33ab5..c160a2a9 100644 --- a/src/test/integration/fastapi/tests/test_fastapi.py +++ b/src/test/integration/fastapi/tests/test_fastapi.py @@ -1,5 +1,5 @@ -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -14,8 +14,7 @@ def test_200_OK(self): self.assertEqual(body, {"message": "Hello FastAPI!"}) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(3, len(spans_container.spans)) @@ -50,8 +49,7 @@ def test_requests_instrumentation(self): self.assertIn("https://api.chucknorris.io/jokes/", body["url"]) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(4, len(spans_container.spans)) @@ -86,8 +84,7 @@ def test_large_span_attribute_size_default_max_size(self): assert body is not None - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(4, len(spans_container.spans)) diff --git a/src/test/integration/flask/tests/test_flask.py b/src/test/integration/flask/tests/test_flask.py index cb8afe8e..975ab88e 100644 --- a/src/test/integration/flask/tests/test_flask.py +++ b/src/test/integration/flask/tests/test_flask.py @@ -1,5 +1,5 @@ -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -14,8 +14,7 @@ def test_200_OK(self): self.assertEqual(body, {"message": "Hello Flask!"}) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(1, len(spans_container.spans)) @@ -37,8 +36,7 @@ def test_requests_instrumentation(self): self.assertIn("https://api.chucknorris.io/jokes/", body["url"]) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() self.assertEqual(2, len(spans_container.spans)) diff --git a/src/test/integration/grpcio/tests/test_grpcio.py b/src/test/integration/grpcio/tests/test_grpcio.py index 5037e445..bce80593 100644 --- a/src/test/integration/grpcio/tests/test_grpcio.py +++ b/src/test/integration/grpcio/tests/test_grpcio.py @@ -1,7 +1,7 @@ -import time -import unittest -import sys import os +import sys +import unittest +from test.test_utils.span_exporter import wait_for_exporter sys.path.append(os.path.join(os.path.dirname(__file__), "..", "app")) @@ -20,8 +20,7 @@ def tearDownClass(cls) -> None: stub.SayHelloUnaryUnary(helloworld_pb2.HelloRequest(name="exit")) def check_spans(self, method: str, request_payload: str, response_payload: str): - # TODO Do something deterministic - time.sleep(5) # allow the exporter to catch up + wait_for_exporter(5) server_file = os.getenv("SERVER_SPANDUMP") server_spans = SpansContainer.get_spans_from_file(server_file) diff --git a/src/test/integration/kafka_python/tests/test_kafka_python.py b/src/test/integration/kafka_python/tests/test_kafka_python.py index 1e2accb3..c3bf71e1 100644 --- a/src/test/integration/kafka_python/tests/test_kafka_python.py +++ b/src/test/integration/kafka_python/tests/test_kafka_python.py @@ -1,6 +1,6 @@ import json -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -43,8 +43,7 @@ def test_kafka_python_instrumentation(self): self.assertEqual(body, {"status": "ok"}) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() diff --git a/src/test/integration/pika/app/__init__.py b/src/test/integration/pika/app/__init__.py index 7180864d..6be1fefe 100644 --- a/src/test/integration/pika/app/__init__.py +++ b/src/test/integration/pika/app/__init__.py @@ -46,6 +46,7 @@ async def invoke_pika_consumer(request: Request): except Exception as e: print(f"Exception while consuming messages: {e}") channel.stop_consuming() + raise e return {"status": "ok"} diff --git a/src/test/integration/pika/tests/test_pika.py b/src/test/integration/pika/tests/test_pika.py index 5494792f..be13c46d 100644 --- a/src/test/integration/pika/tests/test_pika.py +++ b/src/test/integration/pika/tests/test_pika.py @@ -1,6 +1,6 @@ import json -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -48,8 +48,7 @@ def test_pika_instrumentation(self): self.assertEqual(body, {"status": "ok"}) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() diff --git a/src/test/integration/pymongo/tests/test_pymongo.py b/src/test/integration/pymongo/tests/test_pymongo.py index fea95b5e..074ce166 100644 --- a/src/test/integration/pymongo/tests/test_pymongo.py +++ b/src/test/integration/pymongo/tests/test_pymongo.py @@ -1,5 +1,5 @@ -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -15,8 +15,7 @@ def test_mongo_instrumentation(self): self.assertEqual(body, {"status": "ok"}) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() diff --git a/src/test/integration/pymysql/tests/test_pymysql.py b/src/test/integration/pymysql/tests/test_pymysql.py index 3ba8a950..b2575dee 100644 --- a/src/test/integration/pymysql/tests/test_pymysql.py +++ b/src/test/integration/pymysql/tests/test_pymysql.py @@ -1,5 +1,5 @@ -import time import unittest +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer import requests @@ -15,8 +15,7 @@ def test_pymysql_instrumentation(self): self.assertEqual(body, {"status": "ok"}) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + wait_for_exporter() spans_container = SpansContainer.get_spans_from_file() diff --git a/src/test/integration/redis/tests/test_redis.py b/src/test/integration/redis/tests/test_redis.py index 7e0b5eb9..0a637780 100644 --- a/src/test/integration/redis/tests/test_redis.py +++ b/src/test/integration/redis/tests/test_redis.py @@ -1,12 +1,12 @@ import os +import subprocess import sys -import time import unittest -from testcontainers.redis import RedisContainer -import subprocess - +from test.test_utils.span_exporter import wait_for_exporter from test.test_utils.spans_parser import SpansContainer +from testcontainers.redis import RedisContainer + class TestRedisSpans(unittest.TestCase): def test_redis_instrumentation(self): @@ -25,8 +25,9 @@ def test_redis_instrumentation(self): "OTEL_SERVICE_NAME": "app", }, ) - # TODO Do something deterministic - time.sleep(3) # Sleep to allow the exporter to catch up + + wait_for_exporter() + spans_container = SpansContainer.parse_spans_from_file() self.assertGreaterEqual(len(spans_container.spans), 2) diff --git a/src/test/test_utils/span_exporter.py b/src/test/test_utils/span_exporter.py new file mode 100644 index 00000000..0907c77b --- /dev/null +++ b/src/test/test_utils/span_exporter.py @@ -0,0 +1,8 @@ +import time + + +def wait_for_exporter(wait_time_sec: int = 3): + """Wait for the exporter to have collected all the spans.""" + + # TODO Do something deterministic + time.sleep(wait_time_sec) # Sleep to allow the exporter to catch up