快速入門:監視應用程式端對端

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但是您暫時還是會在某些位置看到舊的名稱。我們正在致力更新螢幕擷取畫面、影片和圖表等資產。

本文適用於:❌ 基本/標準✔️企業

本快速入門說明如何使用Application Insights和Log Analytics監視執行 Azure Spring Apps 企業版方案的應用程式。

注意

您可以使用您選擇的任何工具和平臺來監視 Spring 工作負載,包括 App Insights、Log Analytics、New Relic、Dynatrace、AppDynamics、Elastic 或 Splunk。 如需詳細資訊,請參閱 本文稍後使用其他監視工具

必要條件

更新應用程式

您必須手動將 Application Insights 連接字串 提供給 Order Service (ASP.NET core) 和 Cart Service (python) 應用程式。 下列指示說明如何提供此 連接字串,並將取樣率提高到Application Insights。

注意

目前只有 Java 和 NodeJS 應用程式的組建套件支援 Application Insights 檢測。

  1. 使用下列命令建立變數來保存資源名稱。 請務必將佔位元取代為您自己的值。 Azure Spring Apps 服務實例的名稱長度必須介於 4 到 32 個字元之間,且只能包含小寫字母、數位和連字元。 服務名稱的第一個字元必須是字母,最後一個字元必須是字母或數位。

    export RESOURCE_GROUP="<resource-group-name>"
    export APP_INSIGHTS_NAME="<app-insights-name>"
    export KEY_VAULT_NAME="<key-vault-name>"
    export AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME="<Azure-Spring-Apps-service-instance-name>"
    

    注意

    根據預設,APP_INSIGHTS_NAME與AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME相同。

  2. 使用下列命令來擷取 Application Insights 連接字串,並在 金鑰保存庫 中加以設定:

    export CONNECTION_STRING=$(az monitor app-insights component show \
        --resource-group ${RESOURCE_GROUP} \
        --app ${APP_INSIGHTS_NAME} \
        --query "connectionString" \
        --output tsv)
    
    az keyvault secret set \
        --vault-name ${KEY_VAULT_NAME} \
        --name "ApplicationInsights--ConnectionString" \
        --value ${CONNECTION_STRING}
    
  3. 使用下列命令來更新 Application Insights 系結的取樣率,以增加可用的數據量:

    az spring build-service builder buildpack-binding set \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --builder-name default \
        --name default \
        --type ApplicationInsights \
        --properties sampling-rate=100 connection_string=${CONNECTION_STRING}
    
  4. 使用下列命令重新啟動應用程式以重載設定:

    az spring app restart \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --name cart-service
    
    az spring app restart \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --name order-service
    
    az spring app restart \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --name catalog-service
    
    az spring app restart \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --name frontend
    
    az spring app restart \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --name payment-service
    
  5. 如果您已設定 單一登入,請使用下列命令重新啟動應用程式以重載身分識別服務應用程式組態:

    az spring app restart \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --name identity-service
    

    針對 Java 和 NodeJS 應用程式,重新啟動可讓新的取樣率生效。 針對非 Java 應用程式,重新啟動可讓他們從 金鑰保存庫 存取新新增的檢測密鑰。

檢視記錄

有兩種方式可查看 Azure Spring Apps 上的記錄:針對具有進階查詢功能的匯總記錄,針對每個應用程式實例的即時記錄進行記錄串流或 Log Analytics

使用記錄串流

透過應用程式移動、檢視目錄和下訂單,以產生應用程式中的流量。 使用下列命令持續產生流量,直到取消為止:

export GATEWAY_URL=$(az spring gateway show \
    --resource-group ${RESOURCE_GROUP} \
    --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
    --query "properties.url" \
    --output tsv)

export GATEWAY_URL=https://${GATEWAY_URL} 

cd azure-spring-apps-enterprise/load-test/traffic-generator
./gradlew gatlingRun-com.vmware.acme.simulation.GuestSimulation.java

使用下列命令,從目錄服務應用程式取得最新的 100 行應用程式控制台記錄:

az spring app logs \
    --resource-group ${RESOURCE_GROUP} \
    --name catalog-service \
    --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
    --lines 100

藉由新增 --follow 選項,您可以從應用程式取得實時記錄串流。 使用下列命令嘗試目錄服務應用程式的記錄串流:

az spring app logs \
    --resource-group ${RESOURCE_GROUP} \
    --name catalog-service \
    --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
    --follow

提示

您可以使用 az spring app logs --help 來探索更多參數和記錄數據流功能。

使用 Log Analytics

流覽至 Azure 入口網站,然後開啟您所建立的Log Analytics 實例。 您可以在建立 Azure Spring Apps 服務實例的相同資源群組中找到 Log Analytics 實例。

在 [Log Analytics] 頁面上,選取 [ 記錄 ] 窗格,然後針對 Azure Spring Apps 執行下列任何範例查詢。

輸入並執行下列 Kusto 查詢以檢視應用程式記錄:

AppPlatformLogsforSpring
| where TimeGenerated > ago(24h)
| limit 500
| sort by TimeGenerated
| project TimeGenerated, AppName, Log

此查詢會產生類似下列螢幕快照所示的結果:

Screenshot of Azure portal showing example output from all application logs query.

輸入並執行下列 Kusto 查詢以檢視 catalog-service 應用程式記錄:

AppPlatformLogsforSpring
| where AppName has "catalog-service"
| limit 500
| sort by TimeGenerated
| project TimeGenerated, AppName, Log

