Skip to content

Commit 9bd0030

Browse files
authored
Merge branch 'main' into rken-asgi-header-decoding
2 parents 1180e58 + d135f20 commit 9bd0030

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- `opentelemetry-instrumentation-fastapi` Add autoinstrumentation mechanism tests.
11+
([#2860](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2860))
12+
1013
## Version 1.27.0/0.48b0 ()
1114

1215
### Added

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

+57-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
import unittest
1818
from timeit import default_timer
19-
from unittest.mock import patch
19+
from unittest.mock import Mock, patch
2020

2121
import fastapi
2222
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
2323
from fastapi.responses import JSONResponse
2424
from fastapi.testclient import TestClient
25+
from pkg_resources import DistributionNotFound, iter_entry_points
2526

2627
import opentelemetry.instrumentation.fastapi as otel_fastapi
2728
from opentelemetry import trace
@@ -34,6 +35,9 @@
3435
_server_duration_attrs_old,
3536
)
3637
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
38+
from opentelemetry.instrumentation.auto_instrumentation._load import (
39+
_load_instrumentors,
40+
)
3741
from opentelemetry.sdk.metrics.export import (
3842
HistogramDataPoint,
3943
NumberDataPoint,
@@ -1024,13 +1028,65 @@ def client_response_hook(send_span, scope, message):
10241028
)
10251029

10261030

1031+
def get_distribution_with_fastapi(*args, **kwargs):
1032+
dist = args[0]
1033+
if dist == "fastapi~=0.58":
1034+
# Value does not matter. Only whether an exception is thrown
1035+
return None
1036+
raise DistributionNotFound()
1037+
1038+
1039+
def get_distribution_without_fastapi(*args, **kwargs):
1040+
raise DistributionNotFound()
1041+
1042+
10271043
class TestAutoInstrumentation(TestBaseAutoFastAPI):
10281044
"""Test the auto-instrumented variant
10291045
10301046
Extending the manual instrumentation as most test cases apply
10311047
to both.
10321048
"""
10331049

1050+
def test_entry_point_exists(self):
1051+
eps = iter_entry_points("opentelemetry_instrumentor")
1052+
ep = next(eps)
1053+
self.assertEqual(ep.dist.key, "opentelemetry-instrumentation-fastapi")
1054+
self.assertEqual(
1055+
ep.module_name, "opentelemetry.instrumentation.fastapi"
1056+
)
1057+
self.assertEqual(ep.attrs, ("FastAPIInstrumentor",))
1058+
self.assertEqual(ep.name, "fastapi")
1059+
self.assertIsNone(next(eps, None))
1060+
1061+
@patch("opentelemetry.instrumentation.dependencies.get_distribution")
1062+
def test_instruments_with_fastapi_installed(self, mock_get_distribution):
1063+
mock_get_distribution.side_effect = get_distribution_with_fastapi
1064+
mock_distro = Mock()
1065+
_load_instrumentors(mock_distro)
1066+
mock_get_distribution.assert_called_once_with("fastapi~=0.58")
1067+
self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1)
1068+
args = mock_distro.load_instrumentor.call_args.args
1069+
ep = args[0]
1070+
self.assertEqual(ep.dist.key, "opentelemetry-instrumentation-fastapi")
1071+
self.assertEqual(
1072+
ep.module_name, "opentelemetry.instrumentation.fastapi"
1073+
)
1074+
self.assertEqual(ep.attrs, ("FastAPIInstrumentor",))
1075+
self.assertEqual(ep.name, "fastapi")
1076+
1077+
@patch("opentelemetry.instrumentation.dependencies.get_distribution")
1078+
def test_instruments_without_fastapi_installed(
1079+
self, mock_get_distribution
1080+
):
1081+
mock_get_distribution.side_effect = get_distribution_without_fastapi
1082+
mock_distro = Mock()
1083+
_load_instrumentors(mock_distro)
1084+
mock_get_distribution.assert_called_once_with("fastapi~=0.58")
1085+
with self.assertRaises(DistributionNotFound):
1086+
mock_get_distribution("fastapi~=0.58")
1087+
self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 0)
1088+
mock_distro.load_instrumentor.assert_not_called()
1089+
10341090
def _create_app(self):
10351091
# instrumentation is handled by the instrument call
10361092
resource = Resource.create({"key1": "value1", "key2": "value2"})

0 commit comments

Comments
 (0)