使用 OpenCensus Python 跟踪依赖项Track dependencies with OpenCensus Python
依赖项是由应用程序调用的外部组件。A dependency is an external component that is called by your application. 可以使用 OpenCensus Python 及其各种集成收集依赖项数据。Dependency data is collected using OpenCensus Python and its various integrations. 然后,将数据作为 dependencies
遥测发送到 Azure Monitor 下的 Application Insights。The data is then sent to Application Insights under Azure Monitor as dependencies
telemetry.
首先,使用最新版 OpenCensus Python SDK 检测 Python 应用程序。First, instrument your Python application with latest OpenCensus Python SDK.
进程内依赖项In-process dependencies
OpenCensus Python SDK for Azure Monitor 允许发送“进程内”依赖项遥测(应用程序中出现的信息和逻辑)。OpenCensus Python SDK for Azure Monitor allows you to send "in-process" dependency telemetry (information and logic that occurs within your application). 在分析中,进程内依赖项的 type
字段将为 INPROC
。In-process dependencies will have the type
field as INPROC
in analytics.
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer
tracer = Tracer(exporter=AzureExporter(connection_string="InstrumentationKey=<your-ikey-here>"), sampler=ProbabilitySampler(1.0))
with tracer.span(name='foo'): # <-- A dependency telemetry item will be sent for this span "foo"
print('Hello, World!')
使用“requests”集成跟踪的依赖项Dependencies with "requests" integration
通过 OpenCensus requests
集成来跟踪传出请求。Track your outgoing requests with the OpenCensus requests
integration.
从 PyPI 下载并安装 opencensus-ext-requests
,然后将其添加到跟踪集成。Download and install opencensus-ext-requests
from PyPI and add it to the trace integrations. 将跟踪使用 Python requests 库发送的请求。Requests sent using the Python requests library will be tracked.
import requests
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer
config_integration.trace_integrations(['requests']) # <-- this line enables the requests integration
tracer = Tracer(exporter=AzureExporter(connection_string="InstrumentationKey=<your-ikey-here>"), sampler=ProbabilitySampler(1.0))
with tracer.span(name='parent'):
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit') # <-- this request will be tracked
使用“httplib”集成跟踪的依赖项Dependencies with "httplib" integration
通过 OpenCensus httplib
集成跟踪传出请求。Track your outgoing requests with OpenCensus httplib
integration.
从 PyPI 下载并安装 opencensus-ext-httplib
,然后将其添加到跟踪集成。Download and install opencensus-ext-httplib
from PyPI and add it to the trace integrations. 将跟踪使用 Python3 的 http.client 或 Python2 的 httplib 发送的请求。Requests sent using http.client for Python3 or httplib for Python2 will be tracked.
import http.client as httplib
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer
config_integration.trace_integrations(['httplib'])
conn = httplib.HTTPConnection("www.python.org")
tracer = Tracer(
exporter=AzureExporter(),
sampler=ProbabilitySampler(1.0)
)
conn.request("GET", "http://www.python.org", "", {})
response = conn.getresponse()
conn.close()
使用“django”集成跟踪的依赖项Dependencies with "django" integration
通过 OpenCensus django
集成跟踪传出 Django 请求。Track your outgoing Django requests with the OpenCensus django
integration.
备注
跟踪的唯一传出 Django 请求是对数据库的调用。The only outgoing Django requests that are tracked are calls made to a database. 有关对 Django 应用程序发出的请求,请参阅传入请求。For requests made to the Django application, see incoming requests.
从 PyPI 下载并安装 opencensus-ext-django
,然后将以下行添加到 Django settings.py
文件中的 MIDDLEWARE
节。Download and install opencensus-ext-django
from PyPI and add the following line to the MIDDLEWARE
section in the Django settings.py
file.
MIDDLEWARE = [
...
'opencensus.ext.django.middleware.OpencensusMiddleware',
]
可以提供其他配置。如需完整的参考内容,请阅读自定义。Additional configuration can be provided, read customizations for a complete reference.
OPENCENSUS = {
'TRACE': {
'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1)',
'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter(
connection_string="InstrumentationKey=<your-ikey-here>"
)''',
}
}
使用“mysql”集成跟踪的依赖项Dependencies with "mysql" integration
通过 OpenCensus mysql
集成跟踪 MYSQL 依赖项。Track your MYSQL dependencies with the OpenCensus mysql
integration. 此集成支持 mysql-connector 库。This integration supports the mysql-connector library.
从 PyPI 下载并安装 opencensus-ext-mysql
,然后将以下行添加到代码中。Download and install opencensus-ext-mysql
from PyPI and add the following lines to your code.
from opencensus.trace import config_integration
config_integration.trace_integrations(['mysql'])
使用“pymysql”集成跟踪的依赖项Dependencies with "pymysql" integration
通过 OpenCensus pymysql
集成跟踪 PyMySQL 依赖项。Track your PyMySQL dependencies with the OpenCensus pymysql
integration.
从 PyPI 下载并安装 opencensus-ext-pymysql
,然后将以下行添加到代码中。Download and install opencensus-ext-pymysql
from PyPI and add the following lines to your code.
from opencensus.trace import config_integration
config_integration.trace_integrations(['pymysql'])
使用“postgresql”集成跟踪的依赖项Dependencies with "postgresql" integration
通过 OpenCensus postgresql
集成跟踪 PostgreSQL 依赖项。Track your PostgreSQL dependencies with the OpenCensus postgresql
integration. 此集成支持 psycopg2 库。This integration supports the psycopg2 library.
从 PyPI 下载并安装 opencensus-ext-postgresql
,然后将以下行添加到代码中。Download and install opencensus-ext-postgresql
from PyPI and add the following lines to your code.
from opencensus.trace import config_integration
config_integration.trace_integrations(['postgresql'])
使用“pymongo”集成跟踪的依赖项Dependencies with "pymongo" integration
通过 OpenCensus pymongo
集成跟踪 MongoDB 依赖项。Track your MongoDB dependencies with the OpenCensus pymongo
integration. 此集成支持 pymongo 库。This integration supports the pymongo library.
从 PyPI 下载并安装 opencensus-ext-pymongo
,然后将以下行添加到代码中。Download and install opencensus-ext-pymongo
from PyPI and add the following lines to your code.
from opencensus.trace import config_integration
config_integration.trace_integrations(['pymongo'])
使用“sqlalchemy”集成跟踪的依赖项Dependencies with "sqlalchemy" integration
通过 OpenCensus sqlalchemy
集成使用 SQLAlchemy 跟踪依赖项。Track your dependencies using SQLAlchemy using OpenCensus sqlalchemy
integration. 此集成跟踪 sqlalchemy 包的使用情况,而与基础数据库无关。This integration tracks the usage of the sqlalchemy package, regardless of the underlying database.
from opencensus.trace import config_integration
config_integration.trace_integrations(['sqlalchemy'])