Chapter 7. Monitoring your application
This section contains information about monitoring your Spring Boot–based application running on OpenShift.
7.1. Accessing JVM metrics for your application on OpenShift
7.1.1. Accessing JVM metrics using Jolokia on OpenShift
Jolokia is a built-in lightweight solution for accessing JMX (Java Management Extension) metrics over HTTP on OpenShift. Jolokia allows you to access CPU, storage, and memory usage data collected by JMX over an HTTP bridge. Jolokia uses a REST interface and JSON-formatted message payloads. It is suitable for monitoring cloud applications thanks to its comparably high speed and low resource requirements.
For Java-based applications, the OpenShift Web console provides the integrated hawt.io console that collects and displays all relevant metrics output by the JVM running your application.
Prerequistes
-
the
oc
client authenticated - a Java-based application container running in a project on OpenShift
- latest JDK 1.8.0 image
Procedure
List the deployment configurations of the pods inside your project and select the one that corresponds to your application.
oc get dc
NAME REVISION DESIRED CURRENT TRIGGERED BY MY_APP_NAME 2 1 1 config,image(my-app:6) ...
Open the YAML deployment template of the pod running your application for editing.
oc edit dc/MY_APP_NAME
Add the following entry to the
ports
section of the template and save your changes:... spec: ... ports: - containerPort: 8778 name: jolokia protocol: TCP ... ...
Redeploy the pod running your application.
oc rollout latest dc/MY_APP_NAME
The pod is redeployed with the updated deployment configuration and exposes the port
8778
.- Log into the OpenShift Web console.
- In the sidebar, navigate to Applications > Pods, and click on the name of the pod running your application.
- In the pod details screen, click Open Java Console to access the hawt.io console.
Additional resources
7.2. Exposing application metrics using Prometheus with Spring Boot
Prometheus actively connects to a monitored application to collect data; the application does not actively send metrics to a server.
Prerequisites
- Prometheus server running on your cluster
Procedure
Include the
spring-boot-starter-actuator
andmicrometer-registry-prometheus
dependencies in yourpom.xml
.pom.xml
<dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.1.0</version> </dependency> ... </dependencies>
Collect metrics from your application:
By default,
spring-boot-starter-actuator
automatically records aTimer
metric on each endpoint exposed your application. The default name for theTimer
metric is set tohttp.server.requests
. You can set a custom name for the metric:management.metrics.web.server.requests-metric-name=custom.timer.metric.name
The name is
http.server.requests
by default.The
Timer
metric contains a set of dimensions recorded for every request. By default, these dimensions are:method
-
The HTTP method for calling the endpoint, for example,
GET
orPUT
. status
-
The numeric HTTP status code returned in response to the request, for example,
200
,201
,500
. uri
-
The URI template before the variable substitution, for example,
/api/person/{id}
. exception
- If an exception is thrown by your application upon receiving a request, this dimension records the simple name of the exception class.
Alternatively, only record the
Timer
metric for specific endpoints:Disable the
auto-time-requests
property in thespring-boot-starter-actuator
configuration for your application:management.metrics.web.server.auto-time-requests=false
The property is set to
true
by default.Annotate methods or classes with the
@io.micrometer.core.annotation.Timed
annotation, for example:@RestController public class GreetingController { @RequestMapping("/greeting") @Timed public String get() { return "Hello from a timed endpoint"; } }
The
@Timed
annotation is used to keep track of the time it takes to invoke the metered endpoint.
Launch your application:
$ mvn spring-boot:run
Invoke the traced endpoint several times:
$ curl http://localhost:8080/greeting Hello from a timed endpoint
Wait at least 15 seconds for the collection to happen, and see the metrics in Prometheus UI:
-
Open the Prometheus UI at http://localhost:9090/ and type
requests
into the Expression box. -
From the suggestions, select for example
http.server.requests
and click Execute. - In the table that is displayed, you can see how long it takes to invoke the metered endpoint.
Note that all metrics you created are prefixed with
application:
. There are other metrics, automatically exposed by Spring Boot. Those metrics are prefixed withbase:
andvendor:
and expose information about the JVM in which the application runs.-
Open the Prometheus UI at http://localhost:9090/ and type
Additional resources
- For additional information on using Micrometer metrics with Spring Boot, see the Micrometer reference documentation for Spring 2.1.x.