Skip to content
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

Add a way to get the current span #101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ trait Tracer[F[_]] extends TracerMacro[F] {
*/
def meta: Tracer.Meta[F]

/** Returns the span from the scope, falling back to a noop if none is
* available.
*/
def currentSpan: F[Span[F]]
Comment on lines +31 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ship may have already sailed, but this is the signature that I think prevents using Kleisli (instead of IOLocal)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that ship sailed with the Resource[F, Span[F]] that are already exposed via TracerMacro.

This is why Natchez's resource is a natural transformation: typelevel/natchez#526. I do agree we need to decide on #88 before moving forward.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like, is this a new API, or is it natchez-with-lazy-macros?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the signature that I think prevents using Kleisli (instead of IOLocal)

So actually I'm not sure if this is true now. After getting very lost in typelevel/natchez#713 (and dragging poor @bpholt into my delusions 😅 ) I realized, isn't that PR doing exactly this? i.e. making it possible to access the Span[F] within F, without compromising a Kleisli implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went down that same road for a while with Natchez and turned around. I had a hard time with the inner F being a Kleisli, however much I wanted the outer F to be a Kleisli, and I didn't want to introduce a G.

Note that in our case, the local environment is not a Span[F], but a (right now package-private) TraceScope.Scope.


/** Returns the context of a span when it is available in the scope.
*/
def currentSpanContext: F[Option[SpanContext]]
Expand Down Expand Up @@ -137,6 +142,7 @@ object Tracer {
private val builder = SpanBuilder.noop(noopBackend)
private val resourceUnit = Resource.unit[F]
val meta: Meta[F] = Meta.disabled
val currentSpan: F[Span[F]] = builder.startUnmanaged
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative, we can use Applicative[F].pure(Span.fromBackend(noopBackend))

val currentSpanContext: F[Option[SpanContext]] = Applicative[F].pure(None)
def rootScope: Resource[F, Unit] = resourceUnit
def noopScope: Resource[F, Unit] = resourceUnit
Expand Down
8 changes: 7 additions & 1 deletion examples/src/main/scala/TracingExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ object Work {
}

def doWorkInternal =
Console[F].println("Doin' work")
Console[F].println("Doin' work") *>
summon

def summon =
Tracer[F].currentSpan.flatMap { span =>
span.addEvent("I've been summoned.")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cats.effect.Sync
import cats.syntax.functor._
import io.opentelemetry.api.trace.{Span => JSpan}
import io.opentelemetry.api.trace.{Tracer => JTracer}
import org.typelevel.otel4s.trace.Span
import org.typelevel.otel4s.trace.SpanBuilder
import org.typelevel.otel4s.trace.SpanContext
import org.typelevel.otel4s.trace.Tracer
Expand All @@ -33,6 +34,14 @@ private[java] class TracerImpl[F[_]: Sync](
val meta: Tracer.Meta[F] =
Tracer.Meta.enabled

def currentSpan: F[Span[F]] =
scope.current.map {
case TraceScope.Scope.Span(_, jSpan, ctx) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that the context is also valid if ctx.isValid?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so.

Span.fromBackend(new SpanBackendImpl(jSpan, ctx))
case TraceScope.Scope.Noop | TraceScope.Scope.Root(_) =>
Span.fromBackend(Span.Backend.noop)
}

def currentSpanContext: F[Option[SpanContext]] =
scope.current.map {
case TraceScope.Scope.Span(_, _, spanCtx) if spanCtx.isValid =>
Expand Down