Muokkaa

Jaa


How to monitor Spring Boot apps with Elastic APM Java Agent

Note

The Basic, Standard, and Enterprise plans will be deprecated starting from mid-March, 2025, with a 3 year retirement period. We recommend transitioning to Azure Container Apps. For more information, see the Azure Spring Apps retirement announcement.

The Standard consumption and dedicated plan will be deprecated starting September 30, 2024, with a complete shutdown after six months. We recommend transitioning to Azure Container Apps. For more information, see Migrate Azure Spring Apps Standard consumption and dedicated plan to Azure Container Apps.

This article applies to: ✅ Basic/Standard ❎ Enterprise

This article explains how to use Elastic APM Agent to monitor Spring Boot applications running in Azure Spring Apps.

With the Elastic Observability Solution, you can achieve unified observability to:

The following video introduces unified observability for Spring Boot applications using Elastic.


Prerequisites

Deploy the Spring Petclinic application

This article uses the Spring Petclinic sample to walk through the required steps. Use the following steps to deploy the sample application:

  1. Follow the steps in Deploy Spring Boot apps using Azure Spring Apps and MySQL until you reach the Deploy Spring Boot applications and set environment variables section.

  2. Use the Azure Spring Apps extension for Azure CLI with the following command to create an application to run in Azure Spring Apps:

    az spring app create \
       --resource-group <your-resource-group-name> \
       --service <your-Azure-Spring-Apps-instance-name> \
       --name <your-app-name> \
       --is-public true
    

Enable custom persistent storage for Azure Spring Apps

Use the following steps to enable custom persistent storage:

  1. Follow the steps in How to enable your own persistent storage in Azure Spring Apps.

  2. Use the following Azure CLI command to add persistent storage for your Azure Spring Apps apps.

    az spring app append-persistent-storage \
       --resource-group <your-resource-group-name> \
       --service <your-Azure-Spring-Apps-instance-name> \
       --name <your-app-name> \
       --persistent-storage-type AzureFileVolume \
       --share-name <your-Azure-file-share-name> \
       --mount-path <unique-mount-path> \
       --storage-name <your-mounted-storage-name>
    

Activate Elastic APM Java Agent

Before proceeding, you need your Elastic APM server connectivity information handy, which assumes you've deployed Elastic on Azure. For more information, see How to deploy and manage Elastic on Microsoft Azure. To get this information, use the following steps:

  1. In the Azure portal, go to the Overview page of your Elastic deployment, then select Manage Elastic Cloud Deployment.

    Screenshot of the Azure portal Elasticsearch (Elastic Cloud) page.

  2. Under your deployment on Elastic Cloud Console, select the APM & Fleet section to get Elastic APM Server endpoint and secret token.

    Screenshot of the Elastic APM & Fleet page with Copy endpoint and APM Server secret token highlighted.

  3. Download Elastic APM Java Agent from Maven Central.

    Screenshot of Maven Central with jar download highlighted.

  4. Upload Elastic APM Agent to the custom persistent storage you enabled earlier. Go to Azure Fileshare and select Upload to add the agent JAR file.

    Screenshot of the Azure portal that shows the Upload files pane of the File share page.

  5. After you have the Elastic APM endpoint and secret token, use the following command to activate Elastic APM Java agent when deploying applications. The placeholder <agent-location> refers to the mounted storage location of the Elastic APM Java Agent.

    az spring app deploy \
        --name <your-app-name> \
        --artifact-path <unique-path-to-your-app-jar-on-custom-storage> \
        --jvm-options='-javaagent:<elastic-agent-location>' \
        --env ELASTIC_APM_SERVICE_NAME=<your-app-name> \
              ELASTIC_APM_APPLICATION_PACKAGES='<your-app-package-name>' \
              ELASTIC_APM_SERVER_URL='<your-Elastic-APM-server-URL>' \
              ELASTIC_APM_SECRET_TOKEN='<your-Elastic-APM-secret-token>'
    

Automate provisioning

