|
| 1 | +package no.nav.tilleggsstonader.libs.log.filter |
| 2 | + |
| 3 | +import jakarta.servlet.FilterChain |
| 4 | +import jakarta.servlet.http.Cookie |
| 5 | +import jakarta.servlet.http.HttpFilter |
| 6 | +import jakarta.servlet.http.HttpServletRequest |
| 7 | +import jakarta.servlet.http.HttpServletResponse |
| 8 | +import no.nav.tilleggsstonader.libs.log.IdUtils |
| 9 | +import no.nav.tilleggsstonader.libs.log.NavHttpHeaders |
| 10 | +import no.nav.tilleggsstonader.libs.log.mdc.MDCConstants.MDC_CALL_ID |
| 11 | +import no.nav.tilleggsstonader.libs.log.mdc.MDCConstants.MDC_CONSUMER_ID |
| 12 | +import no.nav.tilleggsstonader.libs.log.mdc.MDCConstants.MDC_REQUEST_ID |
| 13 | +import no.nav.tilleggsstonader.libs.log.mdc.MDCConstants.MDC_USER_ID |
| 14 | +import org.slf4j.LoggerFactory |
| 15 | +import org.slf4j.MDC |
| 16 | +import java.io.EOFException |
| 17 | + |
| 18 | +class LogFilter : HttpFilter() { |
| 19 | + |
| 20 | + override fun doFilter( |
| 21 | + request: HttpServletRequest, |
| 22 | + response: HttpServletResponse, |
| 23 | + filterChain: FilterChain, |
| 24 | + ) { |
| 25 | + val userId = resolveUserId(request) |
| 26 | + if (userId.isNullOrEmpty()) { |
| 27 | + // user-id tracking only works if the client is stateful and supports cookies. |
| 28 | + // if no user-id is found, generate one for any following requests but do not use it on the |
| 29 | + // current request to avoid generating large numbers of useless user-ids. |
| 30 | + generateUserIdCookie(response) |
| 31 | + } |
| 32 | + val consumerId = request.getHeader(NavHttpHeaders.NAV_CONSUMER_ID.asString()) |
| 33 | + val callId = resolveCallId(request) |
| 34 | + MDC.put(MDC_CALL_ID, callId) |
| 35 | + MDC.put(MDC_USER_ID, userId) |
| 36 | + MDC.put(MDC_CONSUMER_ID, consumerId) |
| 37 | + MDC.put(MDC_REQUEST_ID, resolveRequestId(request)) |
| 38 | + response.setHeader(NavHttpHeaders.NAV_CALL_ID.asString(), callId) |
| 39 | + |
| 40 | + try { |
| 41 | + filterWithErrorHandling(request, response, filterChain) |
| 42 | + } finally { |
| 43 | + MDC.remove(MDC_CALL_ID) |
| 44 | + MDC.remove(MDC_USER_ID) |
| 45 | + MDC.remove(MDC_CONSUMER_ID) |
| 46 | + MDC.remove(MDC_REQUEST_ID) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private fun filterWithErrorHandling( |
| 51 | + request: HttpServletRequest, |
| 52 | + response: HttpServletResponse, |
| 53 | + filterChain: FilterChain, |
| 54 | + ) { |
| 55 | + try { |
| 56 | + filterChain.doFilter(request, response) |
| 57 | + } catch (e: Exception) { |
| 58 | + if (e is EOFException) { |
| 59 | + LOG.warn(e.message, e) |
| 60 | + } else { |
| 61 | + LOG.error(e.message, e) |
| 62 | + if (response.isCommitted) { |
| 63 | + LOG.error("failed with status={}", response.status) |
| 64 | + throw e |
| 65 | + } |
| 66 | + response.status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private fun resolveCallId(request: HttpServletRequest): String { |
| 72 | + return NAV_CALL_ID_HEADER_NAMES |
| 73 | + .mapNotNull { request.getHeader(it) } |
| 74 | + .firstOrNull { it.isNotEmpty() } |
| 75 | + ?: IdUtils.generateId() |
| 76 | + } |
| 77 | + |
| 78 | + private fun resolveRequestId(request: HttpServletRequest): String { |
| 79 | + return NAV_REQUEST_ID_HEADER_NAMES |
| 80 | + .mapNotNull { request.getHeader(it) } |
| 81 | + .firstOrNull { it.isNotEmpty() } |
| 82 | + ?: IdUtils.generateId() |
| 83 | + } |
| 84 | + |
| 85 | + private fun generateUserIdCookie(response: HttpServletResponse) { |
| 86 | + val userId = IdUtils.generateId() |
| 87 | + val cookie = Cookie(RANDOM_USER_ID_COOKIE_NAME, userId).apply { |
| 88 | + path = "/" |
| 89 | + maxAge = ONE_MONTH_IN_SECONDS |
| 90 | + isHttpOnly = true |
| 91 | + secure = true |
| 92 | + } |
| 93 | + response.addCookie(cookie) |
| 94 | + } |
| 95 | + |
| 96 | + private fun resolveUserId(request: HttpServletRequest): String? { |
| 97 | + return request.cookies?.firstOrNull { it -> RANDOM_USER_ID_COOKIE_NAME == it.name }?.value |
| 98 | + } |
| 99 | + |
| 100 | + companion object { |
| 101 | + // there is no consensus in NAV about header-names for correlation ids, so we support 'em all! |
| 102 | + // https://nav-it.slack.com/archives/C9UQ16AH4/p1538488785000100 |
| 103 | + private val NAV_CALL_ID_HEADER_NAMES = |
| 104 | + listOf( |
| 105 | + NavHttpHeaders.NAV_CALL_ID.asString(), |
| 106 | + "Nav-CallId", |
| 107 | + "Nav-Callid", |
| 108 | + "X-Correlation-Id", |
| 109 | + ) |
| 110 | + |
| 111 | + private val NAV_REQUEST_ID_HEADER_NAMES = |
| 112 | + listOf( |
| 113 | + "X_Request_Id", |
| 114 | + "X-Request-Id", |
| 115 | + ) |
| 116 | + private val LOG = LoggerFactory.getLogger(LogFilter::class.java) |
| 117 | + private const val RANDOM_USER_ID_COOKIE_NAME = "RUIDC" |
| 118 | + private const val ONE_MONTH_IN_SECONDS = 60 * 60 * 24 * 30 |
| 119 | + } |
| 120 | +} |
0 commit comments