<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Denis Mikhaylov on Medium]]></title>
        <description><![CDATA[Stories by Denis Mikhaylov on Medium]]></description>
        <link>https://medium.com/@notxcain?source=rss-cef4c39e44e8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*dNFXi6y_ZIlNMnGP.jpg</url>
            <title>Stories by Denis Mikhaylov on Medium</title>
            <link>https://medium.com/@notxcain?source=rss-cef4c39e44e8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 21:26:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@notxcain/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Exposing JMX MBeans and Dropwizard metrics in Prometheus format using Akka Http]]></title>
            <link>https://medium.com/@notxcain/exposing-jmx-mbeans-and-dropwizard-metrics-in-prometheus-format-using-akka-http-271e8c8eff19?source=rss-cef4c39e44e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/271e8c8eff19</guid>
            <category><![CDATA[dropwizard]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[prometheus]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[akka]]></category>
            <dc:creator><![CDATA[Denis Mikhaylov]]></dc:creator>
            <pubDate>Tue, 15 Aug 2017 11:05:19 GMT</pubDate>
            <atom:updated>2017-08-16T08:13:43.217Z</atom:updated>
            <content:encoded><![CDATA[<p>So recently we started to migrate our infrastructure to Kubernetes. And of course we had to decide how to collect metrics automatically from all services. So we’ve chosen <a href="https://prometheus.io/">Prometheus</a>. It could be configured to watch Kubernetes Pods and automatically collect metrics via configurable port and endpoint. I didn’t find any ready to go solution, and it turned out that it was really easy to factor my own. So here it is.</p><p>First you need to add Akka, Dropwizard and Prometheus Java library to your build.sbt</p><pre>lazy val akkaHttpVersion = &quot;10.0.5&quot;<br>lazy val metricsScalaVersion = &quot;3.5.6&quot;<br>lazy val prometheusVersion = &quot;0.0.26&quot;</pre><pre>libraryDependencies ++= Seq(<br>  // Akka Http<br>  &quot;com.typesafe.akka&quot; %% &quot;akka-http&quot; % akkaHttpVersion,<br>  <br>  // Scala API for dropwizard metrics<br>  &quot;nl.grons&quot; %% &quot;metrics-scala&quot; % metricsScalaVersion,<br>  <br>  // Provides exporters for HotSpot JVM metrics from MBeans<br>  &quot;io.prometheus&quot; % &quot;simpleclient_hotspot&quot; % prometheusVersion,<br> <br>  // Provides exporter that works as an adapter between Dropwizard and Prometheus registries<br>  &quot;io.prometheus&quot; % &quot;simpleclient_dropwizard&quot; % prometheusVersion,  <br>  <br>  // Common stuff that would help us with encoding response in Prometheus format.<br>  &quot;io.prometheus&quot; % &quot;simpleclient_common&quot; % prometheusVersion<br>)</pre><p>Now let’s define an endpoint</p><pre>import java.io.StringWriter<br><br>import akka.http.scaladsl.model._<br>import akka.http.scaladsl.server.Directives._<br>import akka.http.scaladsl.server.Route<br>import io.prometheus.client.CollectorRegistry<br>import io.prometheus.client.exporter.common.TextFormat<br>import scala.collection.JavaConverters._</pre><pre>object PrometheusEndpoint {<br>  private val `text/plain; version=0.0.4; charset=utf-8` = ContentType {<br>    MediaType.customWithFixedCharset(<br>      &quot;text&quot;,<br>      &quot;plain&quot;,<br>      HttpCharsets.`UTF-8`,<br>      params = Map(&quot;version&quot; -&gt; &quot;0.0.4&quot;)<br>    )<br>  }<br>  <br>  private def renderMetrics(registry: CollectorRegistry, names: Set[String]): String = {<br>    val writer = new StringWriter()<br>    TextFormat.write004(writer, registry.filteredMetricFamilySamples(names.toSet.asJava))<br>    writer.toString<br>  }<br>  <br>  def apply(registry: CollectorRegistry): Route =<br>    (get &amp; path(&quot;metrics&quot; / &quot;prometheus&quot;) &amp; parameter(&#39;name.*)) { names =&gt;<br>      val content = renderMetrics(registry, names.toSet)<br>      complete {<br>        HttpResponse(entity = HttpEntity(`text/plain; version=0.0.4; charset=utf-8`, content))<br>      }<br>    }<br>}</pre><p>As you can see it pretty straight forward. First we define custom Content-Typefor <a href="https://prometheus.io/docs/instrumenting/exposition_formats/">Prometheus format</a>. Then we say that we will serve metrics at /metrics/prometheus and will take into account name parameters to allow filtering. Then we use aTextFormat helper to render metrics. And complete request processing with response.</p><p>Next we need to register some exporters (see comments)</p><pre>import io.prometheus.client.CollectorRegistry<br>import io.prometheus.client.dropwizard.DropwizardExports<br>import io.prometheus.client.hotspot.DefaultExports<br>import nl.grons.metrics.scala.DefaultInstrumented<br>import akka.actor.ActorSystem<br>import akka.stream.ActorMaterializer<br>import akka.http.scaladsl.Http</pre><pre>object App extends App with DefaultInstrumented {<br> <br>  // Add JVM metrics exporters to default registry<br>  DefaultExports.initialize()<br>  <br>  // Add Dropwizard adapter exporter<br>  CollectorRegistry.defaultRegistry.register(new DropwizardExports(metricRegistry))<br>  <br>  val prometheusEndpoint = PrometheusEndpoint(CollectorRegistry.defaultRegistry)<br>  <br>  implicit val system = ActorSystem(&quot;system&quot;)<br>  implicit val materializer = ActorMaterializer()<br>  <br>  Http().bindAndHandle(prometheusEndpoint, &quot;0.0.0.0&quot;, 9000)<br>}</pre><p>Now don’t forget to add annotation to your Pod template to enable scraping.</p><pre>annotations:<br>  prometheus.io/scrape: &quot;true&quot;<br>  prometheus.io/path: &quot;/metrics/prometheus&quot;<br>  prometheus.io/port: &quot;9000&quot;</pre><p>That’s it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=271e8c8eff19" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>