-
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 of otel instrumentation usage (#660)
* Add an example how to use otel instrumentation * Add doc (not finished) * Make instrumentation work * Attempt (failed) using Tracing with turned on instrumention * Use GlobalOpenTelemtry tracer * Fix internal health span * Improve docs
- Loading branch information
Showing
16 changed files
with
284 additions
and
37 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,43 @@ | ||
--- | ||
id: opentelemetry-instrumentation-example | ||
title: "OpenTelemetry Automatic Instrumentation Example" | ||
--- | ||
|
||
You can find the source code [here](https://github.com/zio/zio-telemetry/tree/series/2.x/opentelemetry-instrumentation-example). | ||
|
||
Firstly, download OpenTelemetry JVM agent JAR: | ||
```bash | ||
OTEL_AGENT_PATH=$(cs fetch --classpath "io.opentelemetry.javaagent:opentelemetry-javaagent:latest.release") | ||
``` | ||
|
||
Then start Jaeger by running the following command: | ||
```bash | ||
docker run --rm -it \ | ||
-e COLLECTOR_OTLP_ENABLED=true \ | ||
-p 14250:14250 \ | ||
-p 16686:16686 \ | ||
-p 4317:4317 \ | ||
jaegertracing/all-in-one:1.42 | ||
``` | ||
|
||
Then start the server application | ||
```bash | ||
sbt -J-javaagent:$OTEL_AGENT_PATH \ | ||
-J-Dotel.service.name=example-server \ | ||
-J-Dotel.traces.sampler=always_on \ | ||
-J-Dotel.traces.exporter=otlp \ | ||
-J-Dotel.metrics.exporter=none \ | ||
"opentelemetryInstrumentationExample/runMain zio.telemetry.opentelemetry.instrumentation.example.ServerApp" | ||
``` | ||
|
||
and the client application which will send one request to the server application | ||
```bash | ||
sbt -J-javaagent:$OTEL_AGENT_PATH \ | ||
-J-Dotel.service.name=example-client \ | ||
-J-Dotel.traces.sampler=always_on \ | ||
-J-Dotel.traces.exporter=otlp \ | ||
-J-Dotel.metrics.exporter=none \ | ||
"opentelemetryInstrumentationExample/runMain zio.telemetry.opentelemetry.instrumentation.example.ClientApp" | ||
``` | ||
|
||
Head over to [http://localhost:16686/](http://localhost:16686/) to see the result. |
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
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 | ||
} |
29 changes: 29 additions & 0 deletions
29
...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,29 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example | ||
|
||
import sttp.client3.httpclient.zio.HttpClientZioBackend | ||
import zio.config.magnolia.descriptor | ||
import zio.config.typesafe.TypesafeConfig | ||
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig | ||
import zio._ | ||
import zio.config.ReadError | ||
import zio.telemetry.opentelemetry.instrumentation.example.http.HttpClient | ||
|
||
object ClientApp extends ZIOAppDefault { | ||
|
||
private val configLayer: Layer[ReadError[String], AppConfig] = | ||
TypesafeConfig.fromResourcePath(descriptor[AppConfig]) | ||
|
||
private val httpBackendLayer: TaskLayer[Backend] = | ||
ZLayer.scoped { | ||
ZIO.acquireRelease(HttpClientZioBackend())(_.close().ignore) | ||
} | ||
|
||
override def run: Task[ExitCode] = | ||
ZIO | ||
.serviceWithZIO[HttpClient](_.health.exitCode) | ||
.provide( | ||
configLayer, | ||
httpBackendLayer, | ||
HttpClient.live | ||
) | ||
} |
36 changes: 36 additions & 0 deletions
36
...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,36 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry | ||
import io.opentelemetry.api.trace.Tracer | ||
import zio._ | ||
import zio.config.ReadError | ||
import zio.config.typesafe.TypesafeConfig | ||
import zio.config.magnolia._ | ||
import zio.telemetry.opentelemetry.context.ContextStorage | ||
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig | ||
import zio.telemetry.opentelemetry.instrumentation.example.http.{ HttpServer, HttpServerApp } | ||
import zio.telemetry.opentelemetry.tracing.Tracing | ||
|
||
object ServerApp extends ZIOAppDefault { | ||
|
||
private val configLayer: Layer[ReadError[String], AppConfig] = | ||
TypesafeConfig.fromResourcePath(descriptor[AppConfig]) | ||
|
||
private val globalTracerLayer: TaskLayer[Tracer] = | ||
ZLayer.fromZIO( | ||
ZIO.attempt(GlobalOpenTelemetry.getTracer("zio.telemetry.opentelemetry.instrumentation.example.ServerApp")) | ||
) | ||
|
||
override def run: Task[ExitCode] = | ||
ZIO | ||
.serviceWithZIO[HttpServer](_.start.exitCode) | ||
.provide( | ||
configLayer, | ||
HttpServer.live, | ||
HttpServerApp.live, | ||
Tracing.live, | ||
globalTracerLayer, | ||
ContextStorage.openTelemetryContext | ||
) | ||
|
||
} |
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, httpServerApp: HttpServerApp) { | ||
|
||
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 with HttpServerApp, HttpServer] = | ||
ZLayer.fromFunction(HttpServer.apply _) | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...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,30 @@ | ||
package zio.telemetry.opentelemetry.instrumentation.example.http | ||
|
||
import zhttp.http._ | ||
import zio._ | ||
import zio.telemetry.opentelemetry.tracing.Tracing | ||
|
||
case class HttpServerApp(tracing: Tracing) { | ||
|
||
import tracing.aspects._ | ||
|
||
val routes: HttpApp[Any, Throwable] = | ||
Http.collectZIO { case _ @Method.GET -> _ / "health" => | ||
health @@ span("health-endpoint") | ||
} | ||
|
||
def health: UIO[Response] = | ||
for { | ||
_ <- tracing.addEvent("executing health logic") | ||
_ <- tracing.setAttribute("zio", "telemetry") | ||
response <- ZIO.succeed(Response.ok) | ||
} yield response | ||
|
||
} | ||
|
||
object HttpServerApp { | ||
|
||
val live: URLayer[Tracing, HttpServerApp] = | ||
ZLayer.fromFunction(HttpServerApp.apply _) | ||
|
||
} |
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
Oops, something went wrong.