Skip to content

Commit

Permalink
Use otel4s sdk and promethues for ingestor
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Oct 27, 2024
1 parent 221a94c commit 5e600c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ lazy val ingestor = (project in file("modules/ingestor"))
mongo4catsCirce,
log4Cats,
logback,
otel4sJava,
otelJavaExporter,
otelJavaAutoConfig,
otelJavaRuntimeMetrics,
otel4sMetricts,
otel4sSdk,
otel4sPrometheusExporter,
weaver,
weaverScalaCheck,
testContainers
Expand Down
2 changes: 1 addition & 1 deletion modules/app/src/main/scala/app.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object App extends IOApp.Simple:
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]
res <- AppResources.instance(config)
_ <- SearchApp(res, config).run()
yield ()

Expand Down
36 changes: 12 additions & 24 deletions modules/ingestor/src/main/scala/app.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,33 @@ package lila.search
package ingestor

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.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 ingestor with config: $config").toResource
res <- AppResources.instance(config)
_ <- IngestorApp(res, config).run()
given Meter[IO] <- mkMeter
_ <- RuntimeMetrics.register[IO]
config <- AppConfig.load.toResource
_ <- Logger[IO].info(s"Starting lila-search ingestor with config: $config").toResource
res <- AppResources.instance(config)
_ <- IngestorApp(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-ingestor"))

class IngestorApp(res: AppResources, config: AppConfig)(using Logger[IO], LoggerFactory[IO]):
def run(): Resource[IO, Unit] =
Expand Down

0 comments on commit 5e600c7

Please sign in to comment.