Created
July 5, 2020 19:34
-
-
Save asad-awadia/93ba7c1eba6d6b8a8a84e9f20721d983 to your computer and use it in GitHub Desktop.
Vert.x tcp server with prometheus metrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
// init prometheus registry | |
val registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT) | |
// bind micrometer metrics | |
ClassLoaderMetrics().bindTo(registry) | |
JvmMemoryMetrics().bindTo(registry) | |
JvmGcMetrics().bindTo(registry) | |
ProcessorMetrics().bindTo(registry) | |
JvmThreadMetrics().bindTo(registry) | |
UptimeMetrics().bindTo(registry) | |
FileDescriptorMetrics().bindTo(registry) | |
// create custom counter metric for packets received | |
Counter.builder("packets.received") | |
.description("number of reports added") | |
.baseUnit(BaseUnits.TASKS) | |
.register(registry) | |
// set up vertx options to report tcp metrics and start an embedded http server on port 9999 | |
val vertxOptions = VertxOptions().setMetricsOptions( | |
MicrometerMetricsOptions() | |
.setPrometheusOptions( | |
VertxPrometheusOptions().setEnabled(true) | |
.setStartEmbeddedServer(true) | |
.setEmbeddedServerOptions(HttpServerOptions().setPort(9999)) | |
.setEmbeddedServerEndpoint("/metrics") | |
) | |
.setMicrometerRegistry(registry) | |
.setEnabled(true) | |
) | |
val vertx = Vertx.vertx(vertxOptions) | |
// start our tcp server | |
vertx.createNetServer().connectHandler { it.handler { registry.counter("packets.received").increment() } } | |
.listen(9091) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment