Skip to content

Commit 5cff7c9

Browse files
authored
Update FastAPI sample with instrumentation (#34738)
* Update FastAPI sample with instrumentation * uvicorn spelling
1 parent b3411ac commit 5cff7c9

6 files changed

Lines changed: 50 additions & 19 deletions

File tree

.vscode/cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,8 @@
990990
"msecs",
991991
"mycontainer",
992992
"semconv",
993-
"updown"
993+
"updown",
994+
"uvicorn"
994995
]
995996
},
996997
{

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
### Other Changes
1717

18+
- Updated FastAPI sample
19+
([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738))
20+
1821
## 1.0.0b23 (2024-02-28)
1922

2023
### Features Added

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_fastapi.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,49 @@
88
https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask
99
"""
1010
# mypy: disable-error-code="attr-defined"
11+
import os
12+
import fastapi
13+
import uvicorn
14+
1115
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
12-
from fastapi import Depends, FastAPI
13-
from httpx import AsyncClient
1416
from opentelemetry import trace
17+
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
1518
from opentelemetry.sdk.trace import TracerProvider
1619
from opentelemetry.sdk.trace.export import BatchSpanProcessor
1720

18-
app = FastAPI()
21+
# This method instruments all of the FastAPI module.
22+
# You can also use FastAPIInstrumentor().instrument_app(app) to instrument a specific app after it is created.
23+
FastAPIInstrumentor().instrument()
24+
app = fastapi.FastAPI()
1925

26+
trace.set_tracer_provider(TracerProvider())
2027
tracer = trace.get_tracer(__name__)
28+
span_processor = BatchSpanProcessor(
29+
AzureMonitorTraceExporter.from_connection_string(
30+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
31+
)
32+
)
33+
trace.get_tracer_provider().add_span_processor(span_processor)
2134

22-
async def get_client():
23-
async with AsyncClient() as client:
24-
yield client
25-
35+
# Requests made to fastapi endpoints will be automatically captured
2636
@app.get("/")
27-
async def read_root(client=Depends(get_client)):
28-
with tracer.start_as_current_span("read_root"):
29-
response = await client.get("https://httpbin.org/get")
30-
return response.json()
37+
async def test():
38+
return {"message": "Hello World"}
39+
40+
41+
# Exceptions that are raised within the request are automatically captured
42+
@app.get("/exception")
43+
async def exception():
44+
raise Exception("Hit an exception")
45+
46+
47+
# Set the OTEL_PYTHON_EXCLUDE_URLS environment variable to "http://127.0.0.1:8000/exclude"
48+
# Telemetry from this endpoint will not be captured due to excluded_urls config above
49+
@app.get("/exclude")
50+
async def exclude():
51+
return {"message": "Telemetry was not captured"}
3152

3253
if __name__ == "__main__":
3354
# cSpell:disable
34-
import uvicorn
35-
trace.set_tracer_provider(TracerProvider())
36-
exporter = AzureMonitorTraceExporter()
37-
span_processor = BatchSpanProcessor(exporter)
38-
trace.get_tracer_provider().add_span_processor(span_processor)
3955
uvicorn.run("sample_fastapi:app", port=8008, reload=True)
4056
# cSpell:disable
41-

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_flask.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
2020

21-
# Enable instrumentation in the flask library.
21+
# This method instruments all of FastAPI.
22+
# You can also use FlaskInstrumentor().instrument_app(app) to instrument a specific app after it is created.
2223
FlaskInstrumentor().instrument()
2324
app = flask.Flask(__name__)
2425

sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
### Other Changes
1212

13+
- Updated FastAPI sample
14+
([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738))
15+
1316
## 1.3.0 (2024-02-29)
1417

1518
### Features Added

sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
import fastapi
7+
import uvicorn
8+
79
from azure.monitor.opentelemetry import configure_azure_monitor
810

911
# Configure Azure monitor collection telemetry pipeline
@@ -29,3 +31,9 @@ async def exception():
2931
@app.get("/exclude")
3032
async def exclude():
3133
return {"message": "Telemetry was not captured"}
34+
35+
36+
if __name__ == "__main__":
37+
# cSpell:disable
38+
uvicorn.run("http_fastapi:app", port=8008, reload=True)
39+
# cSpell:disable

0 commit comments

Comments
 (0)