Skip to content

Instantly share code, notes, and snippets.

@rossabaker
Created March 9, 2022 01:33
Show Gist options
  • Save rossabaker/b3cd22845f4f72cb6af0b7978c872cd9 to your computer and use it in GitHub Desktop.
Save rossabaker/b3cd22845f4f72cb6af0b7978c872cd9 to your computer and use it in GitHub Desktop.
//> using scala "2.13"
//> using lib "com.armanbilge::bayou:0.1-4fb42c8"
//> using lib "org.typelevel::cats-effect:3.3.7"
//> using lib "co.fs2::fs2-core:3.2.5"
//> using lib "org.typelevel::log4cats-core:2.2.0"
//> using lib "org.typelevel::log4cats-slf4j:2.2.0"
//> using lib "org.tpolecat::natchez-core:0.1.6"
//> using lib "org.tpolecat::natchez-log:0.1.6"
import cats.effect.{Trace => _, _}
import cats.implicits._
import fs2._
import bayou.Trace
import bayou.Trace._
import natchez.log.Log
import natchez.{Trace => _, _}
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.global
import java.net.URI
trait Setup {
val entryPoint = Log.entryPoint[IO]("example")
val trace = entryPoint.root("root").flatMap(r => Resource.eval(Trace.ioTrace(r)))
implicit def logger: Logger[IO] =
new Logger[IO] {
def error(message: => String): IO[Unit] =
IO.println(s"[error] $message\n")
def warn(message: => String): IO[Unit] =
IO.println(s"[warn] $message\n")
def info(message: => String): IO[Unit] =
IO.println(s"[info] $message\n")
def debug(message: => String): IO[Unit] =
IO.println(s"[debug] $message\n")
def trace(message: => String): IO[Unit] =
IO.println(s"[trace] $message\n")
def error(t: Throwable)(message: => String): IO[Unit] =
IO.println(s"[error] $message\n${t.getMessage}")
def warn(t: Throwable)(message: => String): IO[Unit] =
IO.println(s"[warn] $message\n${t.getMessage}")
def info(t: Throwable)(message: => String): IO[Unit] =
IO.println(s"[info] $message\n${t.getMessage}")
def debug(t: Throwable)(message: => String): IO[Unit] =
IO.println(s"[debug] $message\n${t.getMessage}")
def trace(t: Throwable)(message: => String): IO[Unit] =
IO.println(s"[trace] $message\n${t.getMessage}")
}
}
object Single extends IOApp.Simple with Setup {
def run: IO[Unit] = {
trace.use { implicit trace =>
def io(name: String): IO[Unit] =
trace.span(name)(
IO(Thread.currentThread.getName)
.flatMap(t => trace.put("thread" -> t))
)
val acquire = trace.span("acquire")(IO.unit)
val release = trace.span("release")(IO.unit)
val r = trace.spanR("resource") >> Resource.make(acquire)(_ => release)
r.use(_ => trace.span("use")(IO.race(io("a"), io("b")).void))
}
}
}
{
"name" : "root",
"service" : "example",
"timestamp" : "2022-03-09T01:32:42.842Z",
"duration_ms" : 426,
"trace.span_id" : "3cc82e61-4abd-4ccb-9319-8a8cd95502cb",
"trace.parent_id" : null,
"trace.trace_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"exit.case" : "succeeded",
"children" : [
{
"name" : "resource",
"service" : "example",
"timestamp" : "2022-03-09T01:32:43.098Z",
"duration_ms" : 170,
"trace.span_id" : "6d95facf-327b-4639-a0d0-ea91c2ca9e00",
"trace.parent_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"trace.trace_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"exit.case" : "succeeded",
"children" : [
{
"name" : "acquire",
"service" : "example",
"timestamp" : "2022-03-09T01:32:43.102Z",
"duration_ms" : 1,
"trace.span_id" : "17e29b65-0b23-40d0-9e5f-4ce091940d6d",
"trace.parent_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"trace.trace_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"exit.case" : "succeeded",
"children" : [
]
},
{
"name" : "use",
"service" : "example",
"timestamp" : "2022-03-09T01:32:43.248Z",
"duration_ms" : 19,
"trace.span_id" : "097d92cb-125d-4f5b-a3b0-5f91cee471ae",
"trace.parent_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"trace.trace_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"exit.case" : "succeeded",
"children" : [
{
"name" : "a",
"service" : "example",
"timestamp" : "2022-03-09T01:32:43.257Z",
"duration_ms" : 3,
"trace.span_id" : "d2b69f4c-dbf8-4708-ace4-0baf8cb9c9f6",
"trace.parent_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"trace.trace_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"exit.case" : "succeeded",
"thread" : "io-compute-3",
"children" : [
]
},
{
"name" : "b",
"service" : "example",
"timestamp" : "2022-03-09T01:32:43.257Z",
"duration_ms" : 3,
"trace.span_id" : "ca1439fe-a556-4f9a-9d46-78d0a8cef308",
"trace.parent_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"trace.trace_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"exit.case" : "succeeded",
"thread" : "io-compute-1",
"children" : [
]
}
]
},
{
"name" : "release",
"service" : "example",
"timestamp" : "2022-03-09T01:32:43.268Z",
"duration_ms" : 0,
"trace.span_id" : "354d5757-ebc7-4ff4-b0cc-9ebfbf71cefb",
"trace.parent_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"trace.trace_id" : "015645c3-5f23-464a-a07b-fcf588c17703",
"exit.case" : "succeeded",
"children" : [
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment