Skip to content

Commit

Permalink
Merge pull request #59 from jostick/master
Browse files Browse the repository at this point in the history
Update to Play 2.6.2 and Scala 2.12
  • Loading branch information
mosheeshel authored Aug 16, 2017
2 parents 18a6a4f + b52817d commit 80cd071
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 44 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This module provides some support for @codahale [Metrics](https://dropwizard.git

[![codecov.io](http://codecov.io/github/kenshoo/metrics-play/coverage.svg?branch=master)](http://codecov.io/github/kenshoo/metrics-play?branch=master)

Play Version: 2.4.0, Metrics Version: 3.1.1, Scala Versions: 2.11.6, 2.10.4
Play Version: 2.6.2, Metrics Version: 3.2.2, Scala Versions: 2.12.2

## Features

Expand All @@ -22,7 +22,7 @@ Add metrics-play dependency:
```scala
val appDependencies = Seq(
...
"com.kenshoo" %% "metrics-play" % "2.4.0_0.4.1"
"com.kenshoo" %% "metrics-play" % "2.6.2_0.6.1"
)
```

Expand Down Expand Up @@ -139,6 +139,7 @@ instead of `com.kenshoo.play.metrics.PlayModule`

## Changes

* 2.6.2_0.6.1 - Upgrade to play 2.6 and Scala 2.12. Migration: If you get errors like "No configuration setting found ..." when building fat JARs, check your merge strategy for reference.conf.
* 2.4.0_0.4.0 - Re-implement as Play Module
* 2.4.0_0.3.0 - Upgrade to play 2.4, metrics 3.1.2
* 2.3.0_0.2.1 - Breaking Change! prefix jvm metric names to standardize with dropwizard
Expand Down
28 changes: 15 additions & 13 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ organization:= "com.kenshoo"

name := "metrics-play"

version := "2.5.9_0.5.1"
version := "2.6.2_0.6.1"

scalaVersion := "2.11.8"
scalaVersion := "2.12.2"

crossScalaVersions := Seq("2.11.8")
crossScalaVersions := Seq("2.12.2")

scalacOptions := Seq("-unchecked", "-deprecation")

testOptions in Test += Tests.Argument("junitxml", "console")

Expand All @@ -16,19 +18,19 @@ resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/release

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

resolvers += "specs2" at "https://mvnrepository.com/artifact/org.specs2/specs2_2.11"
resolvers += "specs2" at "https://mvnrepository.com/artifact/org.specs2/specs2_2.12"

libraryDependencies ++= Seq(
"io.dropwizard.metrics" % "metrics-core" % "3.2.2",
"io.dropwizard.metrics" % "metrics-json" % "3.2.2",
"io.dropwizard.metrics" % "metrics-jvm" % "3.2.2",
"io.dropwizard.metrics" % "metrics-logback" % "3.2.2",
"com.typesafe.play" %% "play" % "2.5.9" % "provided",
"org.joda" % "joda-convert" % "1.2",
"io.dropwizard.metrics" % "metrics-core" % "3.2.4",
"io.dropwizard.metrics" % "metrics-json" % "3.2.4",
"io.dropwizard.metrics" % "metrics-jvm" % "3.2.4",
"io.dropwizard.metrics" % "metrics-logback" % "3.2.4",
"com.typesafe.play" %% "play" % "2.6.2" % "provided",
"org.joda" % "joda-convert" % "1.8.2",
//test
"com.typesafe.play" %% "play-test" % "2.5.9" % "test",
"com.typesafe.play" %% "play-specs2" % "2.5.9" % "test",
"org.specs2" %% "specs2" % "2.4.15" % "test"
"com.typesafe.play" %% "play-test" % "2.6.2" % "test",
"com.typesafe.play" %% "play-specs2" % "2.6.2" % "test",
"org.specs2" %% "specs2" % "2.4.17" % "test"
)

publishMavenStyle := true
Expand Down
4 changes: 2 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/release

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.0")

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.3.3")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.0")

addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
9 changes: 9 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
metrics {
name = "default"
rateUnit = "SECONDS"
durationUnit = "SECONDS"
showSamples = false
jvm = true
logback = true
enabled = true
}
16 changes: 8 additions & 8 deletions src/main/scala/com/kenshoo/play/metrics/Metrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ trait Metrics {
@Singleton
class MetricsImpl @Inject() (lifecycle: ApplicationLifecycle, configuration: Configuration) extends Metrics {

val validUnits = Some(Set("NANOSECONDS", "MICROSECONDS", "MILLISECONDS", "SECONDS", "MINUTES", "HOURS", "DAYS"))

val registryName = configuration.getString("metrics.name").getOrElse("default")
val rateUnit = configuration.getString("metrics.rateUnit", validUnits).getOrElse("SECONDS")
val durationUnit = configuration.getString("metrics.durationUnit", validUnits).getOrElse("SECONDS")
val showSamples = configuration.getBoolean("metrics.showSamples").getOrElse(false)
val jvmMetricsEnabled = configuration.getBoolean("metrics.jvm").getOrElse(true)
val logbackEnabled = configuration.getBoolean("metrics.logback").getOrElse(true)
val validUnits = Set("NANOSECONDS", "MICROSECONDS", "MILLISECONDS", "SECONDS", "MINUTES", "HOURS", "DAYS")

val registryName = configuration.get[String]("metrics.name")
val rateUnit = configuration.getAndValidate[String]("metrics.rateUnit", validUnits)
val durationUnit = configuration.getAndValidate[String]("metrics.durationUnit", validUnits)
val showSamples = configuration.get[Boolean]("metrics.showSamples")
val jvmMetricsEnabled = configuration.get[Boolean]("metrics.jvm")
val logbackEnabled = configuration.get[Boolean]("metrics.logback")

val mapper: ObjectMapper = new ObjectMapper()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ package com.kenshoo.play.metrics

import javax.inject.Inject

import play.api.mvc.{Action, Controller}
import play.api.mvc.{AbstractController, ControllerComponents}

class MetricsController @Inject() (met: Metrics) extends Controller {
class MetricsController @Inject() (
met: Metrics,
controllerComponents: ControllerComponents)
extends AbstractController(controllerComponents) {

def metrics = Action {
try {
Expand Down
6 changes: 2 additions & 4 deletions src/main/scala/com/kenshoo/play/metrics/MetricsFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import javax.inject.Inject
import akka.stream.Materializer
import play.api.mvc._
import play.api.http.Status
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import com.codahale.metrics._
import com.codahale.metrics.MetricRegistry.name

import scala.concurrent.Future

import scala.concurrent.{ExecutionContext, Future}

trait MetricsFilter extends Filter

Expand All @@ -35,7 +33,7 @@ class DisabledMetricsFilter @Inject()(implicit val mat: Materializer) extends Me
}
}

class MetricsFilterImpl @Inject() (metrics: Metrics)(implicit val mat: Materializer) extends MetricsFilter {
class MetricsFilterImpl @Inject() (metrics: Metrics)(implicit val mat: Materializer, val ec: ExecutionContext) extends MetricsFilter {

def registry: MetricRegistry = metrics.defaultRegistry

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/kenshoo/play/metrics/PlayModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import play.api.inject.Module

class PlayModule extends Module {
def bindings(environment: Environment, configuration: Configuration) = {
if (configuration.getBoolean("metrics.enabled").getOrElse(true)) {
if (configuration.get[Boolean]("metrics.enabled")) {
Seq(
bind[MetricsFilter].to[MetricsFilterImpl].eagerly,
bind[Metrics].to[MetricsImpl].eagerly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
package com.kenshoo.play.metrics

import org.specs2.mutable.Specification
import play.api.test.FakeRequest
import play.api.test.{FakeRequest, Helpers}
import play.api.test.Helpers._

class MetricsControllerSpec extends Specification {

class MetricsControllerSpec extends Specification {

"MetricsController" should {
"return JSON serialized by Metric's toJson with correct headers" in {

val controller = new MetricsController(new Metrics {
def defaultRegistry = throw new NotImplementedError
def toJson = "{}"
})
}, Helpers.stubControllerComponents())

val result = controller.metrics.apply(FakeRequest())
contentAsString(result) must equalTo("{}")
Expand All @@ -37,7 +37,7 @@ class MetricsControllerSpec extends Specification {

"return 500 if metrics module is disabled" in {

val controller = new MetricsController(new DisabledMetrics())
val controller = new MetricsController(new DisabledMetrics(), Helpers.stubControllerComponents())

val result = controller.metrics.apply(FakeRequest())

Expand Down
17 changes: 10 additions & 7 deletions src/test/scala/com/kenshoo/play/metrics/MetricsFilterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import org.specs2.mutable.Specification
import play.api.Application
import play.api.http.HttpFilters
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc.{Result, Action}
import play.api.mvc._
import play.api.routing.Router
import play.api.test._
import play.api.test.Helpers._
import com.codahale.metrics.MetricRegistry

import scala.concurrent.duration.Duration
import scala.concurrent.Await
import play.api.inject.bind
Expand All @@ -35,6 +36,8 @@ import play.api.mvc.Results._
object MetricsFilterSpec extends Specification {
sequential

val ec = scala.concurrent.ExecutionContext.Implicits.global

class Filters @Inject() (metricsFilter: MetricsFilter) extends HttpFilters {
def filters = Seq(metricsFilter)
}
Expand All @@ -44,7 +47,7 @@ object MetricsFilterSpec extends Specification {
lazy val application = new GuiceApplicationBuilder()
.overrides(
bind[Router].to(Router.from {
case _ => Action(result)
case _ => DefaultActionBuilder(BodyParsers.utils.ignore(AnyContentAsEmpty: AnyContent))(ec) { result }
}),
bind[HttpFilters].to[Filters],
bind[MetricsFilter].to[MetricsFilterImpl],
Expand All @@ -60,25 +63,25 @@ object MetricsFilterSpec extends Specification {

"MetricsFilter" should {

"return passed response code" in withApplication(Ok("")) { _ =>
val result = route(FakeRequest()).get
"return passed response code" in withApplication(Ok("")) { app =>
val result = route(app, FakeRequest()).get
status(result) must equalTo(OK)
}

"increment status code counter" in withApplication(Ok("")) { implicit app =>
Await.ready(route(FakeRequest()).get, Duration(2, "seconds"))
Await.ready(route(app, FakeRequest()).get, Duration(2, "seconds"))
val meter = metrics.defaultRegistry.meter(MetricRegistry.name(labelPrefix, "200"))
meter.getCount must equalTo(1)
}

"increment status code counter for uncaught exceptions" in withApplication(throw new RuntimeException("")) { implicit app =>
Await.ready(route(FakeRequest()).get, Duration(2, "seconds"))
Await.ready(route(app, FakeRequest()).get, Duration(2, "seconds"))
val meter = metrics.defaultRegistry.meter(MetricRegistry.name(labelPrefix, "500"))
meter.getCount must equalTo(1)
}

"increment request timer" in withApplication(Ok("")) { implicit app =>
Await.ready(route(FakeRequest()).get, Duration(2, "seconds"))
Await.ready(route(app, FakeRequest()).get, Duration(2, "seconds"))
val timer = metrics.defaultRegistry.timer(MetricRegistry.name(labelPrefix, "requestTimer"))
timer.getCount must beGreaterThan(0L)
}
Expand Down

0 comments on commit 80cd071

Please sign in to comment.