@@ -3,14 +3,34 @@ package com.evolutiongaming.skafka.consumer
3
3
import cats .effect .{Resource , Sync }
4
4
import cats .effect .std .UUIDGen
5
5
import com .evolutiongaming .catshelper .ToTry
6
- import com .evolutiongaming .skafka .{Topic , TopicPartition }
6
+ import com .evolutiongaming .skafka .{ClientId , Topic , TopicPartition }
7
7
import com .evolutiongaming .skafka .metrics .KafkaMetricsRegistry
8
8
import io .prometheus .client .CollectorRegistry
9
9
10
10
import scala .concurrent .duration .FiniteDuration
11
11
12
12
object ConsumerMetricsOf {
13
13
14
+ /**
15
+ * Construct [[ConsumerMetrics ]] that will expose Java Kafka client metrics.
16
+ *
17
+ * @param sourceOf original [[ConsumerMetrics ]] factory
18
+ * @param prometheus instance of Prometheus registry
19
+ * @param prefix metric name prefix
20
+ * @return [[ConsumerMetrics ]] that exposes Java Kafka client metrics
21
+ */
22
+ def withJavaClientMetrics [F [_]: Sync : ToTry : UUIDGen ](
23
+ sourceOf : ClientId => ConsumerMetrics [F ],
24
+ prometheus : CollectorRegistry ,
25
+ prefix : Option [String ],
26
+ ): Resource [F , ClientId => ConsumerMetrics [F ]] =
27
+ for {
28
+ registry <- KafkaMetricsRegistry .of(prometheus, prefix)
29
+ } yield { (clientId : ClientId ) =>
30
+ val source = sourceOf(clientId)
31
+ consumerMetricsOf(source, registry)
32
+ }
33
+
14
34
/**
15
35
* Construct [[ConsumerMetrics ]] that will expose Java Kafka client metrics.
16
36
*
@@ -26,7 +46,13 @@ object ConsumerMetricsOf {
26
46
): Resource [F , ConsumerMetrics [F ]] =
27
47
for {
28
48
registry <- KafkaMetricsRegistry .of(prometheus, prefix)
29
- } yield new ConsumerMetrics [F ] {
49
+ } yield consumerMetricsOf(source, registry)
50
+
51
+ private def consumerMetricsOf [F [_]](
52
+ source : ConsumerMetrics [F ],
53
+ registry : KafkaMetricsRegistry [F ],
54
+ ): ConsumerMetrics [F ] =
55
+ new ConsumerMetrics [F ] {
30
56
override def call (name : String , topic : Topic , latency : FiniteDuration , success : Boolean ): F [Unit ] =
31
57
source.call(name, topic, latency, success)
32
58
0 commit comments