💬

【Azure Functions】 - Application Insightsのログが表示されない問題を解決する方法

2025/01/20に公開

執筆日

2025/1/20

発生事象

以下の記事のようにHTTPトリガーのFunctionsを構築し、テストを実行しました。

https://zenn.dev/headwaters/articles/746a73656cfbbe

その際に、Application Insightsでログを確認したのですが何も表示されませんでした。トランザクション検索で検索しても0件でした。


対処方法

以下のDocsのようにAzure Monitor OpenTelemetryを有効にする。
https://learn.microsoft.com/ja-jp/azure/azure-monitor/app/opentelemetry-enable?tabs=python

手順

  1. requirements.txt に以下を追加する

    azure-monitor-opentelemetry
    
  2. local.setting.json に以下を追加する

    "APPLICATIONINSIGHTS_CONNECTION_STRING":<Application insightsの接続文字列>
    
  3. function_app.py に以下を組み込む

    import os
    from azure.monitor.opentelemetry import configure_azure_monitor
    
    # Retrieve the connection string from environment variables
    connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
    
    # Configure OpenTelemetry to use Azure Monitor with the connection string
    configure_azure_monitor(connection_string=connection_string)
    
  4. 環境をセットアップし、Azure Functionsを開始する

    python -m venv .venv
    .venv\scripts\activate
    pip install -r .\requirements.txt
    func start
    
  5. テストを実施

  6. Application Insightsのログを確認し、ログが出ていることを確認する

ヘッドウォータース

Discussion