-
Notifications
You must be signed in to change notification settings - Fork 38
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]] | ||
|
||
/** Returns the context of a span when it is available in the scope. | ||
*/ | ||
def currentSpanContext: F[Option[SpanContext]] | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an alternative, we can use |
||
val currentSpanContext: F[Option[SpanContext]] = Applicative[F].pure(None) | ||
def rootScope: Resource[F, Unit] = resourceUnit | ||
def noopScope: Resource[F, Unit] = resourceUnit | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check that the context is also valid There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => | ||
|
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.
This ship may have already sailed, but this is the signature that I think prevents using
Kleisli
(instead ofIOLocal
)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.
I think that ship sailed with the
Resource[F, Span[F]]
that are already exposed viaTracerMacro
.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.
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.
Like, is this a new API, or is it natchez-with-lazy-macros?
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.
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]
withinF
, without compromising aKleisli
implementation.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.
I went down that same road for a while with Natchez and turned around. I had a hard time with the inner
F
being aKleisli
, however much I wanted the outerF
to be aKleisli
, and I didn't want to introduce aG
.Note that in our case, the local environment is not a
Span[F]
, but a (right now package-private)TraceScope.Scope
.