{{announcement.body}}
{{announcement.title}}

How to Publish Spring Boot Actuator Metrics to Dynatrace

DZone 's Guide to

How to Publish Spring Boot Actuator Metrics to Dynatrace

The metrics generated by the Spring Boot Actuator module of Spring Boot can be easily published to a Dynatrace cloud instance.

· Performance Zone ·
Free Resource

open, blank book

Learn more about publishing Spring boot actuator metrics!

The metrics generated by the Spring Boot Actuator module of Spring Boot can be easily published to a Dynatrace cloud instance. This article will give you a step-by-step guide for obtaining that.

Prerequisites:

  • JDK 8+
  • Maven 3.x
  • A Dynatrace cloud instance, you can create it for a 15-days free trial very quickly (try it). 
  • A Dynatrace OneAgent installed on your environment, you can install it on Linux and Windows.

If you do not have the OneAgent installed, please:

  • Download the installer from the Deploy Dynatrace web page of your Dynatrace instance (you need administrator rights).
  • Asks a proper administrator to install OneAgent on your PC.
  • Asks a proper administrator to give you access to an environment having the OneAgent already installed and where you can run the PoC that we are going to develop.

Generate a Dynatrace API token

Spring Boot Actuator uses Micrometer  and the metrics will be published thanks to the Micrometer integration with Dynatrace.

If you give a look at the source code it is possible to verify that Micrometer uses the Custom Device and Timeseries API exposed by Dynatrace. But, before using any Dynatrace API, you have to generate an API token.

Please note that for generating an API token you need administrator rights on your Dynatrace instance or otherwise you have to ask a proper administrator to generate that token for you.

Login to your Dynatrace instance and open the Settings menu  in the bottom left panel:

Open the Integration -> Dynatrace API page and clicks the Generate token button.

Generate Token button

Type a name for identifying the token (for example spring_boot_poc) and clicks the Generate button visible scrolling down the page and your new brand new token will be created.

API Token list

You can read the API token value by clicking the arrow under Edit.

Copy the token value and keep it in a safe place.

Create a Spring Boot app

A simple "Hello World!" Spring Boot app will be enough for publishing some custom metrics to Dynatrace. 

It is possible to copy and paste the file contents described below or (more quickly) to clone this GitHub repository.

The pom.xml file will be:

XML







Please note that:

  • The Spring Boot version used is 2.2.3 (SNAPSHOT) because it fixes this bug and so it is possible to set the  management.metrics.export.dynatrace.group  property (see  application.properties  file configuration later). Currently, the final Spring Boot 2.2.3 version has not yet been released.
  • The pom contains the  spring-boot-starter-actuator  dependency for enabling Spring Boot Actuator.
  • The pom contains the  micrometer-registry-dynatrace  dependency needed for publishing the custom metrics to Dynatrace.

Create a simple Spring Boot controller in HelloWorldController.java:

Java







Create a Spring Boot initialization class like the following App.java:

Java







Enable the Spring Boot Actuator health and metrics endpoints in the application.properties file:

Properties files







Build and start the Spring Boot app in one shot with the following command:

Shell







You can check that the application is running properly by putting http://localhost:8080 in your web browser.

Hello World!

You can verify that the Spring Boot Actuator module is running by putting http://localhost:8080/actuator/health in your web browser.

And, finally, checks that the Spring Boot Actuator metrics are collected by putting http://localhost:8080/actuator/metrics in your web browser.

You can check the value of a single metric by adding to the previous URL the metric name. For example http://localhost:8080/actuator/metrics/logback.events

Please note that, if the Dynatrace OneAgent was installed correctly, you should already see the HelloWorldController Spring Boot app in your Dynatrace instance by clicking Transactions and services in the left panel.

Spring Boot app detected in Transactions and services menu

Publish the Custom Metrics to Dynatrace

For enabling the publication of the custom metrics to Dynatrace modify the  application.properties  file as below:

Properties files




Please note that:

  • Your Dynatrace instance URL has to be set in the management.metrics.export.dynatrace.uri property.
  • Your Dynatrace API token has to be set in the management.metrics.export.dynatrace.api-token property.
  • Do not forget to set the management.metrics.export.dynatrace.enabled property to  true.

Rebuild and restart the Spring Boot app with the command:

Shell




If your configuration is correct, you should see the following message in the Spring Boot console log:

Dynatrace enabled message

Wait for 1-2 minutes and enter in your Dynatrace instance. Open the Technologies page on the left menu and clicks the java quadrant for showing the process group set in the  management.metrics.export.dynatrace.group  property previously. Please note that if you are using a Spring Boot version lower than 2.2.3 you could see a random alphanumeric string prefixed with UNKNOWN CUSTOM_DEVICE_GROUP (this is a Spring Boot issue fixed here).

Technologies

Clicks the arrow under Details and then the Group details button.

In the bottom of the web page, you can see the device set in the management.metrics.export.dynatrace.device-id property previously.

Finally, clicks on the device name and you will see the graphs of your metrics!

Spring Boot Actuator metrics published on Dynatrace

Conclusions

This is a guide for publishing the Spring Boot Actuator metrics (collected by using Micrometer) to Dynatrace.

In our example the metrics published are very simple, checks the Spring Boot Actuator documentation for a full list of the supported metrics (caches, connection pools, and other useful metrics can be enabled automatically).

You can create (and publish) custom metrics by injecting a MeterRegistry instance as explained here. Use the Micrometer API for registering new metrics.

And last but not least, try to create custom charts on Dynatrace for a nice visualization of your metrics (check the documentation).

Enjoy your ad-hoc metrics to Dynatrace!

Topics:
spring boot ,dynatrace ,micrometer ,java ,metrics monitoring ,performance ,spring actuator ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}