Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add IDs to requests #222

Merged
merged 3 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions project/src/main/kotlin/org/ionproject/core/CoreApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.ionproject.core.calendar.icalendar.Calendar
import org.ionproject.core.calendar.representations.CalendarSerializer
import org.ionproject.core.common.Media
import org.ionproject.core.common.UriTemplateSerializer
import org.ionproject.core.common.filters.RequestIDFilter
import org.ionproject.core.common.interceptors.ControlAccessInterceptor
import org.ionproject.core.common.interceptors.LoggerInterceptor
import org.ionproject.core.common.interceptors.PageLimitQueryParamInterceptor
Expand All @@ -16,6 +17,8 @@ import org.ionproject.core.common.messageConverters.ProblemJsonMessageConverter
import org.ionproject.core.common.messageConverters.SirenMessageConverter
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
Expand Down Expand Up @@ -77,6 +80,13 @@ class CoreSerializationConfig : WebMvcConfigurer {
.addPathPatterns("/v?/programmes*")
.addPathPatterns("/v?/search*")
}

@Bean
fun registerRequestIDFilter() : FilterRegistrationBean<RequestIDFilter> =
FilterRegistrationBean<RequestIDFilter>().apply {
filter = RequestIDFilter()
order = 1
}
}

fun main(args: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.ionproject.core.common.filters

import org.slf4j.MDC // if we use logback switch the import. The method name is the same so everything should work accordingly
import org.springframework.web.filter.OncePerRequestFilter
import java.util.*
import javax.servlet.FilterChain
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

private const val REQUEST_ID_HEADER = "Request-Id"
const val REQUEST_ID = "req-id"

class RequestIDFilter : OncePerRequestFilter() {
override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
filterChain: FilterChain
) {
val requestId = UUID.randomUUID().toString()

MDC.put(REQUEST_ID, requestId)

response.addHeader(
REQUEST_ID_HEADER,
requestId
)

filterChain.doFilter(request, response)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.ionproject.core.common.interceptors

import org.ionproject.core.common.filters.REQUEST_ID
import org.slf4j.LoggerFactory
import org.slf4j.MDC
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
Expand All @@ -12,7 +14,8 @@ class LoggerInterceptor : HandlerInterceptorAdapter() {
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
JorgePBrown marked this conversation as resolved.
Show resolved Hide resolved
val startTime = System.currentTimeMillis()
logger.info(
"IP:${request.remoteAddr} | Method:${request.method} | Endpoint:${request.requestURI} | Timestamp:${startTime}"
"IP:${request.remoteAddr} | Method:${request.method} | Endpoint:${request.requestURI} | Request-Id:${MDC.get(
JorgePBrown marked this conversation as resolved.
Show resolved Hide resolved
REQUEST_ID)} | Timestamp:${startTime}"
)
request.setAttribute("startTime", startTime)
return true
Expand All @@ -28,7 +31,7 @@ class LoggerInterceptor : HandlerInterceptorAdapter() {
val endTime: Long = System.currentTimeMillis()
logger.info(
"""
Total time taken to proccess request in milliseconds: ${endTime - startTime} ms
Request-Id:${MDC.get(REQUEST_ID)} | Total time taken to proccess request in milliseconds: ${endTime - startTime} ms
""".trimIndent()
)
}
Expand Down