Track incoming requests with OpenCensus Python
Caution
The OpenCensus Python SDK is retired. We recommend the OpenTelemetry-based Python offering and provide migration guidance.
OpenCensus Python and its integrations collect incoming request data. You can track incoming request data sent to your web applications built on top of the popular web frameworks Django, Flask, and Pyramid. Application Insights receives the data as requests
telemetry.
First, instrument your Python application with the latest OpenCensus Python SDK.
Track Django applications
Download and install
opencensus-ext-django
from PyPI. Instrument your application with thedjango
middleware. Incoming requests sent to your Django application are tracked.Include
opencensus.ext.django.middleware.OpencensusMiddleware
in yoursettings.py
file underMIDDLEWARE
.MIDDLEWARE = ( ... 'opencensus.ext.django.middleware.OpencensusMiddleware', ... )
Make sure AzureExporter is configured properly in your
settings.py
underOPENCENSUS
. For requests from URLs that you don't want to track, add them toEXCLUDELIST_PATHS
.OPENCENSUS = { 'TRACE': { 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1)', 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( connection_string="InstrumentationKey=<your-ikey-here>" )''', 'EXCLUDELIST_PATHS': ['https://example.com'], <--- These sites will not be traced if a request is sent to it. } }
You can find a Django sample application in the Azure Monitor OpenCensus Python samples repository.
Track Flask applications
Download and install
opencensus-ext-flask
from PyPI. Instrument your application with theflask
middleware. Incoming requests sent to your Flask application are tracked.from flask import Flask from opencensus.ext.azure.trace_exporter import AzureExporter from opencensus.ext.flask.flask_middleware import FlaskMiddleware from opencensus.trace.samplers import ProbabilitySampler app = Flask(__name__) middleware = FlaskMiddleware( app, exporter=AzureExporter(connection_string="InstrumentationKey=<your-ikey-here>"), sampler=ProbabilitySampler(rate=1.0), ) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': app.run(host='localhost', port=8080, threaded=True)
You can also configure your
flask
application throughapp.config
. For requests from URLs that you don't want to track, add them toEXCLUDELIST_PATHS
.app.config['OPENCENSUS'] = { 'TRACE': { 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)', 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( connection_string="InstrumentationKey=<your-ikey-here>", )''', 'EXCLUDELIST_PATHS': ['https://example.com'], <--- These sites will not be traced if a request is sent to it. } }
Note
To run Flask under uWSGI in a Docker environment, you must first add
lazy-apps = true
to the uWSGI configuration file (uwsgi.ini). For more information, see the issue description.
You can find a Flask sample application that tracks requests in the Azure Monitor OpenCensus Python samples repository.
Track Pyramid applications
Download and install
opencensus-ext-django
from PyPI. Instrument your application with thepyramid
tween. Incoming requests sent to your Pyramid application are tracked.def main(global_config, **settings): config = Configurator(settings=settings) config.add_tween('opencensus.ext.pyramid' '.pyramid_middleware.OpenCensusTweenFactory')
You can configure your
pyramid
tween directly in the code. For requests from URLs that you don't want to track, add them toEXCLUDELIST_PATHS
.settings = { 'OPENCENSUS': { 'TRACE': { 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)', 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( connection_string="InstrumentationKey=<your-ikey-here>", )''', 'EXCLUDELIST_PATHS': ['https://example.com'], <--- These sites will not be traced if a request is sent to it. } } } config = Configurator(settings=settings)
Track FastAPI applications
The following dependencies are required:
-
In a production setting, we recommend that you deploy uvicorn with gunicorn.
Download and install
opencensus-ext-fastapi
from PyPI.pip install opencensus-ext-fastapi
Instrument your application with the
fastapi
middleware.from fastapi import FastAPI from opencensus.ext.fastapi.fastapi_middleware import FastAPIMiddleware app = FastAPI(__name__) app.add_middleware(FastAPIMiddleware) @app.get('/') def hello(): return 'Hello World!'
Run your application. Calls made to your FastAPI application should be automatically tracked. Telemetry should be logged directly to Azure Monitor.