-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PM-2906: Contravariant tracing #3
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c649ace
add kind-projector for type patterns like Tracer[F, *]
42872f3
add distributed tracer based on input-output-hk/contra-tracer
5508ce8
add lemastero to developers section
cd4f6f2
add missing space at the end
075eb24
improve docs for tracer
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
105 changes: 105 additions & 0 deletions
105
metronome/tracing/src/main/scala/io/iohk/metronome/tracer/Tracer.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,105 @@ | ||
|
||
import cats.{Applicative, Contravariant, FlatMap, Id, Monad, Monoid, Show, ~>} | ||
|
||
/** | ||
* Contravariant tracer | ||
* | ||
* Ported from https://github.com/input-output-hk/contra-tracer/blob/master/src/Control/Tracer.hs | ||
*/ | ||
trait Tracer[F[_], -A] extends Function[A, F[Unit]] | ||
|
||
object Tracer { | ||
|
||
/** | ||
* If you know how to trace A and | ||
* - how to enrich A to get value B | ||
* - how to forget some details about B to get A | ||
* then you can create Tracer for B | ||
*/ | ||
implicit def contraTracer[F[_]]: Contravariant[Tracer[F, *]] = | ||
new Contravariant[Tracer[F, *]] { | ||
override def contramap[A, B](fa: Tracer[F, A])(f: B => A): Tracer[F, B] = | ||
a => fa(f(a)) | ||
} | ||
|
||
def noOpTracer[M[_], A](implicit MA: Applicative[M]): Tracer[M, A] = | ||
_ => MA.pure(()) | ||
|
||
implicit def monoidTracer[F[_], S](implicit MA: Applicative[F]): Monoid[Tracer[F, S]] = | ||
new Monoid[Tracer[F, S]] { | ||
|
||
/** Run sequentially two tracers */ | ||
override def combine(a1: Tracer[F, S], a2: Tracer[F, S]): Tracer[F, S] = | ||
s => MA.productR(a1(s))(a2(s)) | ||
|
||
override def empty: Tracer[F, S] = noOpTracer | ||
} | ||
|
||
/** Trace value a using tracer tracer */ | ||
def traceWith[F[_], A](tracer: Tracer[F, A], a: A): F[Unit] = tracer(a) | ||
|
||
/** contravariant Kleisli composition: | ||
* if you can: | ||
* - produce effect M[B] from A | ||
* - trace B's | ||
* then you can trace A's | ||
*/ | ||
def contramapM[F[_], A, B](f: A => F[B], tracer: Tracer[F, B])(implicit MM: FlatMap[F]): Tracer[F, A] = | ||
(a: A) => MM.flatMap(f(a))(tracer) | ||
|
||
/** change the effect F to G using natural transformation nat */ | ||
def natTracer[F[_], G[_], A](nat: F ~> G, tracer: Tracer[F, A]): Tracer[G, A] = | ||
a => nat(tracer(a)) | ||
|
||
/** filter out values to trace if they do not pass predicate p */ | ||
def condTracing[F[_], A](p: A => Boolean, tr: Tracer[F, A])(implicit FM: Applicative[F]): Tracer[F, A] = | ||
(a: A) => | ||
if (p(a)) tr(a) | ||
else FM.pure(()) | ||
|
||
/** filter out values that was send to trace using side effecting predicate */ | ||
def condTracingM[F[_], A](p: F[A => Boolean], tr: Tracer[F, A])(implicit FM: Monad[F]): Tracer[F, A] = | ||
a => | ||
FM.flatMap(p) { pa => | ||
if (pa(a)) tr(a) | ||
else FM.pure(()) | ||
} | ||
|
||
def showTracing[F[_], A]( | ||
tracer: Tracer[F, String] | ||
)(implicit S: Show[A], C: Contravariant[Tracer[F, *]]): Tracer[F, A] = | ||
C.contramap(tracer)(S.show) | ||
|
||
def traceAll[A, B](f: B => List[A], t: Tracer[Id, A]): Tracer[Id, B] = | ||
event => f(event).foreach(t) | ||
} | ||
|
||
object TracerSyntax { | ||
|
||
implicit class TracerOps[F[_], A](val tracer: Tracer[F, A]) extends AnyVal { | ||
|
||
/** Trace value a using tracer */ | ||
def trace(a: A): F[Unit] = tracer(a) | ||
|
||
/** contravariant Kleisli composition: | ||
* if you can: | ||
* - produce effect M[B] from A | ||
* - trace B's | ||
* then you can trace A's | ||
*/ | ||
def >=>[B](f: B => F[A])(implicit MM: FlatMap[F]): Tracer[F, B] = | ||
Tracer.contramapM(f, tracer) | ||
|
||
def nat[G[_]](nat: F ~> G): Tracer[G, A] = | ||
Tracer.natTracer(nat, tracer) | ||
|
||
def filter(p: A => Boolean)(implicit FM: Applicative[F]): Tracer[F, A] = | ||
Tracer.condTracing[F, A](p, tracer) | ||
|
||
def filterNot(p: A => Boolean)(implicit FM: Applicative[F]): Tracer[F, A] = | ||
filter(a => !p(a)) | ||
|
||
def filterM(p: F[A => Boolean])(implicit FM: Monad[F]): Tracer[F, A] = | ||
Tracer.condTracingM(p, tracer) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, cool, I was wondering yesterday how to do this with
mill
, cheers!