Skip to content

Commit

Permalink
enable kafka metrics in micronaut
Browse files Browse the repository at this point in the history
  • Loading branch information
tuantrannav committed Aug 15, 2023
1 parent 14a2b10 commit be9bd79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class KafkaRapid(
private val consumer = KafkaConsumer(consumerConfig, stringDeserializer, stringDeserializer)
private val producer = KafkaProducer(producerConfig, stringSerializer, stringSerializer)

// metric definitions
private val consumerMetric = KafkaClientMetrics(consumer)
private val producerMetric = KafkaClientMetrics(producer)
private val rapidMetric = KafkaRapidMetrics(this)

private val topics = listOf(rapidTopic) + extraTopics

private var seekToBeginning = false
Expand Down Expand Up @@ -244,7 +249,12 @@ class KafkaRapid(
}
}

fun getMetrics() = listOf(KafkaClientMetrics(consumer), KafkaClientMetrics(producer), KafkaRapidMetrics(this))
fun getMetrics() = listOf(consumerMetric, producerMetric, rapidMetric)

fun getConsumerMetric() = consumerMetric
fun getProducerMetric() = producerMetric
fun getRapidMetric() = rapidMetric


companion object {
private const val Stopped = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

package no.nav.hm.rapids_rivers.micronaut

import io.micrometer.core.instrument.binder.kafka.KafkaClientMetrics
import io.micronaut.context.annotation.Bean
import io.micronaut.context.annotation.Factory
import io.micronaut.context.annotation.Primary
import io.micronaut.context.annotation.Requires
import io.micronaut.context.env.Environment
import jakarta.inject.Named
import jakarta.inject.Singleton
import no.nav.helse.rapids_rivers.KafkaConfig
import no.nav.helse.rapids_rivers.KafkaRapid
Expand Down Expand Up @@ -33,9 +35,16 @@ class RapidsRiversFactory {
return KafkaRapid.create(kafkaConfig, kafkaProps.topic, kafkaProps.extraTopics)
}

@Bean
@Singleton
@Primary
fun rapidMetrics(kafkaRapid: KafkaRapid): KafkaRapidMetrics = KafkaRapidMetrics(kafkaRapid)
fun rapidMetrics(kafkaRapid: KafkaRapid): KafkaRapidMetrics = kafkaRapid.getRapidMetric()

@Singleton
@Named("ConsumerMetric")
fun consumerMetric(kafkaRapid: KafkaRapid): KafkaClientMetrics = kafkaRapid.getConsumerMetric()

@Singleton
@Named("ProducerMetric")
fun producerMetric(kafkaRapid: KafkaRapid): KafkaClientMetrics = kafkaRapid.getProducerMetric()


}

0 comments on commit be9bd79

Please sign in to comment.