OpenCensus Python とその統合により、受信要求データが収集されます。 一般的な Web フレームワーク Django、Flask、Pyramid 上に構築された Web アプリケーションに送信された受信要求データを追跡できます。 Application Insights ではデータを requests テレメトリとして受信します。
まず、最新の OpenCensus Python SDK を使用して Python アプリケーションをインストルメント化します。
Django アプリケーションを追跡する
PyPI から
opencensus-ext-djangoをダウンロードしてインストールします。djangoミドルウェアを使用してアプリケーションをインストルメント化します。 Django アプリケーションに送信された受信要求が追跡されます。settings.pyファイルのMIDDLEWAREにopencensus.ext.django.middleware.OpencensusMiddlewareを含めます。MIDDLEWARE = ( ... 'opencensus.ext.django.middleware.OpencensusMiddleware', ... )settings.pyのOPENCENSUSに AzureExporter が正しく構成されていることを確認します。 追跡したくない URL からの要求については、その URL をEXCLUDELIST_PATHSに追加します。OPENCENSUS = { 'TRACE': { 'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1)', 'EXPORTER': '''opencensus.ext.azure.trace_exporter.AzureExporter( connection_string="InstrumentationKey=<your-ikey-here>" )''', 'EXCLUDELIST_PATHS': ['https://example.com'], <--- These sites will not be traced if a request is sent to it. } }
Django サンプル アプリケーションは、Azure Monitor OpenCensus Python サンプル リポジトリ内にあります。
Flask アプリケーションを追跡する
PyPI から
opencensus-ext-flaskをダウンロードしてインストールします。flaskミドルウェアを使用してアプリケーションをインストルメント化します。 Flask アプリケーションに送信された受信要求が追跡されます。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)app.configを使用してflaskアプリケーションを構成することもできます。 追跡したくない URL からの要求については、その URL をEXCLUDELIST_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>", )''', 'EXCLUDELIST_PATHS': ['https://example.com'], <--- These sites will not be traced if a request is sent to it. } }Note
Docker 環境で uWSGI 下の Flask を実行するには、まず uWSGI 設定ファイル (uwsgi.ini) に
lazy-apps = trueを追加する必要があります。 詳細については、「問題の説明」を参照してください。
要求を追跡する Flask サンプル アプリケーションは、Azure Monitor OpenCensus Python サンプル リポジトリ内にあります。
Pyramid アプリケーションを追跡する
PyPI から
opencensus-ext-djangoをダウンロードしてインストールします。pyramidtween を使用してアプリケーションをインストルメント化します。 Pyramid アプリケーションに送信された受信要求が追跡されます。def main(global_config, **settings): config = Configurator(settings=settings) config.add_tween('opencensus.ext.pyramid' '.pyramid_middleware.OpenCensusTweenFactory')コードで直接
pyramidtween を構成できます。 追跡したくない URL からの要求については、その URL をEXCLUDELIST_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>", )''', 'EXCLUDELIST_PATHS': ['https://example.com'], <--- These sites will not be traced if a request is sent to it. } } } config = Configurator(settings=settings)
FastAPI アプリケーションを追跡する
次の依存関係が必要です。
-
運用設定では、gunicorn を使用して uvicorn をデプロイすることをお勧めします。
PyPI から
opencensus-ext-fastapiをダウンロードしてインストールします。pip install opencensus-ext-fastapifastapiミドルウェアを使用してアプリケーションをインストルメント化します。from fastapi import FastAPI from opencensus.ext.fastapi.fastapi_middleware import FastAPIMiddleware app = FastAPI(__name__) app.add_middleware(FastAPIMiddleware) @app.get('/') def hello(): return 'Hello World!'アプリケーションを実行します。 FastAPI アプリケーションに対して行われた呼び出しは、自動的に追跡されます。 テレメトリは、直接 Azure Monitor にログされます。