You can also run a provisioning automation pipeline using Terraform, Bicep, or an Azure Resource Manager template (ARM template). This pipeline can provide a complete hands-off experience to instrument and monitor any new applications that you create and deploy.

Automate provisioning using Terraform

To configure the environment variables in a Terraform template, add the following code to the template, replacing the <...> placeholders with your own values. For more information, see Manages an Active Azure Spring Apps Deployment.

resource "azurerm_spring_cloud_java_deployment" "example" {
  ...
  jvm_options = "-javaagent:<elastic-agent-location>"
  ...
    environment_variables = {
      "ELASTIC_APM_SERVICE_NAME"="<your-app-name>",
      "ELASTIC_APM_APPLICATION_PACKAGES"="<your-app-package>",
      "ELASTIC_APM_SERVER_URL"="<your-Elastic-APM-server-URL>",
      "ELASTIC_APM_SECRET_TOKEN"="<your-Elastic-APM-secret-token>"
  }
}

Automate provisioning using a Bicep file

To configure the environment variables in a Bicep file, add the following code to the file, replacing the <...> placeholders with your own values. For more information, see Microsoft.AppPlatform Spring/apps/deployments.

deploymentSettings: {
  environmentVariables: {
    ELASTIC_APM_SERVICE_NAME='<your-app-name>',
    ELASTIC_APM_APPLICATION_PACKAGES='<your-app-package>',
    ELASTIC_APM_SERVER_URL='<your-Elastic-APM-server-URL>',
    ELASTIC_APM_SECRET_TOKEN='<your-Elastic-APM-secret-token>'
  },
  jvmOptions: '-javaagent:<elastic-agent-location>',
  ...
}

Automate provisioning using an ARM template

To configure the environment variables in an ARM template, add the following code to the template, replacing the <...> placeholders with your own values. For more information, see Microsoft.AppPlatform Spring/apps/deployments.

"deploymentSettings": {
  "environmentVariables": {
    "ELASTIC_APM_SERVICE_NAME"="<your-app-name>",
    "ELASTIC_APM_APPLICATION_PACKAGES"="<your-app-package>",
    "ELASTIC_APM_SERVER_URL"="<your-Elastic-APM-server-URL>",
    "ELASTIC_APM_SECRET_TOKEN"="<your-Elastic-APM-secret-token>"
  },
  "jvmOptions": "-javaagent:<elastic-agent-location>",
  ...
}

Upgrade Elastic APM Java Agent

To plan your upgrade, see Upgrade versions for Elastic Cloud on Azure, and Breaking Changes for APM. After you've upgraded APM Server, upload the Elastic APM Java agent JAR file in the custom persistent storage. Then, restart your apps with the updated JVM options pointing to the upgraded Elastic APM Java agent JAR.

Monitor applications and metrics with Elastic APM

Use the following steps to monitor applications and metrics:

  1. In the Azure portal, go to the Overview page of your Elastic deployment, then select the Kibana link.

    Screenshot of the Azure portal that shows the Elasticsearch page with the Deployment URL Kibana link highlighted.

  2. After Kibana is open, search for APM in the search bar, then select APM.

    Screenshot of Elastic / Kibana that shows the APM search results.

Kibana APM is the curated application to support Application Monitoring workflows. Here you can view high-level details such as request/response times, throughput, and the transactions in a service with the most impact on the duration.

Screenshot of Elastic / Kibana that shows the APM Services Overview page.

You can drill down in a specific transaction to understand the transaction-specific details such as the distributed tracing.

Screenshot of Elastic / Kibana that shows the APM Services Transactions page.

Elastic APM Java agent also captures the JVM metrics from the Azure Spring Apps apps that are available with Kibana App for users for troubleshooting.

Screenshot of Elastic / Kibana that shows the APM Services JVM page.

Using the inbuilt AI engine in the Elastic solution, you can also enable Anomaly Detection on the Azure Spring Apps Services and choose an appropriate action - such as Teams notification, creation of a JIRA issue, a webhook-based API call, and others.

Screenshot of Elastic / Kibana that shows the APM Services page with Create rule pane showing and Actions highlighted.

Next steps