Skip to content

Commit b8a5fd0

Browse files
Merge pull request #91880 from lgayhardt/appinsightspythondepedency1019
App Insights Python dependency
2 parents 1ea957f + 311fc75 commit b8a5fd0

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
title: Dependency Tracking in Azure Application Insights with OpenCensus Python | Microsoft Docs
3+
description: Monitor dependency calls for your Python apps via OpenCensus Python.
4+
services: application-insights
5+
author: lzchen
6+
manager: carmonm
7+
8+
ms.assetid: d15c4ca8-4c1a-47ab-a03d-c322b4bb2a9e
9+
ms.service: application-insights
10+
ms.workload: tbd
11+
ms.tgt_pltfrm: ibiza
12+
ms.topic: conceptual
13+
ms.date: 10/15/2019
14+
ms.author: lechen
15+
16+
---
17+
18+
# Track dependencies with OpenCensus Python
19+
20+
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.
21+
22+
First, instrument your Python application with latest [OpenCensus Python SDK](../../azure-monitor/app/opencensus-python.md).
23+
24+
## In-process dependencies
25+
26+
OpenCensus Python SDK for Azure Monitor allows you to send "in-process" dependency telemetry (information and logic that occurs within your application). In-process dependencies will have the `type` field as `INPROC` in analytics.
27+
28+
```python
29+
from opencensus.ext.azure.trace_exporter import AzureExporter
30+
from opencensus.trace.samplers import ProbabilitySampler
31+
from opencensus.trace.tracer import Tracer
32+
33+
tracer = Tracer(exporter=AzureExporter(connection_string="InstrumentationKey=<your-ikey-here>"), sampler=ProbabilitySampler(1.0))
34+
35+
with tracer.span(name='foo'): # <-- A dependency telemetry item will be sent for this span "foo"
36+
print('Hello, World!')
37+
```
38+
39+
## Dependencies with "requests" integration
40+
41+
Track your outgoing requests with the OpenCensus `requests` integration.
42+
43+
Download and install `opencensus-ext-requests` from [PyPI](https://pypi.org/project/opencensus-ext-requests/) and add it to the trace integrations. Requests sent using the Python [requests](https://pypi.org/project/requests/) library will be tracked.
44+
45+
```python
46+
import requests
47+
from opencensus.ext.azure.trace_exporter import AzureExporter
48+
from opencensus.trace import config_integration
49+
from opencensus.trace.samplers import ProbabilitySampler
50+
from opencensus.trace.tracer import Tracer
51+
52+
config_integration.trace_integrations(['requests']) # <-- this line enables the requests integration
53+
54+
tracer = Tracer(exporter=AzureExporter(connection_string="InstrumentationKey=<your-ikey-here>"), sampler=ProbabilitySampler(1.0))
55+
56+
with tracer.span(name='parent'):
57+
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit') # <-- this request will be tracked
58+
```
59+
60+
## Dependencies with "httplib" integration
61+
62+
Track your outgoing requests with OpenCensus `httplib` integration.
63+
64+
Download and install `opencensus-ext-httplib` from [PyPI](https://pypi.org/project/opencensus-ext-httplib/) and add it to the trace integrations. Requests sent using [http.client](https://docs.python.org/3.7/library/http.client.html) for Python3 or [httplib](https://docs.python.org/2/library/httplib.html) for Python2 will be tracked.
65+
66+
```python
67+
import http.client as httplib
68+
from opencensus.ext.azure.trace_exporter import AzureExporter
69+
from opencensus.trace import config_integration
70+
from opencensus.trace.samplers import ProbabilitySampler
71+
from opencensus.trace.tracer import Tracer
72+
73+
config_integration.trace_integrations(['httplib'])
74+
conn = httplib.HTTPConnection("www.python.org")
75+
76+
tracer = Tracer(
77+
exporter=AzureExporter(),
78+
sampler=ProbabilitySampler(1.0)
79+
)
80+
81+
conn.request("GET", "http://www.python.org", "", {})
82+
response = conn.getresponse()
83+
conn.close()
84+
```
85+
86+
## Dependencies with "django" integration
87+
88+
Track your outgoing Django requests with the OpenCensus `django` integration.
89+
90+
Download and install `opencensus-ext-django` from [PyPI](https://pypi.org/project/opencensus-ext-django/) and add the following line to the `MIDDLEWARE` section in the Django `settings.py` file.
91+
92+
```python
93+
MIDDLEWARE = [
94+
...
95+
'opencensus.ext.django.middleware.OpencensusMiddleware',
96+
]
97+
```
98+
99+
Additional configuration can be provided, read [customizations](https://github.com/census-instrumentation/opencensus-python#customization) for a complete reference.
100+
101+
```python
102+
OPENCENSUS = {
103+
'TRACE': {
104+
'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1)',
105+
'EXPORTER': '''opencensus.ext.ocagent.trace_exporter.TraceExporter(
106+
service_name='foobar',
107+
)''',
108+
}
109+
}
110+
```
111+
112+
## Dependencies with "mysql" integration
113+
114+
Track your MYSQL dependencies with the OpenCensus `mysql` integration. This integration supports the [mysql-connector](https://pypi.org/project/mysql-connector-python/) library.
115+
116+
Download and install `opencensus-ext-mysql` from [PyPI](https://pypi.org/project/opencensus-ext-mysql/) and add the following lines to your code.
117+
118+
```python
119+
from opencensus.trace import config_integration
120+
121+
config_integration.trace_integrations(['mysql'])
122+
```
123+
124+
## Dependencies with "pymysql" integration
125+
126+
Track your PyMySQL dependencies with the OpenCensus `pymysql` integration.
127+
128+
Download and install `opencensus-ext-pymysql` from [PyPI](https://pypi.org/project/opencensus-ext-pymysql/) and add the following lines to your code.
129+
130+
```python
131+
from opencensus.trace import config_integration
132+
133+
config_integration.trace_integrations(['pymysql'])
134+
```
135+
136+
## Dependencies with "postgresql" integration
137+
138+
Track your PostgreSQL dependencies with the OpenCensus `postgresql` integration. This integration supports the [psycopg2](https://pypi.org/project/psycopg2/) library.
139+
140+
Download and install `opencensus-ext-postgresql` from [PyPI](https://pypi.org/project/opencensus-ext-postgresql/) and add the following lines to your code.
141+
142+
```python
143+
from opencensus.trace import config_integration
144+
145+
config_integration.trace_integrations(['postgresql'])
146+
```
147+
148+
## Dependencies with "pymongo" integration
149+
150+
Track your MongoDB dependencies with the OpenCensus `pymongo` integration. This integration supports the [pymongo](https://pypi.org/project/pymongo/) library.
151+
152+
Download and install `opencensus-ext-pymongo` from [PyPI](https://pypi.org/project/opencensus-ext-pymongo/) and add the following lines to your code.
153+
154+
```python
155+
from opencensus.trace import config_integration
156+
157+
config_integration.trace_integrations(['pymongo'])
158+
```
159+
160+
### Dependencies with "sqlalchemy" integration
161+
162+
Track your dependencies using SQLAlchemy using OpenCensus `sqlalchemy` integration. This integration tracks the usage of the [sqlalchemy](https://pypi.org/project/SQLAlchemy/) package, regardless of the underlying database.
163+
164+
```python
165+
from opencensus.trace import config_integration
166+
167+
config_integration.trace_integrations(['sqlalchemy'])
168+
```
169+
170+
## Next steps
171+
172+
* [Application Map](../../azure-monitor/app/app-map.md)
173+
* [Availability](../../azure-monitor/app/monitor-web-app-availability.md)
174+
* [Search](../../azure-monitor/app/diagnostic-search.md)
175+
* [Log (Analytics) query](../../azure-monitor/log-query/log-query-overview.md)
176+
* [Transaction diagnostics](../../azure-monitor/app/transaction-diagnostics.md)

articles/azure-monitor/toc.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@
205205
- name: Node.js
206206
href: app/nodejs.md
207207
- name: Python
208-
href: app/opencensus-python.md
208+
items:
209+
- name: Overview
210+
href: app/opencensus-python.md
211+
- name: Dependencies
212+
href: app/opencensus-python-dependency.md
209213
- name: Web pages
210214
items:
211215
- name: Client-side JavaScript

0 commit comments

Comments
 (0)