-
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.
Modul med metrikker gir mulighet til å telle antall ganger ett endepu…
…nkt har blitt kallt
- Loading branch information
Showing
14 changed files
with
303 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>no.nav.familie.felles</groupId> | ||
<artifactId>felles</artifactId> | ||
<version>${revision}${sha1}${changelist}</version> | ||
</parent> | ||
|
||
<artifactId>metrikker</artifactId> | ||
<version>${revision}${sha1}${changelist}</version> | ||
<name>Felles - Metrikker</name> | ||
<description>Felles kode for metrikker</description> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-stdlib</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.micrometer</groupId> | ||
<artifactId>micrometer-registry-prometheus</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.annotation</groupId> | ||
<artifactId>jakarta.annotation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.servlet</groupId> | ||
<artifactId>jakarta.servlet-api</artifactId> | ||
</dependency> | ||
|
||
|
||
<!-- Test --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.mockk</groupId> | ||
<artifactId>mockk-jvm</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-maven-plugin</artifactId> | ||
<version>${kotlin.version}</version> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>test-compile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>test-compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>testCompile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>testCompile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
65 changes: 65 additions & 0 deletions
65
metrikker/src/main/java/no/nav/familie/metrikker/TellAPIEndepunkterIBrukInitialiserer.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,65 @@ | ||
package no.nav.familie.metrikker | ||
|
||
import io.micrometer.core.instrument.Counter | ||
import io.micrometer.core.instrument.Metrics | ||
import jakarta.annotation.PostConstruct | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.ApplicationContext | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.method.HandlerMethod | ||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping | ||
|
||
@Component | ||
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk") | ||
class TellAPIEndepunkterIBrukInitialiserer( | ||
@Value("\${NAIS_APP_NAME}") private val applicationName: String, | ||
private val applicationContext: ApplicationContext, | ||
) { | ||
init { | ||
metrikker.clear() | ||
} | ||
|
||
@PostConstruct | ||
fun init() { | ||
val requestMappingHandlerMapping: RequestMappingHandlerMapping = | ||
applicationContext | ||
.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping::class.java) | ||
val requestMappings: Map<RequestMappingInfo, HandlerMethod> = requestMappingHandlerMapping.handlerMethods | ||
|
||
requestMappings.forEach { (info, handler) -> | ||
info.patternValues.forEach { path -> | ||
if (path.startsWith("/api")) { | ||
val metrikknavn = "$applicationName.${info.methodsCondition}$path".sanerMetrikkNavn() | ||
metrikker.put( | ||
"${info.methodsCondition}$path", | ||
Metrics.counter( | ||
metrikknavn, | ||
"type", | ||
"endepunkt", | ||
"patternVerdi", | ||
path, | ||
"restMetode", | ||
"${info.methodsCondition}", | ||
), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun String.sanerMetrikkNavn() = | ||
this | ||
.replace("[", "") | ||
.replace("]", "") | ||
.replace("{", "") | ||
.replace("}", "") | ||
.replace("/", "_") | ||
|
||
companion object { | ||
private val metrikker = mutableMapOf<String, Counter>() | ||
val metrikkerForEndepunkter | ||
get() = metrikker.toMap() | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
metrikker/src/main/java/no/nav/familie/metrikker/TellAPIEndepunkterIBrukInterceptor.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,22 @@ | ||
package no.nav.familie.metrikker | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.servlet.AsyncHandlerInterceptor | ||
import org.springframework.web.servlet.HandlerMapping | ||
|
||
@Component | ||
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk") | ||
class TellAPIEndepunkterIBrukInterceptor : AsyncHandlerInterceptor { | ||
override fun preHandle( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
handler: Any, | ||
): Boolean { | ||
val path = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) as String | ||
TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter.get("[${request.method}]$path")?.increment() | ||
return super.preHandle(request, response, handler) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
metrikker/src/main/java/no/nav/familie/metrikker/TellAPIEndpunkterIBrukWebConfig.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,15 @@ | ||
package no.nav.familie.metrikker | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
|
||
@Configuration | ||
@ConditionalOnProperty("familie.tellAPIEndepunkterIBruk") | ||
open class TellAPIEndpunkterIBrukWebConfig : WebMvcConfigurer { | ||
override fun addInterceptors(registry: InterceptorRegistry) { | ||
registry.addInterceptor(TellAPIEndepunkterIBrukInterceptor()).addPathPatterns("/api/**") | ||
super.addInterceptors(registry) | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
metrikker/src/test/java/no/nav/familie/metrikker/TellAPIEndepunkterIBrukTest.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,77 @@ | ||
package no.nav.familie.metrikker | ||
|
||
import io.mockk.clearAllMocks | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.context.ApplicationContext | ||
import org.springframework.web.bind.annotation.RequestMethod | ||
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition | ||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping | ||
|
||
class TellAPIEndepunkterIBrukTest { | ||
val applicationContext: ApplicationContext = mockk() | ||
val requestMappingHandlerMapping: RequestMappingHandlerMapping = mockk() | ||
val info: RequestMappingInfo = mockk() | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
clearAllMocks() | ||
} | ||
|
||
@Test | ||
fun `skal opprette map med key og counter når det finnes en requestmapping`() { | ||
settOppTestData("/api/foo", RequestMethod.GET) | ||
|
||
TellAPIEndepunkterIBrukInitialiserer("test", applicationContext).init() | ||
|
||
assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter).hasSize(1) | ||
assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter.containsKey("[GET]/api/foo")).isTrue() | ||
assertThat( | ||
TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter.get("[GET]/api/foo")?.id?.name, | ||
).isEqualTo("test.GET_api_foo") | ||
} | ||
|
||
@Test | ||
fun `skal opprette map med key og counter når det finnes en requestmapping, hvor requesten har pathParam og navnet på counteren saneres`() { | ||
settOppTestData("/api/foo/{fooId}", RequestMethod.POST) | ||
|
||
TellAPIEndepunkterIBrukInitialiserer("test", applicationContext).init() | ||
|
||
assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter).hasSize(1) | ||
assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter.containsKey("[POST]/api/foo/{fooId}")).isTrue() | ||
assertThat( | ||
TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter.get("[POST]/api/foo/{fooId}")?.id?.name, | ||
).isEqualTo("test.POST_api_foo_fooId") | ||
} | ||
|
||
@Test | ||
fun `skal ikke opprette map med counter for annet enn pathparam som starter med api`() { | ||
settOppTestData("/internal/foobar", RequestMethod.POST) | ||
|
||
TellAPIEndepunkterIBrukInitialiserer("test", applicationContext).init() | ||
|
||
assertThat(TellAPIEndepunkterIBrukInitialiserer.metrikkerForEndepunkter).hasSize(0) | ||
} | ||
|
||
private fun settOppTestData( | ||
path: String, | ||
method: RequestMethod, | ||
) { | ||
every { info.patternValues } returns setOf(path) | ||
every { info.methodsCondition } returns RequestMethodsRequestCondition(method) | ||
|
||
every { requestMappingHandlerMapping.handlerMethods } returns | ||
mapOf(Pair(info, mockk())) | ||
every { | ||
applicationContext.getBean( | ||
"requestMappingHandlerMapping", | ||
RequestMappingHandlerMapping::class.java, | ||
) | ||
} returns | ||
requestMappingHandlerMapping | ||
} | ||
} |
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
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
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
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