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

PI-2375: Added clientId to appinsights #460

Merged
merged 1 commit into from
Aug 5, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.config.telemetry

import io.opentelemetry.api.trace.Span
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.context.annotation.Configuration
import org.springframework.stereotype.Component
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
class ClientTrackingConfiguration(private val clientTrackingInterceptor: ClientTrackingInterceptor) : WebMvcConfigurer {
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(clientTrackingInterceptor).addPathPatterns("/**")
}
}

@Component
class ClientTrackingInterceptor : HandlerInterceptor {
override fun preHandle(
request: HttpServletRequest,
response: HttpServletResponse,
handler: Any,
): Boolean {
val subjectDistinguishedName = request.getAttribute("clientName") as String?
subjectDistinguishedName?.let {
try {
Span.current().setAttribute("clientId", it)
} catch (ignored: Exception) {
// Do nothing - don't create client id span
}
}
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.config.telemetry

import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever

class ClientTrackingInterceptorTest {
private val request: HttpServletRequest = mock(HttpServletRequest::class.java)
private val response: HttpServletResponse = mock(HttpServletResponse::class.java)
private val interceptor = ClientTrackingInterceptor()

@Test
fun `handles missing clientName attribute`() {
whenever(request.getAttribute("clientName")).thenReturn(null)
val result = interceptor.preHandle(request, response, "")
assertTrue(result)
}
}
Loading