Skip to content

Commit

Permalink
Replace log-statements with prometheus counters
Browse files Browse the repository at this point in the history
  • Loading branch information
torchr89 committed Oct 25, 2023
1 parent f0dce86 commit 48dfb0c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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 ->
Expand All @@ -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)
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/no/nav/syfo/metric/Metric.kt
Original file line number Diff line number Diff line change
@@ -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(
name,
Tags.of("type", "info")
).increment()
}

private fun metricPrefix(name: String) =
"aktivitetskrav-backend_$name"
}
8 changes: 5 additions & 3 deletions src/main/kotlin/no/nav/syfo/service/AktivitetskravService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 48dfb0c

Please sign in to comment.