Skip to content

Commit

Permalink
Merge pull request #21197 from MichalMaler/mmaler-reviewing-samllrye-…
Browse files Browse the repository at this point in the history
…metrics

Mmaler reviewing samllrye metrics
  • Loading branch information
jmartisk authored Nov 11, 2021
2 parents ad0faf6 + 720be46 commit 2e23a19
Showing 1 changed file with 87 additions and 83 deletions.
170 changes: 87 additions & 83 deletions docs/src/main/asciidoc/smallrye-metrics.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,70 @@ This guide is maintained in the main Quarkus repository
and pull requests should be submitted there:
https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc
////

= SmallRye Metrics

include::./attributes.adoc[]

This guide demonstrates how your Quarkus application can use https://github.com/smallrye/smallrye-metrics/[SmallRye Metrics],
an implementation of the https://github.com/eclipse/microprofile-metrics/[MicroProfile Metrics] specification.

SmallRye Metrics allows applications to gather various metrics and statistics that provide
insights into what is happening inside the application.
The following guide demonstrates how a Quarkus application can use link:https://github.com/smallrye/smallrye-metrics/[SmallRye Metrics],
an implementation of the link:https://github.com/eclipse/microprofile-metrics/[MicroProfile Metrics] specification.

The metrics can be read remotely using JSON format or the OpenMetrics format, so that
they can be processed by additional tools such as Prometheus, and stored for analysis
and visualisation.
SmallRye Metrics allows applications to gather metrics and statistics that provide insights into what is happening inside an application. The metrics can be read remotely using the JSON or OpenMetrics format to be processed by additional tools such as Prometheus and stored for analysis and visualization.

Apart from application-specific metrics, which are described in this guide, you may also utilize built-in metrics
exposed by various Quarkus extensions. These are described in the guide for each particular extension that supports
built-in metrics.
Apart from application-specific metrics described in this guide, you may also use built-in metrics exposed by various Quarkus extensions. These are described in the guide for each particular extension that supports built-in metrics.

IMPORTANT: link:micrometer[Micrometer] is the recommended approach to metrics for Quarkus.
Use the SmallRye Metrics extension when it's required to retain MicroProfile specification compatibility.
IMPORTANT: link:micrometer[Micrometer] is the recommended approach to metrics for Quarkus. Use the SmallRye Metrics extension when it is required to retain MicroProfile specification compatibility.

== Prerequisites

To complete this guide, you need:

* less than 15 minutes
* an IDE
* JDK 11+ installed with `JAVA_HOME` configured appropriately
* Apache Maven {maven-version}
* Approximately 15 minutes.
* An IDE.
* JDK 11+ installed with `JAVA_HOME` configured appropriately.

== Architecture
In this example, we build a very simple microservice which offers one REST endpoint and that endpoint serves
for determining whether a number is prime. The implementation class is annotated with some metric annotations
so that while responding to user's requests, some metrics are gathered. The meaning of each metric will be explained later.

== Solution
In this example, we build a very simple microservice that offers one REST endpoint. This endpoint serves for determining whether a number is prime. The implementation class is annotated with certain metric annotations so that while responding to users' requests, certain metrics are gathered. The meaning of each metric is explained later.

We recommend that you follow the instructions in the next sections and create the application step by step.
However, you can go right to the completed example.
== Solution

Clone the Git repository: `git clone {quickstarts-clone-url}`, or download an {quickstarts-archive-url}[archive].
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can skip to the completed example.

The solution is located in the `microprofile-metrics-quickstart` {quickstarts-tree-url}/microprofile-metrics-quickstart[directory].
. Clone the Git repository:
+
[source,bash,subs=attributes+]
----
git clone {quickstarts-clone-url}
----

== Creating the Maven Project
* Alternatively, download a {quickstarts-archive-url}[Quickstarts archive]. The solution is located in the `microprofile-metrics-quickstart` {quickstarts-tree-url}/microprofile-metrics-quickstart[directory] and follow with the xref:running-and-using-the-application_{context}[] section.

