-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
173 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
smetrics/src/main/scala/com/evolutiongaming/smetrics/implicits/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.evolutiongaming.smetrics | ||
|
||
import com.evolutiongaming.smetrics.syntax.AllSmetricsSyntax | ||
|
||
package object implicits extends AllSmetricsSyntax |
4 changes: 4 additions & 0 deletions
4
smetrics/src/main/scala/com/evolutiongaming/smetrics/syntax/AllSmetricsSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.evolutiongaming.smetrics.syntax | ||
|
||
trait AllSmetricsSyntax | ||
extends MeasureDurationSyntax |
77 changes: 77 additions & 0 deletions
77
smetrics/src/main/scala/com/evolutiongaming/smetrics/syntax/MeasureDurationSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.evolutiongaming.smetrics.syntax | ||
|
||
import cats.syntax.functor._ | ||
import cats.syntax.flatMap._ | ||
import cats.syntax.applicativeError._ | ||
import cats.syntax.either._ | ||
import cats.{Monad, MonadError} | ||
import com.evolutiongaming.smetrics.MeasureDuration | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
|
||
trait MeasureDurationSyntax { | ||
implicit def measureDurationSyntax[F[_], A](fa: F[A]): MeasureDurationOps[F, A] = | ||
new MeasureDurationOps[F, A](fa) | ||
} | ||
|
||
final class MeasureDurationOps[F[_], A](private val fa: F[A]) extends AnyVal { | ||
|
||
/** | ||
* Calculate time spent on F execution and handle result using provided function. | ||
* | ||
* Example: | ||
* {{{ | ||
* import com.evolutiongaming.smetrics.syntax.measureDuration._ | ||
* val handler: FiniteDuration => F[Unit] = latency => Sync[F].delay { println(s"Successful execution. Latency is $latency") } | ||
* Sync[F] | ||
* .delay { toBeMeasuredSideEffectCall() } | ||
* .measured { handler } | ||
* }}} | ||
* | ||
* @param handleF function to consume calculated duration | ||
* @return measured source F[A] | ||
*/ | ||
def measured( | ||
handleF: FiniteDuration => F[Unit] | ||
)(implicit F: Monad[F], measureDuration: MeasureDuration[F]): F[A] = | ||
for { | ||
measure <- measureDuration.start | ||
result <- fa | ||
duration <- measure | ||
_ <- handleF(duration) | ||
} yield result | ||
|
||
/** | ||
* Calculate time spent on F execution and handle success/failure result cases using provided functions. | ||
* | ||
* Example: | ||
* {{{ | ||
* import com.evolutiongaming.smetrics.syntax.measureDuration._ | ||
* val successHandler: FiniteDuration => F[Unit] = latency => Sync[F].delay { println(s"Successful execution. Latency is $latency") } | ||
* val failureHandler: FiniteDuration => F[Unit] = latency => Sync[F].delay { println(s"Failed execution. Latency is $latency") } | ||
* Sync[F] | ||
* .delay { toBeMeasuredSideEffectCall() } | ||
* .measuredCase { successHandler, failureHandler } | ||
* }}} | ||
* | ||
* @param successF function to consume calculated duration in case of success | ||
* @param failureF function to consume calculated duration in case of failure | ||
* @return measured source F[A] | ||
*/ | ||
def measuredCase[E]( | ||
successF: FiniteDuration => F[Unit], | ||
failureF: FiniteDuration => F[Unit] | ||
)(implicit F: MonadError[F, E], measureDuration: MeasureDuration[F]): F[A] = | ||
for { | ||
measure <- measureDuration.start | ||
result <- fa.attempt | ||
duration <- measure | ||
_ <- result match { | ||
case Right(_) => successF(duration) | ||
case Left(_) => failureF(duration) | ||
} | ||
result <- result.liftTo[F] | ||
} yield result | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
smetrics/src/main/scala/com/evolutiongaming/smetrics/syntax/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.evolutiongaming.smetrics | ||
|
||
package object syntax { | ||
object all extends AllSmetricsSyntax | ||
object measureDuration extends MeasureDurationSyntax | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters