Skip to content

Commit

Permalink
Minske error-logging når noe faktisk ikke er error (#6017)
Browse files Browse the repository at this point in the history
* Kaster forespoerselexception med 503-kode

Hvis vi ikke er klar enda kaster vi i stedet en forespoerselexception
som gir en 503 feilkode. Da ser vi det ikke i loggene, siden det ikke
er en ERROR at appen ikke har satt ready enda

* Fjerner shutdownlogging for sikkerlogg

Mistenker at dette er årsaken for "Uncaught error in thread main: IllegalStateException".
Vi trenger den uansett ikke -- vi kan filtrere på oppstart + pod for å se at den logger
til sikkerlogg riktig

* Fjerner potensiale for error-alerts hvis vi ikke får logget v/ shutdown
  • Loading branch information
oyvindsh authored Oct 18, 2024
1 parent 74ac5b8 commit 803ff86
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package no.nav.etterlatte

import io.ktor.server.application.install
import no.nav.etterlatte.libs.common.Miljoevariabler
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
import no.nav.etterlatte.samordning.sak.behandlingSakRoutes
Expand All @@ -19,7 +19,7 @@ class Server(
applicationContext: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-api")
sikkerLoggOppstart("etterlatte-api")
}

private val engine =
Expand Down
4 changes: 2 additions & 2 deletions apps/etterlatte-behandling/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import no.nav.etterlatte.institusjonsopphold.institusjonsoppholdRoute
import no.nav.etterlatte.kodeverk.kodeverk
import no.nav.etterlatte.krr.krrRoute
import no.nav.etterlatte.libs.common.TimerJob
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.common.logging.sikkerlogger
import no.nav.etterlatte.libs.database.migrate
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
Expand Down Expand Up @@ -67,7 +67,7 @@ private class Server(
private val context: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-behandling")
sikkerLoggOppstart("etterlatte-behandling")
}

private val engine =
Expand Down
4 changes: 2 additions & 2 deletions apps/etterlatte-beregning/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import no.nav.etterlatte.beregning.beregning
import no.nav.etterlatte.beregning.grunnlag.beregningsGrunnlag
import no.nav.etterlatte.config.ApplicationContext
import no.nav.etterlatte.grunnbeloep.grunnbeloep
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.database.migrate
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -20,7 +20,7 @@ class Server(
private val context: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-beregning")
sikkerLoggOppstart("etterlatte-beregning")
}

private val engine =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import no.nav.etterlatte.brev.notat.notatRoute
import no.nav.etterlatte.brev.oversendelsebrev.oversendelseBrevRoute
import no.nav.etterlatte.brev.varselbrev.varselbrevRoute
import no.nav.etterlatte.brev.vedtaksbrev.vedtaksbrevRoute
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.database.migrate
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -19,7 +19,7 @@ private class Server(
val context: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-brev-api") // TODO: naisappame?
sikkerLoggOppstart("etterlatte-brev-api") // TODO: naisappame?
}

val engine =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import no.nav.etterlatte.kafka.KafkaEnvironment
import no.nav.etterlatte.kafka.startLytting
import no.nav.etterlatte.libs.common.EnvEnum
import no.nav.etterlatte.libs.common.Miljoevariabler
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.ktor.httpClientClientCredentials
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServerUtenRest
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -25,7 +25,7 @@ class Server {
private val engine = initEmbeddedServerUtenRest(httpPort = 8080, applicationConfig = defaultConfig)

init {
sikkerLoggOppstartOgAvslutning("etterlatte-egne-ansatte-lytter")
sikkerLoggOppstart("etterlatte-egne-ansatte-lytter")
}

fun run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.ktor.server.application.Application
import no.nav.etterlatte.kafka.startLytting
import no.nav.etterlatte.klage.ApplicationContext
import no.nav.etterlatte.klage.kabalOvesendelseRoute
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
import org.slf4j.LoggerFactory
Expand All @@ -17,7 +17,7 @@ class Server(
private val context: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-klage")
sikkerLoggOppstart("etterlatte-klage")
}

private val engine =
Expand Down
4 changes: 2 additions & 2 deletions apps/etterlatte-pdltjenester/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package no.nav.etterlatte
import io.ktor.server.routing.route
import no.nav.etterlatte.config.ApplicationContext
import no.nav.etterlatte.libs.common.Miljoevariabler
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.common.logging.sikkerlogger
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -23,7 +23,7 @@ class Server(
applicationContext: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-pdltjenester")
sikkerLoggOppstart("etterlatte-pdltjenester")
}

private val engine =
Expand Down
4 changes: 2 additions & 2 deletions apps/etterlatte-statistikk/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package no.nav.etterlatte

import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.statistikk.config.ApplicationContext

fun main() =
with(ApplicationContext()) {
maanedligStatistikkJob.schedule().also { addShutdownHook(it) }
initRapidsConnection()
sikkerLoggOppstartOgAvslutning("etterlatte-statistikk")
sikkerLoggOppstart("etterlatte-statistikk")
}
4 changes: 2 additions & 2 deletions apps/etterlatte-tilbakekreving/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.typesafe.config.ConfigFactory
import io.ktor.server.application.log
import io.ktor.server.routing.application
import no.nav.etterlatte.libs.common.appIsInGCP
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.database.migrate
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -22,7 +22,7 @@ class Server(
val devMode = context.properties.devMode

init {
sikkerLoggOppstartOgAvslutning("etterlatte-tilbakekreving")
sikkerLoggOppstart("etterlatte-tilbakekreving")
}

private val engine =
Expand Down
4 changes: 2 additions & 2 deletions apps/etterlatte-trygdetid/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package no.nav.etterlatte

import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.database.migrate
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -16,7 +16,7 @@ class Server(
private val context: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-trygdetid")
sikkerLoggOppstart("etterlatte-trygdetid")
}

private val engine =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package no.nav.etterlatte

import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.database.migrate
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -19,7 +19,7 @@ class Server(
private val context: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-vedtakdsvurdering")
sikkerLoggOppstart("etterlatte-vedtakdsvurdering")
}

private val engine =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import no.nav.etterlatte.libs.common.EnvEnum
import no.nav.etterlatte.libs.common.Miljoevariabler
import no.nav.etterlatte.libs.common.OpeningHours
import no.nav.etterlatte.libs.common.appIsInGCP
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.database.DataSourceBuilder
import no.nav.etterlatte.libs.jobs.LeaderElection
import no.nav.etterlatte.libs.ktor.AppConfig.ELECTOR_PATH
Expand Down Expand Up @@ -47,7 +47,7 @@ import java.util.UUID

class ApplicationContext {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-vedtaksvurdering")
sikkerLoggOppstart("etterlatte-vedtaksvurdering")
}

val env = Miljoevariabler.systemEnv()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package no.nav.etterlatte

import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.database.migrate
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand All @@ -15,7 +15,7 @@ class Server(
private val context: ApplicationContext,
) {
init {
sikkerLoggOppstartOgAvslutning("etterlatte-vilkaarsvurdering")
sikkerLoggOppstart("etterlatte-vilkaarsvurdering")
}

private val engine =
Expand Down
1 change: 1 addition & 0 deletions libs/etterlatte-ktor/src/main/kotlin/HealthRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import io.ktor.server.routing.route
import no.nav.etterlatte.libs.ktor.initialisering.isReady

fun Route.healthApi() {
route("health") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import no.nav.etterlatte.libs.ktor.healthApi
import no.nav.etterlatte.libs.ktor.ktor.shutdownPolicyEmbeddedServer
import no.nav.etterlatte.libs.ktor.metricsRoute
import no.nav.etterlatte.libs.ktor.restModule
import no.nav.etterlatte.libs.ktor.setReady

fun initEmbeddedServer(
httpPort: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package no.nav.etterlatte.libs.ktor
package no.nav.etterlatte.libs.ktor.initialisering

import io.ktor.http.HttpStatusCode
import no.nav.etterlatte.libs.common.feilhaandtering.ForespoerselException
import java.util.concurrent.atomic.AtomicBoolean

private var ready = AtomicBoolean(false)

fun setReady(value: Boolean = true) = ready.set(value)

fun isReady() = if (ready.get()) HttpStatusCode.OK else HttpStatusCode.ServiceUnavailable
fun isReady() =
if (ready.get()) {
HttpStatusCode.OK
} else {
throw ForespoerselException(
status = HttpStatusCode.ServiceUnavailable.value,
"SERVICE_UNAVAILABLE",
"Service not yet ready",
)
}
1 change: 1 addition & 0 deletions libs/etterlatte-ktor/src/test/kotlin/RestModuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import no.nav.etterlatte.libs.common.feilhaandtering.IkkeFunnetException
import no.nav.etterlatte.libs.common.feilhaandtering.InternfeilException
import no.nav.etterlatte.libs.common.objectMapper
import no.nav.etterlatte.libs.common.toJson
import no.nav.etterlatte.libs.ktor.initialisering.setReady
import no.nav.etterlatte.libs.ktor.route.BEHANDLINGID_CALL_PARAMETER
import no.nav.etterlatte.libs.ktor.route.BehandlingTilgangsSjekk
import no.nav.etterlatte.libs.ktor.route.FoedselsnummerDTO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rapidsandrivers

import io.ktor.server.application.Application
import no.nav.etterlatte.libs.common.Miljoevariabler
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstartOgAvslutning
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.rapidsandrivers.getRapidEnv
import no.nav.helse.rapids_rivers.AivenConfig
import no.nav.helse.rapids_rivers.Config
Expand All @@ -15,7 +15,7 @@ fun initRogR(
configFromEnvironment: (Miljoevariabler) -> Config = { AivenConfig.default },
settOppRivers: (RapidsConnection, rapidEnv: Miljoevariabler) -> Unit,
) {
sikkerLoggOppstartOgAvslutning("etterlatte-$applikasjonsnavn")
sikkerLoggOppstart("etterlatte-$applikasjonsnavn")

val rapidEnv = getRapidEnv()

Expand Down
6 changes: 5 additions & 1 deletion libs/saksbehandling-common/src/main/kotlin/ShutdownHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fun addShutdownHook(vararg timer: Timer) {
},
)
} catch (e: IllegalStateException) {
logger.warn("App er på vei ned allerede, kan ikke legge til shutdownhooks", e)
try {
logger.warn("App er på vei ned allerede, kan ikke legge til shutdownhooks", e)
} catch (e: Exception) {
// Ignorer at vi ikke får logget på grunn av shutdown, da er det ikke noe å gjøre uansett
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,9 @@ private fun generateCorrelationId() = UUID.randomUUID().toString()

fun getCorrelationId(): String = MDC.get(CORRELATION_ID) ?: generateCorrelationId()

fun sikkerLoggOppstartOgAvslutning(applikasjonsnavn: String) {
fun sikkerLoggOppstart(applikasjonsnavn: String) {
val sikkerLogg = sikkerlogger()
sikkerLogg.info("SikkerLogg: $applikasjonsnavn oppstart")
Runtime.getRuntime().addShutdownHook(
Thread {
sikkerLogg.debug("SikkerLogg: $applikasjonsnavn avslutter")
},
)
}

fun sikkerlogger(): Logger = LoggerFactory.getLogger("sikkerLogg")

0 comments on commit 803ff86

Please sign in to comment.