Skip to content

Filters

Angel Sanadinov edited this page Jul 19, 2017 · 4 revisions

Overview

For more info on Play filters check this page. A list of built-in Play filter can be found here. core3 comes with four extra filters: CompressionFilter, MaintenanceModeFilter, MetricsFilter and TraceFilter.

Compression - CompressionFilter

Extends Play's compression filter and allows the specification of minimum content size for compression to be enabled and accepts a list of content types to compress.

Example setup:

import akka.stream.Materializer
import com.google.inject.{AbstractModule, Provides}
import core3.http.filters._

class Module extends AbstractModule {

  //... other providers ...

  @Provides
  def provideCompressionFilter(
    implicit mat: Materializer
  ): CompressionFilter = {
    //allows html and json content above 1400 bytes to be compressed
    new CompressionFilter(Vector("text/html", "application/json"), 1400)
  }

  //... other providers ...
}

Maintenance - MaintenanceModeFilter

Allows maintenance mode to be enabled for the application, which returns a generic 503 message to the user. A list of routes can be supplied to allow some routes to remain unaffected.

Example setup:

import akka.stream.Materializer
import com.google.inject.{AbstractModule, Provides}
import core3.http.filters._
import scala.concurrent.ExecutionContext

class Module extends AbstractModule {

  //... other providers ...

  @Provides
  def provideMaintenanceFilter(
    implicit mat: Materializer, ec: ExecutionContext
  ): MaintenanceModeFilter = {
    //allows the /admin and /test routes to be unaffected by the maintenance
    new MaintenanceModeFilter(Vector("/admin", "/test"))
  }

  //... other providers ...
}

Metrics - MetricsFilter

Allows the collection of metrics for all requests. Each request creates a JSON log entry (with logger named metrics) with the following format:

{
  "created": <entry creation time in ms>,
  "remoteAddress": "...",
  "responseTime": <total response time in ms>,
  "requestMethod": "...",
  "requestURI": "...",
  "responseStatus": "...",
  "responseContentType": "...",
  "responseSize": <response size in bytes>
}

Example setup:

import akka.stream.Materializer
import com.google.inject.{AbstractModule, Provides}
import core3.http.filters._
import scala.concurrent.ExecutionContext

class Module extends AbstractModule {

  //... other providers ...

  @Provides
  def provideMetricsFilter(
    implicit mat: Materializer, ec: ExecutionContext
  ): MetricsFilter = {
    new MetricsFilter()
  }

  //... other providers ...
}

Trace - TraceFilter

Allows the collection of trace data for all requests. Each request creates a JSON log entry (with logger named trace) with the following format:

{
  "start": <request start time in ms>,
  "end": <request end time in ms>,
  "responseTime": <total reponse time in ms>,
  "responseStatus": "...",
  "responseContentType": "...",
  "responseSize": <response size in bytes>,
  "remoteAddress": "...",
  "requestMethod": "...",
  "requestURI": "...",
  "requestSecure": <is client using SSL?>,
  "requestVersion": "<http version used>",
  "requestQuery": "...",
  "requestHeaders": "..."
}

Example setup:

import akka.stream.Materializer
import com.google.inject.{AbstractModule, Provides}
import core3.http.filters._
import scala.concurrent.ExecutionContext

class Module extends AbstractModule {

  //... other providers ...

  @Provides
  def provideTraceFilter(
    implicit mat: Materializer, ec: ExecutionContext
  ): TraceFilter = {
    new TraceFilter()
  }

  //... other providers ...
}