|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +# pylint: disable=too-many-lines |
| 16 | + |
15 | 17 | import sys
|
16 | 18 | import unittest
|
17 | 19 | from timeit import default_timer
|
@@ -626,6 +628,37 @@ def test_basic_metric_success(self):
|
626 | 628 | )
|
627 | 629 | self.assertEqual(point.value, 0)
|
628 | 630 |
|
| 631 | + def test_metric_target_attribute(self): |
| 632 | + expected_target = "/api/user/{id}" |
| 633 | + |
| 634 | + class TestRoute: |
| 635 | + path_format = expected_target |
| 636 | + |
| 637 | + self.scope["route"] = TestRoute() |
| 638 | + app = otel_asgi.OpenTelemetryMiddleware(simple_asgi) |
| 639 | + self.seed_app(app) |
| 640 | + self.send_default_request() |
| 641 | + |
| 642 | + metrics_list = self.memory_metrics_reader.get_metrics_data() |
| 643 | + assertions = 0 |
| 644 | + for resource_metric in metrics_list.resource_metrics: |
| 645 | + for scope_metrics in resource_metric.scope_metrics: |
| 646 | + for metric in scope_metrics.metrics: |
| 647 | + for point in metric.data.data_points: |
| 648 | + if isinstance(point, HistogramDataPoint): |
| 649 | + self.assertEqual( |
| 650 | + point.attributes["http.target"], |
| 651 | + expected_target, |
| 652 | + ) |
| 653 | + assertions += 1 |
| 654 | + elif isinstance(point, NumberDataPoint): |
| 655 | + self.assertEqual( |
| 656 | + point.attributes["http.target"], |
| 657 | + expected_target, |
| 658 | + ) |
| 659 | + assertions += 1 |
| 660 | + self.assertEqual(assertions, 2) |
| 661 | + |
629 | 662 | def test_no_metric_for_websockets(self):
|
630 | 663 | self.scope = {
|
631 | 664 | "type": "websocket",
|
@@ -719,6 +752,37 @@ def test_credential_removal(self):
|
719 | 752 | attrs[SpanAttributes.HTTP_URL], "http://httpbin.org/status/200"
|
720 | 753 | )
|
721 | 754 |
|
| 755 | + def test_collect_target_attribute_missing(self): |
| 756 | + self.assertIsNone(otel_asgi._collect_target_attribute(self.scope)) |
| 757 | + |
| 758 | + def test_collect_target_attribute_fastapi(self): |
| 759 | + class TestRoute: |
| 760 | + path_format = "/api/users/{user_id}" |
| 761 | + |
| 762 | + self.scope["route"] = TestRoute() |
| 763 | + self.assertEqual( |
| 764 | + otel_asgi._collect_target_attribute(self.scope), |
| 765 | + "/api/users/{user_id}", |
| 766 | + ) |
| 767 | + |
| 768 | + def test_collect_target_attribute_fastapi_mounted(self): |
| 769 | + class TestRoute: |
| 770 | + path_format = "/users/{user_id}" |
| 771 | + |
| 772 | + self.scope["route"] = TestRoute() |
| 773 | + self.scope["root_path"] = "/api/v2" |
| 774 | + self.assertEqual( |
| 775 | + otel_asgi._collect_target_attribute(self.scope), |
| 776 | + "/api/v2/users/{user_id}", |
| 777 | + ) |
| 778 | + |
| 779 | + def test_collect_target_attribute_fastapi_starlette_invalid(self): |
| 780 | + self.scope["route"] = object() |
| 781 | + self.assertIsNone( |
| 782 | + otel_asgi._collect_target_attribute(self.scope), |
| 783 | + "HTTP_TARGET values is not None", |
| 784 | + ) |
| 785 | + |
722 | 786 |
|
723 | 787 | class TestWrappedApplication(AsgiTestBase):
|
724 | 788 | def test_mark_span_internal_in_presence_of_span_from_other_framework(self):
|
|
0 commit comments