Chapter 6. Integrating with OpenShift
You can enable your Quarkus application to use the Micrometer metrics library for runtime and application metrics. Micrometer acts like a facade between your applications and third parties like Prometheus, which is embedded in OpenShift. The integration of Quarkus with OpenShift enables you to expose not only embedded metrics that are automatically enabled, but also custom metrics that are registered as part of your application.
For more information on configuring a variety of metrics, see the Quarkus Micrometer Community Guide.
Prerequisites
- You have access to Getting started with the OpenShift CLI, 4.6 or later
- You have an OpenShift instance
Procedure
Complete the following instructions and use embedded Prometheus in OpenShift to expose metrics from your Quarkus applications:
6.1. Enabling monitoring for user-defined projects in OpenShift
In Red Hat OpenShift Container Platform 4.6 or later, you can enable monitoring for your user-defined projects as well as default platform monitoring.
Prerequisites
- Have access to Getting started with the OpenShift CLI, 4.6 or later
- Have an OpenShift instance
Procedure
-
Go to Enabling monitoring for user-defined projects and follow specific instructions on how to enable user-defined project monitoring. In summary, you will create a ConfigMap in the namespace
openshift-monitoring
cluster-monitoring-config.yaml
:
apiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | enableUserWorkload: true
If you are using OpenShift 4.5 or earlier, use:
apiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | techPreviewUserWorkload: enabled: true
After you complete the steps to enable monitoring for user-defined projects, OpenShift automatically creates a namespace, openshift-user-workload-monitoring
that you will deploy when you begin Deploying your Quarkus application to OpenShift and Creating a service monitor in your OpenShift project.
6.2. Deploying your Quarkus application to OpenShift
After setting up the required infrastructure, you must enable Micrometer with Prometheus.
Prerequisites
- You have access to Getting started with the OpenShift CLI, 4.6 or later
- You have an OpenShift instance
- You have created a ConfigMap in the previous section, Enabling monitoring for user-defined projects in OpenShift
Procedure
Implement a REST API:
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-micrometer-registry-prometheus</artifactId> </dependency>
Add the micrometer registry facade:
import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import io.micrometer.core.instrument.MeterRegistry; @Path("/hello") public class GreetingsResource { @Inject MeterRegistry registry; @GET public String sayHello() { registry.counter("greeting_counter").increment(); return "Hello!"; } }
This Micrometer facade creates a counter that will be incremented every time you invoke the services. The registry helps to create custom metrics or use the metrics manually. You could also annotate methods as the following:
@GET @Counted(value = "greeting_counter") public String sayHello() { return "Hello!"; }
Run the application:
mvn compile quarkus:dev
Call your service:
curl http://localhost:8080/hello
The service should return with
Hello!
Browse to
http://localhost:8080/q/metrics
. You should see thegreeting_counter
with count 1.0 (the one you just completed):# HELP greeting_counter_total #TYPE greeting_counter_total counter greeting_counter_total 1.0
Deploy your Quarkus application into OpenShift by entering the extension
quarkus-openshift
into yourpom.xml
:<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-openshift</artifactId> </dependency>
Deploy your application into a newly created project called
my-project
in OpenShift:oc new-project my-project mvn clean package -Dquarkus.kubernetes.deploy=true -Dquarkus.openshift.expose=true -Dquarkus.openshift.labels.app-with-metrics=quarkus-app
The app-with-metrics
is explained in Creating a service monitor in your OpenShift project.
If you are using an OpenShift with unsecure SSL, you also need to append -Dquarkus.kubernetes-client.trust-certs=true
to the Maven command.
6.3. Creating a service monitor in your OpenShift project
Prometheus uses a pull model to get metrics from applications, which means it scrapes or watches endpoints to pull metrics. Although the previous procedure helped to expose your service in your OpenShift instance, you have not configured anything in Prometheus to scrape your service yet. This is why the service monitor is necessary.
A service monitor is a custom resource that you must create in the same project or namespace where your service is running: my-project
.
Procedure
Set up your
service-monitor.yaml
:apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: labels: k8s-app: prometheus-app-monitor name: prometheus-app-monitor namespace: my-project spec: endpoints: - interval: 30s targetPort: 8080 scheme: http selector: matchLabels: app-with-metrics: 'quarkus-app'
Apply your service-monitor.yaml:
oc apply -f service-monitor.yaml
This command creates a service monitor named
prometheus-app-monitor
that will select applications with the labelapp-with-metrics: quarkus-app
. This label was added during the Deploying your Quarkus application to OpenShift procedure. OpenShift calls the endpoint/metrics
for all the services labeled withapp-with-metrics: quarkus-app
.To use your service monitor:
-
Call your greetings service:
curl http://quarkus-micrometer-my-project.ocp.host/hello
. This increments yourgreeting_counter_total
counter. - To see the metrics, browse to the OpenShift Console and select the Developer > Monitoring view.
- Select the Metrics tab.
-
In the Custom Query field, enter
greeting_counter_total
.
-
Call your greetings service:
The metrics display in the table below the Custom Query field.