Skip to content

Commit

Permalink
Muligheter til å ekskludere endepunkter i metrikker i config og renam…
Browse files Browse the repository at this point in the history
…e av configverdi (#802)
  • Loading branch information
stigebil authored May 14, 2024
1 parent caf5f1d commit f013b49
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import org.springframework.web.servlet.mvc.method.RequestMappingInfo
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

@Component
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk")
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk.enabled")
class TellAPIEndepunkterIBrukInitialiserer(
@Value("\${NAIS_APP_NAME}") private val applicationName: String,
private val applicationContext: ApplicationContext,
@Value("\${familie.tellAPIEndepunkterIBruk.paths:/api}") private val pathStartWidth: List<String>,
@Value("\${familie.tellAPIEndepunkterIBruk.ekskluder:/internal}") private val ekskludertePaths: List<String>,
) {
init {
metrikker.clear()
Expand All @@ -31,7 +31,7 @@ class TellAPIEndepunkterIBrukInitialiserer(

requestMappings.forEach { (info, handler) ->
info.patternValues.forEach { path ->
if (pathStartWidth.any { path.startsWith(it) }) {
if (ekskludertePaths.none { path.startsWith(it) }) {
val metrikknavn = "$applicationName.${info.methodsCondition}$path".tilMetrikknavn()
val key = "${info.methodsCondition}$path"
metrikker.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import org.springframework.web.servlet.AsyncHandlerInterceptor
import org.springframework.web.servlet.HandlerMapping

/**
* Sett property familie.tellAPIEndepunkterIBruk: true i application.yaml for å skru
* Sett property familie.tellAPIEndepunkterIBruk.enabled: true i application.yaml for å skru
* på metrikker for hvor mange ganger et endepunkt har blitt kalt
*
* For å konfigurere andre endepunkter enn de som starter på /api så kan man bruke
* familie.tellAPIEndepunkterIBruk.paths: /foo,/bar
* For å ekskludere endepunkter så kan man bruke
* familie.tellAPIEndepunkterIBruk.ekskluder: /foo,/bar
*
*/
@Component
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk")
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk.enabled")
class TellAPIEndepunkterIBrukInterceptor : AsyncHandlerInterceptor {
override fun preHandle(
request: HttpServletRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk")
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk.enabled")
open class TellAPIEndpunkterIBrukWebConfig : WebMvcConfigurer {
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(TellAPIEndepunkterIBrukInterceptor()).addPathPatterns("/api/**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class TellAPIEndepunkterIBrukTest {
fun `skal opprette map med key og counter når det finnes en requestmapping`() {
settOppTestData("/api/foo", RequestMethod.GET)

TellAPIEndepunkterIBrukInitialiserer("test", applicationContext, listOf("/api")).populerMapMedCountersForRestEndepunkt()
TellAPIEndepunkterIBrukInitialiserer(
"test",
applicationContext,
listOf("/internal"),
).populerMapMedCountersForRestEndepunkt()

assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter).hasSize(1)
assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter.containsKey("[GET]/api/foo")).isTrue()
Expand All @@ -39,7 +43,11 @@ class TellAPIEndepunkterIBrukTest {
fun `skal opprette map med key og counter når det finnes en requestmapping med pathParam og navnet på counteren saneres`() {
settOppTestData("/api/foo/{fooId}", RequestMethod.POST)

TellAPIEndepunkterIBrukInitialiserer("test", applicationContext, listOf("/api")).populerMapMedCountersForRestEndepunkt()
TellAPIEndepunkterIBrukInitialiserer(
"test",
applicationContext,
listOf("/internal"),
).populerMapMedCountersForRestEndepunkt()

assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter).hasSize(1)
assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter.containsKey("[POST]/api/foo/{fooId}")).isTrue()
Expand All @@ -52,7 +60,11 @@ class TellAPIEndepunkterIBrukTest {
fun `skal ikke opprette map med counter for annet enn pathparam som starter med api`() {
settOppTestData("/internal/foobar", RequestMethod.POST)

TellAPIEndepunkterIBrukInitialiserer("test", applicationContext, listOf("/api")).populerMapMedCountersForRestEndepunkt()
TellAPIEndepunkterIBrukInitialiserer(
"test",
applicationContext,
listOf("/internal"),
).populerMapMedCountersForRestEndepunkt()

assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter).hasSize(0)
}
Expand Down

0 comments on commit f013b49

Please sign in to comment.