From 59747f05802ccd8baebfcbb2e8f540c5e3ef2381 Mon Sep 17 00:00:00 2001 From: Johan Blomgren Date: Thu, 25 Apr 2024 15:49:35 +0200 Subject: [PATCH] Legger til metric for antall feilede tasks --- .../prosessering/metrics/MetricService.kt | 34 +++++++++++++++++++ .../metrics/TaskMetricRepository.kt | 19 +++++++++++ .../prosessering/rest/RestTaskService.kt | 3 +- .../metrics/TaskMetricRepositoryTest.kt | 34 +++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/MetricService.kt create mode 100644 prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepository.kt create mode 100644 prosessering-core/src/test/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepositoryTest.kt diff --git a/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/MetricService.kt b/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/MetricService.kt new file mode 100644 index 0000000..c2531d9 --- /dev/null +++ b/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/MetricService.kt @@ -0,0 +1,34 @@ +package no.nav.tilleggsstonader.soknad.metrics + +import io.micrometer.core.instrument.Metrics +import io.micrometer.core.instrument.MultiGauge +import io.micrometer.core.instrument.Tag +import io.micrometer.core.instrument.Tags +import no.nav.familie.prosessering.config.ProsesseringInfoProvider +import no.nav.familie.prosessering.metrics.TaskMetricRepository +import org.springframework.scheduling.annotation.Scheduled +import org.springframework.stereotype.Service + +@Service +internal class MetricService( + private val taskMetricRepository: TaskMetricRepository, + private val prosesseringInfoProvider: ProsesseringInfoProvider, +) { + + private val feiledeTasks = MultiGauge.builder("prosessering_tasks_feilet").register(Metrics.globalRegistry) + + @Scheduled(initialDelay = FREKVENS_30_SEC, fixedDelay = FREKVENS_30_MIN) + fun oppdatertFeiledeTasks() { + if (prosesseringInfoProvider.isLeader() != false) { + val rows = taskMetricRepository.finnAntallFeiledeTasksPerTypeOgStatus().map { + MultiGauge.Row.of(Tags.of(Tag.of("type", it.type), Tag.of("status", it.status.name)), it.count) + } + feiledeTasks.register(rows, true) + } + } + + companion object { + const val FREKVENS_30_SEC = 30 * 1000L + const val FREKVENS_30_MIN = 30 * 60 * 1000L + } +} diff --git a/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepository.kt b/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepository.kt new file mode 100644 index 0000000..50d1aa4 --- /dev/null +++ b/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepository.kt @@ -0,0 +1,19 @@ +package no.nav.familie.prosessering.metrics + +import no.nav.familie.prosessering.domene.Status +import no.nav.familie.prosessering.domene.Task +import org.springframework.data.jdbc.repository.query.Query +import org.springframework.stereotype.Repository + +@Repository +interface TaskMetricRepository : org.springframework.data.repository.Repository { + + @Query( + """SELECT t.type, t.status, count(t.id) as count FROM task t + WHERE t.status IN ('FEILET', 'MANUELL_OPPFØLGING') + GROUP by t.type, t.status""", + ) + fun finnAntallFeiledeTasksPerTypeOgStatus(): List +} + +data class AntallTaskAvTypeOgStatus(val type: String, val status: Status, val count: Long) diff --git a/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/rest/RestTaskService.kt b/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/rest/RestTaskService.kt index 4500e15..80f89db 100644 --- a/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/rest/RestTaskService.kt +++ b/prosessering-core/src/main/kotlin/no/nav/familie/prosessering/rest/RestTaskService.kt @@ -29,8 +29,7 @@ class RestTaskService(private val taskService: TaskService) { ) } - fun finnAntallTaskerMedStatusFeiletOgManuellOppfølging(): - Ressurs { + fun finnAntallTaskerMedStatusFeiletOgManuellOppfølging(): Ressurs { val errorMelding = "Henting av antall tasker som har feilet eller er som satt til manuell oppføling feilet." return Result.runCatching { taskService.antallTaskerMedStatusFeiletOgManuellOppfølging() diff --git a/prosessering-core/src/test/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepositoryTest.kt b/prosessering-core/src/test/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepositoryTest.kt new file mode 100644 index 0000000..fabfa8e --- /dev/null +++ b/prosessering-core/src/test/kotlin/no/nav/familie/prosessering/metrics/TaskMetricRepositoryTest.kt @@ -0,0 +1,34 @@ +package no.nav.familie.prosessering.metrics + +import no.nav.familie.prosessering.IntegrationRunnerTest +import no.nav.familie.prosessering.domene.Status +import no.nav.familie.prosessering.domene.Task +import no.nav.familie.prosessering.internal.TaskService +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired + +class TaskMetricRepositoryTest : IntegrationRunnerTest() { + + @Autowired + lateinit var taskService: TaskService + + @Autowired + lateinit var taskMetricRepository: TaskMetricRepository + + @Test + fun `skal beregne riktig`() { + taskService.save(Task("type", "payload").copy(status = Status.FERDIG)) + taskService.save(Task("type", "payload2").copy(status = Status.FEILET)) + taskService.save(Task("type2", "payload2").copy(status = Status.FEILET)) + taskService.save(Task("type", "payload3").copy(status = Status.MANUELL_OPPFØLGING)) + taskService.save(Task("type", "payload4").copy(status = Status.MANUELL_OPPFØLGING)) + + val expected = listOf( + AntallTaskAvTypeOgStatus("type", Status.FEILET, 1), + AntallTaskAvTypeOgStatus("type2", Status.FEILET, 1), + AntallTaskAvTypeOgStatus("type", Status.MANUELL_OPPFØLGING, 2), + ) + assertThat(taskMetricRepository.finnAntallFeiledeTasksPerTypeOgStatus()).containsExactlyInAnyOrderElementsOf(expected) + } +}