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