Skip to content

Commit

Permalink
Add an example how to use otel instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
grouzen committed Feb 28, 2023
1 parent 32a3011 commit c494bf4
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 1 deletion.
18 changes: 17 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ Global / testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramew

addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")
addCommandAlias("compileExamples", "opentracingExample/compile;opentelemetryExample/compile")
addCommandAlias(
"compileExamples",
"opentracingExample/compile;opentelemetryExample/compile;opentelemetryInstrumentationExample/compile"
)

lazy val root =
project
Expand Down Expand Up @@ -79,6 +82,19 @@ lazy val opentelemetryExample =
.settings(libraryDependencies := Dependencies.opentelemetryExample)
.dependsOn(opentelemetry)

lazy val opentelemetryInstrumentationExample =
project
.in(file("opentelemetry-instrumentation-example"))
.settings(stdSettings("opentelemetry-instrumentation-example"))
.settings(publish / skip := true)
.settings(onlyWithScala2)
.settings(libraryDependencies := Dependencies.opentelemetryInstrumentationExample)
.dependsOn(opentelemetry)
.enablePlugins(JavaAgent)
.settings(
javaAgents += "io.opentelemetry.javaagent" % "opentelemetry-javaagent" % Dependencies.Versions.opentelemetry % "compile;runtime"
)

lazy val docs =
project
.in(file("zio-telemetry-docs"))
Expand Down
36 changes: 36 additions & 0 deletions docs/opentelemetry-instrumentation-example.md
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
server {
host = "0.0.0.0"
port = 9000
}
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
)
}
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
)

}
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)
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)
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 _)

}
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 _)

}
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)
}

}
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]

}
6 changes: 6 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,10 @@ object Dependencies {
Orgs.grpc % "grpc-netty-shaded" % ExampleVersions.grpcNetty
)

lazy val opentelemetryInstrumentationExample = example ++ Seq(
Orgs.opentelemetry % "opentelemetry-exporter-jaeger" % Versions.opentelemetry,
Orgs.opentelemetry % "opentelemetry-sdk" % Versions.opentelemetry,
Orgs.grpc % "grpc-netty-shaded" % ExampleVersions.grpcNetty
)

}
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.7")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.7")
addSbtPlugin("dev.zio" % "zio-sbt-website" % "0.3.10")
addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.6")

libraryDependencies += "org.snakeyaml" % "snakeyaml-engine" % "2.6"

Expand Down

0 comments on commit c494bf4

Please sign in to comment.