|
| 1 | +--- |
| 2 | +title: Incoming Request Tracking in Azure Application Insights with OpenCensus Python | Microsoft Docs |
| 3 | +description: Monitor request calls for your Python apps via OpenCensus Python. |
| 4 | +ms.service: azure-monitor |
| 5 | +ms.subservice: application-insights |
| 6 | +ms.topic: conceptual |
| 7 | +author: lzchen |
| 8 | +ms.author: lechen |
| 9 | +ms.date: 10/15/2019 |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +# Track incoming requests with OpenCensus Python |
| 14 | + |
| 15 | +Incoming request data is collected using OpenCensus Python and its various integrations. Track incoming request data sent to your web applications built on top of the popular web frameworks `django`, `flask` and `pyramid`. The data is then sent to Application Insights under Azure Monitor as `requests` telemetry. |
| 16 | + |
| 17 | +First, instrument your Python application with latest [OpenCensus Python SDK](../../azure-monitor/app/opencensus-python.md). |
| 18 | + |
| 19 | +## Tracking Django applications |
| 20 | + |
| 21 | +1. Download and install `opencensus-ext-django` from [PyPI](https://pypi.org/project/opencensus-ext-django/) and instrument your application with the `django` middleware. Incoming requests sent to your `django` application will be tracked. |
| 22 | + |
| 23 | +2. Include `opencensus.ext.django.middleware.OpencensusMiddleware` in your `settings.py` file under `MIDDLEWARE`. |
| 24 | + |
| 25 | + ```python |
| 26 | + MIDDLEWARE = ( |
| 27 | + ... |
| 28 | + 'opencensus.ext.django.middleware.OpencensusMiddleware', |
| 29 | + ... |
| 30 | + ) |
| 31 | + ``` |
| 32 | + |
| 33 | +3. Make sure AzureExporter is properly configured in your `settings.py` under `OPENCENSUS`. |
| 34 | + |
| 35 | + ```python |
| 36 | + OPENCENSUS = { |
| 37 | + 'TRACE': { |
| 38 | + 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=0.5)', |
| 39 | + 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( |
| 40 | + service_name='foobar', |
| 41 | + )''', |
| 42 | + } |
| 43 | + } |
| 44 | + ``` |
| 45 | + |
| 46 | +4. You can also add urls to `settings.py` under `BLACKLIST_PATHS` for requests that you do not want to track. |
| 47 | + |
| 48 | + ```python |
| 49 | + OPENCENSUS = { |
| 50 | + 'TRACE': { |
| 51 | + 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=0.5)', |
| 52 | + 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( |
| 53 | + service_name='foobar', |
| 54 | + )''', |
| 55 | + 'BLACKLIST_PATHS': 'https://example.com', <--- This site will not be traced if a request is sent from it. |
| 56 | + } |
| 57 | + } |
| 58 | + ``` |
| 59 | + |
| 60 | +## Tracking Flask applications |
| 61 | + |
| 62 | +1. Download and install `opencensus-ext-flask` from [PyPI](https://pypi.org/project/opencensus-ext-flask/) and instrument your application with the `flask` middleware. Incoming requests sent to your `flask` application will be tracked. |
| 63 | + |
| 64 | + ```python |
| 65 | + |
| 66 | + from flask import Flask |
| 67 | + from opencensus.ext.azure.trace_exporter import AzureExporter |
| 68 | + from opencensus.ext.flask.flask_middleware import FlaskMiddleware |
| 69 | + from opencensus.trace.samplers import ProbabilitySampler |
| 70 | + |
| 71 | + app = Flask(__name__) |
| 72 | + middleware = FlaskMiddleware( |
| 73 | + app, |
| 74 | + exporter=AzureExporter(connection_string="InstrumentationKey=<your-ikey-here>"), |
| 75 | + sampler=ProbabilitySampler(rate=1.0), |
| 76 | + ) |
| 77 | + |
| 78 | + @app.route('/') |
| 79 | + def hello(): |
| 80 | + return 'Hello World!' |
| 81 | + |
| 82 | + if __name__ == '__main__': |
| 83 | + app.run(host='localhost', port=8080, threaded=True) |
| 84 | + |
| 85 | + ``` |
| 86 | + |
| 87 | +2. You can configure your `flask` middleware directly in the code. For requests from urls that you do not wish to track, add them to `BLACKLIST_PATHS`. |
| 88 | + |
| 89 | + ```python |
| 90 | + app.config['OPENCENSUS'] = { |
| 91 | + 'TRACE': { |
| 92 | + 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)', |
| 93 | + 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( |
| 94 | + service_name='foobar', |
| 95 | + )''', |
| 96 | + 'BLACKLIST_PATHS': 'https://example.com', <--- This site will not be traced if a request is sent to it. |
| 97 | + } |
| 98 | + } |
| 99 | + ``` |
| 100 | + |
| 101 | +## Tracking Pyramid applications |
| 102 | + |
| 103 | +1. Download and install `opencensus-ext-django` from [PyPI](https://pypi.org/project/opencensus-ext-pyramid/) and instrument your application with the `pyramid` tween. Incoming requests sent to your `pyramid` application will be tracked. |
| 104 | + |
| 105 | + ```python |
| 106 | + def main(global_config, **settings): |
| 107 | + config = Configurator(settings=settings) |
| 108 | + |
| 109 | + config.add_tween('opencensus.ext.pyramid' |
| 110 | + '.pyramid_middleware.OpenCensusTweenFactory') |
| 111 | + ``` |
| 112 | + |
| 113 | +2. You can configure your `pyramid` tween directly in the code. For requests from urls that you do not wish to track, add them to `BLACKLIST_PATHS`. |
| 114 | + |
| 115 | + ```python |
| 116 | + settings = { |
| 117 | + 'OPENCENSUS': { |
| 118 | + 'TRACE': { |
| 119 | + 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)', |
| 120 | + 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( |
| 121 | + service_name='foobar', |
| 122 | + )''', |
| 123 | + 'BLACKLIST_PATHS': 'https://example.com', <--- This site will not be traced if a request is sent to it. |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + config = Configurator(settings=settings) |
| 128 | + ``` |
| 129 | + |
| 130 | +## Next steps |
| 131 | + |
| 132 | +* [Application Map](../../azure-monitor/app/app-map.md) |
| 133 | +* [Availability](../../azure-monitor/app/monitor-web-app-availability.md) |
| 134 | +* [Search](../../azure-monitor/app/diagnostic-search.md) |
| 135 | +* [Log (Analytics) query](../../azure-monitor/log-query/log-query-overview.md) |
| 136 | +* [Transaction diagnostics](../../azure-monitor/app/transaction-diagnostics.md) |
0 commit comments