You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Monitor Python applications with Azure Application Insights | Microsoft Docs
3
-
description: Provides instructions to wire up OpenCensus Python with Application Insights
2
+
title: Monitor Python applications with Azure Monitor | Microsoft Docs
3
+
description: Provides instructions to wire up OpenCensus Python with Azure Monitor
4
4
services: application-insights
5
5
keywords:
6
6
author: reyang
7
7
ms.author: reyang
8
-
ms.date: 07/02/2019
8
+
ms.date: 10/11/2019
9
9
ms.service: application-insights
10
10
ms.topic: conceptual
11
11
ms.reviewer: mbullwin
12
12
manager: carmonm
13
13
---
14
14
15
-
# Collect distributed traces from Python (Preview)
15
+
# Set up Azure Monitor for your Python application
16
16
17
-
Application Insights now supports distributed tracingof Python applications through integration with [OpenCensus](https://opencensus.io). This article will walk you step-by-step through the process of setting up OpenCensus for Python and getting your monitoring data to Application Insights.
17
+
Azure Monitor supports distributed tracing, metric collection and logging of Python applications through integration with [OpenCensus](https://opencensus.io). This article will walk you step-by-step through the process of setting up OpenCensus for Python and getting your monitoring data to Azure Monitor.
18
18
19
19
## Prerequisites
20
20
21
21
- You need an Azure Subscription.
22
-
- Python should be installed, this article uses [Python 3.7.0](https://www.python.org/downloads/), though earlier versions will likely work with minor adjustment.
22
+
- Python should be installed, this article uses [Python 3.7.0](https://www.python.org/downloads/), though earlier versions will likely work with minor adjustments.
23
23
24
24
If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account before you begin.
25
25
26
26
## Sign in to the Azure portal
27
27
28
28
Sign in to the [Azure portal](https://portal.azure.com/).
29
29
30
-
## Create Application Insights resource
30
+
## Create Application Insights resource in Azure Monitor
31
31
32
-
First you have to create an Application Insights resource, which will generate an instrumentation key(ikey). The ikey is then used to configure the OpenCensus SDK to send telemetry data to Application Insights.
32
+
First you have to create an Application Insights resource in Azure Monitor, which will generate an instrumentation key(ikey). The ikey is then used to configure the OpenCensus SDK to send telemetry data to Azure Monitor.
@@ -45,7 +45,7 @@ First you have to create an Application Insights resource, which will generate a
45
45
46
46
2. Click **Create**.
47
47
48
-
## Install OpenCensus Azure Monitor Exporters
48
+
## Instrumenting with OpenCensus Python SDK for Azure Monitor
49
49
50
50
1. Install the OpenCensus Azure Monitor Exporters:
51
51
@@ -56,7 +56,11 @@ First you have to create an Application Insights resource, which will generate a
56
56
> [!NOTE]
57
57
> `python -m pip install opencensus-ext-azure` assumes that you have a PATH environment variable set for your Python installation. If you have not configured this, you would need to give the full directory path to where your Python executable is located which would result in a command like: `C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe -m pip install opencensus-ext-azure`.
58
58
59
-
2. First let's generate some trace data locally. In Python IDLE, or your editor of choice, enter the following code:
59
+
2. The SDK utilizes three Azure Monitor exporters to send different types of telemetry to Azure Monitor: trace, metrics, and logs. Take a look at [the data platform overview](https://docs.microsoft.com/azure/azure-monitor/platform/data-platform) for more details on these different types. Follow the instructions below to see how to send these different types via the three exporters.
60
+
61
+
### Trace
62
+
63
+
1. First let's generate some trace data locally. In Python IDLE, or your editor of choice, enter the following code.
60
64
61
65
```python
62
66
from opencensus.trace.samplers import ProbabilitySampler
@@ -77,7 +81,7 @@ First you have to create an Application Insights resource, which will generate a
77
81
main()
78
82
```
79
83
80
-
3. Running the code will repeatedly prompt you to enter a value. With each entry, the value will be printed to the shell, and a corresponding piece of **SpanData** will be generated by the OpenCensus Python Module. The OpenCensus project defines a [_trace as a tree of spans_](https://opencensus.io/core-concepts/tracing/).
84
+
2. Running the code will repeatedly prompt you to enter a value. With each entry, the value will be printed to the shell, and a corresponding piece of **SpanData** will be generated by the OpenCensus Python Module. The OpenCensus project defines a [_trace as a tree of spans_](https://opencensus.io/core-concepts/tracing/).
81
85
82
86
```
83
87
Enter a value: 4
@@ -91,7 +95,7 @@ First you have to create an Application Insights resource, which will generate a
4. While helpful for demonstration purposes, ultimately we want to emit the SpanData to Application Insights. Modify your code from the previous step based on the following code sample:
98
+
3. While helpful for demonstration purposes, ultimately we want to emit the `SpanData` to Azure Monitor. Modify your code from the previous step based on the following code sample:
95
99
96
100
```python
97
101
from opencensus.ext.azure.trace_exporter import AzureExporter
@@ -101,7 +105,7 @@ First you have to create an Application Insights resource, which will generate a
101
105
# TODO: replace the all-zero GUID with your instrumentation key.
@@ -118,7 +122,172 @@ First you have to create an Application Insights resource, which will generate a
118
122
if __name__ == "__main__":
119
123
main()
120
124
```
121
-
5. Now when you run the Python script, you should still be prompted to enter values, but now only the value is being printed in the shell.
125
+
126
+
4. Now when you run the Python script, you should still be prompted to enter values, but now only the value is being printed in the shell. The `SpanData` created will be sent to Azure Monitor. You can find the emitted span data under `dependencies`.
127
+
128
+
### Metrics
129
+
130
+
1. First, let's generate some local metric data. We will create a simple metric to track the number of times the user presses enter.
131
+
132
+
```python
133
+
from datetime import datetime
134
+
from opencensus.stats import aggregation as aggregation_module
135
+
from opencensus.stats import measure as measure_module
136
+
from opencensus.stats import stats as stats_module
137
+
from opencensus.stats import view as view_module
138
+
from opencensus.tags import tag_map as tag_map_module
2. Running the code will repeatedly prompt you to press enter. A metric is created to track the number of enters pressed. With each entry, the value will be incremented and the metric information will be displayed in the console, with the current value and the current timestamp when the metric was updated.
3. While helpful for demonstration purposes, ultimately we want to emit the metric data to Azure Monitor. Modify your code from the previous step based on the following code sample:
182
+
183
+
```python
184
+
from datetime import datetime
185
+
from opencensus.ext.azure import metrics_exporter
186
+
from opencensus.stats import aggregation as aggregation_module
187
+
from opencensus.stats import measure as measure_module
188
+
from opencensus.stats import stats as stats_module
189
+
from opencensus.stats import view as view_module
190
+
from opencensus.tags import tag_map as tag_map_module
4. The exporter will send metric data to Azure Monitor at a fixed interval, the default being every 15 seconds. We are tracking a single metric so this metric data, with whatever value and timestamp it contains, will be sent every interval. You can find the data under `customMetrics`.
230
+
231
+
### Logs
232
+
233
+
1. First, let's generate some local log data.
234
+
235
+
```python
236
+
import logging
237
+
238
+
logger = logging.getLogger(__name__)
239
+
240
+
def valuePrompt():
241
+
line = input("Enter a value: ")
242
+
logger.warning(line)
243
+
244
+
def main():
245
+
while True:
246
+
valuePrompt()
247
+
248
+
if __name__ == "__main__":
249
+
main()
250
+
```
251
+
252
+
2. The code will ask continuously prompt for a value to be entered. A log entry is emitted for every value that is entered containing the said value.
253
+
254
+
```
255
+
Enter a value: 24
256
+
24
257
+
Enter a value: 55
258
+
55
259
+
Enter a value: 123
260
+
123
261
+
Enter a value: 90
262
+
90
263
+
```
264
+
265
+
3. While helpful for demonstration purposes, ultimately we want to emit the metric data to Azure Monitor. Modify your code from the previous step based on the following code sample:
266
+
267
+
```python
268
+
import logging
269
+
from opencensus.ext.azure.log_exporter import AzureLogHandler
270
+
271
+
logger = logging.getLogger(__name__)
272
+
273
+
# TODO: replace the all-zero GUID with your instrumentation key.
4. The exporter will send log data to Azure Monitor. You can find the data under `traces`.
122
291
123
292
## Start monitoring in the Azure portal
124
293
@@ -142,8 +311,23 @@ First you have to create an Application Insights resource, which will generate a
142
311
143
312

144
313
314
+
## View your data with queries
315
+
316
+
1. You can view the telemetry data that was sent from your application through the Logs(Analytics) tab.
317
+
318
+

319
+
320
+
2. For telemetry sent with the Azure Monitor trace exporter, incoming requests will show up under `requests` and out-going/in process requests will show up under `dependencies`.
321
+
322
+
3. For telemetry sent with the Azure Monitor metrics exporter, metrics sent will show up under `customMetrics`.
323
+
324
+
4. For telemetry sent with the Azure Monitor logs exporter, logs will show up under `traces` and exceptions will show up under `exceptions`.
325
+
326
+
5. Take a look at [Logs in Azure Monitor](https://docs.microsoft.com/azure/azure-monitor/platform/data-platform-logs) for more detailed information about how to use queries and logs.
327
+
145
328
## OpenCensus for Python
146
329
330
+
* [OpenCensus Python on GitHub](https://github.com/census-instrumentation/opencensus-python)
* [Availability tests](../../azure-monitor/app/monitor-web-app-availability.md): Create tests to make sure your site is visible on the web.
346
+
* [Smart diagnostics](../../azure-monitor/app/proactive-diagnostics.md): These tests run automatically, so you don't have to do anything to set them up. They tell you if your app has an unusual rate of failed requests.
347
+
* [Metric alerts](../../azure-monitor/app/alerts.md): Set alerts to warn you if a metric crosses a threshold. You can set them on custom metrics that you code into your app.
0 commit comments