Skip to content

Commit

Permalink
Support setting attributes when creating a span (#659)
Browse files Browse the repository at this point in the history
* Support setting attributes when creating a span

* Fix lint error

* Generated Readme

* Fix readme

* Fix version in readme

* Revert to <version> again

* Hand edited README
  • Loading branch information
davidlar authored Feb 28, 2023
1 parent 6d395d6 commit d46551a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package zio.telemetry.opentelemetry.example.http

import io.opentelemetry.api.trace.{SpanKind, StatusCode}
import io.opentelemetry.api.trace.{ SpanKind, StatusCode }
import zhttp.http._
import zio._
import zio.json.EncoderOps
import zio.telemetry.opentelemetry.baggage.Baggage
import zio.telemetry.opentelemetry.baggage.propagation.BaggagePropagator
import zio.telemetry.opentelemetry.context.OutgoingContextCarrier
import zio.telemetry.opentelemetry.tracing.propagation.TraceContextPropagator
import zio.telemetry.opentelemetry.tracing.{ErrorMapper, Tracing}
import zio.telemetry.opentelemetry.tracing.{ ErrorMapper, Tracing }

case class ProxyHttpApp(client: Client, tracing: Tracing, baggage: Baggage) {

Expand All @@ -19,7 +19,7 @@ case class ProxyHttpApp(client: Client, tracing: Tracing, baggage: Baggage) {

val routes: HttpApp[Any, Throwable] =
Http.collectZIO { case Method.GET -> _ / "statuses" =>
statuses @@ root("/statuses", SpanKind.SERVER, errorMapper)
statuses @@ root("/statuses", SpanKind.SERVER, toErrorStatus = errorMapper)
}

def statuses: Task[Response] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ trait Tracing { self =>
carrier: IncomingContextCarrier[C],
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]

Expand Down Expand Up @@ -95,7 +96,8 @@ trait Tracing { self =>
propagator: TraceContextPropagator,
carrier: IncomingContextCarrier[C],
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty()
)(implicit trace: Trace): UIO[(Span, UIO[Any])]

/**
Expand All @@ -120,6 +122,7 @@ trait Tracing { self =>
def root[R, E, A](
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]

Expand All @@ -145,6 +148,7 @@ trait Tracing { self =>
def span[R, E, A](
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]

Expand All @@ -164,7 +168,8 @@ trait Tracing { self =>
*/
def spanUnsafe(
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty()
)(implicit trace: Trace): UIO[(Span, UIO[Any])]

/**
Expand Down Expand Up @@ -271,6 +276,7 @@ trait Tracing { self =>
span: Span,
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]

Expand Down Expand Up @@ -416,42 +422,46 @@ trait Tracing { self =>
carrier: IncomingContextCarrier[C],
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E1] = ErrorMapper.default[E1]
): ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] =
new ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] {
override def apply[R, E >: E1 <: E1, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
self.extractSpan(propagator, carrier, spanName, spanKind, toErrorStatus)(zio)
self.extractSpan(propagator, carrier, spanName, spanKind, attributes, toErrorStatus)(zio)
}

def root[E1](
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E1] = ErrorMapper.default[E1]
): ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] =
new ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] {
override def apply[R, E >: E1 <: E1, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
self.root(spanName, spanKind, toErrorStatus)(zio)
self.root(spanName, spanKind, attributes, toErrorStatus)(zio)
}

def span[E1](
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E1] = ErrorMapper.default[E1]
): ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] =
new ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] {
override def apply[R, E >: E1 <: E1, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
self.span(spanName, spanKind, toErrorStatus)(zio)
self.span(spanName, spanKind, attributes, toErrorStatus)(zio)
}

def inSpan[E1](
span: Span,
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E1] = ErrorMapper.default[E1]
): ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] =
new ZIOAspect[Nothing, Any, E1, E1, Nothing, Any] {
override def apply[R, E >: E1 <: E1, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
self.inSpan(span, spanName, spanKind, toErrorStatus)(zio)
self.inSpan(span, spanName, spanKind, attributes, toErrorStatus)(zio)
}

}
Expand Down Expand Up @@ -498,11 +508,12 @@ object Tracing {
carrier: IncomingContextCarrier[C],
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
extractContext(propagator, carrier).flatMap { context =>
ZIO.acquireReleaseWith {
createChild(context, spanName, spanKind)
createChild(context, spanName, spanKind, attributes)
} { case (endSpan, _) =>
endSpan
} { case (_, ctx) =>
Expand All @@ -514,11 +525,12 @@ object Tracing {
propagator: TraceContextPropagator,
carrier: IncomingContextCarrier[C],
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty()
)(implicit trace: Trace): UIO[(Span, UIO[Any])] =
for {
ctx <- extractContext(propagator, carrier)
updatedCtx <- createChildUnsafe(ctx, spanName, spanKind)
updatedCtx <- createChildUnsafe(ctx, spanName, spanKind, attributes)
oldCtx <- ctxStorage.getAndSet(updatedCtx)
span <- getCurrentSpan
finalize = end *> ctxStorage.set(oldCtx)
Expand All @@ -527,10 +539,11 @@ object Tracing {
override def root[R, E, A](
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
ZIO.acquireReleaseWith {
createRoot(spanName, spanKind)
createRoot(spanName, spanKind, attributes)
} { case (endSpan, _) =>
endSpan
} { case (_, ctx) =>
Expand All @@ -540,11 +553,12 @@ object Tracing {
override def span[R, E, A](
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
getCurrentContext.flatMap { old =>
ZIO.acquireReleaseWith {
createChild(old, spanName, spanKind)
createChild(old, spanName, spanKind, attributes)
} { case (endSpan, _) =>
endSpan
} { case (_, ctx) =>
Expand All @@ -554,11 +568,12 @@ object Tracing {

override def spanUnsafe(
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty()
)(implicit trace: Trace): UIO[(Span, UIO[Any])] =
for {
ctx <- getCurrentContext
updatedCtx <- createChildUnsafe(ctx, spanName, spanKind)
updatedCtx <- createChildUnsafe(ctx, spanName, spanKind, attributes)
_ <- ctxStorage.set(updatedCtx)
span <- getCurrentSpan
finalize = end *> ctxStorage.set(ctx)
Expand Down Expand Up @@ -609,10 +624,11 @@ object Tracing {
span: Span,
spanName: String,
spanKind: SpanKind = SpanKind.INTERNAL,
attributes: Attributes = Attributes.empty(),
toErrorStatus: ErrorMapper[E] = ErrorMapper.default[E]
)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
ZIO.acquireReleaseWith {
createChild(Context.root().`with`(span), spanName, spanKind)
createChild(Context.root().`with`(span), spanName, spanKind, attributes)
} { case (endSpan, _) =>
endSpan
} { case (_, ctx) =>
Expand Down Expand Up @@ -712,14 +728,16 @@ object Tracing {

private def createRoot(
spanName: String,
spanKind: SpanKind
spanKind: SpanKind,
attributes: Attributes
)(implicit trace: Trace): UIO[(UIO[Unit], Context)] =
for {
nanos <- currentNanos
span <- ZIO.succeed(
tracer
.spanBuilder(spanName)
.setNoParent()
.setAllAttributes(attributes)
.setSpanKind(spanKind)
.setStartTimestamp(nanos, TimeUnit.NANOSECONDS)
.startSpan()
Expand All @@ -729,14 +747,16 @@ object Tracing {
private def createChild(
parentCtx: Context,
spanName: String,
spanKind: SpanKind
spanKind: SpanKind,
attributes: Attributes
)(implicit trace: Trace): UIO[(UIO[Unit], Context)] =
for {
nanos <- currentNanos
span <- ZIO.succeed(
tracer
.spanBuilder(spanName)
.setParent(parentCtx)
.setAllAttributes(attributes)
.setSpanKind(spanKind)
.setStartTimestamp(nanos, TimeUnit.NANOSECONDS)
.startSpan()
Expand All @@ -746,7 +766,8 @@ object Tracing {
private def createChildUnsafe(
parentCtx: Context,
spanName: String,
spanKind: SpanKind
spanKind: SpanKind,
attributes: Attributes
)(implicit trace: Trace): UIO[Context] =
for {
nanos <- currentNanos
Expand All @@ -755,6 +776,7 @@ object Tracing {
tracer
.spanBuilder(spanName)
.setParent(parentCtx)
.setAllAttributes(attributes)
.setSpanKind(spanKind)
.setStartTimestamp(nanos, TimeUnit.NANOSECONDS)
.startSpan()
Expand Down

0 comments on commit d46551a

Please sign in to comment.