= Vert.x Micrometer Metrics

This project is an implementation of the Vert.x Metrics Service Provider Interface (SPI).
It uses link:http://micrometer.io/[Micrometer] for managing metrics and reporting to several backends.

== Features

* Vert.x core tools monitoring: TCP/HTTP client and servers, `link:../../yardoc/Vertx/DatagramSocket.html[DatagramSocket]`
, `link:../../yardoc/Vertx/EventBus.html[EventBus]` and pools
* User defined metrics through Micrometer
* Reporting to any backend supported by Micrometer
* Built-in options for https://www.influxdata.com/[InfluxDB], https://prometheus.io/[Prometheus] and JMX reporting.

== InfluxDB

=== Prerequisites

Follow the https://docs.influxdata.com/influxdb/latest/introduction/getting_started/[instructions to get InfluxDb up and running].

=== Getting started

The modules _vertx-micrometer-metrics_ and _micrometer-registry-influx_ must be present in the classpath.

Maven users should add this to their project POM file:

[source,xml,subs="+attributes"]
----
<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-micrometer-metrics</artifactId>
 <version>3.6.0.CR2</version>
</dependency>
<dependency>
 <groupId>io.micrometer</groupId>
 <artifactId>micrometer-registry-influx</artifactId>
 <version>${micrometer.version}</version>
</dependency>
----

And Gradle users, to their build file:

[source,groovy,subs="+attributes"]
----
compile 'io.vertx:vertx-micrometer-metrics:3.6.0.CR2'
compile 'io.micrometer:micrometer-registry-influx:${micrometer.version}'
----

=== Configuration examples

Vert.x does not enable SPI implementations by default. You must enable metric collection in the Vert.x options.

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'influxDbOptions' => {
      'enabled' => true
    },
    'enabled' => true
  }
})

----

==== Using a specific URI and database name

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'influxDbOptions' => {
      'enabled' => true,
      'uri' => "http://influxdb.example.com:8888",
      'db' => "sales-department"
    },
    'enabled' => true
  }
})

----

==== With authentication

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'influxDbOptions' => {
      'enabled' => true,
      'userName' => "username",
      'password' => "password"
    },
    'enabled' => true
  }
})

----

== Prometheus

=== Prerequisites

Follow the https://prometheus.io/docs/prometheus/latest/getting_started/[instructions to get Prometheus up and running].

=== Getting started

The modules _vertx-micrometer-metrics_ and _micrometer-registry-prometheus_ must be present in the classpath.
You may also probably need _vertx-web_, to expose the metrics.

Maven users should add this to their project POM file:

[source,xml,subs="+attributes"]
----
<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-micrometer-metrics</artifactId>
 <version>3.6.0.CR2</version>
</dependency>
<dependency>
 <groupId>io.micrometer</groupId>
 <artifactId>micrometer-registry-prometheus</artifactId>
 <version>${micrometer.version}</version>
</dependency>
----

And Gradle users, to their build file:

[source,groovy,subs="+attributes"]
----
compile 'io.vertx:vertx-micrometer-metrics:3.6.0.CR2'
compile 'io.micrometer:micrometer-registry-prometheus:${micrometer.version}'
----

=== Configuration examples

Vert.x does not enable SPI implementations by default. You must enable metric collection in the Vert.x options

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'prometheusOptions' => {
      'enabled' => true
    },
    'enabled' => true
  }
})

----

==== Using an embedded HTTP server with custom endpoint

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'prometheusOptions' => {
      'enabled' => true,
      'startEmbeddedServer' => true,
      'embeddedServerOptions' => {
        'port' => 8080
      },
      'embeddedServerEndpoint' => "/metrics/vertx"
    },
    'enabled' => true
  }
})

----

If the embedded server endpoint is not specified, it defaults to _/metrics_.

==== Binding metrics to an existing Vert.x Web router

[source,ruby]
----
require 'vertx/vertx'
require 'vertx-web/router'
require 'vertx-micrometer-metrics/prometheus_scraping_handler'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'prometheusOptions' => {
      'enabled' => true
    },
    'enabled' => true
  }
})

# Later on, creating a router
router = VertxWeb::Router.router(vertx)
router.route("/metrics").handler(&VertxMicrometerMetrics::PrometheusScrapingHandler.create())
vertx.create_http_server().request_handler(&router.method(:handle)).listen(8080)

----

== JMX

=== Getting started

The modules _vertx-micrometer-metrics_ and _micrometer-registry-jmx_ must be present in the classpath.

