diff --git a/CHANGELOG.md b/CHANGELOG.md index 97e3808..55e0100 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ([#253](https://github.com/microsoft/ApplicationInsights-Python/pull/253)) - Use entrypoints instead of importlib to load instrumentations ([#254](https://github.com/microsoft/ApplicationInsights-Python/pull/254)) +- Add support for FastAPI instrumentation + ([#255](https://github.com/microsoft/ApplicationInsights-Python/pull/255)) ## [1.0.0b10](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b10) - 2023-02-23 diff --git a/azure-monitor-opentelemetry/README.md b/azure-monitor-opentelemetry/README.md index e6fb75c..85b5be0 100644 --- a/azure-monitor-opentelemetry/README.md +++ b/azure-monitor-opentelemetry/README.md @@ -12,6 +12,7 @@ The following OpenTelemetry instrumentations come bundled in with the Azure moni * [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests] * [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django] +* [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi] * [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask] * [OpenTelemetry Psycopg2 Instrumentation][opentelemetry_instrumentation_psycopg2] @@ -118,6 +119,7 @@ Samples are available [here][samples] to demonstrate how to utilize the above co [ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views [opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests [opentelemetry_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django +[opentelemetry_instrumentation_fastapi]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-fastapi [opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask [opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2 [opentelemetry_spec_resource]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk diff --git a/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py b/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py index c17163d..1c50ce5 100644 --- a/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -36,6 +36,7 @@ _logger = getLogger(__name__) _INSTRUMENTATION_CONFIG_SUFFIX = "_config" _SUPPORTED_INSTRUMENTED_LIBRARIES = ( "django", + "fastapi", "flask", "psycopg2", "requests", diff --git a/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py b/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py new file mode 100644 index 0000000..112f1ac --- /dev/null +++ b/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py @@ -0,0 +1,30 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- +import fastapi +from azure.monitor.opentelemetry import configure_azure_monitor + +# Configure Azure monitor collection telemetry pipeline +configure_azure_monitor( + connection_string="", + disable_logging=True, + disable_metrics=True, + fastapi_config={"excluded_urls": "http://127.0.0.1:8000/exclude"}, + tracing_export_interval_millis=15000, +) + +app = fastapi.FastAPI() + + +# Requests made to fastapi endpoints will be automatically captured +@app.get("/") +async def root(): + return {"message": "Hello World"} + + +# Telemetry from this endpoint will not be captured due to excluded_urls config above +@app.get("/exclude") +async def root(): + return {"message": "Telemetry was not captured"} diff --git a/azure-monitor-opentelemetry/setup.py b/azure-monitor-opentelemetry/setup.py index 0c886b1..36b84f7 100644 --- a/azure-monitor-opentelemetry/setup.py +++ b/azure-monitor-opentelemetry/setup.py @@ -87,9 +87,10 @@ setup( "azure-monitor-opentelemetry-exporter>=1.0.0b12", "opentelemetry-instrumentation~=0.36b0", "opentelemetry-instrumentation-django~=0.36b0", - "opentelemetry-instrumentation-requests~=0.36b0", + "opentelemetry-instrumentation-fastapi~=0.36b0", "opentelemetry-instrumentation-flask~=0.36b0", "opentelemetry-instrumentation-psycopg2~=0.36b0", + "opentelemetry-instrumentation-requests~=0.36b0", "opentelemetry-api==1.15.0", "opentelemetry-sdk==1.15.0", ], diff --git a/azure-monitor-opentelemetry/tests/instrumentation/test_fastapi.py b/azure-monitor-opentelemetry/tests/instrumentation/test_fastapi.py new file mode 100644 index 0000000..6611d7e --- /dev/null +++ b/azure-monitor-opentelemetry/tests/instrumentation/test_fastapi.py @@ -0,0 +1,21 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +import unittest + +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor + + +class TestFastApiInstrumentation(unittest.TestCase): + def test_instrument(self): + excluded_urls = "client/.*/info,healthcheck" + try: + FastAPIInstrumentor().instrument(excluded_urls=excluded_urls) + except Exception as ex: # pylint: disable=broad-except + print(ex) + self.fail( + f"Unexpected exception raised when instrumenting {FastAPIInstrumentor.__name__}" + ) diff --git a/test-requirements.txt b/test-requirements.txt index a6c7e57..b7208bd 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,5 +1,6 @@ pytest -psycopg2 -flask django -requests \ No newline at end of file +fastapi +flask +psycopg2 +requests