Skip to content

Instantly share code, notes, and snippets.

@ryanmiville
Created March 8, 2022 17:58
Show Gist options
  • Save ryanmiville/6e4d756eceac70f9919c1de859e48f5a to your computer and use it in GitHub Desktop.
Save ryanmiville/6e4d756eceac70f9919c1de859e48f5a to your computer and use it in GitHub Desktop.
Child span behavior with single stream
//> 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 java.net.URI
trait Setup {
val entryPoint = Log.entryPoint[IO]("example")
val trace = entryPoint.root("root").flatMap(r => Resource.eval(Trace.ioTrace(r)))
def stream(implicit trace: IOTrace): Stream[IO, Unit] = {
Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.covary[IO]
.chunkN(3)
.flatMap { chunk =>
for {
span <- Stream.resource(entryPoint.root("new_root"))
_ <- Stream.resource(trace.spanR(span))
_ <- Stream.resource(trace.spanR("child"))
_ <- Stream.eval(trace.put("vals" -> chunk.toList.mkString(",")))
_ <- Stream.sleep[IO](1.second)
} yield ()
}
}
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}")
}
}
//> 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 java.net.URI
object Single extends IOApp.Simple with Setup {
def run: IO[Unit] = {
trace.use { implicit trace =>
stream.compile.lastOrError
}
}
}
/**
[info] {
"name" : "new_root",
"service" : "example",
"timestamp" : "2022-03-08T17:36:41.547765Z",
"duration_ms" : 1134,
"trace.span_id" : "0e80dbf1-88bd-4bb4-bb09-11649bff0cae",
"trace.parent_id" : null,
"trace.trace_id" : "58fa9a90-d53a-4751-9b26-dc93d564c9a2",
"exit.case" : "succeeded",
"children" : [
{
"name" : "child",
"service" : "example",
"timestamp" : "2022-03-08T17:36:41.562061Z",
"duration_ms" : 1082,
"trace.span_id" : "7a921e00-6fec-4b23-8e71-53a06d9ec8e3",
"trace.parent_id" : "58fa9a90-d53a-4751-9b26-dc93d564c9a2",
"trace.trace_id" : "58fa9a90-d53a-4751-9b26-dc93d564c9a2",
"exit.case" : "succeeded",
"vals" : "1,2,3",
"children" : [
]
}
]
}
[info] {
"name" : "new_root",
"service" : "example",
"timestamp" : "2022-03-08T17:36:42.692991Z",
"duration_ms" : 1005,
"trace.span_id" : "1eff82f8-53c7-4ddf-8438-f1d7547a72fb",
"trace.parent_id" : null,
"trace.trace_id" : "e83affb1-a22f-4040-a5cc-6e5c4ffda949",
"exit.case" : "succeeded",
"children" : [
{
"name" : "child",
"service" : "example",
"timestamp" : "2022-03-08T17:36:42.694100Z",
"duration_ms" : 1002,
"trace.span_id" : "b500336d-211d-4824-be84-7760df7dba56",
"trace.parent_id" : "e83affb1-a22f-4040-a5cc-6e5c4ffda949",
"trace.trace_id" : "e83affb1-a22f-4040-a5cc-6e5c4ffda949",
"exit.case" : "succeeded",
"vals" : "4,5,6",
"children" : [
]
}
]
}
[info] {
"name" : "new_root",
"service" : "example",
"timestamp" : "2022-03-08T17:36:43.698460Z",
"duration_ms" : 1006,
"trace.span_id" : "98bcd3f8-a24c-4d34-a4a7-7169e95ac41d",
"trace.parent_id" : null,
"trace.trace_id" : "7a32fde5-9d68-4733-8af3-b8a8f8646871",
"exit.case" : "succeeded",
"children" : [
{
"name" : "child",
"service" : "example",
"timestamp" : "2022-03-08T17:36:43.699491Z",
"duration_ms" : 1004,
"trace.span_id" : "414adeca-5443-419d-a443-05cdbad3b095",
"trace.parent_id" : "7a32fde5-9d68-4733-8af3-b8a8f8646871",
"trace.trace_id" : "7a32fde5-9d68-4733-8af3-b8a8f8646871",
"exit.case" : "succeeded",
"vals" : "7,8,9",
"children" : [
]
}
]
}
[info] {
"name" : "new_root",
"service" : "example",
"timestamp" : "2022-03-08T17:36:44.705589Z",
"duration_ms" : 1004,
"trace.span_id" : "4b7a58a9-1b1e-4105-a2d5-00092b92dcfa",
"trace.parent_id" : null,
"trace.trace_id" : "a7c806a7-2086-4f30-b020-b537bc53b050",
"exit.case" : "succeeded",
"children" : [
{
"name" : "child",
"service" : "example",
"timestamp" : "2022-03-08T17:36:44.706658Z",
"duration_ms" : 1003,
"trace.span_id" : "672bd98e-34e2-42d0-96d7-742419293b7f",
"trace.parent_id" : "a7c806a7-2086-4f30-b020-b537bc53b050",
"trace.trace_id" : "a7c806a7-2086-4f30-b020-b537bc53b050",
"exit.case" : "succeeded",
"vals" : "10",
"children" : [
]
}
]
}
[info] {
"name" : "root",
"service" : "example",
"timestamp" : "2022-03-08T17:36:41.109484Z",
"duration_ms" : 4602,
"trace.span_id" : "4ec0c6c1-4204-4257-bbfb-3fd8836110bc",
"trace.parent_id" : null,
"trace.trace_id" : "c68acedc-74ad-4cff-866d-3af60e76458e",
"exit.case" : "succeeded",
"children" : [
]
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment