Skip to content

Commit 286d68c

Browse files
committed
Revert "add check if the app is starlette app"
This reverts commit 65d1818.
1 parent 65d1818 commit 286d68c

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_starlette_patches.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from logging import Logger, getLogger
55
from typing import Collection
66

7-
from starlette import applications
8-
97
from amazon.opentelemetry.distro._utils import is_agent_observability_enabled
108

119
_logger: Logger = getLogger(__name__)
@@ -33,20 +31,17 @@ def patched_instrumentation_dependencies(self) -> Collection[str]:
3331
# Apply the patch
3432
StarletteInstrumentor.instrumentation_dependencies = patched_instrumentation_dependencies
3533

36-
# Patch to exclude http receive/send ASGI event spans from Bedrock AgentCore Starlette applications,
34+
# Patch to exclude http receive/send ASGI event spans from Bedrock AgentCore,
3735
# this Middleware instrumentation is injected internally by Starlette Instrumentor, see:
3836
# https://github.com/open-telemetry/opentelemetry-python-contrib/blob/51da0a766e5d3cbc746189e10c9573163198cfcd/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py#L573
3937
if is_agent_observability_enabled():
4038
original_init = OpenTelemetryMiddleware.__init__
4139

4240
def patched_init(self, app, **kwargs):
4341
original_init(self, app, **kwargs)
44-
# See: https://github.com/encode/starlette/blob/cb8f84f5284dc301ca7a31eb732b9e140769dd48/starlette/applications.py#L77-L78
45-
is_starlette_app = hasattr(app, "user_middleware") and hasattr(app, "middleware_stack")
46-
47-
# Only apply exclude_spans for Starlette apps
48-
if is_starlette_app and hasattr(self, "exclude_receive_span") and hasattr(self, "exclude_send_span"):
42+
if hasattr(self, "exclude_receive_span"):
4943
self.exclude_receive_span = True
44+
if hasattr(self, "exclude_send_span"):
5045
self.exclude_send_span = True
5146

5247
OpenTelemetryMiddleware.__init__ = patched_init

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/patches/test_starlette_patches.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
from unittest import TestCase
3+
import unittest
44
from unittest.mock import MagicMock, patch
55

66
from amazon.opentelemetry.distro.patches._starlette_patches import _apply_starlette_instrumentation_patches
77

88

9-
class TestStarlettePatch(TestCase):
9+
class TestStarlettePatch(unittest.TestCase):
1010
"""Test the Starlette instrumentation patches."""
1111

1212
@patch("amazon.opentelemetry.distro.patches._starlette_patches._logger")
1313
def test_starlette_patch_applied_successfully(self, mock_logger):
1414
"""Test that the Starlette instrumentation patch is applied successfully."""
15-
for patched_starlette_enabled in [True, False]:
16-
with self.subTest(agent_enabled=patched_starlette_enabled):
17-
with patch.dict(
18-
"os.environ", {"AGENT_OBSERVABILITY_ENABLED": "true" if patched_starlette_enabled else "false"}
19-
):
15+
for agent_enabled in [True, False]:
16+
with self.subTest(agent_enabled=agent_enabled):
17+
with patch.dict("os.environ", {"AGENT_OBSERVABILITY_ENABLED": "true" if agent_enabled else "false"}):
2018
# Create a mock StarletteInstrumentor class
2119
mock_instrumentor_class = MagicMock()
2220
mock_instrumentor_class.__name__ = "StarletteInstrumentor"
@@ -54,18 +52,13 @@ def __init__(self, app, **kwargs):
5452
result = mock_instrumentor_class.instrumentation_dependencies(mock_instance)
5553
self.assertEqual(result, ("starlette >= 0.13",))
5654

57-
mock_app = MagicMock()
58-
if patched_starlette_enabled:
59-
mock_app.user_middleware = []
60-
mock_app.middleware_stack = None
61-
6255
mock_middleware_instance = MagicMock()
6356
mock_middleware_instance.exclude_receive_span = False
6457
mock_middleware_instance.exclude_send_span = False
65-
mock_middleware_class.__init__(mock_middleware_instance, mock_app)
58+
mock_middleware_class.__init__(mock_middleware_instance, "app")
6659

6760
# Test middleware patching sets exclude flags
68-
if patched_starlette_enabled:
61+
if agent_enabled:
6962
self.assertTrue(mock_middleware_instance.exclude_receive_span)
7063
self.assertTrue(mock_middleware_instance.exclude_send_span)
7164
else:

0 commit comments

Comments
 (0)