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

Replace SpanBuilder#wrapResource API #273

Merged
merged 3 commits into from
Jul 24, 2023
Merged
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 @@ -17,8 +17,6 @@
package org.typelevel.otel4s
package trace

import cats.effect.kernel.Resource

private[otel4s] trait TracerMacro[F[_]] {
self: Tracer[F] =>

Expand All @@ -39,7 +37,7 @@ private[otel4s] trait TracerMacro[F[_]] {
* {{{
* val tracer: Tracer[F] = ???
* val span: Span[F] = ???
* val customParent: SpanOps.Aux[F, Span[F]] = tracer
* val customParent: SpanOps[F] = tracer
* .spanBuilder("custom-parent")
* .withParent(span.context)
* .build
Expand All @@ -54,7 +52,7 @@ private[otel4s] trait TracerMacro[F[_]] {
* @param attributes
* the set of attributes to associate with the span
*/
def span(name: String, attributes: Attribute[_]*): SpanOps.Aux[F, Span[F]] =
def span(name: String, attributes: Attribute[_]*): SpanOps[F] =
macro TracerMacro.span

/** Creates a new root span. Even if a parent span is available in the scope,
Expand All @@ -72,39 +70,8 @@ private[otel4s] trait TracerMacro[F[_]] {
* @param attributes
* the set of attributes to associate with the span
*/
def rootSpan(
name: String,
attributes: Attribute[_]*
): SpanOps.Aux[F, Span[F]] =
def rootSpan(name: String, attributes: Attribute[_]*): SpanOps[F] =
macro TracerMacro.rootSpan

/** Creates a new child span. The span is automatically attached to a parent
* span (based on the scope).
*
* The lifecycle of the span is managed automatically. That means the span is
* ended upon the finalization of a resource.
*
* The abnormal termination (error, cancelation) is recorded by
* [[SpanFinalizer.Strategy.reportAbnormal default finalization strategy]].
*
* The structure of the inner spans:
* {{{
* > name
* > acquire
* > use
* > release
* }}}
*
* @param name
* the name of the span
*
* @param attributes
* the set of attributes to associate with the span
*/
def resourceSpan[A](name: String, attributes: Attribute[_]*)(
resource: Resource[F, A]
): SpanOps.Aux[F, Span.Res[F, A]] =
macro TracerMacro.resourceSpan[F, A]
}

object TracerMacro {
Expand All @@ -128,14 +95,4 @@ object TracerMacro {
q"(if ($meta.isEnabled) ${c.prefix}.spanBuilder($name).root.addAttributes(..$attributes) else $meta.noopSpanBuilder).build"
}

def resourceSpan[F[_], A](c: blackbox.Context)(
name: c.Expr[String],
attributes: c.Expr[Attribute[_]]*
)(resource: c.Expr[Resource[F, A]]): c.universe.Tree = {
import c.universe._
val meta = q"${c.prefix}.meta"

q"if ($meta.isEnabled) ${c.prefix}.spanBuilder($name).addAttributes(..$attributes).wrapResource($resource).build else $meta.noopResSpan($resource).build"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.typelevel.otel4s
package trace

import cats.effect.kernel.Resource

import scala.quoted.*

private[otel4s] trait TracerMacro[F[_]] {
Expand All @@ -41,7 +39,7 @@ private[otel4s] trait TracerMacro[F[_]] {
* {{{
* val tracer: Tracer[F] = ???
* val span: Span[F] = ???
* val customParent: SpanOps.Aux[F, Span[F]] = tracer
* val customParent: SpanOps[F] = tracer
* .spanBuilder("custom-parent")
* .withParent(span.context)
* .build
Expand All @@ -59,7 +57,7 @@ private[otel4s] trait TracerMacro[F[_]] {
inline def span(
inline name: String,
inline attributes: Attribute[_]*
): SpanOps.Aux[F, Span[F]] =
): SpanOps[F] =
${ TracerMacro.span('self, 'name, 'attributes) }

/** Creates a new root span. Even if a parent span is available in the scope,
Expand All @@ -80,38 +78,9 @@ private[otel4s] trait TracerMacro[F[_]] {
inline def rootSpan(
inline name: String,
inline attributes: Attribute[_]*
): SpanOps.Aux[F, Span[F]] =
): SpanOps[F] =
${ TracerMacro.rootSpan('self, 'name, 'attributes) }

/** Creates a new child span. The span is automatically attached to a parent
* span (based on the scope).
*
* The lifecycle of the span is managed automatically. That means the span is
* ended upon the finalization of a resource.
*
* The abnormal termination (error, cancelation) is recorded by
* [[SpanFinalizer.Strategy.reportAbnormal default finalization strategy]].
*
* The structure of the inner spans:
* {{{
* > name
* > acquire
* > use
* > release
* }}}
*
* @param name
* the name of the span
*
* @param attributes
* the set of attributes to associate with the span
*/
inline def resourceSpan[A](
inline name: String,
inline attributes: Attribute[_]*
)(inline resource: Resource[F, A]): SpanOps.Aux[F, Span.Res[F, A]] =
${ TracerMacro.resourceSpan('self, 'name, 'attributes, 'resource) }

}

object TracerMacro {
Expand All @@ -138,20 +107,4 @@ object TracerMacro {
else $tracer.meta.noopSpanBuilder.build
}

def resourceSpan[F[_], A](
tracer: Expr[Tracer[F]],
name: Expr[String],
attributes: Expr[Seq[Attribute[_]]],
resource: Expr[Resource[F, A]]
)(using Quotes, Type[F], Type[A]) =
'{
if ($tracer.meta.isEnabled)
$tracer
.spanBuilder($name)
.addAttributes($attributes*)
.wrapResource($resource)
.build
else $tracer.meta.noopResSpan($resource).build
}

}
29 changes: 0 additions & 29 deletions core/trace/src/main/scala/org/typelevel/otel4s/trace/Span.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,33 +157,4 @@ object Span {
new Span[F] {
def backend: Backend[F] = back
}

/** The allocation and release stages of a supplied resource are traced by
* separate spans. Carries a value of a wrapped resource.
*
* The structure of the inner spans:
* {{{
* > span-name
* > acquire
* > use
* > release
* }}}
*/
trait Res[F[_], A] extends Span[F] {
def value: A
}

object Res {
def unapply[F[_], A](span: Span.Res[F, A]): Option[A] =
Some(span.value)

private[otel4s] def fromBackend[F[_], A](
a: A,
back: Backend[F]
): Res[F, A] =
new Res[F, A] {
def value: A = a
def backend: Backend[F] = back
}
}
}
Loading