|
| 1 | +package no.nav.familie.tilbake.log |
| 2 | + |
| 3 | +import jakarta.servlet.FilterChain |
| 4 | +import jakarta.servlet.FilterConfig |
| 5 | +import jakarta.servlet.ServletException |
| 6 | +import jakarta.servlet.http.HttpFilter |
| 7 | +import jakarta.servlet.http.HttpServletRequest |
| 8 | +import jakarta.servlet.http.HttpServletResponse |
| 9 | +import org.slf4j.LoggerFactory |
| 10 | +import java.io.EOFException |
| 11 | +import java.io.IOException |
| 12 | +import java.util.UUID |
| 13 | + |
| 14 | +class LogTracingHttpFilter : HttpFilter() { |
| 15 | + private val log = LoggerFactory.getLogger(this::class.java) |
| 16 | + |
| 17 | + @Throws(ServletException::class, IOException::class) |
| 18 | + override fun doFilter( |
| 19 | + httpServletRequest: HttpServletRequest, |
| 20 | + httpServletResponse: HttpServletResponse, |
| 21 | + filterChain: FilterChain, |
| 22 | + ) { |
| 23 | + val consumerId = httpServletRequest.getHeader(HEADER_CONSUMER_ID) |
| 24 | + val callId = resolveCallId(httpServletRequest) |
| 25 | + |
| 26 | + putCallId(callId) |
| 27 | + if (!consumerId.isNullOrEmpty()) { |
| 28 | + putConsumerId(consumerId) |
| 29 | + } |
| 30 | + putRequestId(resolveRequestId(httpServletRequest)) |
| 31 | + httpServletResponse.setHeader(HEADER_NAV_CALL_ID, callId) |
| 32 | + try { |
| 33 | + filterWithErrorHandling(httpServletRequest, httpServletResponse, filterChain) |
| 34 | + } finally { |
| 35 | + removeCallId() |
| 36 | + removeConsumerId() |
| 37 | + removeRequestId() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private fun filterWithErrorHandling( |
| 42 | + httpServletRequest: HttpServletRequest, |
| 43 | + httpServletResponse: HttpServletResponse, |
| 44 | + filterChain: FilterChain, |
| 45 | + ) { |
| 46 | + try { |
| 47 | + filterChain.doFilter(httpServletRequest, httpServletResponse) |
| 48 | + } catch (e: EOFException) { |
| 49 | + log.warn("Got EOF while handling HTTP request", e) |
| 50 | + } catch (e: Exception) { |
| 51 | + if (httpServletResponse.isCommitted) { |
| 52 | + log.error("Caught exception while handling HTTP request, failed with status={}", httpServletResponse.status, e) |
| 53 | + throw e |
| 54 | + } else { |
| 55 | + log.error("Caught exception while handing HTTP request", e) |
| 56 | + } |
| 57 | + httpServletResponse.status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + override fun init(filterConfig: FilterConfig) {} |
| 62 | + |
| 63 | + override fun destroy() {} |
| 64 | + |
| 65 | + companion object { |
| 66 | + private const val HEADER_CONSUMER_ID = "Nav-Consumer-Id" |
| 67 | + private const val HEADER_NAV_CALL_ID: String = "Nav-Call-Id" |
| 68 | + private const val HEADER_NGNINX_REQUEST_ID: String = "X-Request-Id" |
| 69 | + |
| 70 | + private fun resolveCallId(httpServletRequest: HttpServletRequest): String = headerOrRandom(httpServletRequest, HEADER_NAV_CALL_ID, "Nav-CallId", "Nav-Callid", "X-Correlation-Id") |
| 71 | + |
| 72 | + private fun resolveRequestId(httpServletRequest: HttpServletRequest): String = headerOrRandom(httpServletRequest, "X_Request_Id", HEADER_NGNINX_REQUEST_ID) |
| 73 | + |
| 74 | + private fun headerOrRandom( |
| 75 | + httpServletRequest: HttpServletRequest, |
| 76 | + vararg headerNames: String, |
| 77 | + ) = headerNames |
| 78 | + .asSequence() |
| 79 | + .mapNotNull(httpServletRequest::getHeader) |
| 80 | + .firstOrNull(String::isNotEmpty) |
| 81 | + ?: UUID.randomUUID().toString() |
| 82 | + } |
| 83 | +} |
0 commit comments