Skip to content

Commit

Permalink
Merge pull request #381 from sbly/add-logging
Browse files Browse the repository at this point in the history
Add logging capabilities to Trace and Span
  • Loading branch information
mpilquist authored Nov 20, 2022
2 parents 98a92c1 + a22d46d commit 8fbbd8f
Show file tree
Hide file tree
Showing 18 changed files with 570 additions and 245 deletions.
6 changes: 2 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ lazy val lightstep = project
name := "natchez-lightstep",
description := "Lightstep support for Natchez.",
libraryDependencies ++= Seq(
"com.lightstep.tracer" % "lightstep-tracer-jre" % "0.30.5",
"org.scala-lang.modules" %% "scala-collection-compat" % collectionCompatVersion,
"com.lightstep.tracer" % "lightstep-tracer-jre" % "0.30.5"
)
)

Expand Down Expand Up @@ -197,7 +197,6 @@ lazy val opentracing = project
name := "natchez-opentracing",
description := "Base OpenTracing Utilities for Natchez",
libraryDependencies ++= Seq(
"org.scala-lang.modules" %% "scala-collection-compat" % collectionCompatVersion,
"io.opentracing" % "opentracing-api" % "0.33.0" % "provided",
"io.opentracing" % "opentracing-util" % "0.33.0" % "provided"
)
Expand Down Expand Up @@ -332,8 +331,7 @@ lazy val mock = project
name := "natchez-mock",
description := "Mock Open Tracing implementation",
libraryDependencies ++= Seq(
"io.opentracing" % "opentracing-mock" % "0.33.0",
"org.scala-lang.modules" %% "scala-collection-compat" % collectionCompatVersion
"io.opentracing" % "opentracing-mock" % "0.33.0"
))


Expand Down
37 changes: 29 additions & 8 deletions modules/core/shared/src/main/scala/Span.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ import java.net.URI
/** An span that can be passed around and used to create child spans. */
trait Span[F[_]] {

/** Put a sequence of fields into this span. */
/** Puts a sequence of fields into this span. */
def put(fields: (String, TraceValue)*): F[Unit]

/** Logs a sequence of fields on this span. */
def log(fields: (String, TraceValue)*): F[Unit]

/** Logs a single event on this span. */
def log(event: String): F[Unit]

/** Adds error information to this span. */
def attachError(err: Throwable): F[Unit]

/**
* The kernel for this span, which can be sent as headers to remote systems, which can then
* continue this trace by constructing spans that are children of this one.
Expand Down Expand Up @@ -61,15 +70,24 @@ trait Span[F[_]] {

override def kernel: G[Kernel] = f(outer.kernel)

override def attachError(err: Throwable) =
f(outer.attachError(err))

override def log(event: String) =
f(outer.log(event))

override def log(fields: (String, TraceValue)*) =
f(outer.log(fields: _*))

override def span(name: String): Resource[G, Span[G]] = outer
.span(name)
.map(_.mapK(f))
.mapK(f)

override def traceId: G[Option[String]] = f(outer.traceId)

override def spanId: G[Option[String]] = f(outer.spanId)

override def traceId: G[Option[String]] = f(outer.traceId)

override def traceUri: G[Option[URI]] = f(outer.traceUri)

/** Create resource with new span and add current span and kernel to parents of new span */
Expand Down Expand Up @@ -103,11 +121,14 @@ object Span {
def makeRoots[F[_]: Applicative](ep: EntryPoint[F]): Span[F] = new RootsSpan(ep)

private abstract class EphemeralSpan[F[_]: Applicative] extends Span[F] {
def put(fields: (String, TraceValue)*): F[Unit] = ().pure[F]
def kernel: F[Kernel] = Kernel(Map.empty).pure[F]
def traceId: F[Option[String]] = (None: Option[String]).pure[F]
def spanId: F[Option[String]] = (None: Option[String]).pure[F]
def traceUri: F[Option[URI]] = (None: Option[URI]).pure[F]
override def put(fields: (String, TraceValue)*): F[Unit] = ().pure[F]
override def kernel: F[Kernel] = Kernel(Map.empty).pure[F]
override def attachError(err: Throwable) = ().pure[F]
override def log(event: String) = ().pure[F]
override def log(fields: (String, TraceValue)*) = ().pure[F]
override def spanId: F[Option[String]] = (None: Option[String]).pure[F]
override def traceId: F[Option[String]] = (None: Option[String]).pure[F]
override def traceUri: F[Option[URI]] = (None: Option[URI]).pure[F]
}

private class NoopSpan[F[_]: Applicative] extends EphemeralSpan[F] {
Expand Down
Loading

0 comments on commit 8fbbd8f

Please sign in to comment.