From f190a5599407d86418bc7e578cd5ea5a96370fa1 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Sun, 27 Oct 2024 21:05:45 +0100 Subject: [PATCH] Move to otel4s sdk and use prometheus exporter --- build.sbt | 9 ++++--- modules/app/src/main/scala/app.scala | 37 ++++++++++------------------ project/Dependencies.scala | 9 +++++-- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/build.sbt b/build.sbt index be4823a..b5b804e 100644 --- a/build.sbt +++ b/build.sbt @@ -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", "")))) ) @@ -72,7 +73,6 @@ lazy val ingestor = (project in file("modules/ingestor")) commonSettings, publish := {}, publish / skip := true, - resolvers += lilaMaven, libraryDependencies ++= Seq( chess, catsCore, @@ -138,9 +138,10 @@ lazy val app = (project in file("modules/app")) log4Cats, logback, otel4sJava, - otelJavaExporter, otelJavaAutoConfig, - otelJavaRuntimeMetrics, + otel4sMetricts, + otel4sSdk, + otel4sPrometheusExporter, weaver, testContainers ), diff --git a/modules/app/src/main/scala/app.scala b/modules/app/src/main/scala/app.scala index b27aeae..8c3f8bf 100644 --- a/modules/app/src/main/scala/app.scala +++ b/modules/app/src/main/scala/app.scala @@ -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.exporter.prometheus.autoconfigure.PrometheusMetricExporterAutoConfigure +import org.typelevel.otel4s.sdk.metrics.SdkMetrics 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] = diff --git a/project/Dependencies.scala b/project/Dependencies.scala index da7ec72..f194298 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -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" @@ -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 @@ -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"