Maven users should add this to their project POM file:

[source,xml,subs="+attributes"]
----
<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-micrometer-metrics</artifactId>
 <version>3.6.0.CR2</version>
</dependency>
<dependency>
 <groupId>io.micrometer</groupId>
 <artifactId>micrometer-registry-jmx</artifactId>
 <version>${micrometer.version}</version>
</dependency>
----

And Gradle users, to their build file:

[source,groovy,subs="+attributes"]
----
compile 'io.vertx:vertx-micrometer-metrics:3.6.0.CR2'
compile 'io.micrometer:micrometer-registry-jmx:${micrometer.version}'
----

=== Configuration examples

Vert.x does not enable SPI implementations by default. You must enable metric collection in the Vert.x options

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'jmxMetricsOptions' => {
      'enabled' => true
    },
    'enabled' => true
  }
})

----

==== With step and domain

In Micrometer, `step` refers to the reporting period, in seconds. `domain` is the JMX domain under which
MBeans are registered.

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'jmxMetricsOptions' => {
      'enabled' => true,
      'step' => 5,
      'domain' => "my.metrics.domain"
    },
    'enabled' => true
  }
})

----

== Other backends or combinations

Even if not all backends supported by Micrometer are implemented in Vert.x options, it is still possible
to create any Micrometer registry and pass it to Vert.x.

The list of available backends includes Graphite, Ganglia, Atlas, link:http://micrometer.io/docs[etc].
It also enables the link:http://micrometer.io/docs/concepts#_composite_registries[Micrometer Composite Registry]
in order to report the same metrics to multiple backends.

In this example, metrics are reported both for JMX and Graphite:

[source,ruby]
----
require 'vertx/vertx'
myRegistry = Java::IoMicrometerCoreInstrumentComposite::CompositeMeterRegistry.new()
myRegistry.add(Java::IoMicrometerJmx::JmxMeterRegistry.new(lambda { |s|
  nil
}, Java::IoMicrometerCoreInstrument::Clock::SYSTEM))
myRegistry.add(Java::IoMicrometerGraphite::GraphiteMeterRegistry.new(lambda { |s|
  nil
}, Java::IoMicrometerCoreInstrument::Clock::SYSTEM))

vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'micrometerRegistry' => myRegistry,
    'enabled' => true
  }
})

----

== Advanced usage

Please refer to `link:../dataobjects.html#MicrometerMetricsOptions[MicrometerMetricsOptions]` for an exhaustive list of options.

=== Averages and quantiles in Prometheus

By default, when using the Prometheus registry, histogram-kind metrics will not contain averages or quantile stats.

Averages don't come out of the box but they are typically link:https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations[computed at query time],
with `promql`. Example, for HTTP client response time average during the last 5 minutes:

[source]
----
 rate(vertx_http_client_responseTime_seconds_sum[5m])
/
 rate(vertx_http_client_responseTime_seconds_count[5m])
----

To compute quantiles, there are two options available. The first is to activate quantile stats globally
and make them usable for Prometheus function `histogram_quantile`:

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'prometheusOptions' => {
      'enabled' => true,
      'publishQuantiles' => true
    },
    'enabled' => true
  }
})

----

And then, for example the `promql` query for the HTTP client response time, 99th percentile over the last 5 minutes:
[source]
----
 histogram_quantile(0.99, sum(rate(vertx_http_client_responseTime_seconds_bucket[5m])) by (le))
----

The advantage of this option is that it can be leveraged in `promql`, aggregable across dimensions.
The downside is that it creates a lot of timeseries for stats under the hood.

The second option is to create limited stats, non-aggregable across dimensions.
It requires to access directly the Micrometer / Prometheus registry:

[source,ruby]
----
registry = Java::IoVertxMicrometerBackends::BackendRegistries.get_default_now()
registry.config().meter_filter(Java::IoMicrometerCoreInstrumentConfig::MeterFilter.new())

----

See also, more on histograms and percentiles:

* from link:https://micrometer.io/docs/concepts#_histograms_and_percentiles[Micrometer doc]
* from link:https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram_quantile[Prometheus doc]

Furthermore, you can check some link:https://github.com/vert-x3/vertx-examples/tree/master/micrometer-metrics-examples[full working examples].
They come along with few instructions to setup with Prometheus and view dashboards in Grafana.

=== Disable some metric domains

Restricting the Vert.x modules being monitored can be done using
`link:todo[disabledMetricsCategories]`.

For a full list of domains, see `link:../enums.html#MetricsDomain[MetricsDomain]`

