Skip to content

Commit

Permalink
Legger til metric for antall feilede tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
blommish committed Apr 25, 2024
1 parent 0eda75e commit 59747f0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<Task, String> {

@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<AntallTaskAvTypeOgStatus>
}

data class AntallTaskAvTypeOgStatus(val type: String, val status: Status, val count: Long)
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class RestTaskService(private val taskService: TaskService) {
)
}

fun finnAntallTaskerMedStatusFeiletOgManuellOppfølging():
Ressurs<TaskerMedStatusFeiletOgManuellOppfølging> {
fun finnAntallTaskerMedStatusFeiletOgManuellOppfølging(): Ressurs<TaskerMedStatusFeiletOgManuellOppfølging> {
val errorMelding = "Henting av antall tasker som har feilet eller er som satt til manuell oppføling feilet."
return Result.runCatching {
taskService.antallTaskerMedStatusFeiletOgManuellOppfølging()
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 59747f0

Please sign in to comment.