使用 OpenCensus Python 跟踪传入请求Track incoming requests with OpenCensus Python

使用 OpenCensus Python 及其各种集成收集传入请求数据。Incoming request data is collected using OpenCensus Python and its various integrations. 跟踪发送到基于常用 Web 框架 djangoflaskpyramid 构建的 Web 应用程序的传入请求数据。Track incoming request data sent to your web applications built on top of the popular web frameworks django, flask and pyramid. 然后,将数据作为 requests 遥测发送到 Azure Monitor 下的 Application Insights。The data is then sent to Application Insights under Azure Monitor as requests telemetry.

首先,使用最新版 OpenCensus Python SDK 检测 Python 应用程序。First, instrument your Python application with latest OpenCensus Python SDK.

跟踪 Django 应用程序Tracking Django applications

  1. PyPI 下载并安装 opencensus-ext-django,然后使用 django 中间件检测应用程序。Download and install opencensus-ext-django from PyPI and instrument your application with the django middleware. 系统会跟踪发送到 django 应用程序的传入请求。Incoming requests sent to your django application will be tracked.

  2. opencensus.ext.django.middleware.OpencensusMiddleware 添加到 settings.py 文件中的 MIDDLEWARE 下。Include opencensus.ext.django.middleware.OpencensusMiddleware in your settings.py file under MIDDLEWARE.

    MIDDLEWARE = (
        ...
        'opencensus.ext.django.middleware.OpencensusMiddleware',
        ...
    )
    
  3. 确保 AzureExporter 已在 settings.py 中的 OPENCENSUS 下正确配置。Make sure AzureExporter is properly configured in your settings.py under OPENCENSUS. 对于来自不想跟踪的 URL 的请求,请将其添加到 BLACKLIST_PATHS 中。For requests from urls that you do not wish to track, add them to BLACKLIST_PATHS.

    OPENCENSUS = {
        'TRACE': {
            'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1)',
            'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter(
                connection_string="InstrumentationKey=<your-ikey-here>"
            )''',
            'BLACKLIST_PATHS': ['https://example.com'],  <--- These sites will not be traced if a request is sent to it.
        }
    }
    

跟踪 Flask 应用程序Tracking Flask applications

  1. PyPI 下载并安装 opencensus-ext-flask,然后使用 flask 中间件检测应用程序。Download and install opencensus-ext-flask from PyPI and instrument your application with the flask middleware. 系统会跟踪发送到 flask 应用程序的传入请求。Incoming requests sent to your flask application will be tracked.

    
    from flask import Flask
    from opencensus.ext.azure.trace_exporter import AzureExporter
    from opencensus.ext.flask.flask_middleware import FlaskMiddleware
    from opencensus.trace.samplers import ProbabilitySampler
    
    app = Flask(__name__)
    middleware = FlaskMiddleware(
        app,
        exporter=AzureExporter(connection_string="InstrumentationKey=<your-ikey-here>"),
        sampler=ProbabilitySampler(rate=1.0),
    )
    
    @app.route('/')
    def hello():
        return 'Hello World!'
    
    if __name__ == '__main__':
        app.run(host='localhost', port=8080, threaded=True)
    
    
  2. 你也可通过 app.config 配置 flask 应用程序。You can also configure your flask application through app.config. 对于来自不想跟踪的 URL 的请求,请将其添加到 BLACKLIST_PATHS 中。For requests from urls that you do not wish to track, add them to BLACKLIST_PATHS.

    app.config['OPENCENSUS'] = {
        'TRACE': {
            'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)',
            'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter(
                connection_string="InstrumentationKey=<your-ikey-here>",
            )''',
            'BLACKLIST_PATHS': ['https://example.com'],  <--- These sites will not be traced if a request is sent to it.
        }
    }
    

跟踪 Pyramid 应用程序Tracking Pyramid applications

  1. PyPI 下载并安装 opencensus-ext-django,然后使用 pyramid 中间件检测应用程序。Download and install opencensus-ext-django from PyPI and instrument your application with the pyramid tween. 系统会跟踪发送到 pyramid 应用程序的传入请求。Incoming requests sent to your pyramid application will be tracked.

    def main(global_config, **settings):
        config = Configurator(settings=settings)
    
        config.add_tween('opencensus.ext.pyramid'
                         '.pyramid_middleware.OpenCensusTweenFactory')
    
  2. 可以直接在代码中配置 pyramid 中间件。You can configure your pyramid tween directly in the code. 对于来自不想跟踪的 URL 的请求,请将其添加到 BLACKLIST_PATHS 中。For requests from urls that you do not wish to track, add them to BLACKLIST_PATHS.

    settings = {
        'OPENCENSUS': {
            'TRACE': {
                'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)',
                'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter(
                    connection_string="InstrumentationKey=<your-ikey-here>",
                )''',
                'BLACKLIST_PATHS': ['https://example.com'],  <--- These sites will not be traced if a request is sent to it.
            }
        }
    }
    config = Configurator(settings=settings)
    

后续步骤Next steps