你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:端到端监视应用程序

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

本文适用于:❌ 基本版/标准版 ✔️ 企业版

本快速入门展示如何使用 Application Insights 和 Log Analytics 监视运行 Azure Spring Apps 企业计划的应用。

注意

可以使用所选的任何工具和平台(包括 App Insights、Log Analytics、New Relic、Dynatrace、AppDynamics、Elastic 或 Splunk)来端到端监视 Spring 工作负载。 有关详细信息,请参阅本文后面的使用其他监视工具

先决条件

更新应用程序

必须手动将 Application Insights 连接字符串提供给 Order Service (ASP.NET core) 和 Cart Service (python) 应用程序。 以下说明介绍了如何提供此连接字符串并提高 Application Insights 的采样率。

注意

目前只有 Java 和 NodeJS 应用程序的 buildpack 支持 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 连接字符串,并在 Key Vault 中设置:

    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 应用程序,重启将使它们能够从 Key Vault 访问新添加的检测密钥。

查看日志

可通过两种方式在 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 配置网关中的所有日志:

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 服务注册表中的所有日志:

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 控制器 ProductControllerPaymentController 已由类级别的 @Timed Micrometer 注释检测。

acme-catalog 应用程序启用了以下自定义指标:@Timed: store.products

acem-payment 应用程序启用了以下自定义指标:@Timed: store.payment

可以在“指标”窗格中查看这些自定义指标,如以下屏幕截图所示。

Screenshot showing custom metrics instrumented by Micrometer.

导航到“实时指标”窗格。 可在此处屏幕上查看低延迟 < 1 秒的实时指标,如以下屏幕截图所示:

Screenshot showing the live metrics of all applications.

使用其他监视工具

Azure Spring Apps 企业计划还支持将指标导出到其他工具,包括以下工具:

  • AppDynamics
  • ApacheSkyWalking
  • Dynatrace
  • ElasticAPM
  • NewRelic

可以使用以下命令向 Tanzu 生成服务中的生成器添加更多绑定:

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 ..."

后续步骤

继续学习以下任一可选快速入门: