Skip to content

Commit

Permalink
Move to otel4s sdk and use prometheus exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Oct 27, 2024
1 parent 6e6482c commit b579695
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ inThisBuild(
organization := "org.lichess.search",
run / fork := true,
run / javaOptions += "-Dconfig.override_with_env_vars=true",
semanticdbEnabled := true, // for scalafix
semanticdbEnabled := true, // for scalafix
resolvers ++= ourResolvers,
Compile / doc / sources := Seq.empty,
publishTo := Option(Resolver.file("file", new File(sys.props.getOrElse("publishTo", ""))))
)
Expand Down Expand Up @@ -72,7 +73,6 @@ lazy val ingestor = (project in file("modules/ingestor"))
commonSettings,
publish := {},
publish / skip := true,
resolvers += lilaMaven,
libraryDependencies ++= Seq(
chess,
catsCore,
Expand Down Expand Up @@ -138,9 +138,10 @@ lazy val app = (project in file("modules/app"))
log4Cats,
logback,
otel4sJava,
otelJavaExporter,
otelJavaAutoConfig,
otelJavaRuntimeMetrics,
otel4sMetricts,
otel4sSdk,
otel4sPrometheusExporter,
weaver,
testContainers
),
Expand Down
37 changes: 13 additions & 24 deletions modules/app/src/main/scala/app.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,34 @@ package lila.search
package app

import cats.effect.*
import cats.mtl.Local
import cats.syntax.all.*
import io.opentelemetry.api.{ GlobalOpenTelemetry, OpenTelemetry as JOpenTelemetry }
import io.opentelemetry.instrumentation.runtimemetrics.java17.*
import org.typelevel.log4cats.slf4j.{ Slf4jFactory, Slf4jLogger }
import org.typelevel.log4cats.{ Logger, LoggerFactory }
import org.typelevel.otel4s.instances.local.*
import org.typelevel.otel4s.experimental.metrics.*
import org.typelevel.otel4s.metrics.Meter
import org.typelevel.otel4s.oteljava.OtelJava
import org.typelevel.otel4s.oteljava.context.Context
import org.typelevel.otel4s.sdk.*
import org.typelevel.otel4s.sdk.metrics.SdkMetrics
import org.typelevel.otel4s.sdk.exporter.prometheus.autoconfigure.PrometheusMetricExporterAutoConfigure

object App extends IOApp.Simple:

given Logger[IO] = Slf4jLogger.getLogger[IO]
given LoggerFactory[IO] = Slf4jFactory.create[IO]

given (using Local[IO, Context]): IO[OtelJava[IO]] =
IO(GlobalOpenTelemetry.get).map(OtelJava.local[IO])

given [F[_]: Async](using otel4s: OtelJava[F]): F[Meter[F]] =
otel4s.meterProvider.get("lila-search")

override def run: IO[Unit] = app.useForever

def app: Resource[IO, Unit] =
for
given IOLocal[Context] <- IOLocal(Context.root).toResource
given OtelJava[IO] <- summon[IO[OtelJava[IO]]].toResource
given Meter[IO] <- summon[IO[Meter[IO]]].toResource
_ <- registerRuntimeMetrics[IO](summon[OtelJava[IO]].underlying)
config <- AppConfig.load.toResource
_ <- Logger[IO].info(s"Starting lila-search with config: $config").toResource
res <- AppResources.instance(config)
_ <- SearchApp(res, config).run()
config <- AppConfig.load.toResource
_ <- Logger[IO].info(s"Starting lila-search with config: $config").toResource
given Meter[IO] <- mkMeter
res <- AppResources.instance(config)
_ <- RuntimeMetrics.register[IO]
_ <- SearchApp(res, config).run()
yield ()

private def registerRuntimeMetrics[F[_]: Sync](openTelemetry: JOpenTelemetry): Resource[F, Unit] =
val acquire = Sync[F].delay(RuntimeMetrics.create(openTelemetry))
Resource.make(acquire)(r => Sync[F].delay(r.close())).void
def mkMeter = SdkMetrics
.autoConfigured[IO](_.addExporterConfigurer(PrometheusMetricExporterAutoConfigure[IO]))
.evalMap(_.meterProvider.get("lila-search"))

class SearchApp(res: AppResources, config: AppConfig)(using Logger[IO], LoggerFactory[IO], Meter[IO]):
def run(): Resource[IO, Unit] =
Expand Down
9 changes: 7 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.codegen.BuildInfo.version as smithy4sVersion
object Dependencies {

val lilaMaven = "lila-maven" at "https://raw.githubusercontent.com/lichess-org/lila-maven/master"
val ourResolvers = Resolver.sonatypeOssRepos("snapshots") :+ lilaMaven

object V {
val catsEffect = "3.5.4"
Expand All @@ -15,6 +16,7 @@ object Dependencies {
val http4s = "0.23.29"
val iron = "2.5.0"
val mongo4cats = "0.7.11"
val otel4s = "0.11-7d84643-SNAPSHOT"
}

def http4s(artifact: String) = "org.http4s" %% s"http4s-$artifact" % V.http4s
Expand Down Expand Up @@ -54,11 +56,14 @@ object Dependencies {
val mongo4catsCirce = "io.github.kirill5k" %% "mongo4cats-circe" % V.mongo4cats
val circe = "io.circe" %% "circe-core" % "0.14.10"

val otel4sCore = "org.typelevel" %% "otel4s-core" % "0.10.0"
val otel4sJava = "org.typelevel" %% "otel4s-oteljava" % "0.10.0"
val otel4sCore = "org.typelevel" %% "otel4s-core" % V.otel4s
val otel4sJava = "org.typelevel" %% "otel4s-oteljava" % V.otel4s
val otel4sPrometheusExporter = "org.typelevel" %% "otel4s-sdk-exporter-prometheus" % V.otel4s
val otel4sSdk = "org.typelevel" %% "otel4s-sdk" % V.otel4s
val otelJavaExporter = "io.opentelemetry" % "opentelemetry-exporter-otlp" % "1.43.0" % Runtime
val otelJavaAutoConfig = "io.opentelemetry" % "opentelemetry-sdk-extension-autoconfigure" % "1.43.0" % Runtime
val otelJavaRuntimeMetrics = "io.opentelemetry.instrumentation" % "opentelemetry-runtime-telemetry-java17" % "2.5.0-alpha"
val otel4sMetricts = "org.typelevel" %% "otel4s-experimental-metrics" % "0.4.0-4-138383f-SNAPSHOT"

val log4Cats = "org.typelevel" %% "log4cats-slf4j" % "2.7.0"
val logback = "ch.qos.logback" % "logback-classic" % "1.5.12"
Expand Down

0 comments on commit b579695

Please sign in to comment.