Chapter 8. Monitoring your application

This section contains information about monitoring your Spring Boot–based application running on OpenShift.

8.1. Accessing JVM metrics for your application on OpenShift

8.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

  1. 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)
    ...
  2. Open the YAML deployment template of the pod running your application for editing.

    oc edit dc/MY_APP_NAME
  3. Add the following entry to the ports section of the template and save your changes:

    ...
    spec:
      ...
      ports:
      - containerPort: 8778
        name: jolokia
        protocol: TCP
      ...
    ...
  4. 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.

  5. Log into the OpenShift Web console.
  6. In the sidebar, navigate to Applications > Pods, and click on the name of the pod running your application.
  7. In the pod details screen, click Open Java Console to access the hawt.io console.

Additional resources

8.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.

  1. Prerequisites

    • Prometheus server running on your cluster

Procedure

  1. Include the spring-boot-starter-actuator and micrometer-registry-prometheus dependencies in your pom.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>

  2. Collect metrics from your application:

    • By default, spring-boot-starter-actuator automatically records a Timer metric on each endpoint exposed your application. The default name for the Timer metric is set to http.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 or PUT.
      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:

      1. Disable the auto-time-requests property in the spring-boot-starter-actuator configuration for your application:

        management.metrics.web.server.auto-time-requests=false

        The property is set to true by default.

      2. 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.

  3. Launch your application:

    $ mvn spring-boot:run
  4. Invoke the traced endpoint several times:

    $ curl http://localhost:8080/greeting
    Hello from a timed endpoint
  5. Wait at least 15 seconds for the collection to happen, and see the metrics in Prometheus UI:

    1. Open the Prometheus UI at http://localhost:9090/ and type requests into the Expression box.
    2. From the suggestions, select for example http.server.requests and click Execute.
    3. 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 with base: and vendor: and expose information about the JVM in which the application runs.

Additional resources