#mynavi(Azureメモ) #setlinebreak(on); #html(){{ <style> .images img { border: 1px solid #333; } </style> }} * 概要 [#y51ca554] #html(<div class="pl10">) Azureアプリ関数のログの参照方法について記載する。 以下、関数アプリはカスタムハンドラーを利用した Goの実装となっている為、デフォルトでサポートされている言語の場合と異なる箇所もある。 以下、カスタムハンドラーを利用した関数アプリ(Go)となっている為、関数アプリのソースについてはデフォルトのサポート言語の場合と異なる場合もある。 #html(</div>) * 目次 [#v017171e] #contents - 関連 -- [[Azureメモ]] -- [[Azure Functions の異常を検知する]] - 参考 -- [[Application Insights とは何か?>https://docs.microsoft.com/ja-jp/azure/azure-monitor/app/app-insights-overview]] -- [[Application Insights ログベースのメトリック>https://docs.microsoft.com/ja-jp/azure/azure-monitor/platform/app-insights-metrics]] -- [[Application Insights のログベースのメトリックと事前に集計されたメトリック>https://docs.microsoft.com/ja-jp/azure/azure-monitor/app/pre-aggregated-metrics-log-metrics]] -- [[Azure Functions を監視する>https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-monitoring?tabs=cmd]] -- [[Application Insights の使用量とコストを管理する>https://docs.microsoft.com/ja-jp/azure/azure-monitor/app/pricing]] -- [[Azure Monitor でログクエリの使用を開始する>https://docs.microsoft.com/ja-jp/azure/azure-monitor/log-query/get-started-queries]] -- [[Kusto の概要>https://docs.microsoft.com/ja-jp/azure/data-explorer/kusto/concepts/]] -- [[Kusto クエリ言語>https://docs.microsoft.com/ja-jp/azure/data-explorer/kusto/query/]] * Application Insight の有効化 [#yb693fa9] #html(<div class="pl10">) Application Insight を有効化した状態で Functions を作成しておくと、関数アプリのログを一定期間保持し、ログ検索やアラート設定を行う事ができる。 ** 関数アプリをCLI で作成する場合 [#e35b4109] #html(<div class="pl10">) #myterm2(){{ # application insight コンポーネントの作成 az monitor app-insights component create \ --app $insightsName \ --location $insightsRegion \ --resource-group $resourceGroup \ --query-access Enabled \ --retention-time $insightsDays \ ... 保持期間(日数) ※30,60,90,120,180,270,365,550,730 の何れか --subscription $subscriptionId # 関数アプリの作成 az functionapp create \ --name $funcAppName \ : --app-insights $insightsName <-- これ }} #html(</div>) ** 関数アプリをポータルから作成する場合 [#wc017e90] #html(<div class="pl10">) #html(<div class="ib border">) &ref(func_setting_insight1.png,nolink); #html(</div>) #html(</div>) #html(</div>) * カスタムハンドラを使用した関数アプリの場合 [#d749be2a] #html(<div class="pl10">) ** 実装例 [#jf02f66b] #html(<div class="pl10">) カスタムハンドラを使用した関数アプリでは返却データに "Logs" としてログデータを含める事で Application Insight にログを渡す事が出来る。 ※https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-custom-handlers#response-payload 標準出力等に出力した内容も Application Insight には連携されるようだが、格納のされ方が若干異なる(後述)。 goのサンプル #mycode2(){{ : logs := make([]string, 0) // 標準出力に表示 fmt.Println("[INFO] This is std out message1.") fmt.Println("[INFO] This is std out message2.") // 戻り値(Logs)用の配列に追加 logs = append(logs, "[INFO] Sample Log 1.") logs = append(logs, "[INFO] Sample Log 2.") : invokeResponse := InvokeResponse{Logs: logs, ReturnValue: string(returnValue)} // 溜めたログを Logs として返却する js, err := json.Marshal(invokeResponse) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.Write(js) }} #html(</div>) ** ログの参照方法(ポータル) [#kad9a334] #html(<div class="pl10">) ポータルからは対象の関数アプリの画面の [ログ] から検索画面に遷移する事ができる。 検索は [[Kustoというクエリ言語>https://docs.microsoft.com/ja-jp/azure/data-explorer/kusto/query/]] で行うが、上記のようなログデータは ''traces'' というテーブルから検索する事ができる。(Kustoについては後述) [参考] tracesテーブルの定義内容 ... https://docs.microsoft.com/en-us/azure/azure-monitor/reference/tables/traces #html(<div class="images"><div class="ib" style="margin-right: 20px;">) &ref(func_app_log_search0.png,nolink); #html(</div><div class="ib" style="vertical-align: top">) &ref(func_app_log_search1.png,nolink); #html(</div></div>) 他にも Insight のデータを探索する方法はいくつか用意されている。(主にメトリクス) https://docs.microsoft.com/ja-jp/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry #html(</div>) ** ログの参照方法(Azure CLI) [#u69c301d] #html(<div class="pl10">) Azure CLI で検索して JSON を取得する事も可能。 #myterm2(){{ az monitor app-insights query --analytics-query 'Kustoクエリ式' --apps Insight名 -g リソースグループ名 }} 例) #myterm2(){{ query='traces | where cloud_RoleName == "myfuncapp" and message has_cs "[INFO]" | sort by timestamp asc | project timestamp, message | take 2' az monitor app-insights query --analytics-query "${query}" --apps Insight名 -g リソースグループ名 { "tables": [ { "columns": [ { "name": "timestamp", "type": "datetime" }, { "name": "message", "type": "string" } ], "name": "PrimaryResult", "rows": [ [ "2020-08-27T18:12:00.0483909Z", "[INFO] This is std out message1." ], [ "2020-08-27T18:12:00.0485376Z", "[INFO] This is std out message2." ] ] } ] } }} #html(</div>) ** 標準出力 と Logs に設定したログの違い [#s3fa24cd] #html(<div class="pl10">) 標準出力ログとLogに設定したログでは Application Insight への格納のされ方が異なる。 以下は、Insight でログを検索した結果だが、標準出力に出力したもの(1) には、operation_Name が設定されていない事が分かる。 関数アプリ内に複数の関数がある場合、operation_Name を見れば、どの関数から出力されたものかが分かるが、標準出力ログからは判断できない為、 標準出力ログをトレース等に利用する場合は、各ログに関数が識別出来る内容を含む。等の工夫が必要と思われる。 #html(<div class="ib border">) &ref(func_app_log1.png,nolink); #html(</div>) #html(</div>) #html(</div>) * ログファイルの実物は何処から取得出来るのか [#n8eec81a] #html(<div class="pl10">) 以下に説明があるように、関数アプリのコードは、ファイル共有を利用して各インスタンスにマウントされる。 https://docs.microsoft.com/ja-jp/azure/azure-functions/storage-considerations で、このPATH配下にアプリケーションのログも格納されており、ストレージエクスプローラで見る場合ログのPATHは以下の通り。 ※ただし、反映されるまでに数分かかる。(Application Insight もだが) | ログ | 種別 | PATH |h | 関数ログ | File Shares(ファイル共有) | 関数アプリ名/LogFiles/Application/Functions/Function 配下 | | ホストログ | File Shares(ファイル共有) | 関数アプリ名/LogFiles/Application/Functions/Host 配下 | ホストログの格納場所 #html(<div class="images">) &ref(func_app_log2.png,nolink); #html(</div>) ''関数ログには標準出力に出力した内容は含まれない為、関数自体が異常終了した場合等はホスト側のログから調査を行う必要がある。'' #html(</div>) * ログの退避 [#a79bd184] #html(<div class="pl10">) 診断設定を作成する事によってログを永続的に保管しておく事も出来る。 Application Insight のリテンションポリシー(保持期間)を超えて保持しておきたい場合は、こちらも設定しておくのが良いと思われる。 https://docs.microsoft.com/ja-jp/azure/azure-monitor/platform/diagnostic-settings ※ポータルからの設定手順は上記URLに記載がある。 #html(</div>) * Application Insight に送信するカテゴリとログレベルを構成する [#xf45f68d] #html(<div class="pl10">) host.json で構成する事ができる模様。 Azure Functions を監視する - カテゴリとログ レベルを構成する https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-monitoring?tabs=cmd#configure-categories-and-log-levels Azure Functions 2.x 以降の host.json のリファレンス https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-host-json #html(</div>) * アラートの設定 [#zb5a8bf1] #html(<div class="pl10">) [[Azure Functions の異常を検知する]] を参照。 #html(</div>) * 料金 [#k81e81a1] #html(<div class="pl10">) https://azure.microsoft.com/ja-jp/pricing/details/monitor/ には下表の通り記載されている。(2020/8月現在) ** Application Insights [#i5610c11] #html(<div>) | 機能 | 含まれている無料ユニット | 料金 |h | データ インジェスト | 1か月あたり課金アカウントごとに 5GB | ¥374.08/GB | | データ保持 | 90日(※) | 1GB あたり ¥16.80/月 | | 複数ステップ Webテスト | なし | ¥1,120 /テスト/月 | | Web テストの ping | 無制限 | 無料 | ※Application Insights によって取り込まれたデータは 1GB ずつ 90日間無料で保持される。 #html(</div>) #html(</div>)