Skip to content

Commit

Permalink
Merge pull request vert-x3#201 from AlexBischof/master
Browse files Browse the repository at this point in the history
Add Prometheus metrics example
  • Loading branch information
cescoffier authored Jun 13, 2017
2 parents 65f2dce + 16fa1f8 commit 609f225
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
37 changes: 37 additions & 0 deletions metrics-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,42 @@ export JAVA_OPTS="-javaagent:path/to/jolokia-jvm-x.x.x-agent.jar=port=7777,host=

Once defined, relaunch the application with the JMX metrics enabled.

== Monitoring for prometheus

This example is a monitoring setup for Prometheus via Dropwizard metrics.

The link:src/main/java/io/vertx/example/metrics/prometheus/[Java monitoring example]
The link:src/main/groovy/io/vertx/example/metrics/prometheus/[Groovy monitoring example]
The link:src/main/js/io/vertx/example/metrics/prometheus/[JavaScript monitoring example]
The link:src/main/ruby/io/vertx/example/metrics/prometheus/[Ruby monitoring example]
The link:src/main/kotlin/io/vertx/example/metrics/prometheus/[Kotlin monitoring example]

The verticle uses Dropwizard metrics to register a Prometheus CollectorRegistry (in this
case the defaultRegistry) which is bound to a router via MetricsHandler. To create some metrics
data a counter "testCounter" is periodically incremented.

Run the verticle either in your IDE or on the command line, then open your browser and hit
link:http://localhost:8080/metrics to see if the counter is increased.

You can also launch the example using the `vertx` cli as follows:

----
# compile the example, you need to have built example-utils before.
mvn clean package
# java
vertx run io.vertx.example.metrics.prometheus.PrometheusMetricsVerticle -cp target/metrics-examples-3.4.1.jar:src/main/java/io/vertx/example/metrics/prometheus -Dvertx.metrics.options.enabled=true
# javascript
cd src/main/js/io/vertx/example/metrics/prometheus
vertx run prometheus.js -cp ./../../../../../../../../target/metrics-examples-3.4.1.jar:. -Dvertx.metrics.options.enabled=true
# groovy
cd src/main/groovy/io/vertx/example/metrics/prometheus
vertx run prometheus.groovy -cp ./../../../../../../../../target/metrics-examples-3.4.1.jar:. -Dvertx.metrics.options.enabled=true
# ruby
cd src/main/ruby/io/vertx/example/metrics/prometheus
vertx run prometheus.rb -cp ./../../../../../../../../target/metrics-examples-3.4.1.jar:. -Dvertx.metrics.options.enabled=true
16 changes: 16 additions & 0 deletions metrics-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@
<artifactId>vertx-dropwizard-metrics</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_dropwizard</artifactId>
<version>0.0.23</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>client</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_vertx</artifactId>
<version>0.0.23</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import io.vertx.ext.web.Router
def metricRegistry = com.codahale.metrics.SharedMetricRegistries.getOrCreate("exported")
io.prometheus.client.CollectorRegistry.defaultRegistry.register(new io.prometheus.client.dropwizard.DropwizardExports(metricRegistry))

//Bind metrics handler to /metrics
def router = Router.router(vertx)
router.get("/metrics").handler(new io.prometheus.client.vertx.MetricsHandler())

//Start httpserver on localhost:8080
vertx.createHttpServer().requestHandler(router.&accept).listen(8080)

//Increase counter every second
vertx.setPeriodic(1000L, { e ->
metricRegistry.counter("testCounter").inc()
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.vertx.example.metrics.prometheus;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.vertx.MetricsHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.example.util.Runner;
import io.vertx.ext.web.Router;

/**
* @author <a href="https://github.com/AlexBischof">Alex Bischof</a>
*/
public class PrometheusMetricsVerticle extends AbstractVerticle {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(PrometheusMetricsVerticle.class);
}

@Override
public void start() throws Exception {
MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate("exported");
CollectorRegistry.defaultRegistry.register(new DropwizardExports(metricRegistry));

//Bind metrics handler to /metrics
Router router = Router.router(vertx);
router.get("/metrics").handler(new MetricsHandler());

//Start httpserver on localhost:8080
vertx.createHttpServer().requestHandler(router::accept).listen(8080);

//Increase counter every second
vertx.setPeriodic(1_000L, e -> metricRegistry.counter("testCounter").inc());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Router = require("vertx-web-js/router");
var metricRegistry = Java.type("com.codahale.metrics.SharedMetricRegistries").getOrCreate("exported");
Java.type("io.prometheus.client.CollectorRegistry").defaultRegistry.register(new (Java.type("io.prometheus.client.dropwizard.DropwizardExports"))(metricRegistry));

//Bind metrics handler to /metrics
var router = Router.router(vertx);
router.get("/metrics").handler(new (Java.type("io.prometheus.client.vertx.MetricsHandler"))());

//Start httpserver on localhost:8080
vertx.createHttpServer().requestHandler(router.accept).listen(8080);

//Increase counter every second
vertx.setPeriodic(1000, function (e) {
metricRegistry.counter("testCounter").inc();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.vertx.example.metrics.prometheus

import io.vertx.ext.web.Router

class PrometheusMetricsVerticle : io.vertx.core.AbstractVerticle() {
override fun start() {
var metricRegistry = com.codahale.metrics.SharedMetricRegistries.getOrCreate("exported")
io.prometheus.client.CollectorRegistry.defaultRegistry.register(io.prometheus.client.dropwizard.DropwizardExports(metricRegistry))

//Bind metrics handler to /metrics
var router = Router.router(vertx)
router.get("/metrics").handler(io.prometheus.client.vertx.MetricsHandler())

//Start httpserver on localhost:8080
vertx.createHttpServer().requestHandler({ router.accept(it) }).listen(8080)

//Increase counter every second
vertx.setPeriodic(1000L, { e ->
metricRegistry.counter("testCounter").inc()
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'vertx-web/router'
metricRegistry = Java::ComCodahaleMetrics::SharedMetricRegistries.get_or_create("exported")
Java::IoPrometheusClient::CollectorRegistry::defaultRegistry.register(Java::IoPrometheusClientDropwizard::DropwizardExports.new(metricRegistry))

#Bind metrics handler to /metrics
router = VertxWeb::Router.router($vertx)
router.get("/metrics").handler(&Java::IoPrometheusClientVertx::MetricsHandler.new())

#Start httpserver on localhost:8080
$vertx.create_http_server().request_handler(&router.method(:accept)).listen(8080)

#Increase counter every second
$vertx.set_periodic(1000) { |e|
metricRegistry.counter("testCounter").inc()
}

0 comments on commit 609f225

Please sign in to comment.