Skip to content

Commit 4c54e11

Browse files
Update documentation about opencensus fastapi plugin
1 parent 921d1b5 commit 4c54e11

File tree

1 file changed

+13
-45
lines changed

1 file changed

+13
-45
lines changed

articles/azure-monitor/app/opencensus-python-request.md

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -121,64 +121,32 @@ You can find a Flask sample application that tracks requests in the [Azure Monit
121121

122122
## Track FastAPI applications
123123

124-
OpenCensus doesn't have an extension for FastAPI. To write your own FastAPI middleware:
125-
126124
1. The following dependencies are required:
127125
- [fastapi](https://pypi.org/project/fastapi/)
128126
- [uvicorn](https://pypi.org/project/uvicorn/)
129127

130128
In a production setting, we recommend that you deploy [uvicorn with gunicorn](https://www.uvicorn.org/deployment/#gunicorn).
131129

132-
1. Add [FastAPI middleware](https://fastapi.tiangolo.com/tutorial/middleware/). Make sure that you set the span kind server: `span.span_kind = SpanKind.SERVER`.
133-
134-
1. Run your application. Calls made to your FastAPI application should be automatically tracked. Telemetry should be logged directly to Azure Monitor.
135-
136-
```python
137-
# Opencensus imports
138-
from opencensus.ext.azure.trace_exporter import AzureExporter
139-
from opencensus.trace.samplers import ProbabilitySampler
140-
from opencensus.trace.tracer import Tracer
141-
from opencensus.trace.span import SpanKind
142-
from opencensus.trace.attributes_helper import COMMON_ATTRIBUTES
143-
# FastAPI imports
144-
from fastapi import FastAPI, Request
145-
# uvicorn
146-
import uvicorn
147-
148-
app = FastAPI()
149-
150-
HTTP_URL = COMMON_ATTRIBUTES['HTTP_URL']
151-
HTTP_STATUS_CODE = COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
152-
153-
exporter=AzureExporter(connection_string='<your-appinsights-connection-string-here>')
154-
sampler=ProbabilitySampler(1.0)
155-
156-
# fastapi middleware for opencensus
157-
@app.middleware("http")
158-
async def middlewareOpencensus(request: Request, call_next):
159-
tracer = Tracer(exporter=exporter, sampler=sampler)
160-
with tracer.span("main") as span:
161-
span.span_kind = SpanKind.SERVER
130+
2. Download and install `opencensus-ext-fastapi` from [PyPI](https://pypi.org/project/opencensus-ext-fastapi/).
162131

163-
response = await call_next(request)
132+
`pip install opencensus-ext-fastapi`
164133

165-
tracer.add_attribute_to_current_span(
166-
attribute_key=HTTP_STATUS_CODE,
167-
attribute_value=response.status_code)
168-
tracer.add_attribute_to_current_span(
169-
attribute_key=HTTP_URL,
170-
attribute_value=str(request.url))
134+
3. Instrument your application with the `fastapi` middleware.
171135

172-
return response
136+
```python
137+
from fastapi import FastAPI
138+
from opencensus.ext.fastapi.fastapi_middleware import FastAPIMiddleware
173139

174-
@app.get("/")
175-
async def root():
176-
return "Hello World!"
140+
app = FastAPI(__name__)
141+
app.add_middleware(FastAPIMiddleware)
177142

178-
if __name__ == '__main__':
179-
uvicorn.run("example:app", host="127.0.0.1", port=5000, log_level="info")
143+
@app.get('/')
144+
def hello():
145+
return 'Hello World!'
180146
```
181147

148+
4. Run your application. Calls made to your FastAPI application should be automatically tracked. Telemetry should be logged directly to Azure Monitor.
149+
182150
## Next steps
183151

184152
* [Application Map](./app-map.md)

0 commit comments

Comments
 (0)