Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
Download and install opencensus-ext-django
from PyPI. Instrument your application with the django
middleware. Incoming requests sent to your Django application are tracked.
Include opencensus.ext.django.middleware.OpencensusMiddleware
in your settings.py
file under MIDDLEWARE
.
MIDDLEWARE = (
...
'opencensus.ext.django.middleware.OpencensusMiddleware',
...
)
Make sure AzureExporter is configured properly in your settings.py
under OPENCENSUS
. For requests from URLs that you don't want to track, add them to EXCLUDELIST_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.
Download and install opencensus-ext-flask
from PyPI. Instrument your application with the flask
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 through app.config
. For requests from URLs that you don't want to track, add them to EXCLUDELIST_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.
Download and install opencensus-ext-django
from PyPI. Instrument your application with the pyramid
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 to EXCLUDELIST_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)
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.
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in