First, we need a new project. Create a new project with the following command:
[id="creating-a-maven-project_{context}"]
== Creating a Maven project

. To create a new Maven project:
+
[source,bash,subs=attributes+]
----
mvn io.quarkus.platform:quarkus-maven-plugin:{quarkus-version}:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=microprofile-metrics-quickstart \
-DclassName="org.acme.microprofile.metrics.PrimeNumberResource" \
-DclassName="org.acme.microprofile.metrics.PrimeNumberChecker" \
-Dpath="/" \
-Dextensions="resteasy,smallrye-metrics"
cd microprofile-metrics-quickstart
----
+
This command generates a Maven-based Quarkus project that uses the `smallrye-metrics` extension.

This command generates a Maven project, importing the `smallrye-metrics`.

If you already have your Quarkus project configured, you can add the `smallrye-metrics` extension
to your project by running the following command in your project base directory:

* If you already have your Quarkus project configured, you can add the `smallrye-metrics` extension to your project by running the following command in your project base directory:
+
[source,bash]
----
./mvnw quarkus:add-extension -Dextensions="smallrye-metrics"
----

This will add the following to your `pom.xml`:

+
This adds the following to your `pom.xml` file:
+
[source,xml]
----
<dependency>
Expand All @@ -82,20 +75,18 @@ This will add the following to your `pom.xml`:
</dependency>
----

== Writing the application
[id="writing-an-application_{context}"]
== Writing an application

The application consists of a single class that implements an algorithm for checking whether a number is prime.
This algorithm is exposed over a REST interface. Additionally, we need a few annotations to make sure
that our desired metrics are calculated over time and can be exported for manual analysis or processing by additional tooling.
The following procedures create a Maven-based Quarkus application that consists of a single class that implements an algorithm for checking whether a number is prime. This algorithm is exposed over a REST interface. Additionally, specific annotations are required to ensure that the desired metrics are calculated over time and can be exported for manual analysis or processing by additional tooling.

The metrics that we will gather are these:
The application will gather the following metrics:

* `performedChecks`: A counter which is increased by one each time the user asks about a number.
* `highestPrimeNumberSoFar`: This is a gauge that stores the highest number that was asked about by the user and which was determined to be prime.
* `checksTimer`: This is a timer, therefore a compound metric that benchmarks how much time the primality tests take. We will explain that one in more details later.

The full source code looks like this:
* `performedChecks`: A counter that increases by one each time the user asks about a number.
* `highestPrimeNumberSoFar`: A gauge that stores the highest number asked about by the user if the number was determined to be prime.
* `checksTimer`: A compound metric that benchmarks how much time the primality tests take. Additional details are provided later.

The full source code looks as follows:
[source,java]
----
package org.acme.microprofile.metrics;
Expand Down Expand Up @@ -153,74 +144,87 @@ public class PrimeNumberChecker {
}
----

[id="running-and-using-the-application_{context}"]
== Running and using the application

To run the microservice in dev mode, use `./mvnw clean compile quarkus:dev`
To execute the application created in xref:writing-an-application_{context}[], do the following:

. Run the microservice in the dev mode:
+
[source,bash]
----
./mvnw clean quarkus:dev
----

=== Generate some values for the metrics
First, ask the endpoint whether some numbers are prime numbers.
. Generate values for the metrics.

.. Query the endpoint to determine whether some numbers are prime numbers:
+
[source,bash]
----
curl localhost:8080/350
----

+
The application will respond that 350 is not a prime number because it can be divided by 2.

Now for some large prime number so that the test takes a bit more time:

* For large prime numbers, the test takes more time.
+
[source,bash]
----
curl localhost:8080/629521085409773
----
+
The application will respond that 629521085409773 is a prime number.

The application will respond that 629521085409773 is a prime number.
If you want, try some more calls with numbers of your choice.
.. Perform additional calls with numbers of your choice.

=== Review the generated metrics
To view the metrics, execute `curl -H"Accept: application/json" localhost:8080/q/metrics/application`
. Review the generated metrics:
+
[source,bash]
----
curl -H"Accept: application/json" localhost:8080/q/metrics/application
----
+
You will receive a response such as:

