Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frank-dspeed/213338bcb75c58ec79f27ae498a41958 to your computer and use it in GitHub Desktop.
Save frank-dspeed/213338bcb75c58ec79f27ae498a41958 to your computer and use it in GitHub Desktop.
Example Kotlin 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