diff --git a/.vscode/cspell.json b/.vscode/cspell.json index f934b304cb8f..a6a998c3109f 100644 --- a/.vscode/cspell.json +++ b/.vscode/cspell.json @@ -987,7 +987,8 @@ "msecs", "mycontainer", "semconv", - "updown" + "updown", + "uvicorn" ] }, { diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index 8ca743f46635..61b6c5feaa30 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -10,6 +10,9 @@ ### Other Changes +- Updated FastAPI sample + ([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738)) + ## 1.0.0b23 (2024-02-28) ### Features Added diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_fastapi.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_fastapi.py index 193f39f99394..649312ca8336 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_fastapi.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_fastapi.py @@ -8,34 +8,49 @@ https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask """ # mypy: disable-error-code="attr-defined" +import os +import fastapi +import uvicorn + from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter -from fastapi import Depends, FastAPI -from httpx import AsyncClient from opentelemetry import trace +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor -app = FastAPI() +# This method instruments all of the FastAPI module. +# You can also use FastAPIInstrumentor().instrument_app(app) to instrument a specific app after it is created. +FastAPIInstrumentor().instrument() +app = fastapi.FastAPI() +trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) +span_processor = BatchSpanProcessor( + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + ) +) +trace.get_tracer_provider().add_span_processor(span_processor) -async def get_client(): - async with AsyncClient() as client: - yield client - +# Requests made to fastapi endpoints will be automatically captured @app.get("/") -async def read_root(client=Depends(get_client)): - with tracer.start_as_current_span("read_root"): - response = await client.get("https://httpbin.org/get") - return response.json() +async def test(): + return {"message": "Hello World"} + + +# Exceptions that are raised within the request are automatically captured +@app.get("/exception") +async def exception(): + raise Exception("Hit an exception") + + +# Set the OTEL_PYTHON_EXCLUDE_URLS environment variable to "http://127.0.0.1:8000/exclude" +# Telemetry from this endpoint will not be captured due to excluded_urls config above +@app.get("/exclude") +async def exclude(): + return {"message": "Telemetry was not captured"} if __name__ == "__main__": # cSpell:disable - import uvicorn - trace.set_tracer_provider(TracerProvider()) - exporter = AzureMonitorTraceExporter() - span_processor = BatchSpanProcessor(exporter) - trace.get_tracer_provider().add_span_processor(span_processor) uvicorn.run("sample_fastapi:app", port=8008, reload=True) # cSpell:disable - diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_flask.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_flask.py index 4a96786dfbe4..85d2caa995b6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_flask.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_flask.py @@ -18,7 +18,8 @@ from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter -# Enable instrumentation in the flask library. +# This method instruments all of FastAPI. +# You can also use FlaskInstrumentor().instrument_app(app) to instrument a specific app after it is created. FlaskInstrumentor().instrument() app = flask.Flask(__name__) diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index c2be9837fa36..ea741a3be623 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -10,6 +10,9 @@ ### Other Changes +- Updated FastAPI sample + ([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738)) + ## 1.3.0 (2024-02-29) ### Features Added diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py index 96d5edd5787d..5a2e22f7ad98 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py @@ -4,6 +4,8 @@ # license information. # -------------------------------------------------------------------------- import fastapi +import uvicorn + from azure.monitor.opentelemetry import configure_azure_monitor # Configure Azure monitor collection telemetry pipeline @@ -29,3 +31,9 @@ async def exception(): @app.get("/exclude") async def exclude(): return {"message": "Telemetry was not captured"} + + +if __name__ == "__main__": + # cSpell:disable + uvicorn.run("http_fastapi:app", port=8008, reload=True) + # cSpell:disable