如何使用 Azure CLI 脚本将 Azure Application Insights 正确连接到 App Service Spring 应用程序?

How to properly connect Azure Application Insights to the App Service Spring Application with Azure CLI scripts?

我的 Spring 引导应用程序通过 Azure 应用服务和 Azure CLI 脚本托管。我还使用 Azure CLI 脚本在同一订阅上部署了 Azure Application Insights。我的下一步是仅使用 az 命令和补充文件将 App Insights 与 App Service 连接起来。

我已阅读 this 有关如何以无代码方式连接 App Insights 的文档。但看起来我仍然缺少很多指标,如请求、依赖项、异常等(尽管我在类路径中有微观指标)。应用洞察 |搜索只有痕迹,所以我尝试从 Azure 门户连接它并且成功了。此集成重新启动了我的应用程序并做了一些我不知道的魔术:

这是我从 Azure 门户连接 App Insights 之前和之后的图像:

查看应用服务的配置,我看到了文档中未描述的几个新值:

{
  "XDT_MicrosoftApplicationInsights_PreemptSdk": "disabled",
  "XDT_MicrosoftApplicationInsights_Mode": "recommended",
  "XDT_MicrosoftApplicationInsights_BaseExtensions": "disabled",
  "SnapshotDebugger_EXTENSION_VERSION": "disabled",
  "InstrumentationEngine_EXTENSION_VERSION": "disabled",
  "DiagnosticServices_EXTENSION_VERSION": "~3",
  "APPINSIGHTS_PROFILERFEATURE_VERSION": "1.0.0",
  "APPINSIGHTS_INSTRUMENTATIONKEY": "key",
  "APPINSIGHTS_SNAPSHOTFEATURE_VERSION": "1.0.0",
  "ApplicationInsightsAgent_EXTENSION_VERSION": "~2"
}

所以我的问题是“如何使用我的 Azure CLI 脚本模拟此按钮,以便它对我的日志和指标产生完全相同的影响?”

在将 jar 文件部署到应用程序服务之前,请确保您在类路径中具有与 AI SDK 的最新依赖项。提供由 Gradle:

导入的一组依赖项
//    Application Insights
    implementation "com.microsoft.azure:applicationinsights-spring-boot-starter:$appInsightsVersion"
    implementation "com.microsoft.azure:applicationinsights-logging-logback:$appInsightsVersion"
    implementation 'com.microsoft.azure:azure-spring-boot-metrics-starter'

编译应用程序后,现在可以使用此 Azure CLI 脚本将 Application Insights 连接到应用服务:

az webapp config appsettings set \
  -n ${APP_NAME} \
  -g ${GROUP_NAME} \
  --settings \
APPINSIGHTS_INSTRUMENTATIONKEY=${APPINSIGHTS_INSTRUMENTATIONKEY} \
JAVA_OPTS="${APP_SERVICE_JAVA_OPTS}"

这是我的 VM 环境变量:APP_SERVICE_JAVA_OPTS="-javaagent:/home/site/wwwroot/applicationinsights-agent.jar -Dserver.port=80"

如您所见,我有一条通往 applicationinsights-agent.jar 的路径,这是我在使用 config-zip 部署期间自己复制的 jar:

cp ./build/libs/app-0.0.1-SNAPSHOT.jar ./deploymentrepo/app.jar
cp ./build/resources/main/applicationinsights-agent*.jar ./deploymentrepo/applicationinsights-agent.jar
cp ./build/resources/main/ApplicationInsights.json ./deploymentrepo/ApplicationInsights.json
cd ./deploymentrepo
zip target.zip -r ./*
az webapp deployment source config-zip \
  --src target.zip \
  -n ${ANALYTICS_APP_NAME} \
  -g ${ANALYTICS_GROUP_NAME}

我的 ApplicationInsights.json 看起来像这样:

{
  "instrumentationSettings": {
    "preview": {
      "roleName": "ApplicationName",
      "heartbeat": {
        "intervalSeconds": 60
      },
      "instrumentation": {
        "logging": {
          "threshold": "INFO"
        },
        "micrometer": {
          "enabled": true
        }
      },
      "selfDiagnostics": {
        "destination": "file",
        "directory": "/var/log/applicationinsights",
        "level": "INFO",
        "maxSizeMB": 10
      }
    }
  }
}