Skip to content

Commit

Permalink
Merge pull request #64 from hmrc/API-3951
Browse files Browse the repository at this point in the history
API-3951: Make Metrics module self-contained with its own scheduling
  • Loading branch information
lburgos authored Oct 4, 2019
2 parents 0cab14b + 88b7277 commit b2ba248
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class ConfigurationModule extends Module {
bind[UpliftVerificationExpiryJobConfig].toProvider[UpliftVerificationExpiryJobConfigProvider],
bind[SetLastAccessedDateJobConfig].toProvider[SetLastAccessDateJobConfigProvider],
bind[ReconcileRateLimitsJobConfig].toProvider[ReconcileRateLimitsJobConfigProvider],
bind[RefreshMetricsJobConfig].toProvider[RefreshMetricsJobConfigProvider],
bind[ApiDefinitionConfig].toProvider[ApiDefinitionConfigProvider],
bind[ApiSubscriptionFieldsConfig].toProvider[ApiSubscriptionFieldsConfigProvider],
bind[ApiStorageConfig].toProvider[ApiStorageConfigProvider],
Expand Down Expand Up @@ -137,17 +136,6 @@ class ReconcileRateLimitsJobConfigProvider @Inject()(val runModeConfiguration: C
}
}

@Singleton
class RefreshMetricsJobConfigProvider @Inject()(val runModeConfiguration: Configuration, environment: Environment) extends Provider[RefreshMetricsJobConfig] with ServicesConfig {
override protected def mode: Mode = environment.mode

override def get(): RefreshMetricsJobConfig = {
val jobConfig = runModeConfiguration.underlying.as[Option[JobConfig]](s"$env.refreshMetricsJob")
.getOrElse(JobConfig(FiniteDuration(30, SECONDS), FiniteDuration(1, HOURS), enabled = true))
RefreshMetricsJobConfig(jobConfig.initialDelay, jobConfig.interval, jobConfig.enabled)
}
}

@Singleton
class ApiDefinitionConfigProvider @Inject()(val runModeConfiguration: Configuration, environment: Environment)
extends Provider[ApiDefinitionConfig] with ServicesConfig {
Expand Down
29 changes: 4 additions & 25 deletions app/uk/gov/hmrc/thirdpartyapplication/config/Scheduler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,12 @@ class Scheduler @Inject()(upliftVerificationExpiryJobConfig: UpliftVerificationE
app: Application) extends RunningOfScheduledJobs {

override val scheduledJobs: Seq[ExclusiveScheduledJob] = {

val upliftJob: Seq[ExclusiveScheduledJob] = if (upliftVerificationExpiryJobConfig.enabled) {
Seq(upliftVerificationExpiryJob)
} else {
Seq.empty
}

val refreshJob = if (refreshSubscriptionsJobConfig.enabled) {
Seq(refreshSubscriptionsScheduledJob)
} else {
Seq.empty
}

val accessDateJob = if (setLastAccessedDateJobConfig.enabled) {
Seq(setLastAccessedDateJob)
} else {
Seq.empty
}

val rateLimitsJob = if (reconcileRateLimitsJobConfig.enabled) {
Seq(reconcileRateLimitsJob)
} else {
Seq.empty
}
val upliftJob = if (upliftVerificationExpiryJobConfig.enabled) Seq(upliftVerificationExpiryJob) else Seq.empty
val refreshJob = if (refreshSubscriptionsJobConfig.enabled) Seq(refreshSubscriptionsScheduledJob) else Seq.empty
val accessDateJob = if (setLastAccessedDateJobConfig.enabled) Seq(setLastAccessedDateJob) else Seq.empty
val rateLimitsJob = if (reconcileRateLimitsJobConfig.enabled) Seq(reconcileRateLimitsJob) else Seq.empty

upliftJob ++ refreshJob ++ accessDateJob ++ rateLimitsJob

}

onStart(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package uk.gov.hmrc.thirdpartyapplication.metrics

import javax.inject.Inject
import play.api.Logger
import uk.gov.hmrc.metrix.domain.MetricSource
import uk.gov.hmrc.thirdpartyapplication.repository.ApplicationRepository


import scala.concurrent.{ExecutionContext, Future}

class ApplicationCount @Inject()(val applicationRepository: ApplicationRepository) extends MetricSource {
override def metrics(implicit ec: ExecutionContext): Future[Map[String, Int]] =
applicationRepository.count.map(applicationCount => Map("applicationCount" -> applicationCount))
applicationRepository.count.map(applicationCount => {
Logger.info(s"[METRIC] Application Count: $applicationCount")
Map("applicationCount" -> applicationCount)
})
}
25 changes: 22 additions & 3 deletions app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@

package uk.gov.hmrc.thirdpartyapplication.metrics

import akka.actor.ActorSystem
import com.kenshoo.play.metrics.Metrics
import javax.inject.{Inject, Provider, Singleton}
import org.joda.time.Duration
import play.api.inject.{Binding, Module}
import play.api.{Configuration, Environment}
import play.api.inject.{ApplicationLifecycle, Binding, Module}
import play.api.{Configuration, Environment, Logger}
import play.modules.reactivemongo.ReactiveMongoComponent
import reactivemongo.api.DB
import uk.gov.hmrc.lock.{ExclusiveTimePeriodLock, LockRepository}
import uk.gov.hmrc.metrix.MetricOrchestrator
import uk.gov.hmrc.metrix.domain.MetricSource
import uk.gov.hmrc.metrix.persistence.MongoMetricRepository

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}

class MetricsModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
Expand All @@ -36,6 +40,21 @@ class MetricsModule extends Module {
}
}

@Singleton
class MetricsScheduler @Inject()(env: Environment,
lifecycle: ApplicationLifecycle,
actorSystem: ActorSystem,
configuration: Configuration,
metricOrchestrator: MetricOrchestrator)(implicit val ec: ExecutionContext) {
actorSystem.scheduler.schedule(2 minutes, 1 hour) {
Logger.info(s"Running Metrics Collection Process")
metricOrchestrator
.attemptToUpdateAndRefreshMetrics()
.map(_.andLogTheResult())
.recover { case e: RuntimeException => Logger.error(s"An error occurred processing metrics: ${e.getMessage}", e) }
}
}

@Singleton
class MetricsOrchestratorProvider @Inject()(configuration: Configuration,
metricsSources: MetricsSources,
Expand All @@ -47,7 +66,7 @@ class MetricsOrchestratorProvider @Inject()(configuration: Configuration,
val Lock: ExclusiveTimePeriodLock = new ExclusiveTimePeriodLock {
override def repo: LockRepository = new LockRepository()
override def lockId: String = "MetricsLock"
override def holdLockFor: Duration = new Duration(10000)
override def holdLockFor: Duration = Duration.standardMinutes(2)
}

override def get(): MetricOrchestrator = {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class SubscriptionControllerSpec extends UnitSpec with ScalaFutures with Mockito

override implicit lazy val app: Application = GuiceApplicationBuilder()
.disable[com.kenshoo.play.metrics.PlayModule]
.disable[uk.gov.hmrc.thirdpartyapplication.metrics.MetricsModule]
.configure("metrics.enabled" -> false)
.overrides(bind[SubscriptionRepository].to(mockSubscriptionRepository))
.build
Expand Down

0 comments on commit b2ba248

Please sign in to comment.