Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UnboundLocalError local variable 'start' referenced before assignment #1889

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- `opentelemetry-instrumentation-asgi` Fix UnboundLocalError local variable 'start' referenced before assignment
([#1889](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1889))

## Version 1.19.0/0.40b0 (2023-07-13)
- `opentelemetry-instrumentation-asgi` Add `http.server.request.size` metric
([#1867](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1867))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ async def __call__(self, scope, receive, send):
receive: An awaitable callable yielding dictionaries
send: An awaitable callable taking a single dictionary as argument.
"""
start = default_timer()
if scope["type"] not in ("http", "websocket"):
return await self.app(scope, receive, send)

Expand Down Expand Up @@ -591,7 +592,6 @@ async def __call__(self, scope, receive, send):
send,
duration_attrs,
)
start = default_timer()

await self.app(scope, otel_receive, otel_send)
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# pylint: disable=too-many-lines

import asyncio
import sys
import unittest
from timeit import default_timer
Expand Down Expand Up @@ -796,5 +797,38 @@ async def wrapped_app(scope, receive, send):
)


class TestAsgiApplicationRaisingError(AsgiTestBase):
def tearDown(self):
pass

@mock.patch(
"opentelemetry.instrumentation.asgi.collect_custom_request_headers_attributes",
side_effect=ValueError("whatever"),
)
def test_asgi_issue_1883(
self, mock_collect_custom_request_headers_attributes
):
"""
Test that exception UnboundLocalError local variable 'start' referenced before assignment is not raised
See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1883
"""
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
self.seed_app(app)
self.send_default_request()
try:
asyncio.get_event_loop().run_until_complete(
self.communicator.stop()
)
except ValueError as exc_info:
self.assertEqual(exc_info.args[0], "whatever")
except Exception as exc_info: # pylint: disable=W0703
self.fail(
"expecting ValueError('whatever'), received instead: "
+ str(exc_info)
)
else:
self.fail("expecting ValueError('whatever')")


if __name__ == "__main__":
unittest.main()
Loading