diff --git a/src/main/kotlin/no/nav/syfo/kafka/consumer/AktivitetskravKafkaListener.kt b/src/main/kotlin/no/nav/syfo/kafka/consumer/AktivitetskravKafkaListener.kt index d4cd439..88f5748 100644 --- a/src/main/kotlin/no/nav/syfo/kafka/consumer/AktivitetskravKafkaListener.kt +++ b/src/main/kotlin/no/nav/syfo/kafka/consumer/AktivitetskravKafkaListener.kt @@ -8,6 +8,7 @@ import com.fasterxml.jackson.module.kotlin.registerKotlinModule import no.nav.syfo.kafka.config.aktivitetskravVarselTopic import no.nav.syfo.kafka.config.aktivitetskravVurderingTopic import no.nav.syfo.logger +import no.nav.syfo.metric.Metric import no.nav.syfo.service.AktivitetskravService import org.apache.kafka.clients.consumer.ConsumerRecord import org.springframework.beans.factory.annotation.Autowired @@ -18,7 +19,8 @@ import kotlin.system.exitProcess @Component class AktivitetskravKafkaListener @Autowired constructor( - private val aktivitetskravService: AktivitetskravService + private val aktivitetskravService: AktivitetskravService, + private val metric: Metric ) { val log = logger() @@ -34,7 +36,7 @@ class AktivitetskravKafkaListener @Autowired constructor( ack: Acknowledgment ) { val topic = record.topic() - log.info("Received record from topic: $topic") + metric.countRecordReceived() try { when (topic) { aktivitetskravVurderingTopic -> @@ -48,6 +50,7 @@ class AktivitetskravKafkaListener @Autowired constructor( ack.acknowledge() } catch (e: RuntimeException) { log.error("Error during record processing from topic $topic. Shutting down application ...", e) + metric.countKafkaErrorShutdown() exitProcess(1) } } diff --git a/src/main/kotlin/no/nav/syfo/metric/Metric.kt b/src/main/kotlin/no/nav/syfo/metric/Metric.kt new file mode 100644 index 0000000..b447424 --- /dev/null +++ b/src/main/kotlin/no/nav/syfo/metric/Metric.kt @@ -0,0 +1,29 @@ +package no.nav.syfo.metric + +import io.micrometer.core.instrument.MeterRegistry +import io.micrometer.core.instrument.Tags +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Component + +@Component +class Metric @Autowired constructor( + private val registry: MeterRegistry +) { + fun countKafkaErrorShutdown() = countEvent("shutdown_due_to_kafka_error") + + fun countRecordReceived() = countEvent("kafka_record_received") + + fun countAktivitetskravVurderingProcessed() = countEvent("aktivitetskrav_vurdering_processed") + + fun countAktivitetskravVarselProcessed() = countEvent("aktivitetskrav_varsel_processed") + + fun countEvent(name: String) { + registry.counter( + metricPrefix(name), + Tags.of("type", "info") + ).increment() + } + + private fun metricPrefix(name: String) = + "aktivitetskrav_backend_$name" +} diff --git a/src/main/kotlin/no/nav/syfo/service/AktivitetskravService.kt b/src/main/kotlin/no/nav/syfo/service/AktivitetskravService.kt index 458be5b..bc99e3a 100644 --- a/src/main/kotlin/no/nav/syfo/service/AktivitetskravService.kt +++ b/src/main/kotlin/no/nav/syfo/service/AktivitetskravService.kt @@ -3,24 +3,26 @@ package no.nav.syfo.service import no.nav.syfo.kafka.consumer.domain.KAktivitetskravVarsel import no.nav.syfo.kafka.consumer.domain.KAktivitetskravVurdering import no.nav.syfo.logger +import no.nav.syfo.metric.Metric import no.nav.syfo.persistence.AktivitetskravDAO import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service @Service class AktivitetskravService @Autowired constructor( - private val aktivitetskravDAO: AktivitetskravDAO + private val aktivitetskravDAO: AktivitetskravDAO, + private val metric: Metric ) { private val log = logger() fun processAktivitetskravVurdering(vurdering: KAktivitetskravVurdering) { - log.info("Processing aktivitetskrav vurdering: ${vurdering.uuid}") aktivitetskravDAO.storeAktivitetkravVurdering(vurdering) + metric.countAktivitetskravVurderingProcessed() } fun processAktivitetskravVarsel(varsel: KAktivitetskravVarsel) { - log.info("Processing aktivitetskrav varsel: ${varsel.aktivitetskravUuid}") aktivitetskravDAO.storeAktivitetkravVarsel(varsel) + metric.countAktivitetskravVarselProcessed() } }