Skip to content

Commit

Permalink
fix: fastapi integration tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
therightstuff committed Oct 12, 2023
1 parent eaa1573 commit 6a05ffc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 31 deletions.
10 changes: 10 additions & 0 deletions src/test/integration/fastapi/start_uvicorn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import uvicorn


def serve():
"""Serve the web application."""
uvicorn.run("app:app", port=8000)


if __name__ == "__main__":
serve()
5 changes: 1 addition & 4 deletions src/test/integration/fastapi/tests/app_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ def __init__(self):
"OTEL_SERVICE_NAME": "fastapi_test_app",
}
print(f"env = {env}")
venv_bin_path = Path(sys.executable).parent
print(f"venv_bin_path = {venv_bin_path}")
cmd = f". {venv_bin_path}/activate; uvicorn app:app --port 8000 &"
cmd = [sys.executable, "start_uvicorn.py"]
print(f"cmd = {cmd}")
self.process = subprocess.Popen(
cmd,
cwd=cwd,
env=env,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
Expand Down
93 changes: 66 additions & 27 deletions src/test/integration/fastapi/tests/test_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from test.test_utils.span_exporter import wait_for_exporter
from test.test_utils.spans_parser import SpansContainer

import requests
Expand Down Expand Up @@ -53,32 +52,54 @@ def test_requests_instrumentation(self):

self.assertIn("https://api.chucknorris.io/jokes/", body["url"])

wait_for_exporter()

spans_container = SpansContainer.get_spans_from_file()
spans_container = SpansContainer.get_spans_from_file(
wait_time_sec=10, expected_span_count=4
)
self.assertEqual(4, len(spans_container.spans))

# assert root
root = spans_container.get_first_root()
self.assertIsNotNone(root)
self.assertEqual(root["kind"], "SpanKind.SERVER")
self.assertEqual(root["attributes"]["http.status_code"], 200)
self.assertIsNotNone(root["attributes"]["http.request.headers"])
self.assertEqual(
root["attributes"]["http.url"], "http://127.0.0.1:8000/invoke-requests"
)

# assert child spans
children = spans_container.get_non_internal_children()
self.assertEqual(1, len(children))
self.assertEqual(children[0]["attributes"]["http.method"], "GET")
# assert external request span
non_internal_children = spans_container.get_non_internal_children()
self.assertEqual(1, len(non_internal_children))
external_request_span = non_internal_children[0]
self.assertEqual(external_request_span["attributes"]["http.method"], "GET")
self.assertEqual(
children[0]["attributes"]["http.url"],
external_request_span["attributes"]["http.url"],
"https://api.chucknorris.io/jokes/random",
)
self.assertEqual(children[0]["attributes"]["http.status_code"], 200)
self.assertIsNotNone(children[0]["attributes"]["http.request.headers"])
self.assertIsNotNone(children[0]["attributes"]["http.response.headers"])
self.assertIsNotNone(children[0]["attributes"]["http.response.body"])
self.assertEqual(
external_request_span["attributes"]["http.status_code"], 200
)

# assert internal spans
internals = spans_container.get_internals()
self.assertEqual(2, len(internals))
# assert than either of the internal spans have the required attributes
self.assertIsNotNone(
spans_container.get_attribute_from_list_of_spans(
internals, "http.response.headers"
)
)
self.assertIsNotNone(
spans_container.get_attribute_from_list_of_spans(
internals, "http.response.body"
)
)
self.assertEqual(
spans_container.get_attribute_from_list_of_spans(
internals, "http.status_code"
),
200,
)

def test_large_span_attribute_size_default_max_size(self):
with FastApiSample():
Expand All @@ -91,9 +112,9 @@ def test_large_span_attribute_size_default_max_size(self):

assert body is not None

wait_for_exporter()

spans_container = SpansContainer.get_spans_from_file()
spans_container = SpansContainer.get_spans_from_file(
wait_time_sec=10, expected_span_count=4
)
self.assertEqual(4, len(spans_container.spans))

# assert root
Expand All @@ -107,18 +128,36 @@ def test_large_span_attribute_size_default_max_size(self):
)
self.assertEqual(root_attributes["http.method"], "GET")

# assert child spans
children = spans_container.get_non_internal_children()
self.assertEqual(1, len(children))
children_attributes = children[0]["attributes"]
self.assertEqual(children_attributes["http.method"], "GET")
# assert external request span
non_internal_children = spans_container.get_non_internal_children()
self.assertEqual(1, len(non_internal_children))
external_request_span = non_internal_children[0]
self.assertEqual(external_request_span["attributes"]["http.method"], "GET")
self.assertEqual(
children_attributes["http.url"],
external_request_span["attributes"]["http.url"],
"http://universities.hipolabs.com/search?country=United+States",
)
self.assertEqual(
external_request_span["attributes"]["http.status_code"], 200
)

self.assertEqual(len(children_attributes["http.response.body"]), 2048)
self.assertEqual(children_attributes["http.status_code"], 200)
self.assertIsNotNone(children_attributes["http.request.headers"])
self.assertIsNotNone(children_attributes["http.response.headers"])
self.assertIsNotNone(children_attributes["http.response.body"])
# assert internal spans
internals = spans_container.get_internals()
self.assertEqual(2, len(internals))
# assert than either of the internal spans have the required attributes
self.assertIsNotNone(
spans_container.get_attribute_from_list_of_spans(
internals, "http.response.headers"
)
)
http_response_body_attr = spans_container.get_attribute_from_list_of_spans(
internals, "http.response.body"
)
self.assertIsNotNone(http_response_body_attr)
self.assertEqual(len(http_response_body_attr), 2048)
self.assertEqual(
spans_container.get_attribute_from_list_of_spans(
internals, "http.status_code"
),
200,
)

0 comments on commit 6a05ffc

Please sign in to comment.