Skip to content

Commit 1beab82

Browse files
Fix UnboundLocalError local variable 'start' referenced before assignment (#1889)
Co-authored-by: Pablo Collins <[email protected]>
1 parent 7603a1f commit 1beab82

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

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

88
## Unreleased
99

10+
### Fixed
11+
12+
- `opentelemetry-instrumentation-asgi` Fix UnboundLocalError local variable 'start' referenced before assignment
13+
([#1889](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1889))
14+
1015
## Version 1.19.0/0.40b0 (2023-07-13)
1116
- `opentelemetry-instrumentation-asgi` Add `http.server.request.size` metric
1217
([#1867](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1867))

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ async def __call__(self, scope, receive, send):
538538
receive: An awaitable callable yielding dictionaries
539539
send: An awaitable callable taking a single dictionary as argument.
540540
"""
541+
start = default_timer()
541542
if scope["type"] not in ("http", "websocket"):
542543
return await self.app(scope, receive, send)
543544

@@ -591,7 +592,6 @@ async def __call__(self, scope, receive, send):
591592
send,
592593
duration_attrs,
593594
)
594-
start = default_timer()
595595

596596
await self.app(scope, otel_receive, otel_send)
597597
finally:

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

+34
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# pylint: disable=too-many-lines
1616

17+
import asyncio
1718
import sys
1819
import unittest
1920
from timeit import default_timer
@@ -796,5 +797,38 @@ async def wrapped_app(scope, receive, send):
796797
)
797798

798799

800+
class TestAsgiApplicationRaisingError(AsgiTestBase):
801+
def tearDown(self):
802+
pass
803+
804+
@mock.patch(
805+
"opentelemetry.instrumentation.asgi.collect_custom_request_headers_attributes",
806+
side_effect=ValueError("whatever"),
807+
)
808+
def test_asgi_issue_1883(
809+
self, mock_collect_custom_request_headers_attributes
810+
):
811+
"""
812+
Test that exception UnboundLocalError local variable 'start' referenced before assignment is not raised
813+
See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1883
814+
"""
815+
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
816+
self.seed_app(app)
817+
self.send_default_request()
818+
try:
819+
asyncio.get_event_loop().run_until_complete(
820+
self.communicator.stop()
821+
)
822+
except ValueError as exc_info:
823+
self.assertEqual(exc_info.args[0], "whatever")
824+
except Exception as exc_info: # pylint: disable=W0703
825+
self.fail(
826+
"expecting ValueError('whatever'), received instead: "
827+
+ str(exc_info)
828+
)
829+
else:
830+
self.fail("expecting ValueError('whatever')")
831+
832+
799833
if __name__ == "__main__":
800834
unittest.main()

0 commit comments

Comments
 (0)