diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyHttpApp.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyHttpApp.scala index 80970205..18992ffa 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyHttpApp.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyHttpApp.scala @@ -1,6 +1,6 @@ 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 @@ -8,7 +8,7 @@ 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) { @@ -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] = { diff --git a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/tracing/Tracing.scala b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/tracing/Tracing.scala index 26a6219d..51d049aa 100644 --- a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/tracing/Tracing.scala +++ b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/tracing/Tracing.scala @@ -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] @@ -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])] /** @@ -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] @@ -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] @@ -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])] /** @@ -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] @@ -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) } } @@ -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) => @@ -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) @@ -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) => @@ -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) => @@ -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) @@ -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) => @@ -712,7 +728,8 @@ object Tracing { private def createRoot( spanName: String, - spanKind: SpanKind + spanKind: SpanKind, + attributes: Attributes )(implicit trace: Trace): UIO[(UIO[Unit], Context)] = for { nanos <- currentNanos @@ -720,6 +737,7 @@ object Tracing { tracer .spanBuilder(spanName) .setNoParent() + .setAllAttributes(attributes) .setSpanKind(spanKind) .setStartTimestamp(nanos, TimeUnit.NANOSECONDS) .startSpan() @@ -729,7 +747,8 @@ 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 @@ -737,6 +756,7 @@ object Tracing { tracer .spanBuilder(spanName) .setParent(parentCtx) + .setAllAttributes(attributes) .setSpanKind(spanKind) .setStartTimestamp(nanos, TimeUnit.NANOSECONDS) .startSpan() @@ -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 @@ -755,6 +776,7 @@ object Tracing { tracer .spanBuilder(spanName) .setParent(parentCtx) + .setAllAttributes(attributes) .setSpanKind(spanKind) .setStartTimestamp(nanos, TimeUnit.NANOSECONDS) .startSpan()