From d6c5a2532d12f135903e3f4dce4d4a90af8e6490 Mon Sep 17 00:00:00 2001 From: Miklos <57664299+miklos-da@users.noreply.github.com> Date: Tue, 28 Apr 2020 15:58:15 +0200 Subject: [PATCH] Add counting future metrics (#5743) * Added counting metrics. * Code tidying. CHANGELOG_BEGIN CHANGELOG_END * Rename based on review suggestion. --- .../src/main/scala/com/daml/metrics/Timed.scala | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ledger/metrics/src/main/scala/com/daml/metrics/Timed.scala b/ledger/metrics/src/main/scala/com/daml/metrics/Timed.scala index 3b21cf0f017..bebcdbdee4f 100644 --- a/ledger/metrics/src/main/scala/com/daml/metrics/Timed.scala +++ b/ledger/metrics/src/main/scala/com/daml/metrics/Timed.scala @@ -7,7 +7,7 @@ import java.util.concurrent.CompletionStage import akka.Done import akka.stream.scaladsl.{Keep, Source} -import com.codahale.metrics.Timer +import com.codahale.metrics.{Counter, Timer} import com.daml.dec.DirectExecutionContext import scala.concurrent.Future @@ -32,6 +32,15 @@ object Timed { result } + def trackedFuture[T](counter: Counter, future: => Future[T]): Future[T] = { + counter.inc() + future.andThen { case _ => counter.dec() }(DirectExecutionContext) + } + + def timedAndTrackedFuture[T](timer: Timer, counter: Counter, future: => Future[T]): Future[T] = { + Timed.future(timer, trackedFuture(counter, future)) + } + def source[Out, Mat](timer: Timer, source: => Source[Out, Mat]): Source[Out, Mat] = { val ctx = timer.time() source