Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace log-statements with prometheus counters #6

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
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()
}
}