-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-implement java kafka metrics collection using registry, kudos to Z…
…1kkurat
- Loading branch information
Denys Fakhritdinov
committed
Jul 12, 2024
1 parent
1da4c59
commit eac1fdd
Showing
7 changed files
with
118 additions
and
52 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
80 changes: 80 additions & 0 deletions
80
modules/metrics/src/main/scala/com/evolutiongaming/skafka/metrics/KafkaMetricsRegistry.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,80 @@ | ||
package com.evolutiongaming.skafka.metrics | ||
|
||
import cats.syntax.all._ | ||
import cats.effect.syntax.resource._ | ||
import cats.effect.{Resource, Ref, Sync} | ||
import cats.effect.std.UUIDGen | ||
import com.evolutiongaming.catshelper.ToTry | ||
import com.evolutiongaming.skafka.ClientMetric | ||
import io.prometheus.client.CollectorRegistry | ||
|
||
import java.util.UUID | ||
|
||
/** | ||
* Allows reporting metrics of multiple Kafka clients inside a single VM. | ||
* | ||
* Example: | ||
* {{{ | ||
* val prometheus: CollectorRegistry = ??? | ||
* val consumerOf: ConsumerOf[F] = ??? | ||
* val producerOf: ProducerOf[F] = ??? | ||
* | ||
* for { | ||
* registry <- KafkaMetricsRegistry.of(prometheus) | ||
* | ||
* consumer <- consumerOf(consumerConfig) | ||
* _ <- registry.register(consumer.clientMetrics) | ||
* | ||
* producer <- producerOf(producerConfig) | ||
* _ <- registry.register(producer.clientMetrics) | ||
* } yield () | ||
* }}} | ||
*/ | ||
trait KafkaMetricsRegistry[F[_]] { | ||
|
||
/** | ||
* Register a function to obtain a list of client metrics. | ||
* Normally, you would pass | ||
* [[com.evolutiongaming.skafka.consumer.Consumer#clientMetrics]] or | ||
* [[com.evolutiongaming.skafka.producer.Producer#clientMetrics]] | ||
*/ | ||
def register(metrics: F[Seq[ClientMetric[F]]]): Resource[F, Unit] | ||
} | ||
|
||
object KafkaMetricsRegistry { | ||
|
||
def of[F[_]: Sync: ToTry: UUIDGen]( | ||
prometheus: CollectorRegistry, | ||
prefix: Option[String] = None, | ||
): Resource[F, KafkaMetricsRegistry[F]] = | ||
for { | ||
sources <- Ref[F].of(Map.empty[UUID, F[Seq[ClientMetric[F]]]]).toResource | ||
|
||
metrics = sources.get.flatMap { sources => | ||
sources.toSeq.flatTraverse { | ||
case (uuid, metrics) => | ||
metrics.map { metrics => | ||
metrics.map { metric => metric.copy(tags = metric.tags + ("uuid" -> uuid.toString)) } | ||
} | ||
} | ||
} | ||
|
||
collector = new KafkaMetricsCollector[F](metrics, prefix) | ||
allocate = Sync[F].delay { prometheus.register(collector) } | ||
release = Sync[F].delay { prometheus.unregister(collector) } | ||
|
||
_ <- Resource.make(allocate)(_ => release) | ||
} yield new KafkaMetricsRegistry[F] { | ||
|
||
def register(metrics: F[Seq[ClientMetric[F]]]): Resource[F, Unit] = | ||
for { | ||
uuid <- UUIDGen[F].randomUUID.toResource | ||
|
||
allocate = sources.update { sources => sources.updated(uuid, metrics) } | ||
release = sources.update { sources => sources - uuid } | ||
|
||
_ <- Resource.make(allocate)(_ => release) | ||
} yield {} | ||
} | ||
|
||
} |
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
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