Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
3 changes: 3 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# license information.
# --------------------------------------------------------------------------
import fastapi
import uvicorn

from azure.monitor.opentelemetry import configure_azure_monitor

# Configure Azure monitor collection telemetry pipeline
Expand All @@ -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