Skip to content

Commit

Permalink
Merge pull request #1505 from softwaremill/zio-opentracing-2
Browse files Browse the repository at this point in the history
Re-enable zio-opentracing for zio2
  • Loading branch information
adamw authored Jul 20, 2022
2 parents d416963 + dcd6208 commit 804d11d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ lazy val allAggregates = projectsWithOptionalNative ++
prometheusBackend.projectRefs ++
openTelemetryMetricsBackend.projectRefs ++
openTelemetryTracingZio1Backend.projectRefs ++
// openTelemetryTracingZioBackend.projectRefs ++ TODO: enable when a ZIO2-compatible version is released
openTelemetryTracingZioBackend.projectRefs ++
finagleBackend.projectRefs ++
armeriaBackend.projectRefs ++
armeriaScalazBackend.projectRefs ++
Expand Down Expand Up @@ -917,7 +917,7 @@ lazy val openTelemetryTracingZioBackend = (projectMatrix in file("observability/
.settings(
name := "opentelemetry-tracing-zio-backend",
libraryDependencies ++= Seq(
"dev.zio" %% "zio-opentelemetry" % "2.0.0-RC3",
"dev.zio" %% "zio-opentelemetry" % "2.0.0",
"io.opentelemetry" % "opentelemetry-sdk-testing" % openTelemetryVersion % Test
)
)
Expand Down
4 changes: 2 additions & 2 deletions docs/backends/wrappers/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To use, add the following dependency to your project (the `zio-*` modules depend
This backend depends on [zio-opentelemetry](https://github.com/zio/zio-telemetry).

The OpenTelemetry backend wraps a `Task` based ZIO backend.
In order to do that, you need to provide the wrapper with a `Tracing.Service` from zio-telemetry.
In order to do that, you need to provide the wrapper with a `Tracing` from zio-telemetry.

Here's how you construct `ZioTelemetryOpenTelemetryBackend`. I would recommend wrapping this is in `ZLayer`

Expand All @@ -71,7 +71,7 @@ import zio.telemetry.opentelemetry._
import sttp.client3.opentelemetry.zio._

val zioBackend: SttpBackend[Task, Any] = ???
val tracing: Tracing.Service = ???
val tracing: Tracing = ???

OpenTelemetryTracingZioBackend(zioBackend, tracing)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import scala.collection.mutable
private class OpenTelemetryTracingZioBackend[+P](
delegate: SttpBackend[Task, P],
tracer: OpenTelemetryZioTracer,
tracing: Tracing.Service
tracing: Tracing
) extends DelegateSttpBackend[Task, P](delegate) {
def send[T, R >: P with Effect[Task]](request: Request[T, R]): Task[Response[T]] = {
val carrier: mutable.Map[String, String] = mutable.Map().empty
Expand All @@ -35,7 +35,7 @@ private class OpenTelemetryTracingZioBackend[+P](
object OpenTelemetryTracingZioBackend {
def apply[P](
other: SttpBackend[Task, P],
tracing: Tracing.Service,
tracing: Tracing,
tracer: OpenTelemetryZioTracer = OpenTelemetryZioTracer.Default
): SttpBackend[Task, P] =
new OpenTelemetryTracingZioBackend[P](other, tracer, tracing)
Expand All @@ -44,18 +44,18 @@ object OpenTelemetryTracingZioBackend {

trait OpenTelemetryZioTracer {
def spanName[T](request: Request[T, Nothing]): String
def before[T](request: Request[T, Nothing]): RIO[Tracing.Service, Unit]
def after[T](response: Response[T]): RIO[Tracing.Service, Unit]
def before[T](request: Request[T, Nothing]): RIO[Tracing, Unit]
def after[T](response: Response[T]): RIO[Tracing, Unit]
}

object OpenTelemetryZioTracer {
val Default: OpenTelemetryZioTracer = new OpenTelemetryZioTracer {
override def spanName[T](request: Request[T, Nothing]): String = s"HTTP ${request.method.method}"
override def before[T](request: Request[T, Nothing]): RIO[Tracing.Service, Unit] =
override def before[T](request: Request[T, Nothing]): RIO[Tracing, Unit] =
Tracing.setAttribute("http.method", request.method.method) *>
Tracing.setAttribute("http.url", request.uri.toString()) *>
ZIO.unit
override def after[T](response: Response[T]): RIO[Tracing.Service, Unit] =
override def after[T](response: Response[T]): RIO[Tracing, Unit] =
Tracing.setAttribute("http.status_code", response.code.code.toLong) *>
ZIO.unit
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import sttp.client3.impl.zio.{RIOMonadAsyncError, ZioTestBase}
import sttp.client3.testing.SttpBackendStub
import sttp.client3.{Request, Response, SttpBackend, UriContext, basicRequest}
import sttp.model.StatusCode
import zio.{Task, ZIO}
import zio.{Runtime, Task, Unsafe, ZIO}
import zio.telemetry.opentelemetry.Tracing
import scala.collection.JavaConverters._

import scala.collection.JavaConverters._
import scala.collection.mutable

class OpenTelemetryTracingZioBackendTest extends AnyFlatSpec with Matchers with BeforeAndAfter with ZioTestBase {
Expand All @@ -24,7 +24,9 @@ class OpenTelemetryTracingZioBackendTest extends AnyFlatSpec with Matchers with

private val mockTracer =
SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).build().get(getClass.getName)
private val mockTracing = runtime.unsafeRun(ZIO.scoped(Tracing.scoped(mockTracer)))
private val mockTracing = Unsafe.unsafeCompat { implicit u =>
Runtime.default.unsafe.run(ZIO.scoped(Tracing.scoped(mockTracer))).getOrThrow()
}

private val backend: SttpBackend[Task, Any] =
OpenTelemetryTracingZioBackend(
Expand All @@ -44,7 +46,9 @@ class OpenTelemetryTracingZioBackendTest extends AnyFlatSpec with Matchers with
}

"ZioTelemetryOpenTelemetryBackend" should "record spans for requests" in {
val response = runtime.unsafeRun(basicRequest.post(uri"http://stub/echo").send(backend))
val response = Unsafe.unsafeCompat { implicit u =>
Runtime.default.unsafe.run(basicRequest.post(uri"http://stub/echo").send(backend)).getOrThrow()
}
response.code shouldBe StatusCode.Ok

val spans = spanExporter.getFinishedSpanItems.asScala
Expand All @@ -53,7 +57,9 @@ class OpenTelemetryTracingZioBackendTest extends AnyFlatSpec with Matchers with
}

it should "propagate span" in {
val response = runtime.unsafeRun(basicRequest.post(uri"http://stub/echo").send(backend))
val response = Unsafe.unsafeCompat { implicit u =>
Runtime.default.unsafe.run(basicRequest.post(uri"http://stub/echo").send(backend)).getOrThrow()
}
response.code shouldBe StatusCode.Ok

val spans = spanExporter.getFinishedSpanItems.asScala
Expand All @@ -65,7 +71,9 @@ class OpenTelemetryTracingZioBackendTest extends AnyFlatSpec with Matchers with
}

it should "set span status in case of error" in {
runtime.unsafeRunSync(basicRequest.post(uri"http://stub/error").send(backend))
Unsafe.unsafeCompat { implicit u =>
Runtime.default.unsafe.run(basicRequest.post(uri"http://stub/error").send(backend))
}

val spans = spanExporter.getFinishedSpanItems.asScala
spans should have size 1
Expand Down

0 comments on commit 804d11d

Please sign in to comment.