此查詢會產生類似下列螢幕快照所示的結果:

Screenshot of Azure portal showing example output from Catalog Service application logs.

輸入並執行下列 Kusto 查詢,以查看每個應用程式擲回的錯誤和例外狀況:

AppPlatformLogsforSpring
| where Log contains "error" or Log contains "exception"
| extend FullAppName = strcat(ServiceName, "/", AppName)
| summarize count_per_app = count() by FullAppName, ServiceName, AppName, _ResourceId
| sort by count_per_app desc
| render piechart

此查詢會產生類似下列螢幕快照所示的結果:

Screenshot of Azure portal showing example output from the Ingress Logs.

輸入並執行下列 Kusto 查詢,以查看 Azure Spring Apps 的輸入呼叫中的所有專案:

AppPlatformIngressLogs
| project TimeGenerated, RemoteAddr, Host, Request, Status, BodyBytesSent, RequestTime, ReqId, RequestHeaders
| sort by TimeGenerated

輸入並執行下列 Kusto 查詢,以查看來自 Azure Spring Apps 所管理之受控 Spring Cloud Config Gateway 的所有記錄:

AppPlatformSystemLogs
| where LogType contains "SpringCloudGateway"
| project TimeGenerated,Log

此查詢會產生類似下列螢幕快照所示的結果:

Screenshot of Azure portal showing example output from the Spring Cloud Gateway Logs.

輸入並執行下列 Kusto 查詢,以查看來自 Azure Spring Apps 所管理之受控 Spring Cloud Service Registry 的所有記錄:

AppPlatformSystemLogs
| where LogType contains "ServiceRegistry"
| project TimeGenerated, Log

此查詢會產生類似下列螢幕快照所示的結果:

Screenshot of Azure portal showing example output from service registry logs.

使用追蹤

在 Azure 入口網站 中,開啟 Azure Spring Apps 所建立的 Application Insights 實例,並開始監視 Spring Boot 應用程式。 您可以在建立 Azure Spring Apps 服務實例的相同資源群組中找到 Application Insights 實例。

瀏覽至 [ 應用程式對應 ] 窗格,其類似下列螢幕快照:

Screenshot of Azure portal showing the Application Map of Azure Application Insights.

瀏覽至 [ 效能] 窗格,其類似下列螢幕快照:

Screenshot of Azure portal showing the Performance pane of Azure Application Insights.

流覽至 [ 效能/相依性 ] 窗格。 您可以在這裡看到相依性的效能號碼,特別是 SQL 呼叫,類似於下列螢幕快照所示的內容:

Screenshot of Azure portal showing the Dependencies section of the Performance pane of Azure Application Insights.

流覽至 [ 效能/角色 ] 窗格。 您可以在這裡檢視個別實體或角色的效能計量,類似於下列螢幕快照所示的內容:

Screenshot of Azure portal showing the Roles section of the Performance pane of Azure Application Insights.

選取 SQL 呼叫以檢視內容中的端對端交易,類似於下列螢幕快照所示的內容:

Screenshot of Azure portal showing the end-to-end transaction of an S Q L call.

流覽至 [ 失敗/例外狀況 ] 窗格。 您可以在這裡看到例外狀況的集合,類似於下列螢幕快照所示的內容:

Screenshot of Azure portal showing application failures graphed.

檢視計量

流覽至 [ 計量] 窗格。 您可以在這裏查看 Spring Boot 應用程式、Spring Cloud 模組和相依性所貢獻的計量。 下列螢幕快照中的圖表顯示已使用http_server_requests堆積記憶體:

Screenshot of Azure portal showing metrics over time graph.

Spring Boot 會註冊大量的核心計量:JVM、CPU、Tomcat、Logback 等等。 Spring Boot 自動設定可檢測 Spring MVC 所處理的要求。 REST 控制器 ProductController ,並且 PaymentController 已由類別層級的 @Timed Micrometer 註釋進行檢測。

應用程式acme-catalog已開啟下列自訂計量: @Timedstore.products

應用程式acem-payment已開啟下列自訂計量: @Timedstore.payment

您可以在 [計量] 窗格中看到這些自定義計量,如下列螢幕快照所示。

Screenshot showing custom metrics instrumented by Micrometer.

流覽至 [ 即時計量] 窗格。 您可以在這裡看到畫面上低延遲 < 1 秒的即時計量,如下列螢幕快照所示:

Screenshot showing the live metrics of all applications.

使用其他監視工具

Azure Spring Apps Enterprise 方案也支援將計量導出至其他工具,包括下列工具:

  • AppDynamics
  • ApacheSkyWalking
  • Dynatrace
  • ElasticAPM
  • NewRelic

您可以使用下列命令,將更多系結新增至 Tanzu Build Service 中的產生器:

az spring build-service builder buildpack-binding create \
    --resource-group <resource-group-name> \
    --service <Azure-Spring-Apps-service-instance-name> \
    --builder-name <builder-name> \
    --name <binding-name> \
    --type <ApplicationInsights|AppDynamics|ApacheSkyWalking|Dynatrace|ElasticAPM|NewRelic> \
    --properties <connection-properties>
    --secrets <secret-properties>

清除資源

如果您打算繼續進行後續的快速入門和教學課程,您可以讓這些資源留在原處。 如果不再需要,請刪除資源群組,這會刪除資源群組中的資源。 若要使用 Azure CLI 刪除資源群組,請使用下列命令:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName &&
echo "Press [ENTER] to continue ..."

下一步

繼續進行下列任一選擇性快速入門: