Last active
May 2, 2017 18:52
-
-
Save jnesbitt/7f8dc491e1b7df057052f905626eb598 to your computer and use it in GitHub Desktop.
Prometheus metrics controller for Play! Framework 2.5
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
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 = {} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this necessary: "new String(new String(charArray, offset, length).getBytes("UTF-8"), "UTF-8")"
wouldn't using StringWriter have the same effect?