=== User-defined metrics

The Micrometer registries are accessible, in order to create new metrics or fetch the existing ones.
By default, an unique registry is used and will be shared across the Vert.x instances of the JVM:

[source,ruby]
----
registry = Java::IoVertxMicrometerBackends::BackendRegistries.get_default_now()

----

It is also possible to have separate registries per Vertx instance, by giving a registry name in metrics options.
Then it can be retrieved specifically:

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'influxDbOptions' => {
      'enabled' => true
    },
    'registryName' => "my registry",
    'enabled' => true
  }
})

# Later on:
registry = Java::IoVertxMicrometerBackends::BackendRegistries.get_now("my registry")

----

As an example, here is a custom timer that will track the execution time of a piece of code that is regularly called:

[source,ruby]
----
registry = Java::IoVertxMicrometerBackends::BackendRegistries.get_default_now()
timer = Java::IoMicrometerCoreInstrument::Timer.builder("my.timer").description("a description of what this timer does").register(registry)

@vertx.set_periodic(1000) { |l|
  timer.record(lambda {
    # Running here some operation to monitor
  })
}

----

For more examples, documentation about the Micrometer registry and how to create metrics, check
link:http://micrometer.io/docs/concepts#_registry[Micrometer doc].

=== Other instrumentation

Since plain access to Micrometer registries is provided, it is possible to leverage the Micrometer API.
For instance, to instrument the JVM:

[source,ruby]
----
registry = Java::IoVertxMicrometerBackends::BackendRegistries.get_default_now()

Java::IoMicrometerCoreInstrumentBinderJvm::ClassLoaderMetrics.new().bind_to(registry)
Java::IoMicrometerCoreInstrumentBinderJvm::JvmMemoryMetrics.new().bind_to(registry)
Java::IoMicrometerCoreInstrumentBinderJvm::JvmGcMetrics.new().bind_to(registry)
Java::IoMicrometerCoreInstrumentBinderSystem::ProcessorMetrics.new().bind_to(registry)
Java::IoMicrometerCoreInstrumentBinderJvm::JvmThreadMetrics.new().bind_to(registry)

----

_From link:http://micrometer.io/docs/ref/jvm[Micrometer documentation]._

=== Labels and matchers

Vert.x Micrometer Metrics defines a set of labels (aka tags or fields) that are used to provide dimensionality
to a metric. For instance, metrics related to event bus messages have an _address_ label, which allows then to query
timeseries for a specific event bus address, or compare timeseries per address, or perform any kind of aggregation
that the query API allows.

While setting up metrics options, you can specify which labels you want to enable or not:

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'prometheusOptions' => {
      'enabled' => true
    },
    'labels' => Java::JavaUtil::EnumSet.of(:REMOTE, :LOCAL, :HTTP_CODE, :HTTP_PATH),
    'enabled' => true
  }
})

----

The full list of labels is detailed here: `link:../enums.html#Label[Label]`.

WARNING: Enabling labels may result in a high cardinality in values, which can cause troubles on the metrics backend and affect performances.
So it must be used with care.
In general, it is fine to enable labels when the set of possible values is bounded.

For that reason, labels enabled by default are restricted to the ones with known bounded values.

It is possible to interact with labels further than just enabling/disabling. There are two ways for that:

==== Using Matchers

`link:../dataobjects.html#Match[Match]`  objects can be used to filter or rename some label value
by matching it with either an exact string or a regular expression (the former being more efficient).

Here is an example to restrict HTTP server metrics to those with label _local=localhost:8080_ only:

[source,ruby]
----
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx({
  'metricsOptions' => {
    'prometheusOptions' => {
      'enabled' => true
    },
    'labelMatchs' => [
      {
        'domain' => "HTTP_SERVER",
        'label' => "local",
        'value' => "localhost:8080"
      }
    ],
    'enabled' => true
  }
})

----

When an _alias_ is specified in the Match, it will be used to rename value instead of filtering.

Matchers are especially useful to control labelling through configuration as they are set via
`link:../dataobjects.html#MicrometerMetricsOptions[MicrometerMetricsOptions]`.

==== Using Micrometer's MeterFilter

Micrometer's link:http://micrometer.io/docs/concepts#_meter_filters[MeterFilter API] can be accessed directly in order to define rules on labels.
Compared to Matchers, it offers more features in manipulating the labels, but cannot be defined from configuration.
So both have their advantages.

Here is an example to replace the actual `path` label of HTTP requests with a generic form using regex:

[source,ruby]
----
registry = Java::IoVertxMicrometerBackends::BackendRegistries.get_default_now()
pattern = Java::JavaUtilRegex::Pattern.compile("/foo/bar/.*")

registry.config().meter_filter(Java::IoMicrometerCoreInstrumentConfig::MeterFilter.replace_tag_values(:HTTP_PATH.to_string(), lambda { |actualPath|
  m = pattern.matcher(actualPath)
  if (m.matches?())
    return "/foo/bar/:id"
  end
  return actualPath
}, ""))

----

NOTE: Matchers use MeterFilters under the hood.

=== Snapshots

A `link:../../yardoc/VertxMicrometerMetrics/MetricsService.html[MetricsService]` can be created out of a `link:../../yardoc/Vertx/Measured.html[Measured]` object
in order to take a snapshot of its related metrics and measurements.
The snapshot is returned as a `link:unavailable[JsonObject]`.

A well known _Measured_ object is simply `link:../../yardoc/Vertx/Vertx.html[Vertx]`:

[source,ruby]
----
require 'vertx-micrometer-metrics/metrics_service'
metricsService = VertxMicrometerMetrics::MetricsService.create(@vertx)
metrics = metricsService.get_metrics_snapshot()
puts metrics

----

Other components, such as an `link:../../yardoc/Vertx/EventBus.html[EventBus]` or a `link:../../yardoc/Vertx/HttpServer.html[HttpServer]` are
measurable:

[source,ruby]
----
require 'vertx-micrometer-metrics/metrics_service'
server = @vertx.create_http_server()
metricsService = VertxMicrometerMetrics::MetricsService.create(server)
metrics = metricsService.get_metrics_snapshot()
puts metrics

----

Finally it is possible to filter the returned metrics from their base names:

[source,ruby]
----
require 'vertx-micrometer-metrics/metrics_service'
metricsService = VertxMicrometerMetrics::MetricsService.create(@vertx)
# Client + server
metrics = metricsService.get_metrics_snapshot("vertx.http")
puts metrics

----

== Vert.x core tools metrics

This section lists all the metrics generated by monitoring the Vert.x core tools.

NOTE: The metric backends may have different conventions or rules for naming metrics.
For instance, Influx convention is to use dot as a word separator, whereas Prometheus expects underscore.
The names described below are given with underscore separators, but the actual names will vary
according to the backend used.

=== Net Client

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Gauge
|`vertx_net_client_connections{local=<local address>,remote=<remote address>}`
|Number of connections to the remote host currently opened.

|Summary
|`vertx_net_client_bytesReceived{local=<local address>,remote=<remote address>}`
|Number of bytes received from the remote host.

|Summary
|`vertx_net_client_bytesSent{local=<local address>,remote=<remote address>}`
|Number of bytes sent to the remote host.

|Counter
|`vertx_net_client_errors{local=<local address>,remote=<remote address>,class=<class>}`
|Number of errors.

|===

=== HTTP Client

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Gauge
|`vertx_http_client_connections{local=<local address>,remote=<remote address>}`
|Number of connections to the remote host currently opened.

|Summary
|`vertx_http_client_bytesReceived{local=<local address>,remote=<remote address>}`
|Number of bytes received from the remote host.

|Summary
|`vertx_http_client_bytesSent{local=<local address>,remote=<remote address>}`
|Number of bytes sent to the remote host.

|Counter
|`vertx_http_client_errors{local=<local address>,remote=<remote address>,class=<class>}`
|Number of errors.

|Gauge
|`vertx_http_client_requests{local=<local address>,remote=<remote address>}`
|Number of requests waiting for a response.

|Counter
|`vertx_http_client_requestCount{local=<local address>,remote=<remote address>,method=<http method>}`
|Number of requests sent.

|Timer
|`vertx_http_client_responseTime{local=<local address>,remote=<remote address>}`
|Response time.

|Counter
|`vertx_http_client_responseCount{local=<local address>,remote=<remote address>,code=<response code>}`
|Number of received responses.

|Gauge
|`vertx_http_client_wsConnections{local=<local address>,remote=<remote address>}`
|Number of websockets currently opened.

|===

=== Datagram socket

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Summary
|`vertx_datagram_bytesReceived{local=<local>,remote=<remote>}`
|Total number of bytes received on the `<host>:<port>` listening address.

|Summary
|`vertx_datagram_bytesSent{remote=<remote>}`
|Total number of bytes sent to the remote host.

|Counter
|`vertx_datagram_errors{class=<class>}`
|Total number of errors.

