事象
- PythonのFlaskで、Azure Monitor(Application Insights)にログを送信するために、azure-monitor-opentelemetryのconfigure_azure_monitor()を使用します。
import logging from flask import Flask from azure.monitor.opentelemetry import configure_azure_monitor CONNECTION_STRING = "InstrumentationKey=xxx;IngestionEndpoint=xxx" configure_azure_monitor( connection_string=CONNECTION_STRING, ) app = Flask(__name__) logger = logging.getLogger(__name__) @app.route("/") def hello_world(): return "<h1>Hello, Flask!</h1>" - ですが、次のエラーでFlaskを起動できなくなります。configure_azure_monitor()をコメントアウトすると、Flaskを起動できます。
Error: Failed to find Flask application or factory in module 'app'. Use 'app:name' to specify one. - 実行環境はWSL(Ubuntu24.04)です。python 3.14.2, azure-monitor-opentelemetry 1.8.5 を使用しました。
対応
- 両者のパッケージが干渉するようです。importを含め、Flask関連の処理を後に移動するとエラーが解消しました。
import logging from azure.monitor.opentelemetry import configure_azure_monitor CONNECTION_STRING = "InstrumentationKey=xxx;IngestionEndpoint=xxx" configure_azure_monitor( connection_string=CONNECTION_STRING, ) from flask import Flask app = Flask(__name__) logger = logging.getLogger(__name__) @app.route("/") def hello_world(): return "<h1>Hello, Flask!</h1>" - この辺のサンプルの内容を参考にさせていただきました。GitHub
This repository is for active development of the Azure SDK f…
- なお、FastAPIではこのような問題はありませんでしたが、次のような情報もあります。
Python で OpenTelemetry に関する問題のトラブルシューティングを行う方法について説明します。 Azu…
