File tree 2 files changed +57
-0
lines changed
main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry
test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry
2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.config.telemetry
2
+
3
+ import io.opentelemetry.api.trace.Span
4
+ import jakarta.servlet.http.HttpServletRequest
5
+ import jakarta.servlet.http.HttpServletResponse
6
+ import org.springframework.context.annotation.Configuration
7
+ import org.springframework.stereotype.Component
8
+ import org.springframework.web.servlet.HandlerInterceptor
9
+ import org.springframework.web.servlet.config.annotation.InterceptorRegistry
10
+ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
11
+
12
+ @Configuration
13
+ class ClientTrackingConfiguration (private val clientTrackingInterceptor : ClientTrackingInterceptor ) : WebMvcConfigurer {
14
+ override fun addInterceptors (registry : InterceptorRegistry ) {
15
+ registry.addInterceptor(clientTrackingInterceptor).addPathPatterns(" /**" )
16
+ }
17
+ }
18
+
19
+ @Component
20
+ class ClientTrackingInterceptor : HandlerInterceptor {
21
+ override fun preHandle (
22
+ request : HttpServletRequest ,
23
+ response : HttpServletResponse ,
24
+ handler : Any ,
25
+ ): Boolean {
26
+ val subjectDistinguishedName = request.getAttribute(" clientName" ) as String?
27
+ subjectDistinguishedName?.let {
28
+ try {
29
+ Span .current().setAttribute(" clientId" , it)
30
+ } catch (ignored: Exception ) {
31
+ // Do nothing - don't create client id span
32
+ }
33
+ }
34
+ return true
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.config.telemetry
2
+
3
+ import jakarta.servlet.http.HttpServletRequest
4
+ import jakarta.servlet.http.HttpServletResponse
5
+ import org.junit.jupiter.api.Assertions.assertTrue
6
+ import org.junit.jupiter.api.Test
7
+ import org.mockito.Mockito.mock
8
+ import org.mockito.kotlin.whenever
9
+
10
+ class ClientTrackingInterceptorTest {
11
+ private val request: HttpServletRequest = mock(HttpServletRequest ::class .java)
12
+ private val response: HttpServletResponse = mock(HttpServletResponse ::class .java)
13
+ private val interceptor = ClientTrackingInterceptor ()
14
+
15
+ @Test
16
+ fun `handles missing clientName attribute` () {
17
+ whenever(request.getAttribute(" clientName" )).thenReturn(null )
18
+ val result = interceptor.preHandle(request, response, " " )
19
+ assertTrue(result)
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments