Skip to content

Instantly share code, notes, and snippets.

@asad-awadia
Created July 5, 2020 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asad-awadia/93ba7c1eba6d6b8a8a84e9f20721d983 to your computer and use it in GitHub Desktop.
Save asad-awadia/93ba7c1eba6d6b8a8a84e9f20721d983 to your computer and use it in GitHub Desktop.
Vert.x tcp server with prometheus metrics
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