|
| 1 | +--- |
| 2 | +title: How to monitor Spring Boot apps with Elastic APM Java Agent |
| 3 | +description: How to use Elastic APM Java Agent to monitor Spring Boot applications running in Azure Spring Cloud |
| 4 | +author: karlerickson |
| 5 | +ms.author: karler |
| 6 | +ms.service: spring-cloud |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 12/07/2021 |
| 9 | +ms.custom: devx-track-java |
| 10 | +--- |
| 11 | + |
| 12 | +# How to monitor Spring Boot apps with Elastic APM Java Agent |
| 13 | + |
| 14 | +This article explains how to use Elastic APM Agent to monitor Spring Boot applications running in Azure Spring Cloud. |
| 15 | + |
| 16 | +With the Elastic Observability Solution, you can achieve unified observability to: |
| 17 | + |
| 18 | +* Monitor apps using the Elastic APM Java Agent and using persistent storage with Azure Spring Cloud. |
| 19 | +* Use diagnostic settings to ship Azure Spring Cloud logs to Elastic. For more information, see [Analyze logs with Elastic (ELK) using diagnostics settings](https://github.com/hemantmalik/azure-docs/blob/master/articles/spring-cloud/how-to-elastic-diagnostic-settings.md). |
| 20 | + |
| 21 | +The following video introduces unified observability for Spring Boot applications using Elastic. |
| 22 | + |
| 23 | +<br> |
| 24 | + |
| 25 | +> [!VIDEO https://www.youtube.com/embed/KjmQX1SxZdA] |
| 26 | +
|
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +* [Azure CLI](/cli/azure/install-azure-cli) |
| 30 | +* [Deploy Elastic on Azure](https://www.elastic.co/blog/getting-started-with-the-azure-integration-enhancement) |
| 31 | +* [Elastic APM Endpoint and Secret Token from the Elastic Deployment](https://www.elastic.co/guide/en/cloud/current/ec-manage-apm-and-fleet.html) |
| 32 | + |
| 33 | +## Deploy the Spring Petclinic application |
| 34 | + |
| 35 | +This article uses the Spring Petclinic sample to walk through the required steps. Use the following steps to deploy the sample application: |
| 36 | + |
| 37 | +1. Follow the steps in [Deploy Spring Boot apps using Azure Spring Cloud and MySQL](https://github.com/Azure-Samples/spring-petclinic-microservices#readme) until you reach the [Deploy Spring Boot applications and set environment variables](https://github.com/Azure-Samples/spring-petclinic-microservices#deploy-spring-boot-applications-and-set-environment-variables) section. |
| 38 | + |
| 39 | +1. Use the Azure Spring Cloud extension for Azure CLI with the following command to create an application to run in Azure Spring Cloud: |
| 40 | + |
| 41 | + ```azurecli |
| 42 | + az spring-cloud app create \ |
| 43 | + --resource-group <your-resource-group-name> \ |
| 44 | + --service <your-Azure-Spring-Cloud-instance-name> \ |
| 45 | + --name <your-app-name> \ |
| 46 | + --is-public true |
| 47 | + ``` |
| 48 | + |
| 49 | +## Enable custom persistent storage for Azure Spring Cloud |
| 50 | + |
| 51 | +Use the following steps to enable custom persistent storage: |
| 52 | + |
| 53 | +1. Follow the steps in [How to enable your own persistent storage in Azure Spring Cloud](how-to-custom-persistent-storage.md). |
| 54 | + |
| 55 | +1. Use the following Azure CLI command to add persistent storage for your Azure Spring Cloud apps. |
| 56 | + |
| 57 | + ```azurecli |
| 58 | + az spring-cloud app append-persistent-storage \ |
| 59 | + --resource-group <your-resource-group-name> \ |
| 60 | + --service <your-Azure-Spring-Cloud-instance-name> \ |
| 61 | + --name <your-app-name> \ |
| 62 | + --persistent-storage-type AzureFileVolume \ |
| 63 | + --share-name <your-Azure-file-share-name> \ |
| 64 | + --mount-path <unique-mount-path> \ |
| 65 | + --storage-name <your-mounted-storage-name> |
| 66 | + ``` |
| 67 | + |
| 68 | +## Activate Elastic APM Java Agent |
| 69 | + |
| 70 | +Before proceeding, you'll 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](https://www.elastic.co/blog/getting-started-with-the-azure-integration-enhancement). To get this information, use the following steps: |
| 71 | + |
| 72 | +1. In the Azure portal, go to the **Overview** page of your Elastic deployment, then select **Manage Elastic Cloud Deployment**. |
| 73 | + |
| 74 | + :::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-get-link-from-microsoft-azure.png" alt-text="Azure portal screenshot of 'Elasticsearch (Elastic Cloud)' page." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-get-link-from-microsoft-azure.png"::: |
| 75 | + |
| 76 | +1. Under your deployment on Elastic Cloud Console, select the **APM & Fleet** section to get Elastic APM Server endpoint and secret token. |
| 77 | + |
| 78 | + :::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-endpoint-secret.png" alt-text="Elastic screenshot 'APM & Fleet' page." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-endpoint-secret.png"::: |
| 79 | + |
| 80 | +1. Download Elastic APM Java Agent from [Maven Central](https://search.maven.org/search?q=g:co.elastic.apm%20AND%20a:elastic-apm-agent). |
| 81 | + |
| 82 | + :::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/maven-central-repository-search.png" alt-text="Maven Central screenshot with jar download highlighted." lightbox="media/how-to-elastic-apm-java-agent-monitor/maven-central-repository-search.png"::: |
| 83 | + |
| 84 | +1. 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. |
| 85 | + |
| 86 | + :::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/upload-files-microsoft-azure.png" alt-text="Azure portal screenshot showing 'Upload files' pane of 'File share' page." lightbox="media/how-to-elastic-apm-java-agent-monitor/upload-files-microsoft-azure.png"::: |
| 87 | + |
| 88 | +1. 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. |
| 89 | + |
| 90 | + ```azurecli |
| 91 | + az spring-cloud app deploy \ |
| 92 | + --name <your-app-name> \ |
| 93 | + --artifact-path <unique-path-to-your-app-jar-on-custom-storage> \ |
| 94 | + --jvm-options='-javaagent:<agent-location>' \ |
| 95 | + --env ELASTIC_APM_SERVICE_NAME=<your-app-name> \ |
| 96 | + ELASTIC_APM_APPLICATION_PACKAGES='<your-app-package-name>' \ |
| 97 | + ELASTIC_APM_SERVER_URL='<your-Elastic-APM-server-URL>' \ |
| 98 | + ELASTIC_APM_SECRET_TOKEN='<your-Elastic-APM-secret-token>' |
| 99 | + ``` |
| 100 | + |
| 101 | +## Automate provisioning |
| 102 | + |
| 103 | +You can also run a provisioning automation pipeline using Terraform 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. |
| 104 | + |
| 105 | +### Automate provisioning using Terraform |
| 106 | + |
| 107 | +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 Cloud Deployment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/spring_cloud_active_deployment). |
| 108 | + |
| 109 | +```terraform |
| 110 | +resource "azurerm_spring_cloud_java_deployment" "example" { |
| 111 | + ... |
| 112 | + jvm_options = "-javaagent:<unique-path-to-your-app-jar-on-custom-storage>" |
| 113 | + ... |
| 114 | + environment_variables = { |
| 115 | + "ELASTIC_APM_SERVICE_NAME"="<your-app-name>", |
| 116 | + "ELASTIC_APM_APPLICATION_PACKAGES"="<your-app-package>", |
| 117 | + "ELASTIC_APM_SERVER_URL"="<your-Elastic-APM-server-URL>", |
| 118 | + "ELASTIC_APM_SECRET_TOKEN"="<your-Elastic-APM-secret-token>" |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### Automate provisioning using an ARM template |
| 124 | + |
| 125 | +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](/azure/templates/microsoft.appplatform/spring/apps/deployments?tabs=json). |
| 126 | + |
| 127 | +```arm |
| 128 | +"deploymentSettings": { |
| 129 | + "environmentVariables": { |
| 130 | + "ELASTIC_APM_SERVICE_NAME"="<your-app-name>", |
| 131 | + "ELASTIC_APM_APPLICATION_PACKAGES"="<your-app-package>", |
| 132 | + "ELASTIC_APM_SERVER_URL"="<your-Elastic-APM-server-URL>", |
| 133 | + "ELASTIC_APM_SECRET_TOKEN"="<your-Elastic-APM-secret-token>" |
| 134 | + }, |
| 135 | + "jvmOptions": "-javaagent:<unique-path-to-your-app-jar-on-custom-storage>", |
| 136 | + ... |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Upgrade Elastic APM Java Agent |
| 141 | + |
| 142 | +To plan your upgrade, see [Upgrade versions](https://www.elastic.co/guide/en/cloud/current/ec-upgrade-deployment.html) for Elastic Cloud on Azure, and [Breaking Changes](https://www.elastic.co/guide/en/apm/server/current/breaking-changes.html) for APM. After you've upgraded APM Server, upload the Elastic APM Java agent JAR file in the custom persistent storage and restart apps with updated JVM options pointing to the upgraded Elastic APM Java agent JAR. |
| 143 | + |
| 144 | +## Monitor applications and metrics with Elastic APM |
| 145 | + |
| 146 | +Use the following steps to monitor applications and metrics: |
| 147 | + |
| 148 | +1. In the Azure portal, go to the **Overview** page of your Elastic deployment, then select the Kibana link. |
| 149 | + |
| 150 | + :::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-get-kibana-link.png" alt-text="Azure portal screenshot showing Elasticsearch page with 'Deployment URL / Kibana' highlighted." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-get-kibana-link.png"::: |
| 151 | + |
| 152 | +1. After Kibana is open, search for *APM* in the search bar, then select **APM**. |
| 153 | + |
| 154 | + :::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-kibana-search-apm.png" alt-text="Elastic / Kibana screenshot showing APM search results." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-kibana-search-apm.png"::: |
| 155 | + |
| 156 | +Kibana APM is the curated application to support Application Monitoring workflows. Here you can view high-level details such as request/response times, throughput, transactions in a service with most impact on the duration. |
| 157 | + |
| 158 | +:::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-customer-service.png" alt-text="Elastic / Kibana screenshot showing APM Services Overview page." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-customer-service.png"::: |
| 159 | + |
| 160 | +You can drill down in a specific transaction to understand the transaction-specific details such as the distributed tracing. |
| 161 | + |
| 162 | +:::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-customer-service-latency-distribution.png" alt-text="Elastic / Kibana screenshot showing APM Services Transactions page." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-customer-service-latency-distribution.png"::: |
| 163 | + |
| 164 | +Elastic APM Java agent also captures the JVM metrics from the Azure Spring Cloud apps that are available with Kibana App for users for troubleshooting. |
| 165 | + |
| 166 | +:::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-customer-service-jvm-metrics.png" alt-text="Elastic / Kibana screenshot showing APM Services JVMs page." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-customer-service-jvm-metrics.png"::: |
| 167 | + |
| 168 | +Using the inbuilt AI engine in the Elastic solution, you can also enable Anomaly Detection on the Azure Spring Cloud Services and choose an appropriate action - such as Teams notification, creation of a JIRA issue, a webhook-based API call, and others. |
| 169 | + |
| 170 | +:::image type="content" source="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-alert-anomaly.png" alt-text="Elastic / Kibana screenshot showing APM Services page with 'Create rule' pane showing and Actions highlighted." lightbox="media/how-to-elastic-apm-java-agent-monitor/elastic-apm-alert-anomaly.png"::: |
| 171 | + |
| 172 | +## Next steps |
| 173 | + |
| 174 | +* [Quickstart: Deploy your first Spring Boot app in Azure Spring Cloud](./quickstart.md) |
| 175 | +* [Deploy Elastic on Azure](https://www.elastic.co/blog/getting-started-with-the-azure-integration-enhancement) |
0 commit comments