
[id='exposing-application-metrics_{context}']
= Exposing application metrics

In this example, you:

* Configure your application to expose metrics.
* Collect and view the data using Prometheus.

Note that Prometheus actively connects to a monitored application to collect data; the application does not actively send metrics to a server.

.Prerequisites

* Prometheus configured to collect metrics from the application:
+
--
. Download and extract the link:https://prometheus.io/download/[archive^] with the latest Prometheus release:
+
[source,bash,options="nowrap"]
----
$ wget https://github.com/prometheus/prometheus/releases/download/v2.4.3/prometheus-2.4.3.linux-amd64.tar.gz
$ tar -xvf  prometheus-2.4.3.linux-amd64.tar.gz
----

. Navigate to the directory with Prometheus:
+
[source,bash,options="nowrap"]
----
$ cd  prometheus-2.4.3.linux-amd64
----

. Append the following snippet to the `prometheus.yml` file to make Prometheus automatically collect metrics from your application:
+
[source,yaml,options="nowrap"]
----
include::prometheus.yml[tag=thorntail]
----
+
The default behavior of {Thorntail}-based applications is to expose metrics at the `/metrics` endpoint.
This is what the MicroProfile Metrics specification requires, and also what Prometheus expects.
--

* The Prometheus server started on `localhost`:
+
--
Start Prometheus and wait until the `Server is ready to receive web requests` message is displayed in the console.

[source,bash,options="nowrap"]
----
$ ./prometheus
----
--


.Procedure

. Include the `microprofile-metrics` fraction in the `pom.xml` file in your application:
+
.pom.xml
[source,xml]
----
<dependencies>
include::pom.xml[tag=mp-metrics,indent=2]
</dependencies>
----

. Annotate methods or classes with the metrics annotations, for example:
+
--
[source,java]
----
include::src/main/java/org/wildfly/swarm/howto/metrics/MyResource.java[tag=metrics-resource-method,indent=0]
----

Here, the `@Counted` annotation is used to keep track of how many times this method was invoked.
The `@Timed` annotation is used to keep track of how long the invocations took.

In this example, a JAX-RS resource method was annotated directly, but you can annotate any CDI bean in your application as well.
--

. Launch your application:
+
[source,bash,opts="nowrap"]
----
$ mvn thorntail:run
----

. Invoke the traced endpoint several times:
+
[source,bash,opts="nowrap"]
----
$ curl http://localhost:8080/
Hello from counted and timed endpoint
----

. Wait at least 15 seconds for the collection to happen, and see the metrics in Prometheus UI:
+
--
. Open the Prometheus UI at link:http://localhost:9090/[http://localhost:9090/^] and type `hello` into the _Expression_ box.
. From the suggestions, select for example `application:hello_count` and click _Execute_.
. In the table that is displayed, you can see how many times the resource method was invoked.
. Alternatively, select `application:hello_time_mean_seconds` to see the mean time of all the invocations.

Note that all metrics you created are prefixed with `application:`.
There are other metrics, automatically exposed by {Thorntail} as the MicroProfile Metrics specification requires.
Those metrics are prefixed with `base:` and `vendor:` and expose information about the JVM in which the application runs.
--

.Additional resources

* For additional types of metrics, see the link:https://github.com/eclipse/microprofile-metrics[Eclipse MicroProfile Metrics documentation^].
