【應用程序見解 Application Insights】Application Insights 使用 Application Maps 構建請求鏈路視圖
- 2020 年 11 月 6 日
- 筆記
- 【應用程序見解 Application Insights】, Application Insights, Application Maps, 請求鏈路視圖
Applicaotn Insigths 使用 Application Maps 構建請求鏈路視圖
構建系統時,請求的邏輯操作大多數情況下都需要在不同的服務,或接口中完成整個請求鏈路。一個請求可以經歷多個組件,極有可能出現客戶端請求站點1,站點1請求站點2, … 站點N才是最終處理數據然後依次返回。
在這樣的情況,如果有一個直觀的視圖來展示請求在每一個站點上的狀態(成功,失敗),當問題發生時,非常有幫助定位問題發生的地點。藉助Azure 應用程序見解(Application Insights)中的遙測關聯建立的Application Maps就能實現
效果展示
實現原理
Application Insights定義了用於分配遙測關聯的數據模型,每個傳出操作(例如,對另一個組件的 HTTP 調用)是由依賴項遙測表示的。 依賴項遙測也定義了自身的全局獨一無二的 id,此依賴項調用發起的請求遙測將此 id 用作其 operation_parentId。通過operation_Id、operation_parentId 和 request.id,即可以生成分佈式邏輯操作的視圖 (這些字段也定義了遙測調用的因果關係順序)。
實例構建Application Maps
在實例中,這一個請求進行了4段轉發。
第一段:本地代碼訪問APIM(test01.azure-api.cn),
第二段:APIM訪問站點1(lbphptest.chinacloudsites.cn)
第三段:站點1訪問站點2(lbjavatest.chinacloudsites.cn)
第四段:站點2訪問最終處理請求的Azure Function(Http Trigger)( functionapp120201013155425.chinacloudsites.cn).
準備條件
- 創建Application Insights (lbphptest202011050549)
- 創建APIM(test01)
- 創建兩個App Service (lbphptest 和lbjavatest)
- 創建一個Azure Function(functionapp120201013155425)
注:如不熟悉如何創建以上資源,可以導航到文末的參考資料部分
步驟一:在Azure Funciton中啟用並關聯Application Insights服務
進入Azure Funciton門戶,選擇Application Insights功能,根據頁面提示選擇已創建好的Application Insights (lbphptest202011050549).
創建的Function為默認的HttpTrigger模式,測試目的,代碼可以不需任何修改。參考下圖獲取到Function的URL,用於下一步在站點2中調用。
步驟二:在站點2(lbjavatest)中啟用並關聯Application Insights服務,並部署代碼請求Azure Function
進入站點2的門戶頁面,在Application Insights目錄中根據提示Enable Application Insights並選擇相同的Application Insights。
部署代碼調用步驟一中的Azure Function
//Level 3 [HttpGet] [Route("[Action]")] public string Fun([FromQuery] string name) { using (HttpClient httpClient = new HttpClient()) { var url = $"//functionapp120201013155425.chinacloudsites.cn/api/HttpTrigger1?name={name}"; HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url); httpRequest.Headers.Add("Accept", "application/json, text/plain, */*"); var response = httpClient.SendAsync(httpRequest).Result; string responseContent = response.Content.ReadAsStringAsync().Result; return responseContent; } }
步驟三:在站點1(lbphptest)中啟用並關聯Application Insights服務,並部署代碼請求站點2
進入站點1的門戶頁面,在Application Insights目錄中根據提示Enable Application Insights並選擇相同的Application Insights。
部署代碼調用步驟二中的站點2的URL,代碼與訪問Azure Function相似。
//Level 2 [HttpGet] [Route("[Action]")] public string FunSub([FromQuery] string name) { using (HttpClient httpClient = new HttpClient()) { var url = $"//lbjavatest.chinacloudsites.cn/WeatherForecast/fun?name={name}"; HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url); httpRequest.Headers.Add("Accept", "application/json, text/plain, */*"); var response = httpClient.SendAsync(httpRequest).Result; string responseContent = response.Content.ReadAsStringAsync().Result; return responseContent; } }
步驟四:在APIM中啟用並關聯Application Insights服務, 並設置API訪問站點1
進入APIM的門戶頁面,在Application Insights目錄中添加相同的Application Insights。
在APIM配置API訪問站點1(lbphptest)
- 點擊「Add API」 按鈕
- 選擇從App Service中創建
- 選擇站點1(lbphptest)
在接口中添加操作一個新操作,訪問站點1中的接口/weatherforecast/funsub?name={name}
步驟五:在ASP.NET Core代碼中添加Application Insights SDK並配置連接字符串,在接口中訪問APIM.
在本地代碼中添加Application Insights SDK
配置連接字符串(字符串中Application Insights的Overview頁面複製)
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=xxx-xxx-xxx-xxx-xxxxx;EndpointSuffix=applicationinsights.azure.cn;IngestionEndpoint=//chinaeast2-0.in.applicationinsights.azure.cn/"
},
"AllowedHosts": "*"
}
啟動本地程序,並通過在瀏覽器中訪問 //localhost:44323/weatherforecast/Funsubfornt?name=test from local — apim — app 1 – app 2 — function
查看最終效果圖:
【END】
參考文檔:
在 Azure 門戶中創建第一個函數: //docs.azure.cn/zh-cn/azure-functions/functions-create-first-azure-function
適用於 ASP.NET Core 應用程序的 Application Insights : //docs.azure.cn/zh-cn/azure-monitor/app/asp-net-core
關於 API 管理: //docs.azure.cn/zh-cn/api-management/api-management-key-concepts
在 Azure 中創建 ASP.NET Core Web 應用: //docs.azure.cn/zh-cn/app-service/quickstart-dotnetcore?pivots=platform-linux