#author("2020-08-27T18:44:38+00:00","","") #mynavi(Azureメモ) #setlinebreak(on); #html(){{ <style> .images img { border: 1px solid #333; } </style> }} * 概要 [#y51ca554] #html(<div class="pl10">) #TODO 以下、関数アプリはカスタムハンドラーを利用した Goの実装となっている為、デフォルトでサポートされている言語の場合と異なる箇所もある。 #html(</div>) * 目次 [#v017171e] #contents - 関連 -- [[Azureメモ]] - 参考 -- [[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 App Service のアプリの監視>https://docs.microsoft.com/ja-jp/azure/app-service/web-sites-monitor]] -- [[Azure での Web アプリケーションの監視>https://docs.microsoft.com/ja-jp/azure/architecture/reference-architectures/app-service-web-app/app-monitoring]] -- [[Microsoft Azure のアラートの概要>https://docs.microsoft.com/ja-jp/azure/azure-monitor/platform/alerts-overview]] -- [[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>) ** 標準出力 と 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(){{ <pre style="background: #eee; font-size: 120% !important; margin: 10px 0; display: inline-block;"> Application Insight のデータは <a href="https://docs.microsoft.com/ja-jp/azure/data-explorer/kusto/query/" target="_blank">Kustoというクエリ言語</a> で検索するが、上記のようなログデータは traces テーブルから検索する事ができる。 ※ポータルからは対象の関数アプリの画面の [ログ] から検索画面に遷移可能。 </pre> }} #html(</div>) #html(</div>) * 実際のログファイルは何処から取得するのか [#mdcd0425] #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 ポータルからは以下の手順で設定する事が可能。 #html(</div>) * アラートの設定 [#zb5a8bf1] #html(<div class="pl10">) Azure Monitor でログの検索、アラートの設定を行う事が出来る。 //Azure Monitor の統合アラート エクスペリエンス <- 以前は Log Analytics と Application Insights によって管理されていたアラートも含まれるようになった。 クエリサンプル #mycode2(){{ traces | where operation_Name=="BlobTrigger2" and message has_cs "test" traces | summarize errorCount=countif((operation_Name=="BlobTrigger2" and message has_cs "test" and timestamp > ago(1h)) > 0) }} #html(</div>)