Skip to content

Commit 4a3b2db

Browse files
authored
Merge pull request #93977 from lzchen/requests
Add incoming requests doc to OpenCensus Python
2 parents 5ea365f + 410eee1 commit 4a3b2db

File tree

5 files changed

+148
-4
lines changed

5 files changed

+148
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.date: 10/15/2019
1212

1313
# Track dependencies with OpenCensus Python
1414

15-
A dependency is an external component that is called by your application. Dependency data is collected using OpenCensus Python and its various integrations. The data is then sent to Application Insights under Azure Monitor.
15+
A dependency is an external component that is called by your application. Dependency data is collected using OpenCensus Python and its various integrations. The data is then sent to Application Insights under Azure Monitor as `dependencies` telemetry.
1616

1717
First, instrument your Python application with latest [OpenCensus Python SDK](../../azure-monitor/app/opencensus-python.md).
1818

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Azure Monitor supports distributed tracing, metric collection, and logging of Py
2020
- An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/) before you begin.
2121
- Python installation. This article uses [Python 3.7.0](https://www.python.org/downloads/), though earlier versions will likely work with minor changes.
2222

23-
24-
2523
## Sign in to the Azure portal
2624

2725
Sign in to the [Azure portal](https://portal.azure.com/).
@@ -52,6 +50,8 @@ Install the OpenCensus Azure Monitor exporters:
5250
python -m pip install opencensus-ext-azure
5351
```
5452

53+
For a full list of packages and integrations, see [OpenCensus packages](https://docs.microsoft.com/azure/azure-monitor/app/nuget#common-packages-for-python-using-opencensus).
54+
5555
> [!NOTE]
5656
> The `python -m pip install opencensus-ext-azure` command assumes that you have a `PATH` environment variable set for your Python installation. If you haven't configured this variable, you need to give the full directory path to where your Python executable is located. The result is a command like this: `C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe -m pip install opencensus-ext-azure`.
5757
@@ -124,6 +124,10 @@ The SDK uses three Azure Monitor exporters to send different types of telemetry
124124

125125
4. Now when you run the Python script, you should still be prompted to enter values, but only the value is being printed in the shell. The created `SpanData` will be sent to Azure Monitor. You can find the emitted span data under `dependencies`.
126126

127+
5. For information on sampling in OpenCensus, take a look at [sampling in OpenCensus](https://docs.microsoft.com/azure/azure-monitor/app/sampling#configuring-fixed-rate-sampling-in-opencensus-python).
128+
129+
6. For details on telemetry correlation in your trace data, take a look at OpenCensus [telemetry correlation](https://docs.microsoft.com/azure/azure-monitor/app/correlation#telemetry-correlation-in-opencensus-python).
130+
127131
### Metrics
128132

129133
1. First, let's generate some local metric data. We'll create a simple metric to track the number of times the user presses Enter.
@@ -288,6 +292,8 @@ The SDK uses three Azure Monitor exporters to send different types of telemetry
288292

289293
4. The exporter will send log data to Azure Monitor. You can find the data under `traces`.
290294

295+
5. For details on how to enrich your logs with trace context data, see OpenCensus Python [logs integration](https://docs.microsoft.com/azure/azure-monitor/app/correlation#logs-correlation).
296+
291297
## Start monitoring in the Azure portal
292298

293299
1. You can now reopen the Application Insights **Overview** pane in the Azure portal, to view details about your currently running application. Select **Live Metrics Stream**.

articles/azure-monitor/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@
318318
href: app/opencensus-python.md
319319
- name: Dependencies
320320
href: app/opencensus-python-dependency.md
321+
- name: Requests
322+
href: app/opencensus-python-request.md
321323
- name: Web pages
322324
items:
323325
- name: Client-side JavaScript

articles/virtual-network/virtual-network-test-latency.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,4 @@ This SockPerf example uses a 350 byte message size because it is a typical avera
190190
* Improve latency with [Azure Proximity Placement Group](https://docs.microsoft.com/azure/virtual-machines/linux/co-location)
191191
* Learn about how to [Optimize networking for VMs](../virtual-network/virtual-network-optimize-network-bandwidth.md) for your scenario.
192192
* Read about how [bandwidth is allocated to virtual machines](../virtual-network/virtual-machine-network-throughput.md)
193-
* Learn more with [Azure Virtual Network frequently asked questions (FAQ)](../virtual-network/virtual-networks-faq.md)
193+
* Learn more with [Azure Virtual Network frequently asked questions (FAQ)](../virtual-network/virtual-networks-faq.md)

0 commit comments

Comments
 (0)