Skip to content

Commit ad7b328

Browse files
authored
Merge branch 'main' into hectorhdzg/aiohttpmetrics
2 parents 8ce86fc + 0db4d0b commit ad7b328

File tree

14 files changed

+198
-118
lines changed

14 files changed

+198
-118
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
1212
## Unreleased
1313

14+
### Fixed
15+
16+
- `opentelemetry-instrumentation-fastapi`: fix wrapping of middlewares
17+
([#3012](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3012))
18+
1419
### Breaking changes
1520

1621
- `opentelemetry-instrumentation-botocore` Use `cloud.region` instead of `aws.region` span attribute as per semantic conventions.
1722
([#3474](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3474))
18-
23+
- `opentelemetry-instrumentation-fastapi`: Drop support for FastAPI versions earlier than `0.92`
24+
([#3012](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3012))
1925

2026
### Added
2127
- `opentelemetry-instrumentation-aiohttp-client` Add support for HTTP metrics

RELEASING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ The workflow will create two pull requests, one against the `main` and one again
4141
* Press the "Run workflow" button, then select the release branch from the dropdown list,
4242
e.g. `release/v1.9.x`, then enter the pull request number that you want to backport,
4343
then click the "Run workflow" button below that.
44+
* Add the label `backport` to the generated pull request.
45+
* In case label automation doesn't work, just close and reopen the PR so that the workflow will take into account the label automation we have in place.
4446
* Review and merge the backport pull request that it generates.
4547
* Merge a pull request to the release branch updating the `CHANGELOG.md`.
4648
* The heading for the unreleased entries should be `## Unreleased`.
4749
* Run the [Prepare patch release workflow](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/workflows/prepare-patch-release.yml).
4850
* Press the "Run workflow" button, then select the release branch from the dropdown list,
4951
e.g. `release/v1.9.x`, and click the "Run workflow" button below that.
5052
* Review and merge the pull request that it creates for updating the version.
53+
* Note: If you are doing a patch release in `-contrib` repo, you should also do an equivalent patch release in `-core` repo (even if there's no fix to release), otherwise tests in CI will fail.
5154

5255
### Preparing a patch release for individual package
5356

docs/nitpick-exceptions.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ py-class=
4444
psycopg.Connection
4545
psycopg.AsyncConnection
4646
ObjectProxy
47+
fastapi.applications.FastAPI
4748

4849
any=
4950
; API

instrumentation/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
| --------------- | ------------------ | --------------- | -------------- |
44
| [opentelemetry-instrumentation-aio-pika](./opentelemetry-instrumentation-aio-pika) | aio_pika >= 7.2.0, < 10.0.0 | No | development
55
| [opentelemetry-instrumentation-aiohttp-client](./opentelemetry-instrumentation-aiohttp-client) | aiohttp ~= 3.0 | Yes | migration
6-
| [opentelemetry-instrumentation-aiohttp-server](./opentelemetry-instrumentation-aiohttp-server) | aiohttp ~= 3.0 | No | development
6+
| [opentelemetry-instrumentation-aiohttp-server](./opentelemetry-instrumentation-aiohttp-server) | aiohttp ~= 3.0 | Yes | development
77
| [opentelemetry-instrumentation-aiokafka](./opentelemetry-instrumentation-aiokafka) | aiokafka >= 0.8, < 1.0 | No | development
88
| [opentelemetry-instrumentation-aiopg](./opentelemetry-instrumentation-aiopg) | aiopg >= 0.13.0, < 2.0.0 | No | development
99
| [opentelemetry-instrumentation-asgi](./opentelemetry-instrumentation-asgi) | asgiref ~= 3.0 | Yes | migration
@@ -22,7 +22,7 @@
2222
| [opentelemetry-instrumentation-django](./opentelemetry-instrumentation-django) | django >= 1.10 | Yes | development
2323
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 6.0 | No | development
2424
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon >= 1.4.1, < 5.0.0 | Yes | migration
25-
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58 | Yes | migration
25+
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.92 | Yes | migration
2626
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0 | Yes | migration
2727
| [opentelemetry-instrumentation-grpc](./opentelemetry-instrumentation-grpc) | grpcio >= 1.42.0 | No | development
2828
| [opentelemetry-instrumentation-httpx](./opentelemetry-instrumentation-httpx) | httpx >= 0.18.0 | No | migration

instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/package.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414

1515

1616
_instruments = ("aiohttp ~= 3.0",)
17+
18+
_supports_metrics = True

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,9 @@ def __init__(
665665
# pylint: disable=too-many-statements
666666
async def __call__(
667667
self,
668-
scope: dict[str, Any],
669-
receive: Callable[[], Awaitable[dict[str, Any]]],
670-
send: Callable[[dict[str, Any]], Awaitable[None]],
668+
scope: typing.MutableMapping[str, Any],
669+
receive: Callable[[], Awaitable[typing.MutableMapping[str, Any]]],
670+
send: Callable[[typing.MutableMapping[str, Any]], Awaitable[None]],
671671
) -> None:
672672
"""The ASGI application
673673

instrumentation/opentelemetry-instrumentation-elasticsearch/tests/test_elasticsearch.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
ElasticsearchInstrumentor,
3232
)
3333
from opentelemetry.instrumentation.elasticsearch.utils import sanitize_body
34-
from opentelemetry.semconv.trace import SpanAttributes
34+
from opentelemetry.semconv._incubating.attributes.db_attributes import (
35+
DB_STATEMENT,
36+
DB_SYSTEM,
37+
)
3538
from opentelemetry.test.test_base import TestBase
3639
from opentelemetry.trace import StatusCode
3740

@@ -76,15 +79,15 @@ def get_elasticsearch_client(*args, **kwargs):
7679
)
7780
class TestElasticsearchIntegration(TestBase):
7881
search_attributes = {
79-
SpanAttributes.DB_SYSTEM: "elasticsearch",
82+
DB_SYSTEM: "elasticsearch",
8083
"elasticsearch.url": "/test-index/_search",
8184
"elasticsearch.method": helpers.dsl_search_method,
8285
"elasticsearch.target": "test-index",
83-
SpanAttributes.DB_STATEMENT: str({"query": {"bool": {"filter": "?"}}}),
86+
DB_STATEMENT: str({"query": {"bool": {"filter": "?"}}}),
8487
}
8588

8689
create_attributes = {
87-
SpanAttributes.DB_SYSTEM: "elasticsearch",
90+
DB_SYSTEM: "elasticsearch",
8891
"elasticsearch.url": "/test-index",
8992
"elasticsearch.method": "HEAD",
9093
}
@@ -361,13 +364,13 @@ def test_dsl_create(self, request_mock):
361364
)
362365

363366
attributes = {
364-
SpanAttributes.DB_SYSTEM: "elasticsearch",
367+
DB_SYSTEM: "elasticsearch",
365368
"elasticsearch.url": "/test-index",
366369
"elasticsearch.method": "PUT",
367370
}
368371
self.assertSpanHasAttributes(span2, attributes)
369372
self.assertEqual(
370-
literal_eval(span2.attributes[SpanAttributes.DB_STATEMENT]),
373+
literal_eval(span2.attributes[DB_STATEMENT]),
371374
helpers.dsl_create_statement,
372375
)
373376

@@ -408,13 +411,13 @@ def test_dsl_index(self, request_mock):
408411
span = spans[0]
409412
self.assertEqual(span.name, helpers.dsl_index_span_name)
410413
attributes = {
411-
SpanAttributes.DB_SYSTEM: "elasticsearch",
414+
DB_SYSTEM: "elasticsearch",
412415
"elasticsearch.url": helpers.dsl_index_url,
413416
"elasticsearch.method": "PUT",
414417
}
415418
self.assertSpanHasAttributes(span, attributes)
416419
self.assertEqual(
417-
literal_eval(span.attributes[SpanAttributes.DB_STATEMENT]),
420+
literal_eval(span.attributes[DB_STATEMENT]),
418421
{
419422
"body": "A few words here, a few words there",
420423
"title": "About searching",

instrumentation/opentelemetry-instrumentation-fastapi/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dependencies = [
3535

3636
[project.optional-dependencies]
3737
instruments = [
38-
"fastapi ~= 0.58",
38+
"fastapi ~= 0.92",
3939
]
4040

4141
[project.entry-points.opentelemetry_instrumentor]

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

Lines changed: 84 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,16 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
182182

183183
from __future__ import annotations
184184

185+
import functools
185186
import logging
187+
import types
186188
from typing import Collection, Literal
187189

188190
import fastapi
191+
from starlette.applications import Starlette
192+
from starlette.middleware.errors import ServerErrorMiddleware
189193
from starlette.routing import Match
194+
from starlette.types import ASGIApp
190195

191196
from opentelemetry.instrumentation._semconv import (
192197
_get_schema_url,
@@ -203,9 +208,9 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
203208
from opentelemetry.instrumentation.fastapi.package import _instruments
204209
from opentelemetry.instrumentation.fastapi.version import __version__
205210
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
206-
from opentelemetry.metrics import get_meter
211+
from opentelemetry.metrics import MeterProvider, get_meter
207212
from opentelemetry.semconv.attributes.http_attributes import HTTP_ROUTE
208-
from opentelemetry.trace import get_tracer
213+
from opentelemetry.trace import TracerProvider, get_tracer
209214
from opentelemetry.util.http import (
210215
get_excluded_urls,
211216
parse_excluded_urls,
@@ -226,13 +231,13 @@ class FastAPIInstrumentor(BaseInstrumentor):
226231

227232
@staticmethod
228233
def instrument_app(
229-
app,
234+
app: fastapi.FastAPI,
230235
server_request_hook: ServerRequestHook = None,
231236
client_request_hook: ClientRequestHook = None,
232237
client_response_hook: ClientResponseHook = None,
233-
tracer_provider=None,
234-
meter_provider=None,
235-
excluded_urls=None,
238+
tracer_provider: TracerProvider | None = None,
239+
meter_provider: MeterProvider | None = None,
240+
excluded_urls: str | None = None,
236241
http_capture_headers_server_request: list[str] | None = None,
237242
http_capture_headers_server_response: list[str] | None = None,
238243
http_capture_headers_sanitize_fields: list[str] | None = None,
@@ -284,21 +289,56 @@ def instrument_app(
284289
schema_url=_get_schema_url(sem_conv_opt_in_mode),
285290
)
286291

287-
app.add_middleware(
288-
OpenTelemetryMiddleware,
289-
excluded_urls=excluded_urls,
290-
default_span_details=_get_default_span_details,
291-
server_request_hook=server_request_hook,
292-
client_request_hook=client_request_hook,
293-
client_response_hook=client_response_hook,
294-
# Pass in tracer/meter to get __name__and __version__ of fastapi instrumentation
295-
tracer=tracer,
296-
meter=meter,
297-
http_capture_headers_server_request=http_capture_headers_server_request,
298-
http_capture_headers_server_response=http_capture_headers_server_response,
299-
http_capture_headers_sanitize_fields=http_capture_headers_sanitize_fields,
300-
exclude_spans=exclude_spans,
292+
# Instead of using `app.add_middleware` we monkey patch `build_middleware_stack` to insert our middleware
293+
# as the outermost middleware.
294+
# Otherwise `OpenTelemetryMiddleware` would have unhandled exceptions tearing through it and would not be able
295+
# to faithfully record what is returned to the client since it technically cannot know what `ServerErrorMiddleware` is going to do.
296+
297+
def build_middleware_stack(self: Starlette) -> ASGIApp:
298+
inner_server_error_middleware: ASGIApp = ( # type: ignore
299+
self._original_build_middleware_stack() # type: ignore
300+
)
301+
otel_middleware = OpenTelemetryMiddleware(
302+
inner_server_error_middleware,
303+
excluded_urls=excluded_urls,
304+
default_span_details=_get_default_span_details,
305+
server_request_hook=server_request_hook,
306+
client_request_hook=client_request_hook,
307+
client_response_hook=client_response_hook,
308+
# Pass in tracer/meter to get __name__and __version__ of fastapi instrumentation
309+
tracer=tracer,
310+
meter=meter,
311+
http_capture_headers_server_request=http_capture_headers_server_request,
312+
http_capture_headers_server_response=http_capture_headers_server_response,
313+
http_capture_headers_sanitize_fields=http_capture_headers_sanitize_fields,
314+
exclude_spans=exclude_spans,
315+
)
316+
# Wrap in an outer layer of ServerErrorMiddleware so that any exceptions raised in OpenTelemetryMiddleware
317+
# are handled.
318+
# This should not happen unless there is a bug in OpenTelemetryMiddleware, but if there is we don't want that
319+
# to impact the user's application just because we wrapped the middlewares in this order.
320+
if isinstance(
321+
inner_server_error_middleware, ServerErrorMiddleware
322+
): # usually true
323+
outer_server_error_middleware = ServerErrorMiddleware(
324+
app=otel_middleware,
325+
)
326+
else:
327+
# Something else seems to have patched things, or maybe Starlette changed.
328+
# Just create a default ServerErrorMiddleware.
329+
outer_server_error_middleware = ServerErrorMiddleware(
330+
app=otel_middleware
331+
)
332+
return outer_server_error_middleware
333+
334+
app._original_build_middleware_stack = app.build_middleware_stack
335+
app.build_middleware_stack = types.MethodType(
336+
functools.wraps(app.build_middleware_stack)(
337+
build_middleware_stack
338+
),
339+
app,
301340
)
341+
302342
app._is_instrumented_by_opentelemetry = True
303343
if app not in _InstrumentedFastAPI._instrumented_fastapi_apps:
304344
_InstrumentedFastAPI._instrumented_fastapi_apps.add(app)
@@ -309,11 +349,12 @@ def instrument_app(
309349

310350
@staticmethod
311351
def uninstrument_app(app: fastapi.FastAPI):
312-
app.user_middleware = [
313-
x
314-
for x in app.user_middleware
315-
if x.cls is not OpenTelemetryMiddleware
316-
]
352+
original_build_middleware_stack = getattr(
353+
app, "_original_build_middleware_stack", None
354+
)
355+
if original_build_middleware_stack:
356+
app.build_middleware_stack = original_build_middleware_stack
357+
del app._original_build_middleware_stack
317358
app.middleware_stack = app.build_middleware_stack()
318359
app._is_instrumented_by_opentelemetry = False
319360

@@ -341,12 +382,7 @@ def _instrument(self, **kwargs):
341382
_InstrumentedFastAPI._http_capture_headers_sanitize_fields = (
342383
kwargs.get("http_capture_headers_sanitize_fields")
343384
)
344-
_excluded_urls = kwargs.get("excluded_urls")
345-
_InstrumentedFastAPI._excluded_urls = (
346-
_excluded_urls_from_env
347-
if _excluded_urls is None
348-
else parse_excluded_urls(_excluded_urls)
349-
)
385+
_InstrumentedFastAPI._excluded_urls = kwargs.get("excluded_urls")
350386
_InstrumentedFastAPI._meter_provider = kwargs.get("meter_provider")
351387
_InstrumentedFastAPI._exclude_spans = kwargs.get("exclude_spans")
352388
fastapi.FastAPI = _InstrumentedFastAPI
@@ -365,43 +401,29 @@ class _InstrumentedFastAPI(fastapi.FastAPI):
365401
_server_request_hook: ServerRequestHook = None
366402
_client_request_hook: ClientRequestHook = None
367403
_client_response_hook: ClientResponseHook = None
404+
_http_capture_headers_server_request: list[str] | None = None
405+
_http_capture_headers_server_response: list[str] | None = None
406+
_http_capture_headers_sanitize_fields: list[str] | None = None
407+
_exclude_spans: list[Literal["receive", "send"]] | None = None
408+
368409
_instrumented_fastapi_apps = set()
369410
_sem_conv_opt_in_mode = _StabilityMode.DEFAULT
370411

371412
def __init__(self, *args, **kwargs):
372413
super().__init__(*args, **kwargs)
373-
tracer = get_tracer(
374-
__name__,
375-
__version__,
376-
_InstrumentedFastAPI._tracer_provider,
377-
schema_url=_get_schema_url(
378-
_InstrumentedFastAPI._sem_conv_opt_in_mode
379-
),
380-
)
381-
meter = get_meter(
382-
__name__,
383-
__version__,
384-
_InstrumentedFastAPI._meter_provider,
385-
schema_url=_get_schema_url(
386-
_InstrumentedFastAPI._sem_conv_opt_in_mode
387-
),
388-
)
389-
self.add_middleware(
390-
OpenTelemetryMiddleware,
391-
excluded_urls=_InstrumentedFastAPI._excluded_urls,
392-
default_span_details=_get_default_span_details,
393-
server_request_hook=_InstrumentedFastAPI._server_request_hook,
394-
client_request_hook=_InstrumentedFastAPI._client_request_hook,
395-
client_response_hook=_InstrumentedFastAPI._client_response_hook,
396-
# Pass in tracer/meter to get __name__and __version__ of fastapi instrumentation
397-
tracer=tracer,
398-
meter=meter,
399-
http_capture_headers_server_request=_InstrumentedFastAPI._http_capture_headers_server_request,
400-
http_capture_headers_server_response=_InstrumentedFastAPI._http_capture_headers_server_response,
401-
http_capture_headers_sanitize_fields=_InstrumentedFastAPI._http_capture_headers_sanitize_fields,
402-
exclude_spans=_InstrumentedFastAPI._exclude_spans,
414+
FastAPIInstrumentor.instrument_app(
415+
self,
416+
server_request_hook=self._server_request_hook,
417+
client_request_hook=self._client_request_hook,
418+
client_response_hook=self._client_response_hook,
419+
tracer_provider=self._tracer_provider,
420+
meter_provider=self._meter_provider,
421+
excluded_urls=self._excluded_urls,
422+
http_capture_headers_server_request=self._http_capture_headers_server_request,
423+
http_capture_headers_server_response=self._http_capture_headers_server_response,
424+
http_capture_headers_sanitize_fields=self._http_capture_headers_sanitize_fields,
425+
exclude_spans=self._exclude_spans,
403426
)
404-
self._is_instrumented_by_opentelemetry = True
405427
_InstrumentedFastAPI._instrumented_fastapi_apps.add(self)
406428

407429
def __del__(self):

instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515

16-
_instruments = ("fastapi ~= 0.58",)
16+
_instruments = ("fastapi ~= 0.92",)
1717

1818
_supports_metrics = True
1919

0 commit comments

Comments
 (0)