Skip to content

ISSUE-3317: Remove maximum version constraint, increase minimum version on starlette by fixing unit tests #3456

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-[asynclick/click]` Add missing opentelemetry-instrumentation dep
([#3447](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3447))
- `opentelemetry-instrumentation-botocore` Capture server attributes for botocore API calls
([#3448](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3448))
([#3448](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3448
- `opentelemetry-instrumentation-starlette` Remove max version constraint on starlete, increase min version ([#3456](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3456))
Comment on lines +32 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
([#3448](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3448
- `opentelemetry-instrumentation-starlette` Remove max version constraint on starlete, increase min version ([#3456](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3456))
([#3448](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3448))
- `opentelemetry-instrumentation-starlette` Bump starlette supported version to 0.37.2
([#3456](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3456))

Is there a reason we picked 0.37.2?


## Version 1.32.0/0.53b0 (2025-04-10)

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
| [opentelemetry-instrumentation-requests](./opentelemetry-instrumentation-requests) | requests ~= 2.0 | Yes | migration
| [opentelemetry-instrumentation-sqlalchemy](./opentelemetry-instrumentation-sqlalchemy) | sqlalchemy >= 1.0.0, < 2.1.0 | Yes | development
| [opentelemetry-instrumentation-sqlite3](./opentelemetry-instrumentation-sqlite3) | sqlite3 | No | development
| [opentelemetry-instrumentation-starlette](./opentelemetry-instrumentation-starlette) | starlette >= 0.13, <0.15 | Yes | development
| [opentelemetry-instrumentation-starlette](./opentelemetry-instrumentation-starlette) | starlette >= 0.37.2 | Yes | development
| [opentelemetry-instrumentation-system-metrics](./opentelemetry-instrumentation-system-metrics) | psutil >= 5 | No | development
| [opentelemetry-instrumentation-threading](./opentelemetry-instrumentation-threading) | threading | No | development
| [opentelemetry-instrumentation-tornado](./opentelemetry-instrumentation-tornado) | tornado >= 5.1.1 | Yes | development
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = [
]

[project.optional-dependencies]
instruments = ["starlette >= 0.13, <0.15"]
instruments = ["starlette >= 0.37.2"]

[project.entry-points.opentelemetry_instrumentor]
starlette = "opentelemetry.instrumentation.starlette:StarletteInstrumentor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def __init__(self, *args: Any, **kwargs: Any):
_InstrumentedStarlette._instrumented_starlette_apps.add(self)

def __del__(self):
_InstrumentedStarlette._instrumented_starlette_apps.remove(self)
_InstrumentedStarlette._instrumented_starlette_apps.discard(self)


def _get_route_details(scope: dict[str, Any]) -> str | None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.


_instruments = ("starlette >= 0.13, <0.15",)
_instruments = ("starlette >= 0.37.2",)

_supports_metrics = True
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
# opentelemetry-instrumentation-asgi
# opentelemetry-instrumentation-starlette
anyio==4.5.2 ; python_full_version < '3.9'
# via httpx
# via
# httpx
# starlette
anyio==4.8.0 ; python_full_version >= '3.9'
# via httpx
# via
# httpx
# starlette
asgiref==3.8.1
# via opentelemetry-instrumentation-asgi
certifi==2025.1.31
Expand Down Expand Up @@ -72,14 +76,17 @@ requests==2.32.3
# -r instrumentation/opentelemetry-instrumentation-starlette/test-requirements.in
sniffio==1.3.1
# via anyio
starlette==0.14.2
starlette==0.44.0 ; python_full_version < '3.9'
# via opentelemetry-instrumentation-starlette
starlette==0.46.2 ; python_full_version >= '3.9'
# via opentelemetry-instrumentation-starlette
tomli==2.2.1 ; python_full_version < '3.11'
# via pytest
typing-extensions==4.12.2 ; python_full_version < '3.13'
# via
# anyio
# asgiref
# starlette
urllib3==2.2.3 ; python_full_version < '3.9'
# via requests
urllib3==2.3.0 ; python_full_version >= '3.9'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,37 @@
# opentelemetry-instrumentation-asgi
# opentelemetry-instrumentation-starlette
anyio==4.5.2 ; python_full_version < '3.9'
# via httpx
anyio==4.8.0 ; python_full_version >= '3.9'
# via httpx
asgiref==3.8.1
# via
# httpx
# starlette
anyio==4.9.0 ; python_full_version >= '3.9'
# via
# httpx
# starlette
asgiref==3.0.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you downgrading this?

Copy link
Author

@MattiasDC MattiasDC May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All changes to oldest.txt and latest.txt were obtained by running the 'uv pip compile' commands which were listed on top of the respective files. Should I manually adjust them or should the pip compile command be changed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What command did you run?

Copy link
Author

@MattiasDC MattiasDC May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using uv 0.6.14:

# uv pip compile --python 3.8 --universal --resolution lowest-direct --no-emit-package opentelemetry-api --no-emit-package opentelemetry-semantic-conventions -c dev-requirements.txt instrumentation/opentelemetry-instrumentation-starlette/test-requirements.in -o instrumentation/opentelemetry-instrumentation-starlette/test-requirements.oldest.txt

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if a package version in oldest.txt was manually increased, uv pip compile will leave it higher than it's own resolution.. You can check this if you omit the -o option

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can for example see that here, 3.0 is the minimum allowed:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aabmass, do you remember manually changing things after running the uv pip compile ?

# via opentelemetry-instrumentation-asgi
certifi==2025.1.31
async-timeout==3.0.1
# via asgiref
certifi==2025.4.26
# via
# httpcore
# httpx
# requests
charset-normalizer==3.4.1
charset-normalizer==3.4.2
# via requests
colorama==0.4.6 ; sys_platform == 'win32'
# via pytest
deprecated==1.2.14
deprecated==1.2.18
# via
# opentelemetry-api
# opentelemetry-semantic-conventions
exceptiongroup==1.2.2 ; python_full_version < '3.11'
# via
# anyio
# pytest
h11==0.14.0
h11==0.16.0
# via httpcore
httpcore==1.0.7
httpcore==1.0.9
# via httpx
httpx==0.28.0
# via -r instrumentation/opentelemetry-instrumentation-starlette/test-requirements.in
Expand All @@ -54,14 +60,18 @@ importlib-metadata==8.5.0 ; python_full_version < '3.9'
# via opentelemetry-api
importlib-metadata==8.6.1 ; python_full_version >= '3.9'
# via opentelemetry-api
iniconfig==2.0.0
iniconfig==2.1.0
# via pytest
packaging==24.0
packaging==18.0
# via
# opentelemetry-instrumentation
# pytest
pluggy==1.5.0
# via pytest
pyparsing==3.1.4 ; python_full_version < '3.9'
# via packaging
pyparsing==3.2.3 ; python_full_version >= '3.9'
# via packaging
pytest==7.4.4
# via
# -c dev-requirements.txt
Expand All @@ -70,21 +80,23 @@ requests==2.32.3
# via
# -c dev-requirements.txt
# -r instrumentation/opentelemetry-instrumentation-starlette/test-requirements.in
six==1.17.0
# via packaging
sniffio==1.3.1
# via anyio
starlette==0.13.8
starlette==0.37.2
# via opentelemetry-instrumentation-starlette
tomli==2.2.1 ; python_full_version < '3.11'
# via pytest
typing-extensions==4.12.2 ; python_full_version < '3.13'
typing-extensions==4.13.2 ; python_full_version < '3.13'
# via
# anyio
# asgiref
# starlette
urllib3==2.2.3 ; python_full_version < '3.9'
# via requests
urllib3==2.3.0 ; python_full_version >= '3.9'
urllib3==2.4.0 ; python_full_version >= '3.9'
# via requests
wrapt==1.16.0
wrapt==1.10.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was supposed to not work on this version and raise an error like:
ImportError: cannot import name 'getargspec' from 'inspect'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is failing with that error (as was expected?). I got these new versions of oldest.txt and latest.txt by using the 'uv pip compile' commands in the respective files. Should these commands be updated, or should I update the '.in' file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emdneto did you manually change the version of wrapt in oldest.txt, because for me uv pip compile (without-ooption) resolves to 1.10.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MattiasDC could you please change the ".in" file to

# Used to generate test-requirements.{oldest,latest}.txt with uv pip compile
pytest~=7.4.4
requests~=2.32
httpx~=0.28
wrapt~=1.14.0
-e opentelemetry-instrumentation
-e instrumentation/opentelemetry-instrumentation-asgi
-e util/opentelemetry-util-http
-e instrumentation/opentelemetry-instrumentation-starlette[instruments]

and run uv pip compile command again?

# via
# deprecated
# opentelemetry-instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from starlette import applications
from starlette.responses import PlainTextResponse
from starlette.routing import Mount, Route
from starlette.routing import Mount, Route, WebSocketRoute
from starlette.testclient import TestClient
from starlette.websockets import WebSocket

Expand Down Expand Up @@ -621,10 +621,7 @@ def tearDown(self) -> None:

@staticmethod
def create_starlette_app():
app = applications.Starlette()

@app.route("/foobar")
def _(request):
def foobar(request):
return PlainTextResponse(
content="hi",
headers={
Expand All @@ -636,8 +633,7 @@ def _(request):
},
)

@app.websocket_route("/foobar_web")
async def _(websocket: WebSocket) -> None:
async def foobar_web(websocket: WebSocket) -> None:
message = await websocket.receive()
if message.get("type") == "websocket.connect":
await websocket.send(
Expand All @@ -663,20 +659,30 @@ async def _(websocket: WebSocket) -> None:
if message.get("type") == "websocket.disconnect":
pass

return app
return applications.Starlette(
routes=[
Route("/foobar", foobar),
WebSocketRoute("/foobar_web", foobar_web),
]
)


class TestHTTPAppWithCustomHeaders(TestBaseWithCustomHeaders):
@patch.dict(
"os.environ",
{
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS: ".*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,Regex-Test-Header-.*,Regex-Invalid-Test-Header-.*,.*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,my-custom-regex-header-.*,invalid-regex-header-.*,.*my-secret.*",
},
)
def setUp(self) -> None:
def setUp(self):
super().setUp()
self.test_env_patch = patch.dict(
"os.environ",
{
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS: ".*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,Regex-Test-Header-.*,Regex-Invalid-Test-Header-.*,.*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,my-custom-regex-header-.*,invalid-regex-header-.*,.*my-secret.*",
},
)
self.test_env_patch.start()

def tearDown(self):
self.test_env_patch.stop()
super().tearDown()

def test_custom_request_headers_in_span_attributes(self):
expected = {
Expand Down Expand Up @@ -793,17 +799,22 @@ def test_custom_response_headers_not_in_span_attributes(self):


class TestWebSocketAppWithCustomHeaders(TestBaseWithCustomHeaders):
@patch.dict(
"os.environ",
{
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS: ".*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,Regex-Test-Header-.*,Regex-Invalid-Test-Header-.*,.*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,my-custom-regex-header-.*,invalid-regex-header-.*,.*my-secret.*",
},
)
def setUp(self) -> None:
def setUp(self):
self.test_env_patch = patch.dict(
"os.environ",
{
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS: ".*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,Regex-Test-Header-.*,Regex-Invalid-Test-Header-.*,.*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,my-custom-regex-header-.*,invalid-regex-header-.*,.*my-secret.*",
},
)
self.test_env_patch.start()
super().setUp()

def tearDown(self):
super().tearDown()
self.test_env_patch.stop()

def test_custom_request_headers_in_span_attributes(self):
expected = {
"http.request.header.custom_test_header_1": (
Expand Down Expand Up @@ -918,23 +929,29 @@ def test_custom_response_headers_not_in_span_attributes(self):
self.assertNotIn(key, server_span.attributes)


@patch.dict(
"os.environ",
{
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS: ".*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,Regex-Test-Header-.*,Regex-Invalid-Test-Header-.*,.*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,my-custom-regex-header-.*,invalid-regex-header-.*,.*my-secret.*",
},
)
class TestNonRecordingSpanWithCustomHeaders(TestBaseWithCustomHeaders):
def setUp(self):
super().setUp()
self.test_env_patch = patch.dict(
"os.environ",
{
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS: ".*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,Regex-Test-Header-.*,Regex-Invalid-Test-Header-.*,.*my-secret.*",
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3,my-custom-regex-header-.*,invalid-regex-header-.*,.*my-secret.*",
},
)
self.test_env_patch.start()

reset_trace_globals()
set_tracer_provider(tracer_provider=NoOpTracerProvider())

self._app = self.create_app()
self._client = TestClient(self._app)

def tearDown(self):
self.test_env_patch.stop()
super().tearDown()

def test_custom_header_not_present_in_non_recording_span(self):
resp = self._client.get(
"/foobar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"instrumentation": "opentelemetry-instrumentation-sqlalchemy==0.54b0.dev",
},
{
"library": "starlette >= 0.13, <0.15",
"library": "starlette >= 0.37.2",
"instrumentation": "opentelemetry-instrumentation-starlette==0.54b0.dev",
},
{
Expand Down
Loading
Loading