-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example how to use otel instrumentation
- Loading branch information
Showing
14 changed files
with
203 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
id: opentelemetry-instrumentation-example | ||
title: "OpenTelemetry Automatic Instrumentation Example" | ||
--- | ||
|
||
Firstly, start Jaeger by running the following command: | ||
```bash | ||
docker run --rm -it \ | ||
-p 16686:16686 \ | ||
-p 14250:14250 \ | ||
jaegertracing/all-in-one:1.36 | ||
``` | ||
|
||
Then start the server application | ||
```bash | ||
sbt -J-Dotel.service.name=example-server \ | ||
-J-Dotel.traces.exporter=jaeger \ | ||
-J-Dotel.exporter.jaeger.endpoint=http://localhost:14250 \ | ||
-J-Dotel.propagators=jaeger \ | ||
-J-Dotel.traces.sampler=always_on \ | ||
"opentelemetryInstrumentationExample/runMain zio.telemetry.opentelemetry.instrumentation.example.ServerApp" | ||
|
||
|
||
``` | ||
|
||
and the client application which will send one request to the server application | ||
```bash | ||
sbt -J-Dotel.service.name=example-client \ | ||
-J-Dotel.traces.exporter=jaeger \ | ||
-J-Dotel.exporter.jaeger.endpoint=http://localhost:14250 \ | ||
-J-Dotel.propagators=jaeger \ | ||
-J-Dotel.traces.sampler=always_on \ | ||
"opentelemetryInstrumentationExample/runMain zio.telemetry.opentelemetry.instrumentation.example.ClientApp" | ||
``` | ||
|
||
Head over to [http://localhost:16686/](http://localhost:16686/) to see the result. |
4 changes: 4 additions & 0 deletions
4
opentelemetry-instrumentation-example/src/main/resources/application.conf
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,4 @@ | ||
server { | ||
host = "0.0.0.0" | ||
port = 9000 | ||
} |
27 changes: 27 additions & 0 deletions
27
...xample/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ClientApp.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,27 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example | ||
|
||
import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend | ||
import zio.config.magnolia.descriptor | ||
import zio.config.typesafe.TypesafeConfig | ||
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig | ||
import zio._ | ||
import zio.telemetry.opentelemetry.instrumentation.example.http.HttpClient | ||
|
||
object ClientApp extends ZIOAppDefault { | ||
|
||
private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) | ||
|
||
private val httpBackendLayer: TaskLayer[Backend] = | ||
ZLayer.scoped { | ||
ZIO.acquireRelease(AsyncHttpClientZioBackend())(_.close().ignore) | ||
} | ||
|
||
override def run: Task[ExitCode] = | ||
ZIO | ||
.serviceWithZIO[HttpClient](_.health.flatMap(r => Console.printLine(s"Health: $r")).exitCode) | ||
.provide( | ||
configLayer, | ||
httpBackendLayer, | ||
HttpClient.live | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
...xample/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ServerApp.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,23 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example | ||
|
||
import zio._ | ||
import zio.config.typesafe.TypesafeConfig | ||
import zio.config.magnolia._ | ||
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig | ||
import zio.telemetry.opentelemetry.instrumentation.example.http.HttpServer | ||
import zio.telemetry.opentelemetry.context.ContextStorage | ||
|
||
object ServerApp extends ZIOAppDefault { | ||
|
||
private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) | ||
|
||
override def run: Task[ExitCode] = | ||
ZIO | ||
.serviceWithZIO[HttpServer](_.start.exitCode) | ||
.provide( | ||
configLayer, | ||
HttpServer.live, | ||
ContextStorage.fiberRef | ||
) | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...src/main/scala/zio/telemetry/opentelemetry/instrumentation/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.instrumentation.example.config | ||
|
||
case class AppConfig(server: ServerConfig) |
3 changes: 3 additions & 0 deletions
3
.../main/scala/zio/telemetry/opentelemetry/instrumentation/example/config/ServerConfig.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.instrumentation.example.config | ||
|
||
case class ServerConfig(host: String, port: Int) |
32 changes: 32 additions & 0 deletions
32
.../src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/http/HttpClient.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,32 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example.http | ||
|
||
import sttp.client3._ | ||
import sttp.model.Uri | ||
import zio._ | ||
import zio.telemetry.opentelemetry.instrumentation.example.Backend | ||
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig | ||
|
||
case class HttpClient(backend: Backend, config: AppConfig) { | ||
|
||
private val backendUrl = | ||
Uri | ||
.safeApply(config.server.host, config.server.port) | ||
.map(_.withPath("status")) | ||
.left | ||
.map(new IllegalArgumentException(_)) | ||
|
||
def health: Task[String] = | ||
for { | ||
url <- ZIO.fromEither(backendUrl) | ||
response <- backend.send(basicRequest.get(url.withPath("health")).response(asStringAlways)) | ||
result = response.body | ||
} yield result | ||
|
||
} | ||
|
||
object HttpClient { | ||
|
||
val live: RLayer[AppConfig with Backend, HttpClient] = | ||
ZLayer.fromFunction(HttpClient.apply _) | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
.../src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/http/HttpServer.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,24 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example.http | ||
|
||
import zio._ | ||
import zhttp.service.Server | ||
import zio.Console.printLine | ||
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig | ||
|
||
case class HttpServer(config: AppConfig) { | ||
|
||
def start: ZIO[Any, Throwable, Nothing] = | ||
for { | ||
_ <- Server.start(config.server.port, HttpServerApp.routes) | ||
_ <- printLine(s"HttpServer started on port ${config.server.port}") | ||
never <- ZIO.never | ||
} yield never | ||
|
||
} | ||
|
||
object HttpServer { | ||
|
||
val live: URLayer[AppConfig, HttpServer] = | ||
ZLayer.fromFunction(HttpServer.apply _) | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...c/main/scala/zio/telemetry/opentelemetry/instrumentation/example/http/HttpServerApp.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,13 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example.http | ||
|
||
import zhttp.http._ | ||
import zio.ZIO | ||
|
||
object HttpServerApp { | ||
|
||
val routes: HttpApp[Any, Throwable] = | ||
Http.collectZIO { case _ @Method.GET -> _ / "health" => | ||
ZIO.succeed(Response.ok) | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/package.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,12 @@ | ||
package zio.telemetry.opentelemetry.instrumentation | ||
|
||
import sttp.capabilities.WebSockets | ||
import sttp.capabilities.zio.ZioStreams | ||
import sttp.client3.SttpBackend | ||
import zio.Task | ||
|
||
package object example { | ||
|
||
type Backend = SttpBackend[Task, ZioStreams with WebSockets] | ||
|
||
} |
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