概要 †Azureアプリ関数のログの参照方法について記載する。 目次 †
Application Insight の有効化 †Application Insight を有効化した状態で Functions を作成しておくと、関数アプリのログを一定期間保持し、ログ検索やアラート設定を行う事ができる。 関数アプリをCLI で作成する場合 †# 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 <-- これ
関数アプリをポータルから作成する場合 †
カスタムハンドラを使用した関数アプリの場合 †実装例 †カスタムハンドラを使用した関数アプリでは返却データに "Logs" としてログデータを含める事で Application Insight にログを渡す事が出来る。 標準出力等に出力した内容も Application Insight には連携されるようだが、格納のされ方が若干異なる(後述)。 goのサンプル :
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)
ログの参照方法(ポータル) †ポータルからは対象の関数アプリの画面の [ログ] から検索画面に遷移する事ができる。 [参考] tracesテーブルの定義内容 ... https://docs.microsoft.com/en-us/azure/azure-monitor/reference/tables/traces
他にも Insight のデータを探索する方法はいくつか用意されている。(主にメトリクス) ログの参照方法(Azure CLI) †Azure CLI で検索して JSON を取得する事も可能。 az monitor app-insights query --analytics-query 'Kustoクエリ式' --apps Insight名 -g リソースグループ名 例) 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."
]
]
}
]
}
標準出力 と Logs に設定したログの違い †標準出力ログとLogに設定したログでは Application Insight への格納のされ方が異なる。 以下は、Insight でログを検索した結果だが、標準出力に出力したもの(1) には、operation_Name が設定されていない事が分かる。
ログファイルの実物は何処から取得出来るのか †以下に説明があるように、関数アプリのコードは、ファイル共有を利用して各インスタンスにマウントされる。 で、このPATH配下にアプリケーションのログも格納されており、ストレージエクスプローラで見る場合ログのPATHは以下の通り。
ホストログの格納場所
関数ログには標準出力に出力した内容は含まれない為、関数自体が異常終了した場合等はホスト側のログから調査を行う必要がある。 ログの退避 †診断設定を作成する事によってログを永続的に保管しておく事も出来る。 ※ポータルからの設定手順は上記URLに記載がある。 Application Insight に送信するカテゴリとログレベルを構成する †host.json で構成する事ができる模様。 Azure Functions を監視する - カテゴリとログ レベルを構成する Azure Functions 2.x 以降の host.json のリファレンス アラートの設定 †料金 †https://azure.microsoft.com/ja-jp/pricing/details/monitor/ には下表の通り記載されている。(2020/8月現在) Application Insights †
※Application Insights によって取り込まれたデータは 1GB ずつ 90日間無料で保持される。 |