|===

=== Net Server

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Gauge
|`vertx_net_server_connections{local=<local address>}`
|Number of opened connections to the Net Server.

|Summary
|`vertx_net_server_bytesReceived{local=<local address>}`
|Number of bytes received by the Net Server.

|Summary
|`vertx_net_server_bytesSent{local=<local address>}`
|Number of bytes sent by the Net Server.

|Counter
|`vertx_net_server_errors{local=<local address>,class=<class>}`
|Number of errors.

|===

=== HTTP Server

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Gauge
|`vertx_http_server_connections{local=<local address>}`
|Number of opened connections to the HTTP Server.

|Summary
|`vertx_http_server_bytesReceived{local=<local address>}`
|Number of bytes received by the HTTP Server.

|Summary
|`vertx_http_server_bytesSent{local=<local address>}`
|Number of bytes sent by the HTTP Server.

|Counter
|`vertx_http_server_errors{local=<local address>,class=<class>}`
|Number of errors.

|Gauge
|`vertx_http_server_requests{local=<local address>}`
|Number of requests being processed.

|Counter
|`vertx_http_server_requestCount{local=<local address>,method=<http method>,code=<response code>}`
|Number of processed requests.

|Counter
|`vertx_http_server_requestResetCount{local=<local address>}`
|Number of requests reset.

|Timer
|`vertx_http_server_processingTime{local=<local address>}`
|Request processing time.

|Gauge
|`vertx_http_client_wsConnections{local=<local address>}`
|Number of websockets currently opened.

|===

=== Event Bus

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Gauge
|`vertx_eventbus_handlers{address=<address>}`
|Number of event bus handlers in use.

|Counter
|`vertx_eventbus_errors{address=<address>,class=<class>}`
|Number of errors.

|Summary
|`vertx_eventbus_bytesWritten{address=<address>}`
|Total number of bytes sent while sending messages to event bus cluster peers.

|Summary
|`vertx_eventbus_bytesRead{address=<address>}`
|Total number of bytes received while reading messages from event bus cluster peers.

|Gauge
|`vertx_eventbus_pending{address=<address>,side=<local/remote>}`
|Number of messages not processed yet. One message published will count for `N` pending if `N` handlers
are registered to the corresponding address.

|Counter
|`vertx_eventbus_published{address=<address>,side=<local/remote>}`
|Number of messages published (publish / subscribe).

|Counter
|`vertx_eventbus_sent{address=<address>,side=<local/remote>}`
|Number of messages sent (point-to-point).

|Counter
|`vertx_eventbus_received{address=<address>,side=<local/remote>}`
|Number of messages received.

|Counter
|`vertx_eventbus_delivered{address=<address>,side=<local/remote>}`
|Number of messages delivered to handlers.

|Counter
|`vertx_eventbus_replyFailures{address=<address>,failure=<failure name>}`
|Number of message reply failures.

|Timer
|`vertx_eventbus_processingTime{address=<address>}`
|Processing time for handlers listening to the `address`.

|===

== Vert.x pool metrics

This section lists all the metrics generated by monitoring Vert.x pools.

There are two types currently supported:

* _worker_ (see `link:../../yardoc/Vertx/WorkerExecutor.html[WorkerExecutor]`)
* _datasource_ (created with Vert.x JDBC client)

NOTE: Vert.x creates two worker pools upfront, _worker-thread_ and _internal-blocking_.

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Timer
|`vertx_pool_queue_delay{pool_type=<type>,pool_name=<name>}`
|Time waiting for a resource (queue time).

|Gauge
|`vertx_pool_queue_size{pool_type=<type>,pool_name=<name>}`
|Number of elements waiting for a resource.

|Timer
|`vertx_pool_usage{pool_type=<type>,pool_name=<name>}`
|Time using a resource (i.e. processing time for worker pools).

|Gauge
|`vertx_pool_inUse{pool_type=<type>,pool_name=<name>}`
|Number of resources used.

|Counter
|`vertx_pool_completed{pool_type=<type>,pool_name=<name>}`
|Number of elements done with the resource (i.e. total number of tasks executed for worker pools).

|Gauge
|`vertx_pool_ratio{pool_type=<type>,pool_name=<name>}`
|Pool usage ratio, only present if maximum pool size could be determined.

|===

== Verticle metrics

[cols="15,50,35", options="header"]
|===
|Metric type
|Metric name
|Description

|Gauge
|`vertx_verticle_deployed{name=<name>}`
|Number of verticle instances deployed.

|===