From 548c7200ddc0c05c767a37e958d06c3e3d133342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Velimir=20Milinkovi=C4=87?= <81649656+mvelimir@users.noreply.github.com> Date: Thu, 5 Aug 2021 00:22:10 +0200 Subject: [PATCH] Rewrite the examples (#409) --- .../src/main/resources/application.conf | 4 +- .../opentelemetry/example/BackendServer.scala | 60 +++++++++--------- .../opentelemetry/example/JaegerTracer.scala | 7 +-- .../opentelemetry/example/ProxyServer.scala | 63 ++++++++++--------- .../example/config/AppConfig.scala | 3 + .../example/config/BackendConfig.scala | 5 ++ .../opentelemetry/example/config/Config.scala | 16 ----- .../example/config/Configuration.scala | 16 ----- .../example/config/ProxyConfig.scala | 5 ++ .../example/config/TracerHost.scala | 3 + .../example/config/package.scala | 9 --- .../opentelemetry/example/http/Client.scala | 24 +++---- .../opentelemetry/example/http/Status.scala | 6 +- .../example/http/StatusService.scala | 45 ++++++------- .../opentelemetry/example/http/Statuses.scala | 6 +- .../example/http/StatusesService.scala | 16 ++--- .../opentelemetry/example/http/package.scala | 10 +-- .../src/main/resources/application.conf | 4 +- .../opentracing/example/BackendServer.scala | 48 +++++++------- .../opentracing/example/JaegerTracer.scala | 4 +- .../opentracing/example/ProxyServer.scala | 47 +++++++------- .../example/config/AppConfig.scala | 3 + .../example/config/BackendConfig.scala | 3 + .../example/config/BackendUrl.scala | 3 + .../opentracing/example/config/Config.scala | 17 ----- .../example/config/Configuration.scala | 23 ------- .../example/config/ProxyConfig.scala | 3 + .../example/config/TracerHost.scala | 3 + .../opentracing/example/config/package.scala | 9 --- .../opentracing/example/http/Client.scala | 16 ++--- .../opentracing/example/http/Status.scala | 7 +-- .../example/http/StatusService.scala | 28 +++------ .../opentracing/example/http/Statuses.scala | 6 +- .../example/http/StatusesService.scala | 37 +++++------ .../opentracing/example/http/package.scala | 10 --- project/Dependencies.scala | 45 ++++++------- 36 files changed, 252 insertions(+), 362 deletions(-) create mode 100644 opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/AppConfig.scala create mode 100644 opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/BackendConfig.scala delete mode 100644 opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Config.scala delete mode 100644 opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Configuration.scala create mode 100644 opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/ProxyConfig.scala create mode 100644 opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/TracerHost.scala delete mode 100644 opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/package.scala create mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/AppConfig.scala create mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendConfig.scala create mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendUrl.scala delete mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Config.scala delete mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Configuration.scala create mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/ProxyConfig.scala create mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/TracerHost.scala delete mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/package.scala delete mode 100644 opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/package.scala diff --git a/opentelemetry-example/src/main/resources/application.conf b/opentelemetry-example/src/main/resources/application.conf index 412dbd4f..2577e3e7 100644 --- a/opentelemetry-example/src/main/resources/application.conf +++ b/opentelemetry-example/src/main/resources/application.conf @@ -6,5 +6,7 @@ backend { host = "http://localhost:9000" } -tracer = "localhost:14250" +tracer { + host = "http://localhost:14250" +} diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala index 3cc1f839..88e35c40 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala @@ -1,40 +1,40 @@ package zio.telemetry.opentelemetry.example -import org.http4s.server.{ Router, _ } -import org.http4s.server.blaze.BlazeServerBuilder -import zio.clock.Clock -import zio.interop.catz._ +import zio.console.putStrLn +import zio.magic._ +import zio.config.getConfig +import zio.config.typesafe.TypesafeConfig +import zio.config.magnolia.{ descriptor, Descriptor } import zio.telemetry.opentelemetry.Tracing -import zio.telemetry.opentelemetry.example.config.{ Config, Configuration } -import zio.telemetry.opentelemetry.example.http.{ AppEnv, AppTask, Client, StatusService } -import zio.{ ExitCode, Managed, ZIO, ZLayer } -import org.http4s.syntax.kleisli._ -import sttp.client.asynchttpclient.zio.AsyncHttpClientZioBackend +import zio.telemetry.opentelemetry.example.config.AppConfig +import zio.telemetry.opentelemetry.example.http.StatusService +import zio.{ App, ZIO } +import sttp.model.Uri +import zhttp.service.{ EventLoopGroup, Server } +import zhttp.service.server.ServerChannelFactory -object BackendServer extends zio.App { - val router = Router[AppTask]("/" -> StatusService.routes).orNotFound +object BackendServer extends App { + implicit val sttpUriDescriptor: Descriptor[Uri] = + Descriptor[String].transformOrFailLeft(Uri.parse)(_.toString) val server = - ZIO - .runtime[AppEnv] - .flatMap { implicit runtime => - implicit val ec = runtime.platform.executor.asEC - BlazeServerBuilder[AppTask](ec) - .bindHttp( - runtime.environment.get[Config].backend.host.port.getOrElse(defaults.HttpPort), - runtime.environment.get[Config].backend.host.host - ) - .withHttpApp(router) - .serve - .compile - .drain - } + getConfig[AppConfig].flatMap { conf => + val port = conf.backend.host.port.getOrElse(9000) + (Server.port(port) ++ Server.app(StatusService.routes)).make.use(_ => + putStrLn(s"BackendServer started on port $port") *> ZIO.never + ) + } - val httpBackend = ZLayer.fromManaged(Managed.make(AsyncHttpClientZioBackend())(_.close().ignore)) - val client = Configuration.live ++ httpBackend >>> Client.live - val tracer = Configuration.live >>> JaegerTracer.live - val envLayer = tracer ++ Clock.live >>> Tracing.live ++ Configuration.live ++ client + val configLayer = TypesafeConfig.fromDefaultLoader(descriptor[AppConfig]) override def run(args: List[String]) = - server.provideCustomLayer(envLayer).fold(_ => ExitCode.failure, _ => ExitCode.success) + server + .injectCustom( + configLayer, + JaegerTracer.live, + Tracing.live, + ServerChannelFactory.auto, + EventLoopGroup.auto(0) + ) + .exitCode } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala index 8869b928..60edd108 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala @@ -3,22 +3,21 @@ package zio.telemetry.opentelemetry.example import io.opentelemetry.api.trace.Tracer import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter import zio._ -import zio.telemetry.opentelemetry.example.config.{ Config, Configuration } +import zio.telemetry.opentelemetry.example.config.AppConfig import io.opentelemetry.sdk.OpenTelemetrySdk import io.opentelemetry.sdk.trace.SdkTracerProvider import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor object JaegerTracer { - def live: RLayer[Configuration, Has[Tracer]] = - ZLayer.fromServiceM((conf: Config) => + def live: RLayer[Has[AppConfig], Has[Tracer]] = + ZLayer.fromServiceM((conf: AppConfig) => for { spanExporter <- Task(JaegerGrpcSpanExporter.builder().setEndpoint(conf.tracer.host).build()) spanProcessor <- UIO(SimpleSpanProcessor.create(spanExporter)) tracerProvider <- UIO(SdkTracerProvider.builder().addSpanProcessor(spanProcessor).build()) openTelemetry <- UIO(OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build()) tracer <- UIO(openTelemetry.getTracer("zio.telemetry.opentelemetry.example.JaegerTracer")) - } yield tracer ) diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala index a3b84761..0c1515c8 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala @@ -1,41 +1,44 @@ package zio.telemetry.opentelemetry.example -import org.http4s.server.blaze.BlazeServerBuilder -import org.http4s.server.{ defaults, Router } -import zio.clock.Clock -import zio.interop.catz._ +import zio.console.putStrLn +import zio.magic._ +import zio.config.getConfig +import zio.config.typesafe.TypesafeConfig +import zio.config.magnolia.{ descriptor, Descriptor } import zio.telemetry.opentelemetry.Tracing -import zio.telemetry.opentelemetry.example.config.{ Config, Configuration } -import zio.telemetry.opentelemetry.example.http.{ AppEnv, AppTask, Client, StatusesService } -import zio.{ ExitCode, Managed, ZIO, ZLayer } -import org.http4s.syntax.kleisli._ -import sttp.client.asynchttpclient.zio.AsyncHttpClientZioBackend +import zio.telemetry.opentelemetry.example.config.AppConfig +import zio.telemetry.opentelemetry.example.http.{ Client, StatusesService } +import zio.{ App, Managed, ZIO, ZLayer } +import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend +import sttp.model.Uri +import zhttp.service.{ EventLoopGroup, Server } +import zhttp.service.server.ServerChannelFactory -object ProxyServer extends zio.App { - - val router = Router[AppTask]("/" -> StatusesService.routes).orNotFound +object ProxyServer extends App { + implicit val sttpUriDescriptor: Descriptor[Uri] = + Descriptor[String].transformOrFailLeft(Uri.parse)(_.toString) val server = - ZIO - .runtime[AppEnv] - .flatMap { implicit runtime => - implicit val ec = runtime.platform.executor.asEC - BlazeServerBuilder[AppTask](ec) - .bindHttp( - runtime.environment.get[Config].proxy.host.port.getOrElse(defaults.HttpPort), - runtime.environment.get[Config].proxy.host.host - ) - .withHttpApp(router) - .serve - .compile - .drain - } + getConfig[AppConfig].flatMap { conf => + val port = conf.proxy.host.port.getOrElse(8080) + (Server.port(port) ++ Server.app(StatusesService.routes)).make.use(_ => + putStrLn(s"ProxyServer started on port $port") *> ZIO.never + ) + } + val configLayer = TypesafeConfig.fromDefaultLoader(descriptor[AppConfig]) val httpBackend = ZLayer.fromManaged(Managed.make(AsyncHttpClientZioBackend())(_.close().ignore)) - val client = Configuration.live ++ httpBackend >>> Client.live - val tracer = Configuration.live >>> JaegerTracer.live - val envLayer = tracer ++ Clock.live >>> Tracing.live ++ Configuration.live ++ client override def run(args: List[String]) = - server.provideCustomLayer(envLayer).fold(_ => ExitCode.failure, _ => ExitCode.success) + server + .injectCustom( + configLayer, + httpBackend, + Client.live, + JaegerTracer.live, + Tracing.live, + ServerChannelFactory.auto, + EventLoopGroup.auto(0) + ) + .exitCode } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/AppConfig.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/AppConfig.scala new file mode 100644 index 00000000..44460f84 --- /dev/null +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/AppConfig.scala @@ -0,0 +1,3 @@ +package zio.telemetry.opentelemetry.example.config + +final case class AppConfig(proxy: ProxyConfig, backend: BackendConfig, tracer: TracerHost) diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/BackendConfig.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/BackendConfig.scala new file mode 100644 index 00000000..02ef5d65 --- /dev/null +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/BackendConfig.scala @@ -0,0 +1,5 @@ +package zio.telemetry.opentelemetry.example.config + +import sttp.model.Uri + +final case class BackendConfig(host: Uri) diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Config.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Config.scala deleted file mode 100644 index 3c5c05b3..00000000 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Config.scala +++ /dev/null @@ -1,16 +0,0 @@ -package zio.telemetry.opentelemetry.example.config - -import Config._ -import sttp.model.Uri - -final case class Config(proxy: ProxyConfig, backend: BackendConfig, tracer: TracerHost) - -object Config { - - final case class ProxyConfig(host: Uri) - - final case class BackendConfig(host: Uri) - - final case class TracerHost(host: String) extends AnyVal - -} diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Configuration.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Configuration.scala deleted file mode 100644 index 40a01b6f..00000000 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Configuration.scala +++ /dev/null @@ -1,16 +0,0 @@ -package zio.telemetry.opentelemetry.example.config - -import pureconfig.{ ConfigReader, ConfigSource } -import pureconfig.error.CannotConvert -import pureconfig.generic.auto._ -import sttp.model.Uri -import zio.{ Task, TaskLayer, URIO, ZIO, ZLayer } - -object Configuration { - implicit val uriReader = - ConfigReader.fromString(str => Uri.parse(str).left.map(CannotConvert(str, "Uri", _))) - - val live: TaskLayer[Configuration] = ZLayer.fromEffect(Task(ConfigSource.default.loadOrThrow[Config])) - - def get: URIO[Configuration, Config] = ZIO.access[Configuration](_.get) -} diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/ProxyConfig.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/ProxyConfig.scala new file mode 100644 index 00000000..5862282a --- /dev/null +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/ProxyConfig.scala @@ -0,0 +1,5 @@ +package zio.telemetry.opentelemetry.example.config + +import sttp.model.Uri + +final case class ProxyConfig(host: Uri) diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/TracerHost.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/TracerHost.scala new file mode 100644 index 00000000..6b3f8964 --- /dev/null +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/TracerHost.scala @@ -0,0 +1,3 @@ +package zio.telemetry.opentelemetry.example.config + +final case class TracerHost(host: String) extends AnyVal diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/package.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/package.scala deleted file mode 100644 index 930685e0..00000000 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/package.scala +++ /dev/null @@ -1,9 +0,0 @@ -package zio.telemetry.opentelemetry.example - -import zio.Has - -package object config { - - type Configuration = Has[Config] - -} diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Client.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Client.scala index 6fe1f101..a6ec13ae 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Client.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Client.scala @@ -1,14 +1,14 @@ package zio.telemetry.opentelemetry.example.http -import sttp.client._ -import sttp.client.asynchttpclient.WebSocketHandler -import sttp.client.circe.asJson -import zio.telemetry.opentelemetry.example.config.Config +import sttp.client3._ +import sttp.client3.ziojson._ +import sttp.capabilities.zio.ZioStreams +import sttp.capabilities.WebSockets +import zio.telemetry.opentelemetry.example.config.AppConfig import zio.{ Task, ZIO, ZLayer } -import zio.stream.ZStream object Client { - type Backend = SttpBackend[Task, ZStream[Any, Throwable, Byte], WebSocketHandler] + type Backend = SttpBackend[Task, ZioStreams with WebSockets] trait Service { def status(headers: Map[String, String]): Task[Statuses] @@ -19,17 +19,17 @@ object Client { val up = Status.up("proxy") - val live = ZLayer.fromServices((backend: Backend, conf: Config) => + val live = ZLayer.fromServices((backend: Backend, conf: AppConfig) => new Service { def status(headers: Map[String, String]): Task[Statuses] = backend .send( - basicRequest.get(conf.backend.host.path("status")).headers(headers).response(asJson[Status]) + basicRequest.get(conf.backend.host.withPath("status")).headers(headers).response(asJson[Status]) ) - .map(_.body match { - case Right(s) => Statuses(List(s, up)) - case _ => Statuses(List(Status.down("backend"), up)) - }) + .map { response => + val status = response.body.getOrElse(Status.down("backend")) + Statuses(List(status, up)) + } } ) } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Status.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Status.scala index 91387133..e03af9a1 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Status.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Status.scala @@ -1,13 +1,11 @@ package zio.telemetry.opentelemetry.example.http -import io.circe._ -import io.circe.generic.semiauto._ +import zio.json.{ DeriveJsonCodec, JsonCodec } final case class Status(name: String, status: String) object Status { - implicit val decoder: Decoder[Status] = deriveDecoder - implicit val encoder: Encoder[Status] = deriveEncoder + implicit val codec: JsonCodec[Status] = DeriveJsonCodec.gen[Status] final def up(component: String): Status = Status(component, status = "up") final def down(component: String): Status = Status(component, status = "down") diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusService.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusService.scala index aae6219a..09ebd06a 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusService.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusService.scala @@ -1,47 +1,38 @@ package zio.telemetry.opentelemetry.example.http -import io.circe.Encoder -import io.circe.syntax._ import io.opentelemetry.api.trace.SpanKind import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator import io.opentelemetry.context.propagation.{ TextMapGetter, TextMapPropagator } -import org.http4s._ -import org.http4s.circe.jsonEncoderOf -import org.http4s.dsl.Http4sDsl -import org.http4s.util.CaseInsensitiveString -import zio.interop.catz._ import zio.telemetry.opentelemetry.Tracing import zio.telemetry.opentelemetry.TracingSyntax._ import zio.telemetry.opentelemetry.example.http.{ Status => ServiceStatus } +import zhttp.http.{ ->, /, Header, Http, HttpApp, Method, Response, Root } +import zio.json.EncoderOps +import zio.ZIO import java.lang import scala.jdk.CollectionConverters._ object StatusService { - val dsl: Http4sDsl[AppTask] = Http4sDsl[AppTask] - import dsl._ + val propagator: TextMapPropagator = W3CTraceContextPropagator.getInstance() + val getter: TextMapGetter[List[Header]] = new TextMapGetter[List[Header]] { + override def keys(carrier: List[Header]): lang.Iterable[String] = + carrier.map(_.name.toString).asJava - implicit def encoder[A: Encoder]: EntityEncoder[AppTask, A] = jsonEncoderOf[AppTask, A] - - val propagator: TextMapPropagator = W3CTraceContextPropagator.getInstance() - val getter: TextMapGetter[Headers] = new TextMapGetter[Headers] { - override def keys(carrier: Headers): lang.Iterable[String] = - carrier.toList.map(_.name.value).asJava - - override def get(carrier: Headers, key: String): String = - carrier.get(CaseInsensitiveString(key)).map(_.value).orNull + override def get(carrier: List[Header], key: String): String = + carrier.find(_.name.toString == key).map(_.value.toString).orNull } - val routes: HttpRoutes[AppTask] = HttpRoutes.of[AppTask] { case request @ GET -> Root / "status" => - val response = for { - _ <- Tracing.addEvent("event from backend before response") - response <- Ok(ServiceStatus.up("backend").asJson) - _ <- Tracing.addEvent("event from backend after response") - } yield response + val routes: HttpApp[Tracing, Throwable] = + Http.collectM { case request @ Method.GET -> Root / "status" => + val response = for { + _ <- Tracing.addEvent("event from backend before response") + response <- ZIO.succeed(Response.jsonString(ServiceStatus.up("backend").toJson)) + _ <- Tracing.addEvent("event from backend after response") + } yield response - response.spanFrom(propagator, request.headers, getter, "/status", SpanKind.SERVER) - - } + response.spanFrom(propagator, request.headers, getter, "/status", SpanKind.SERVER) + } } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Statuses.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Statuses.scala index 42ad2e62..6916b46f 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Statuses.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Statuses.scala @@ -1,11 +1,9 @@ package zio.telemetry.opentelemetry.example.http -import io.circe._ -import io.circe.generic.semiauto._ +import zio.json.{ DeriveJsonCodec, JsonCodec } final case class Statuses(data: List[Status]) extends AnyVal object Statuses { - implicit val decoder: Decoder[Statuses] = deriveDecoder - implicit val encoder: Encoder[Statuses] = deriveEncoder + implicit val codec: JsonCodec[Statuses] = DeriveJsonCodec.gen[Statuses] } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusesService.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusesService.scala index 6b8ae769..8a306d58 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusesService.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusesService.scala @@ -1,39 +1,31 @@ package zio.telemetry.opentelemetry.example.http -import io.circe.Encoder import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator import io.opentelemetry.api.trace.{ SpanKind, StatusCode } import io.opentelemetry.context.propagation.{ TextMapPropagator, TextMapSetter } -import org.http4s.circe.jsonEncoderOf -import org.http4s.dsl.Http4sDsl -import org.http4s.{ EntityEncoder, HttpRoutes } import zio.UIO -import zio.interop.catz._ import zio.telemetry.opentelemetry.Tracing.root import zio.telemetry.opentelemetry.Tracing +import zhttp.http.{ ->, /, Http, HttpApp, Method, Response, Root } +import zio.json.EncoderOps import scala.collection.mutable object StatusesService { - val dsl: Http4sDsl[AppTask] = Http4sDsl[AppTask] - import dsl._ - - implicit def encoder[A: Encoder]: EntityEncoder[AppTask, A] = jsonEncoderOf[AppTask, A] - val propagator: TextMapPropagator = W3CTraceContextPropagator.getInstance() val setter: TextMapSetter[mutable.Map[String, String]] = (carrier, key, value) => carrier.update(key, value) val errorMapper: PartialFunction[Throwable, StatusCode] = { case _ => StatusCode.UNSET } - val routes: HttpRoutes[AppTask] = HttpRoutes.of[AppTask] { case GET -> Root / "statuses" => + val routes: HttpApp[Client with Tracing, Throwable] = Http.collectM { case Method.GET -> Root / "statuses" => root("/statuses", SpanKind.SERVER, errorMapper) { for { carrier <- UIO(mutable.Map[String, String]().empty) _ <- Tracing.setAttribute("http.method", "get") _ <- Tracing.addEvent("proxy-event") _ <- Tracing.inject(propagator, carrier, setter) - res <- Client.status(carrier.toMap).flatMap(Ok(_)) + res <- Client.status(carrier.toMap).map(s => Response.jsonString(s.toJson)) } yield res } } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/package.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/package.scala index ad9bc5df..4a500213 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/package.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/package.scala @@ -1,15 +1,7 @@ package zio.telemetry.opentelemetry.example -import zio.{ Has, RIO } -import zio.clock.Clock -import zio.telemetry.opentelemetry.Tracing -import zio.telemetry.opentelemetry.example.config.Configuration +import zio.Has package object http { - type Client = Has[Client.Service] - - type AppEnv = Tracing with Configuration with Clock with Client - type AppTask[A] = RIO[AppEnv, A] - } diff --git a/opentracing-example/src/main/resources/application.conf b/opentracing-example/src/main/resources/application.conf index 307eb19d..9f6d5bb2 100644 --- a/opentracing-example/src/main/resources/application.conf +++ b/opentracing-example/src/main/resources/application.conf @@ -8,4 +8,6 @@ backend { port = 9000 } -tracer = "0.0.0.0:9411" +tracer { + host = "0.0.0.0:9411" +} diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala index 53fe67c4..702b83cf 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala @@ -1,35 +1,37 @@ package zio.telemetry.opentracing.example -import cats.effect.{ ExitCode => catsExitCode } -import org.http4s.server.Router -import org.http4s.server.blaze.BlazeServerBuilder -import org.http4s.syntax.kleisli._ -import zio.{ ExitCode, ZEnv, ZIO } +import zio.{ App, ExitCode, ZEnv, ZIO } import zio.clock.Clock -import zio.interop.catz._ +import zio.console.putStrLn +import zio.magic._ +import zio.config.typesafe.TypesafeConfig +import zio.config.getConfig +import zio.config.magnolia.DeriveConfigDescriptor.descriptor import zio.telemetry.opentracing.example.JaegerTracer.makeService -import zio.telemetry.opentracing.example.config.Configuration -import zio.telemetry.opentracing.example.http.{ AppTask, StatusService } +import zio.telemetry.opentracing.example.config.AppConfig +import zio.telemetry.opentracing.example.http.StatusService +import zhttp.service.{ EventLoopGroup, Server } +import zhttp.service.server.ServerChannelFactory -object BackendServer extends CatsApp { +object BackendServer extends App { + + private val configLayer = TypesafeConfig.fromDefaultLoader(descriptor[AppConfig]) override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] = { val exit = ZIO.runtime[Clock].flatMap { implicit runtime => - implicit val ec = runtime.platform.executor.asEC - for { - conf <- Configuration.load.provideLayer(Configuration.live) - service = makeService(conf.tracer.host, "zio-backend") - router = Router[AppTask]("/" -> StatusService.status(service)).orNotFound - result <- BlazeServerBuilder[AppTask](ec) - .bindHttp(conf.backend.port, conf.backend.host) - .withHttpApp(router) - .serve - .compile[AppTask, AppTask, catsExitCode] - .drain - .as(ExitCode.success) - } yield result + getConfig[AppConfig].flatMap { conf => + val service = makeService(conf.tracer.host, "zio-backend") + (Server.port(conf.backend.port) ++ Server.app(StatusService.status(service))).make + .use(_ => putStrLn(s"BackendServer started at ${conf.backend.port}") *> ZIO.never) + .exitCode + }.injectCustom( + configLayer, + ServerChannelFactory.auto, + EventLoopGroup.auto(0) + ) } - exit.orElse(ZIO.succeed(ExitCode.failure)) + + exit orElse ZIO.succeed(ExitCode.failure) } } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala index afec3210..2bafb77f 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala @@ -12,7 +12,7 @@ import zipkin2.reporter.okhttp3.OkHttpSender object JaegerTracer { - def makeService(host: String, serviceName: String): ZLayer[Clock, Throwable, Clock with OpenTracing] = { + def makeService(host: String, serviceName: String): ZLayer[Clock, Throwable, OpenTracing] = { val url = new URIBuilder().setScheme("http").setHost(host).setPath("/api/v2/spans").build.toString val senderBuilder = OkHttpSender.newBuilder.compressionEnabled(true).endpoint(url) @@ -21,6 +21,6 @@ object JaegerTracer { .withReporter(new ZipkinV2Reporter(AsyncReporter.create(senderBuilder.build))) .build - OpenTracing.live(tracer) ++ Clock.live + OpenTracing.live(tracer) } } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala index baaa2ba6..b4f62aa8 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala @@ -1,37 +1,38 @@ package zio.telemetry.opentracing.example -import cats.effect.{ ExitCode => catsExitCode } -import org.http4s.server.Router -import org.http4s.server.blaze.BlazeServerBuilder -import org.http4s.syntax.kleisli._ +import zio.{ App, ExitCode, ZEnv, ZIO } +import zio.console.putStrLn import sttp.model.Uri -import zio.{ ExitCode, ZEnv, ZIO } -import zio.clock.Clock -import zio.interop.catz._ +import zio.magic._ +import zio.config.typesafe.TypesafeConfig +import zio.config.getConfig +import zio.config.magnolia.DeriveConfigDescriptor.descriptor import zio.telemetry.opentracing.example.JaegerTracer.makeService -import zio.telemetry.opentracing.example.config.Configuration -import zio.telemetry.opentracing.example.http.{ AppTask, StatusesService } +import zio.telemetry.opentracing.example.config.AppConfig +import zio.telemetry.opentracing.example.http.StatusesService +import zhttp.service.{ EventLoopGroup, Server } +import zhttp.service.server.ServerChannelFactory -object ProxyServer extends CatsApp { +object ProxyServer extends App { + + private val configLayer = TypesafeConfig.fromDefaultLoader(descriptor[AppConfig]) override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] = { val exit = - ZIO.runtime[Clock].flatMap { implicit runtime => - implicit val ec = runtime.platform.executor.asEC + getConfig[AppConfig].flatMap { conf => + val service = makeService(conf.tracer.host, "zio-proxy") for { - conf <- Configuration.load.provideLayer(Configuration.live) - service = makeService(conf.tracer.host, "zio-proxy") backendUrl <- ZIO.fromEither(Uri.safeApply(conf.backend.host, conf.backend.port)) - router = Router[AppTask]("/" -> StatusesService.statuses(backendUrl, service)).orNotFound - result <- BlazeServerBuilder[AppTask](ec) - .bindHttp(conf.proxy.port, conf.proxy.host) - .withHttpApp(router) - .serve - .compile[AppTask, AppTask, catsExitCode] - .drain - .as(ExitCode.success) + result <- (Server.port(conf.proxy.port) ++ Server.app(StatusesService.statuses(backendUrl, service))).make + .use(_ => putStrLn(s"ProxyServer started on ${conf.proxy.port}") *> ZIO.never) + .exitCode } yield result - } + }.injectCustom( + configLayer, + ServerChannelFactory.auto, + EventLoopGroup.auto(0) + ) + exit orElse ZIO.succeed(ExitCode.failure) } } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/AppConfig.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/AppConfig.scala new file mode 100644 index 00000000..36d18bec --- /dev/null +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/AppConfig.scala @@ -0,0 +1,3 @@ +package zio.telemetry.opentracing.example.config + +final case class AppConfig(proxy: ProxyConfig, backend: BackendConfig, tracer: TracerHost) diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendConfig.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendConfig.scala new file mode 100644 index 00000000..34b2dd3d --- /dev/null +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendConfig.scala @@ -0,0 +1,3 @@ +package zio.telemetry.opentracing.example.config + +final case class BackendConfig(host: String, port: Int) diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendUrl.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendUrl.scala new file mode 100644 index 00000000..868a5b4e --- /dev/null +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/BackendUrl.scala @@ -0,0 +1,3 @@ +package zio.telemetry.opentracing.example.config + +final case class BackendUrl(url: String) extends AnyVal diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Config.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Config.scala deleted file mode 100644 index c37069bd..00000000 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Config.scala +++ /dev/null @@ -1,17 +0,0 @@ -package zio.telemetry.opentracing.example.config - -import zio.telemetry.opentracing.example.config.Config._ - -final case class Config(proxy: ProxyConfig, backend: BackendConfig, tracer: TracerHost) - -object Config { - - final case class ProxyConfig(host: String, port: Int) - - final case class BackendUrl(url: String) extends AnyVal - - final case class BackendConfig(host: String, port: Int) - - final case class TracerHost(host: String) extends AnyVal - -} diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Configuration.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Configuration.scala deleted file mode 100644 index a72e6d7a..00000000 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/Configuration.scala +++ /dev/null @@ -1,23 +0,0 @@ -package zio.telemetry.opentracing.example.config - -import pureconfig.ConfigSource -import pureconfig.generic.auto._ -import zio.Task -import zio.ZLayer -import zio.ZIO - -object Configuration { - - trait Service { - val load: Task[Config] - } - - object Live extends Service { - val load: Task[Config] = Task.effect(ConfigSource.default.loadOrThrow[Config]) - } - - val live: ZLayer[Any, Throwable, Configuration] = ZLayer.succeed(Live) - - val load: ZIO[Configuration, Throwable, Config] = ZIO.accessM[Configuration](_.get.load) - -} diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/ProxyConfig.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/ProxyConfig.scala new file mode 100644 index 00000000..ac2b6a85 --- /dev/null +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/ProxyConfig.scala @@ -0,0 +1,3 @@ +package zio.telemetry.opentracing.example.config + +final case class ProxyConfig(host: String, port: Int) diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/TracerHost.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/TracerHost.scala new file mode 100644 index 00000000..5b1d7e95 --- /dev/null +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/TracerHost.scala @@ -0,0 +1,3 @@ +package zio.telemetry.opentracing.example.config + +final case class TracerHost(host: String) extends AnyVal diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/package.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/package.scala deleted file mode 100644 index aa67e732..00000000 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/config/package.scala +++ /dev/null @@ -1,9 +0,0 @@ -package zio.telemetry.opentracing.example - -import zio.Has - -package object config { - - type Configuration = Has[Configuration.Service] - -} diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Client.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Client.scala index 4e24d59f..fc05be42 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Client.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Client.scala @@ -1,17 +1,19 @@ package zio.telemetry.opentracing.example.http -import io.circe.Error -import sttp.client._ -import sttp.client.asynchttpclient.zio._ -import sttp.client.circe.asJson +import sttp.client3._ +import sttp.client3.asynchttpclient.zio._ +import sttp.client3.ziojson._ import sttp.model.Uri import zio.Task object Client { private val backend = AsyncHttpClientZioBackend() - def status(uri: Uri, headers: Map[String, String]): Task[Response[Either[ResponseError[Error], Status]]] = - backend.flatMap { implicit backend => - basicRequest.get(uri).headers(headers).response(asJson[Status]).send() + def status( + uri: Uri, + headers: Map[String, String] + ): Task[Response[Either[ResponseException[String, String], Status]]] = + backend.flatMap { backend => + basicRequest.get(uri).headers(headers).response(asJson[Status]).send(backend) } } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Status.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Status.scala index f1b0f361..6dc92657 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Status.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Status.scala @@ -1,15 +1,12 @@ package zio.telemetry.opentracing.example.http -import io.circe._ -import io.circe.generic.semiauto._ +import zio.json.{ DeriveJsonCodec, JsonCodec } final case class Status(name: String, status: String) object Status { - implicit val decoder: Decoder[Status] = deriveDecoder - implicit val encoder: Encoder[Status] = deriveEncoder + implicit val codec: JsonCodec[Status] = DeriveJsonCodec.gen[Status] final def up(component: String): Status = Status(component, status = "up") final def down(component: String): Status = Status(component, status = "down") - } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusService.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusService.scala index d59c96cd..6a2e4e86 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusService.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusService.scala @@ -1,34 +1,24 @@ package zio.telemetry.opentracing.example.http -import io.circe.Encoder -import io.circe.syntax._ import io.opentracing.propagation.Format.Builtin.{ HTTP_HEADERS => HttpHeadersFormat } import io.opentracing.propagation.TextMapAdapter -import org.http4s._ -import org.http4s.circe.jsonEncoderOf -import org.http4s.dsl.Http4sDsl import zio.clock.Clock -import zio.interop.catz._ +import zio.magic._ import zio.telemetry.opentracing.example.http.{ Status => ServiceStatus } import zio.telemetry.opentracing._ -import zio.ZIO -import zio.ZLayer +import zio.{ ZIO, ZLayer } +import zhttp.http.{ ->, /, Http, HttpApp, Method, Response, Root } +import zio.json.EncoderOps import scala.jdk.CollectionConverters._ object StatusService { - - val dsl: Http4sDsl[AppTask] = Http4sDsl[AppTask] - import dsl._ - - implicit def encoder[A: Encoder]: EntityEncoder[AppTask, A] = jsonEncoderOf[AppTask, A] - - def status(service: ZLayer[Clock, Throwable, Clock with OpenTracing]): HttpRoutes[AppTask] = - HttpRoutes.of[AppTask] { case request @ GET -> Root / "status" => - val headers = request.headers.toList.map(h => h.name.value -> h.value).toMap + def status(service: ZLayer[Clock, Throwable, OpenTracing]): HttpApp[Clock, Throwable] = + Http.collectM { case request @ Method.GET -> Root / "status" => + val headers = request.headers.map(h => h.name.toString -> h.value.toString).toMap ZIO.unit .spanFrom(HttpHeadersFormat, new TextMapAdapter(headers.asJava), "/status") - .provideLayer(service) *> Ok(ServiceStatus.up("backend").asJson) + .as(Response.jsonString(ServiceStatus.up("backend").toJson)) + .inject(service, Clock.live) } - } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Statuses.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Statuses.scala index 009c88b9..75f1194d 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Statuses.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/Statuses.scala @@ -1,11 +1,9 @@ package zio.telemetry.opentracing.example.http -import io.circe._ -import io.circe.generic.semiauto._ +import zio.json.{ DeriveJsonCodec, JsonCodec } final case class Statuses(data: List[Status]) extends AnyVal object Statuses { - implicit val decoder: Decoder[Statuses] = deriveDecoder - implicit val encoder: Encoder[Statuses] = deriveEncoder + implicit val codec: JsonCodec[Statuses] = DeriveJsonCodec.gen[Statuses] } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusesService.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusesService.scala index d2cc4587..d7dd3911 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusesService.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/StatusesService.scala @@ -1,54 +1,45 @@ package zio.telemetry.opentracing.example.http -import io.circe.Encoder import io.opentracing.propagation.Format.Builtin.{ HTTP_HEADERS => HttpHeadersFormat } import io.opentracing.propagation.TextMapAdapter import io.opentracing.tag.Tags -import org.http4s.circe.jsonEncoderOf -import org.http4s.dsl.Http4sDsl -import org.http4s.{ EntityEncoder, HttpRoutes } import sttp.model.Uri +import sttp.model.Method.GET import zio.clock.Clock -import zio.interop.catz._ +import zio.magic._ import zio.telemetry.opentracing.OpenTracing -import zio.UIO -import zio.ZLayer +import zio.{ UIO, ZLayer } +import zhttp.http.{ ->, /, HttpApp, Method, Response, Root } +import zio.json.EncoderOps import scala.collection.mutable import scala.jdk.CollectionConverters._ object StatusesService { - - def statuses(backendUri: Uri, service: ZLayer[Clock, Throwable, Clock with OpenTracing]): HttpRoutes[AppTask] = { - val dsl: Http4sDsl[AppTask] = Http4sDsl[AppTask] - import dsl._ - - implicit def encoder[A: Encoder]: EntityEncoder[AppTask, A] = jsonEncoderOf[AppTask, A] - - HttpRoutes.of[AppTask] { case GET -> Root / "statuses" => + def statuses(backendUri: Uri, service: ZLayer[Clock, Throwable, OpenTracing]): HttpApp[Clock, Throwable] = + HttpApp.collectM { case Method.GET -> Root / "statuses" => val zio = for { _ <- OpenTracing.tag(Tags.SPAN_KIND.getKey, Tags.SPAN_KIND_CLIENT) - _ <- OpenTracing.tag(Tags.HTTP_METHOD.getKey, GET.name) + _ <- OpenTracing.tag(Tags.HTTP_METHOD.getKey, GET.method) _ <- OpenTracing.setBaggageItem("proxy-baggage-item-key", "proxy-baggage-item-value") buffer <- UIO.succeed(new TextMapAdapter(mutable.Map.empty[String, String].asJava)) _ <- OpenTracing.inject(HttpHeadersFormat, buffer) headers <- extractHeaders(buffer) up = Status.up("proxy") res <- Client - .status(backendUri.path("status"), headers) - .map(_.body) - .flatMap { - case Right(s) => Ok(Statuses(List(s, up))) - case _ => Ok(Statuses(List(Status.down("backend"), up))) + .status(backendUri.withPath("status"), headers) + .map { res => + val status = res.body.getOrElse(Status.down("backend")) + val statuses = Statuses(List(status, up)) + Response.jsonString(statuses.toJson) } } yield res zio .root("/statuses") - .provideLayer(service) + .inject(service, Clock.live) } - } private def extractHeaders(adapter: TextMapAdapter): UIO[Map[String, String]] = { val m = mutable.Map.empty[String, String] diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/package.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/package.scala deleted file mode 100644 index 2f1a8bb8..00000000 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/package.scala +++ /dev/null @@ -1,10 +0,0 @@ -package zio.telemetry.opentracing.example - -import zio.ZIO -import zio.clock.Clock - -package object http { - - type AppTask[A] = ZIO[Clock, Throwable, A] - -} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index af71f125..c23c6ea3 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -2,15 +2,17 @@ import sbt._ object Dependencies { object Versions { - val http4s = "0.21.25" - val jaeger = "1.6.0" - val sttp = "2.2.9" - val opentracing = "0.33.0" - val opentelemetry = "1.4.1" - val opencensus = "0.28.3" - val zipkin = "2.16.3" - val zio = "1.0.10" - val zioInteropCats = "2.5.1.0" + val jaeger = "1.6.0" + val sttp3 = "3.3.11" + val opentracing = "0.33.0" + val opentelemetry = "1.4.1" + val opencensus = "0.28.3" + val zipkin = "2.16.3" + val zio = "1.0.10" + val zioHttp = "1.0.0.0-RC17" + val zioJson = "0.1.5" + val zioConfig = "1.0.6" + val zioMagic = "0.3.6" } lazy val zio = Seq( @@ -40,19 +42,18 @@ object Dependencies { ) lazy val example = Seq( - "org.typelevel" %% "cats-core" % "2.6.1", - "io.circe" %% "circe-generic" % "0.14.1", - "org.http4s" %% "http4s-core" % Versions.http4s, - "org.http4s" %% "http4s-blaze-server" % Versions.http4s, - "org.http4s" %% "http4s-dsl" % Versions.http4s, - "org.http4s" %% "http4s-circe" % Versions.http4s, - "io.jaegertracing" % "jaeger-core" % Versions.jaeger, - "io.jaegertracing" % "jaeger-client" % Versions.jaeger, - "io.jaegertracing" % "jaeger-zipkin" % Versions.jaeger, - "com.github.pureconfig" %% "pureconfig" % "0.16.0", - "com.softwaremill.sttp.client" %% "async-http-client-backend-zio" % Versions.sttp, - "com.softwaremill.sttp.client" %% "circe" % Versions.sttp, - "dev.zio" %% "zio-interop-cats" % Versions.zioInteropCats + "org.typelevel" %% "cats-core" % "2.6.1", + "io.jaegertracing" % "jaeger-core" % Versions.jaeger, + "io.jaegertracing" % "jaeger-client" % Versions.jaeger, + "io.jaegertracing" % "jaeger-zipkin" % Versions.jaeger, + "com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % Versions.sttp3, + "com.softwaremill.sttp.client3" %% "zio-json" % Versions.sttp3, + "io.github.kitlangton" %% "zio-magic" % Versions.zioMagic, + "io.d11" %% "zhttp" % Versions.zioHttp, + "dev.zio" %% "zio-json" % Versions.zioJson, + "dev.zio" %% "zio-config" % Versions.zioConfig, + "dev.zio" %% "zio-config-magnolia" % Versions.zioConfig, + "dev.zio" %% "zio-config-typesafe" % Versions.zioConfig ) lazy val opentracingExample = example ++ Seq(