在 Azure Spring Apps 中使用 Application Insights Java In-Process 代理程式
注意
Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。
本文適用於: ✔️標準取用和專用 (預覽) ✔️ 基本/標準 ❌️ 企業版
本文說明如何在 Azure Spring Apps 中使用 Application Insights Java 代理程式來監視應用程式。
透過這項功能,您可以:
- 搜尋具有不同篩選條件的追蹤數據。
- 檢視 Spring 應用程式的相依性對應。
- 檢查要求效能。
- 監視即時實時計量。
- 檢查要求失敗。
- 檢查應用程式計量。
- 檢查應用程式記錄。
Application Insights 可以提供許多可觀察的觀點,包括:
- 應用程式對應
- 效能
- 失敗
- 計量
- 即時計量
- 可用性
- 記錄
使用 Application Insights 功能
啟用 Application Insights 功能時,您可以:
在瀏覽窗格中,選取 [Application Insights] 以檢視 Application Insights 的 [概觀] 頁面。 [概觀] 頁面會顯示所有執行中應用程式的概觀。
選取 [應用程式對應 ] 以查看應用程式之間的呼叫狀態。
選取客戶與服務
petclinic
之間的連結,以查看更多詳細數據,例如來自 SQL 的查詢。選取端點以查看向端點提出要求的所有應用程式。
在瀏覽窗格中,選取 [效能 ] 以查看所有應用程式作業、相依性和角色的效能數據。
在瀏覽窗格中,選取 [失敗] 以查看應用程式的任何非預期失敗或例外狀況。
在瀏覽窗格中,選取 [計量 ],然後選取命名空間以查看 Spring Boot 計量和自定義計量,如果有的話。
在瀏覽窗格中,選取 [即時計量 ],以查看不同維度的即時計量。
在瀏覽窗格中,選取 [可用性] 以監視 Web 應用程式的可用性和回應性,方法是在 Application Insights 中建立可用性測試。
在瀏覽窗格中,選取 [記錄 ] 以檢視所有應用程式的記錄,或在篩選時
cloud_RoleName
檢視一個應用程式的記錄。
使用 Azure 入口網站 管理 Application Insights
使用下列程式啟用 Java In-Process Agent。
移至服務 |服務實例的概觀頁面,然後在 [監視] 區段中選取 [Application Insights]。
選取 [啟用 Application Insights ] 以在 Azure Spring Apps 中啟用 Application Insights。
選取現有的 Application Insights 實例,或建立新的實例。
啟用 Application Insights 時,您可以設定一個選擇性取樣率(預設為 10.0%)。
按一下 [儲存],即可儲存變更。
注意
請勿在不同的 Azure Spring Apps 實例中使用相同的 Application Insights 實例,或顯示混合數據。
您可以使用入口網站來檢查或更新 Application Insights 中的目前設定。
使用 Azure 入口網站 啟用 Application Insights
選取 [Application Insights]。
選取 [編輯系結] 或 [取消系結] 超連結,以啟用 Application Insights。
編輯 Application Insights 或 取樣率,然後選取 [ 儲存]。
停用 Application Insights
選取 [Application Insights]。
選取 [解除系結 ] 以停用 Application Insights。
變更 Application Insights 設定
選取 Application Insights 數據行底下的名稱,以開啟 Application Insights 區段。
在組建服務中編輯Application Insights組建套件系結
若要檢查並更新建置服務中 Application Insights 組建套件系結的目前設定,請遵循下列步驟:
- 選取 [ 建置服務]。
- 選擇您的建置者。
- 選取 [系結] 資料行底下的 [編輯 ]。
Application Insights 設定位於 [系結類型] 資料行底下的 ApplicationInsights 專案中。
選取 [ 系結 ] 超連結,或選取 省略號下的 [編輯 系結],以開啟和編輯 Application Insights 組建套件系結。
編輯系結設定,然後選取 [ 儲存]。
使用 Azure CLI 管理 Application Insights
您可以使用 Azure CLI 命令來管理 Application Insights。 在下列命令中,請務必將 <佔位元> 文字取代為所述的值。 <service-instance-name> 佔位符是指 Azure Spring Apps 實例的名稱。
啟用 Application Insights
若要在建立 Azure Spring Apps 實例時設定 Application Insights,請使用下列命令。 針對 自 app-insights
變數,您可以指定 Application Insights 名稱或資源識別碼。
az spring create \
--resource-group <resource-group-name> \
--name "service-instance-name" \
--app-insights <name-or-resource-ID> \
--sampling-rate <sampling-rate>
az spring create \
--resource-group <resource-group-name> \
--name "service-instance-name" \
--app-insights <name-or-resource-ID> \
--sampling-rate <sampling-rate>
--sku Enterprise
您也可以使用Application Insights 連接字串(慣用)或檢測密鑰,如下列範例所示。
az spring create \
--resource-group <resource-group-name> \
--name <service-instance-name> \
--app-insights-key <connection-string-or-instrumentation-key> \
--sampling-rate <sampling-rate>
az spring create \
--resource-group <resource-group-name> \
--name <service-instance-name> \
--app-insights-key <connection-string-or-instrumentation-key> \
--sampling-rate <sampling-rate>
--sku Enterprise
停用 Application Insights
若要在建立 Azure Spring Apps 實例時停用 Application Insights,請使用下列命令:
az spring create \
--resource-group <resource-group-name> \
--name <service-instance-name> \
--disable-app-insights
az spring create \
--resource-group <resource-group-name> \
--name <service-instance-name> \
--disable-app-insights
--sku Enterprise
檢查 Application Insights 設定
若要檢查現有 Azure Spring Apps 實例的 Application Insights 設定,請使用下列命令:
az spring app-insights show \
--resource-group <resource-group-name> \
--name <service-instance-name>
更新 Application Insights
若要更新 Application Insights 以使用 連接字串(慣用)或檢測密鑰,請使用下列命令:
az spring app-insights update \
--resource-group <resource-group-name> \
--name <service-instance-name> \
--app-insights-key <connection-string-or-instrumentation-key> \
--sampling-rate <sampling-rate>
若要更新 Application Insights 以使用資源名稱或識別碼,請使用下列命令:
az spring app-insights update \
--resource-group <resource-group-name> \
--name <service-instance-name> \
--app-insights <name-or-resource-ID> \
--sampling-rate <sampling-rate>
使用更新命令停用 Application Insights
若要在現有的 Azure Spring Apps 實例上停用 Application Insights,請使用下列命令:
az spring app-insights update \
--resource-group <resource-group-name> \
--name <service-instance-name> \
--disable
管理 Application Insights 組建套件系結
本節僅適用於企業方案,並提供補充上一節的指示。
Azure Spring Apps Enterprise 方案會使用 buildpack 系結,將 Azure 應用程式 Insights 與 類型ApplicationInsights
整合。 如需詳細資訊,請參閱 如何設定 APM 整合和 CA 憑證。
若要建立 Application Insights 組建套件系結,請使用下列命令:
az spring build-service builder buildpack-binding create \
--resource-group <your-resource-group-name> \
--service <your-service-instance-name> \
--name <your-binding-name> \
--builder-name <your-builder-name> \
--type ApplicationInsights \
--properties sampling-percentage=<your-sampling-percentage> \
connection-string=<your-connection-string>
若要列出所有 buildpack 系結,並尋找 Application Insights 系結類型 ApplicationInsights
,請使用下列命令:
az spring build-service builder buildpack-binding list \
--resource-group <your-resource-group-name> \
--service <your-service-resource-name> \
--builder-name <your-builder-name>
若要取代 Application Insights 組建套件系結,請使用下列命令:
az spring build-service builder buildpack-binding set \
--resource-group <your-resource-group-name> \
--service <your-service-instance-name> \
--name <your-binding-name> \
--builder-name <your-builder-name> \
--type ApplicationInsights \
--properties sampling-percentage=<your-sampling-percentage> \
connection-string=<your-connection-string>
若要取得 Application Insights 組建套件系結,請使用下列命令:
az spring build-service builder buildpack-binding show \
--resource-group <your-resource-group-name> \
--service <your-service-instance-name> \
--name <your-binding-name> \
--builder-name <your-builder-name> \
若要刪除 Application Insights 組建套件系結,請使用下列命令:
az spring build-service builder buildpack-binding delete \
--resource-group <your-resource-group-name> \
--service <your-service-instance-name> \
--name <your-binding-name> \
--builder-name <your-builder-name> \
自動化
下列各節說明如何使用 Bicep、Azure Resource Manager 範本(ARM 範本)或 Terraform 將部署自動化。
Bicep
若要使用 Bicep 檔案進行部署,請將下列內容 複製到 main.bicep 檔案。 如需詳細資訊,請參閱 Microsoft.AppPlatform Spring/monitoring 設定。
param springName string
param location string = resourceGroup().location
resource spring 'Microsoft.AppPlatform/Spring@2020-07-01' = {
name: springName
location: location
properties: {}
}
resource monitorSetting 'Microsoft.AppPlatform/Spring/monitoringSettings@2020-11-01-preview' = {
parent: spring
name: 'default'
properties: {
appInsightsInstrumentationKey: '00000000-0000-0000-0000-000000000000'
appInsightsSamplingRate: 88
}
}
ARM 範本
若要使用 ARM 範本進行部署,請將下列內容 複製到 azuredeploy.json 檔案。 如需詳細資訊,請參閱 Microsoft.AppPlatform Spring/monitoring 設定。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"springName": {
"type": "string"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.AppPlatform/Spring",
"apiVersion": "2020-07-01",
"name": "[parameters('springName')]",
"location": "[parameters('location')]",
"properties": {}
},
{
"type": "Microsoft.AppPlatform/Spring/monitoringSettings",
"apiVersion": "2020-11-01-preview",
"name": "[format('{0}/{1}', parameters('springName'), 'default')]",
"properties": {
"appInsightsInstrumentationKey": "00000000-0000-0000-0000-000000000000",
"appInsightsSamplingRate": 88
},
"dependsOn": [
"[resourceId('Microsoft.AppPlatform/Spring', parameters('springName'))]"
]
}
]
}
Terraform
針對 Terraform 部署,請使用下列範本。 如需詳細資訊,請參閱 azurerm_spring_cloud_service。
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
}
resource "azurerm_application_insights" "example" {
name = "tf-test-appinsights"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
application_type = "web"
}
resource "azurerm_spring_cloud_service" "example" {
name = "example-springcloud"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku_name = "S0"
config_server_git_setting {
uri = "https://github.com/Azure-Samples/piggymetrics"
label = "config"
search_paths = ["dir1", "dir2"]
}
trace {
connection_string = azurerm_application_insights.example.connection_string
sample_rate = 10.0
}
tags = {
Env = "staging"
}
}
企業方案中的自動化擱置支援。 一旦檔可供使用,就會立即新增檔。
Java 代理程式更新/升級
Java 代理程式會使用 JDK 定期更新/升級,這可能會影響下列案例。
注意
JDK 版本每年每季更新/升級。
- 更新/升級之前使用 JAVA 代理程式的現有應用程式不會受到影響。
- 更新/升級之後建立的應用程式會使用新版本的 JAVA 代理程式。
- 先前未使用 JAVA 代理程式的現有應用程式需要重新開機或重新部署,才能使用新版本的 JAVA 代理程式。
更新組建套件時,JAVA 代理程式會更新/升級。
JAVA 代理程式組態熱載入
Azure Spring Apps 已啟用熱載入機制,可調整代理程式設定的設定,而不需要重新開機應用程式。
注意
熱載入機制在幾分鐘內有延遲。
- 先前啟用 JAVA 代理程式時,Application Insights 實例和/或 SamplingRate 的變更不需要重新開機應用程式。
- 如果您啟用 JAVA 代理程式,則必須重新開機應用程式。
- 當您停用 JAVA 代理程式時,應用程式會在幾分鐘後停止傳送所有監視資料。 您可以重新開機應用程式,以從 JAVA 執行時間環境中移除代理程式。
Azure Spring Apps 與 Application Insights 之間的概念比對
Azure Spring Apps | Application Insights |
---|---|
App |
* 應用程式對應/角色 * 即時計量/角色 * 失敗/角色/雲端角色 * 效能/角色/可能的角色 |
App Instance |
* 應用程式對應/角色實例 * 即時計量/服務名稱 * 失敗/角色/雲端實例 * 效能/角色/可能實例 |
Azure Spring Apps 的名稱 App Instance
會在下列案例中變更或產生:
- 您可以建立新的應用程式。
- 您可以將 JAR 檔案或原始碼部署到現有的應用程式。
- 您起始藍色/綠色部署。
- 您重新啟動應用程式。
- 您停止部署應用程式,然後重新啟動它。
當數據儲存在 Application Insights 中時,它會包含自啟用 Java 代理程式後所建立或部署的 Azure Spring Apps 應用程式實例歷程記錄。 例如,在 Application Insights 入口網站中,您可以看到昨天建立的應用程式數據,但在特定的時間範圍內刪除,例如過去 24 小時。 下列案例顯示其運作方式:
- 您今天上午 8:00 左右從已啟用 Java 代理程式的 Azure Spring Apps 建立應用程式,然後您今天上午 8:10 左右將 JAR 檔案部署到此應用程式。 經過一些測試,您今天上午 8:30 將程式代碼變更並部署新的 JAR 檔案至此應用程式。 然後,您休息一下,當您在上午 11:00 左右回來時,會檢查 Application Insights 的某些數據。 你看:
- 應用程式對應中的三個實例,過去24小時內具有時間範圍,以及失敗、效能和計量。
- 應用程式對應中的一個實例,其時間範圍在最後一小時,以及失敗、效能和計量。
- 即時計量中的一個實例。
- 您今天上午 8:00 左右從已啟用 Java 代理程式的 Azure Spring Apps 建立應用程式,然後您今天上午 8:10 左右將 JAR 檔案部署到此應用程式。 今天上午 8:30 左右,您會嘗試使用另一個 JAR 檔案進行藍色/綠色部署。 目前,您有兩個此應用程式的部署。 在今天上午 11:00 左右休息之後,您想要從 Application Insights 檢查一些數據。 你看:
- 應用程式對應中的三個實例,過去24小時內具有時間範圍,以及失敗、效能和計量。
- 應用程式對應中的兩個實例,其時間範圍在最後一小時,以及失敗、效能和計量。
- 即時計量中的兩個實例。