lundi 13 janvier 2014

How to monitor a Play Framework app in production

Today, we will try to explain how to monitor a play application with Metrics and JMX.


Metrics-play is a Play plugin for the Metrics library.

This plugin provides a lot of information about requests (count, response time, requests/second, errors…) and JVM (CPU, memory...)

To install it, add metrics-play dependency in your Build.scala or build.sbt file :

"com.kenshoo" %% "metrics-play" % "0.1.3"

Then add this line to conf/play.plugins

To enable metrics for each request, we will use a predefined filter.
Let’s define a Global object using this filter :

import play.api.mvc._

object Global extends WithFilters(MetricsFilter)

In the route file, add a new entry

GET     /admin/metrics    

This route will display all request metrics in Json format.

JMX Support

Json is nice, but we would like to retrieve this metrics using JMX.

Metrics provides some JMX bindings, so let’s edit the Global object to launch a JMX registry automatically when the application starts :

import com.codahale.metrics.JmxReporter
import play.api.GlobalSettings
import play.api.Application
import play.api.mvc._

object Global extends WithFilters(MetricsFilter) with GlobalSettings{
  override def onStart(app: Application) {
     val jmxReporter = JmxReporter.forRegistry(MetricsRegistry.default).build

To start your application with the JMX settings, you will need to add a few parameters :

play start -Djava.rmi.server.hostname=imacdeldescotte.lan

Note : Filters are a part of the Play Scala API, but we can load them in a Play-Java application overriding the Class<T>[] filters()method from GlobalSettings in the file.

Note 2 : Metrics can also stream data directly to Graphite.

Bonus : access logs

Play does not provide access logs out of the box.
But with Scala filters, it is quite easy to define some hooks to write requests information in a log file.
(Source :!topic/play-framework/rmPbH1I08b0)

First define a new Filter :

object AccessLog extends Filter {
  override def apply(next: RequestHeader => Result)(request: RequestHeader): Result = {
    val msg = s"method=${request.method} uri=${request.uri} remote-address=${request.remoteAddress} " +
      s"domain=${request.domain} query-string=${request.rawQueryString} " +

      s"referer=${request.headers.get("referer").getOrElse("N/A")} " +


Then, update the Global object to use this filter :

object Global extends WithFilters(MetricsFilter, AccessLogs) ...

Finally, configure logback to write this new logs :

 <appender name="ACCESS_LOG_FILE" class="ch.qos.logback.core.FileAppender">
            <pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} %message%n</pattern>

    <!-- additivity=false ensures access log data only goes to the access log -->
    <logger name="accesslog" level="INFO" additivity="false">
        <appender-ref ref="ACCESS_LOG_FILE" />