+
[source,json]
----
{
"org.acme.microprofile.metrics.PrimeNumberChecker.checksTimer" : {
"p50": 217.231273,
"org.acme.microprofile.metrics.PrimeNumberChecker.checksTimer" : { <1>
"p50": 217.231273, <2>
"p75": 217.231273,
"p95": 217.231273,
"p98": 217.231273,
"p99": 217.231273,
"p999": 217.231273,
"min": 0.58961,
"mean": 112.15909190834819,
"max": 217.231273,
"stddev": 108.2721053982776,
"count": 2,
"meanRate": 0.04943519091742238,
"min": 0.58961, <3>
"mean": 112.15909190834819, <4>
"max": 217.231273, <5>
"stddev": 108.2721053982776, <6>
"count": 2, <7>
"meanRate": 0.04943519091742238, <8>
"oneMinRate": 0.2232140583080189,
"fiveMinRate": 0.3559527083952095,
"fifteenMinRate": 0.38474303050928976
},
"org.acme.microprofile.metrics.PrimeNumberChecker.performedChecks" : 2,
"org.acme.microprofile.metrics.PrimeNumberChecker.highestPrimeNumberSoFar" : 629521085409773
"org.acme.microprofile.metrics.PrimeNumberChecker.performedChecks" : 2, <9>
"org.acme.microprofile.metrics.PrimeNumberChecker.highestPrimeNumberSoFar" : 629521085409773 <10>
}
----

Let's explain the meaning of each metric:

* `performedChecks`: A counter which is increased by one each time the user asks about a number.
* `highestPrimeNumberSoFar`: This is a gauge that stores the highest number that was asked about by the user and which was determined to be prime.
* `checksTimer`: This is a timer, therefore a compound metric that benchmarks how much time the primality tests take. All durations are measured in milliseconds. It consists of these values:
** `min`: The shortest duration it took to perform a primality test, probably it was performed for a small number.
** `max`: The longest duration, probably it was with a large prime number.
** `mean`: The mean value of the measured durations.
** `stddev`: The standard deviation.
** `count`: The number of observations (so it will be the same value as `performedChecks`).
** `p50, p75, p95, p99, p999`: Percentiles of the durations. For example the value in `p95` means that 95 % of the measurements were faster than this duration.
** `meanRate, oneMinRate, fiveMinRate, fifteenMinRate`: Mean throughput and one-, five-, and fifteen-minute exponentially-weighted moving average throughput.
<1> `checksTimer`: A compound metric that benchmarks how much time the primality tests take. All durations are measured in milliseconds. It consists of these values below.
<2> `p50, p75, p95, p99, p999`: Percentiles of the durations. For example, the value in `p95` means that 95 % of the measurements were faster than this duration.
<3> `min`: The shortest duration it took to perform a primality test was probably performed for a small number.
<4> `mean`: The mean value of the measured durations.
<5> `max`: The longest duration, probably it was with a large prime number.
<6> `stddev`: The standard deviation.
<7> `count`: The number of observations, the value of which is the same as `performedChecks`.
<8> `meanRate, oneMinRate, fiveMinRate, fifteenMinRate`: Mean throughput and one-, five-, and fifteen-minute exponentially-weighted moving average throughput.
<9> `performedChecks`: A counter which is increased by one each time the user asks about a number.
<10> `highestPrimeNumberSoFar`: A gauge that stores the highest number that was asked about by the user and which was determined to be prime.

If you prefer an OpenMetrics export rather than the JSON format, remove the `-H"Accept: application/json"` argument from your command line.
NOTE: If you prefer an OpenMetrics export rather than the JSON format, remove the `-H"Accept: application/json"` argument from your command line.

== Configuration Reference
.Configuration Reference

include::{generated-dir}/config/quarkus-smallrye-metrics.adoc[opts=optional, leveloffset=+1]

0 comments on commit 2e23a19

Please sign in to comment.