Skip to content

Commit

Permalink
Legger til at man må konfigurere at man skal spore bruker, som defaul…
Browse files Browse the repository at this point in the history
…t er disabled
  • Loading branch information
blommish committed Jan 21, 2025
1 parent d370f00 commit 62b2ec1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
31 changes: 22 additions & 9 deletions log/main/no/nav/tilleggsstonader/libs/log/filter/LogFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@ import org.slf4j.LoggerFactory
import org.slf4j.MDC
import java.io.EOFException

class LogFilter : HttpFilter() {
/**
* @param sporBruker settes hvis vi skal sette cookie som sporer brukeren for å hjelpe ved feilsøing
*/
class LogFilter(val sporBruker: Boolean) : HttpFilter() {

override fun doFilter(
request: HttpServletRequest,
response: HttpServletResponse,
filterChain: FilterChain,
) {
val userId = resolveUserId(request)
if (userId.isNullOrEmpty()) {
// user-id tracking only works if the client is stateful and supports cookies.
// if no user-id is found, generate one for any following requests but do not use it on the
// current request to avoid generating large numbers of useless user-ids.
generateUserIdCookie(response)
}
sporBruker(request, response)
val consumerId = request.getHeader(NavHttpHeaders.NAV_CONSUMER_ID.asString())
val callId = resolveCallId(request)
MDC.put(MDC_CALL_ID, callId)
MDC.put(MDC_USER_ID, userId)
MDC.put(MDC_CONSUMER_ID, consumerId)
MDC.put(MDC_REQUEST_ID, resolveRequestId(request))
response.setHeader(NavHttpHeaders.NAV_CALL_ID.asString(), callId)
Expand All @@ -47,6 +43,23 @@ class LogFilter : HttpFilter() {
}
}

private fun sporBruker(
request: HttpServletRequest,
response: HttpServletResponse,
) {
if (!sporBruker) {
return
}
val userId = resolveUserId(request)
if (userId.isNullOrEmpty()) {
// user-id tracking only works if the client is stateful and supports cookies.
// if no user-id is found, generate one for any following requests but do not use it on the
// current request to avoid generating large numbers of useless user-ids.
generateUserIdCookie(response)
}
MDC.put(MDC_USER_ID, userId)
}

private fun filterWithErrorHandling(
request: HttpServletRequest,
response: HttpServletResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package no.nav.tilleggsstonader.libs.log.filter

import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class LogFilterConfiguration {
class LogFilterConfiguration(
@Value("\${log.sporBruker:false}")
private val sporBruker: Boolean,
) {

private val logger = LoggerFactory.getLogger(javaClass)

@Bean
fun logFilter(): FilterRegistrationBean<LogFilter> {
logger.info("Registering LogFilter filter")
val filterRegistration = FilterRegistrationBean<LogFilter>()
filterRegistration.filter = LogFilter()
filterRegistration.filter = LogFilter(sporBruker = sporBruker)
filterRegistration.order = 1
return filterRegistration
}
Expand Down

0 comments on commit 62b2ec1

Please sign in to comment.