-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): add x-request-id header to trace requests across artif…
…acts in logs resolves #1072
- Loading branch information
1 parent
f4d6e13
commit 08cb3c2
Showing
6 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
backend/src/main/kotlin/org/loculus/backend/controller/LoculusCustomHeaders.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.loculus.backend.controller | ||
|
||
object LoculusCustomHeaders { | ||
const val REQUEST_ID = "x-request-id" | ||
const val X_TOTAL_RECORDS = "x-total-records" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
backend/src/main/kotlin/org/loculus/backend/log/RequestId.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.loculus.backend.log | ||
|
||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.loculus.backend.controller.LoculusCustomHeaders.REQUEST_ID | ||
import org.slf4j.MDC | ||
import org.springframework.core.annotation.Order | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.context.annotation.RequestScope | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
import java.util.UUID | ||
|
||
@Component | ||
@RequestScope | ||
class RequestIdContext { | ||
var requestId: String? = null | ||
} | ||
|
||
const val REQUEST_ID_MDC_KEY = "RequestId" | ||
private const val HIGH_PRECEDENCE_BUT_LOW_ENOUGH_TO_HAVE_REQUEST_SCOPE_AVAILABLE = -100 | ||
const val REQUEST_ID_HEADER_DESCRIPTION = """ | ||
A UUID that uniquely identifies the request for tracing purposes. | ||
If none is provided in the request, the backend will generate one. | ||
""" | ||
|
||
@Component | ||
@Order(HIGH_PRECEDENCE_BUT_LOW_ENOUGH_TO_HAVE_REQUEST_SCOPE_AVAILABLE) | ||
class RequestIdFilter(private val requestIdContext: RequestIdContext) : OncePerRequestFilter() { | ||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain, | ||
) { | ||
val requestId = request.getHeader(REQUEST_ID) ?: UUID.randomUUID().toString() | ||
|
||
MDC.put(REQUEST_ID_MDC_KEY, requestId) | ||
requestIdContext.requestId = requestId | ||
response.addHeader(REQUEST_ID, requestId) | ||
|
||
try { | ||
filterChain.doFilter(request, response) | ||
} finally { | ||
MDC.remove(REQUEST_ID_MDC_KEY) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters