-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1320 from softwaremill/zio2-opentelemetry
Add separate modules for zio-opentelemetry 2, keeping zio1 compatibility in zio1 modules
- Loading branch information
Showing
8 changed files
with
246 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...main/scala/sttp/client3/ziotelemetry/opentelemetry/ZioTelemetryOpenTelemetryBackend.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package sttp.client3.ziotelemetry.opentelemetry | ||
|
||
import io.opentelemetry.api.trace.{SpanKind, StatusCode} | ||
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator | ||
import io.opentelemetry.context.propagation.{TextMapPropagator, TextMapSetter} | ||
import sttp.capabilities.Effect | ||
import sttp.client3._ | ||
import zio._ | ||
import zio.telemetry.opentelemetry.TracingSyntax.OpenTelemetryZioOps | ||
import zio.telemetry.opentelemetry._ | ||
|
||
import scala.collection.mutable | ||
|
||
private class ZioTelemetryOpenTelemetryBackend[+P]( | ||
delegate: SttpBackend[Task, P], | ||
tracer: ZioTelemetryOpenTelemetryTracer, | ||
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 | ||
val propagator: TextMapPropagator = W3CTraceContextPropagator.getInstance() | ||
val setter: TextMapSetter[mutable.Map[String, String]] = (carrier, key, value) => carrier.update(key, value) | ||
|
||
(for { | ||
_ <- Tracing.inject(propagator, carrier, setter) | ||
_ <- tracer.before(request) | ||
resp <- delegate.send(request.headers(carrier.toMap)) | ||
_ <- tracer.after(resp) | ||
} yield resp) | ||
.span(tracer.spanName(request), SpanKind.CLIENT, { case _ => StatusCode.ERROR }) | ||
.provide(tracing) | ||
} | ||
} | ||
|
||
object ZioTelemetryOpenTelemetryBackend { | ||
def apply[P]( | ||
other: SttpBackend[Task, P], | ||
tracing: Tracing.Service, | ||
tracer: ZioTelemetryOpenTelemetryTracer = ZioTelemetryOpenTelemetryTracer.empty | ||
): SttpBackend[Task, P] = | ||
new ZioTelemetryOpenTelemetryBackend[P](other, tracer, Has(tracing)) | ||
|
||
} | ||
|
||
trait ZioTelemetryOpenTelemetryTracer { | ||
def spanName[T](request: Request[T, Nothing]): String = s"HTTP ${request.method.method}" | ||
def before[T](request: Request[T, Nothing]): RIO[Tracing, Unit] | ||
def after[T](response: Response[T]): RIO[Tracing, Unit] | ||
} | ||
|
||
object ZioTelemetryOpenTelemetryTracer { | ||
val empty: ZioTelemetryOpenTelemetryTracer = new ZioTelemetryOpenTelemetryTracer { | ||
def before[T](request: Request[T, Nothing]): RIO[Tracing, Unit] = ZIO.unit | ||
def after[T](response: Response[T]): RIO[Tracing, Unit] = ZIO.unit | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
.../scala/sttp/client3/ziotelemetry/opentelemetry/ZioTelemetryOpenTelemetryBackendTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package sttp.client3.ziotelemetry.opentelemetry | ||
|
||
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider | ||
import io.opentelemetry.sdk.trace.`export`.SimpleSpanProcessor | ||
import org.scalatest.BeforeAndAfter | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
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 | ||
import zio.telemetry.opentelemetry.Tracing | ||
import scala.collection.JavaConverters._ | ||
|
||
import scala.collection.mutable | ||
|
||
class ZioTelemetryOpenTelemetryBackendTest extends AnyFlatSpec with Matchers with BeforeAndAfter with ZioTestBase { | ||
|
||
private val recordedRequests = mutable.ListBuffer[Request[_, _]]() | ||
|
||
private val spanExporter = InMemorySpanExporter.create() | ||
|
||
private val mockTracer = | ||
SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).build().get(getClass.getName) | ||
private val mockTracing = runtime.unsafeRun(Tracing.managed(mockTracer).useNow) | ||
|
||
private val backend: SttpBackend[Task, Any] = | ||
ZioTelemetryOpenTelemetryBackend( | ||
SttpBackendStub(new RIOMonadAsyncError[Any]).whenRequestMatchesPartial { | ||
case r if r.uri.toString.contains("echo") => | ||
recordedRequests += r | ||
Response.ok("") | ||
case r if r.uri.toString.contains("error") => | ||
throw new RuntimeException("something went wrong") | ||
}, | ||
mockTracing | ||
) | ||
|
||
before { | ||
recordedRequests.clear() | ||
spanExporter.reset() | ||
} | ||
|
||
"ZioTelemetryOpenTelemetryBackend" should "record spans for requests" in { | ||
val response = runtime.unsafeRun(basicRequest.post(uri"http://stub/echo").send(backend)) | ||
response.code shouldBe StatusCode.Ok | ||
|
||
val spans = spanExporter.getFinishedSpanItems.asScala | ||
spans should have size 1 | ||
spans.head.getName shouldBe "HTTP POST" | ||
} | ||
|
||
it should "propagate span" in { | ||
val response = runtime.unsafeRun(basicRequest.post(uri"http://stub/echo").send(backend)) | ||
response.code shouldBe StatusCode.Ok | ||
|
||
val spans = spanExporter.getFinishedSpanItems.asScala | ||
spans should have size 1 | ||
|
||
val spanId = spans.head.getSpanId | ||
val traceId = spans.head.getTraceId | ||
recordedRequests(0).header("traceparent") shouldBe Some(s"00-${traceId}-${spanId}-01") | ||
} | ||
|
||
it should "set span status in case of error" in { | ||
runtime.unsafeRunSync(basicRequest.post(uri"http://stub/error").send(backend)) | ||
|
||
val spans = spanExporter.getFinishedSpanItems.asScala | ||
spans should have size 1 | ||
|
||
spans.head.getStatus.getStatusCode shouldBe io.opentelemetry.api.trace.StatusCode.ERROR | ||
} | ||
|
||
} |
Oops, something went wrong.