-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
252 additions
and
362 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,7 @@ backend { | |
host = "http://localhost:9000" | ||
} | ||
|
||
tracer = "localhost:14250" | ||
tracer { | ||
host = "http://localhost:14250" | ||
} | ||
|
60 changes: 30 additions & 30 deletions
60
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.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 |
---|---|---|
@@ -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 | ||
} |
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
63 changes: 33 additions & 30 deletions
63
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.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 |
---|---|---|
@@ -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 | ||
} |
3 changes: 3 additions & 0 deletions
3
...lemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/AppConfig.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,3 @@ | ||
package zio.telemetry.opentelemetry.example.config | ||
|
||
final case class AppConfig(proxy: ProxyConfig, backend: BackendConfig, tracer: TracerHost) |
5 changes: 5 additions & 0 deletions
5
...try-example/src/main/scala/zio/telemetry/opentelemetry/example/config/BackendConfig.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,5 @@ | ||
package zio.telemetry.opentelemetry.example.config | ||
|
||
import sttp.model.Uri | ||
|
||
final case class BackendConfig(host: Uri) |
16 changes: 0 additions & 16 deletions
16
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Config.scala
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
...try-example/src/main/scala/zio/telemetry/opentelemetry/example/config/Configuration.scala
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
...metry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/ProxyConfig.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,5 @@ | ||
package zio.telemetry.opentelemetry.example.config | ||
|
||
import sttp.model.Uri | ||
|
||
final case class ProxyConfig(host: Uri) |
3 changes: 3 additions & 0 deletions
3
...emetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/TracerHost.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,3 @@ | ||
package zio.telemetry.opentelemetry.example.config | ||
|
||
final case class TracerHost(host: String) extends AnyVal |
9 changes: 0 additions & 9 deletions
9
...telemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/config/package.scala
This file was deleted.
Oops, something went wrong.
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
6 changes: 2 additions & 4 deletions
6
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Status.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
45 changes: 18 additions & 27 deletions
45
...metry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/StatusService.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 |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |
6 changes: 2 additions & 4 deletions
6
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Statuses.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 |
---|---|---|
@@ -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] | ||
} |
Oops, something went wrong.