#author("2020-08-27T09:29:07+00:00","","")
#mynavi(Azureメモ)
#setlinebreak(on);

* 概要 [#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 \
    --subscription $subscriptionId

# 関数アプリの作成
az functionapp create \
  --name $funcAppName \
    :
  --app-insights $insightsName    <-- これ
}}
※ retention-time は ログの保持期間(日数)で 30,60,90,120,180,270,365,550,730 の何れかを設定する。
#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">)

カスタムハンドラを使用した関数アプリの場合は、Insight に扱ってもらう為には、戻り値として Logs を返却しておく必要がある。
※https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-custom-handlers#response-payload

goの場合
#mycode2(){{

    logs := make([]string, 0)
    :
    :
    logs = append(logs, fmt.Sprintf("[INFO] %s", "サンプルログ1"))
    logs = append(logs, fmt.Sprintf("[INFO] %s", "サンプルログ2"))

    fmt.Printf("[INFO] 標準出力にメッセージを出力")

    invokeResponse := InvokeResponse{Logs: logs, ReturnValue: string(returnValue)}
    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>)

* ホストのログは何処で見れるのか [#b4e1d41c]
#html(<div class="pl10">)

以下に説明があるように、関数アプリのコードは、ファイル共有を利用して各インスタンスにマウントされる。
https://docs.microsoft.com/ja-jp/azure/azure-functions/storage-considerations

で、このPATH配下にアプリケーションのログも格納されており、ストレージエクスプローラで見る場合ログのPATHは以下の通り。

| ログ | 種別 | PATH |h
| 関数アプリのログ | File Shares(ファイル共有) | 関数アプリ名/LogFiles/Application/Functions/Function 配下 |
| ホストのログ | File Shares(ファイル共有) | 関数アプリ名/LogFiles/Application/Functions/Host 配下 |

#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>)

トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS