
[id='exposing-metrics_{context}']
= Exposing 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:
+
--
. Get the default Prometheus configuration using the following command:
+
[source,bash,opts="nowrap"]
----
# docker run -it --rm --entrypoint /bin/sh prom/prometheus -c 'cat /etc/prometheus/prometheus.yml'
----
+
Store the output in the `prometheus.yml` file in the directory with your application.

. Append the following snippet to the `prometheus.yml` file to make Prometheus automatically collect metrics from your application:
+
[source,yaml]
----
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 running on `localhost` using Docker:
+
--
[source,bash,opts="nowrap"]
----
# docker run -it --rm --network host -p 9090:9090 -v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
----

Wait until the `Server is ready to receive web requests` message is displayed in the console.

Notice the `--network host` option so that Prometheus can connect to your application.
--

.Procedure

. Include the `microprofile-metrics` fraction in your `pom.xml`.
+
.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(monotonic = true)` 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[MicroProfile Metrics documentation^].

