Skip to content

Instantly share code, notes, and snippets.

@jnesbitt
Last active May 2, 2017 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jnesbitt/7f8dc491e1b7df057052f905626eb598 to your computer and use it in GitHub Desktop.
Save jnesbitt/7f8dc491e1b7df057052f905626eb598 to your computer and use it in GitHub Desktop.
Prometheus metrics controller for Play! Framework 2.5
package controllers
import java.io.Writer
import akka.util.ByteString
import io.prometheus.client._
import io.prometheus.client.exporter.common.TextFormat
import play.api.http.HttpEntity
import play.api.mvc._
class PrometheusMetricsController extends Controller {
def index = Action {
val samples = new StringBuilder()
val writer = new WriterAdapter(samples)
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples())
writer.close()
Result(
header = ResponseHeader(200, Map.empty),
body = HttpEntity.Strict(ByteString(samples.toString), Some(TextFormat.CONTENT_TYPE_004))
)
}
}
class WriterAdapter(buffer: StringBuilder) extends Writer {
override def write(charArray: Array[Char], offset: Int, length: Int): Unit = {
buffer ++= new String(new String(charArray, offset, length).getBytes("UTF-8"), "UTF-8")
}
override def flush(): Unit = {}
override def close(): Unit = {}
}
@fedork
Copy link

fedork commented Feb 16, 2017

is this necessary: "new String(new String(charArray, offset, length).getBytes("UTF-8"), "UTF-8")"

wouldn't using StringWriter have the same effect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment