Skip to content

Commit

Permalink
Merge pull request #75 from navikt/metrics
Browse files Browse the repository at this point in the history
Metrics
  • Loading branch information
igorweber authored Sep 13, 2022
2 parents b0df75e + 9e6c030 commit 740560b
Show file tree
Hide file tree
Showing 41 changed files with 873 additions and 690 deletions.
5 changes: 4 additions & 1 deletion .nais/nais.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ spec:
- "azp_name"
image: {{image}}
port: 8090
prometheus:
enabled: true
path: /meldekortservice/internal/metrics
secureLogs:
enabled: true
ingresses:
Expand Down Expand Up @@ -46,7 +49,7 @@ spec:
webproxy: true
resources:
limits:
cpu: '1'
cpu: 1000m
memory: 1500Mi
requests:
cpu: 500m
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
Microservice / proxy som henter data fra meldekort ORDS (Arena DB).

## Kom i gang
1. Bygg meldekortservice ved å kjøre `./gradlew clean build`. Dette vil også kjøre testene. Det er også mulig å kjøre `gradle clean build`, men da må man ha en riktig versjon av gradle installert (som støtter Java 17)
2. Start lokal instans av Postgres ved å kjøre `docker-compose up -d`.
3. For å være sikker på at man får en ny tom database, kan man kjøre kommandoen: `docker-compose down -v`.
Bygg meldekortservice ved å kjøre `./gradlew clean build`. Dette vil også kjøre testene.
Det er også mulig å kjøre `gradle clean build`, men da må man ha en riktig versjon av gradle installert (som støtter Java 17)

## Lokal kjøring
Start appen ved å kjøre Server.kt sin main-metode eller kjør `gradle runServer`.
0. Ikke nødvendig: for å være sikker på at man får en ny tom database, kan man kjøre kommandoen: `docker-compose down -v`.
1. Start lokal instans av Postgres ved å kjøre `docker-compose up -d`.
2. Start appen ved å kjøre `./gradlew runServerTest`.
Det er også mulig å kjøre Server.kt sin main-metode eller `./gradlew runServer`, men da må man sette miljøvariablene:
IDPORTEN_WELL_KNOWN_URL
IDPORTEN_ACCEPTED_AUDIENCE
TOKEN_X_WELL_KNOWN_URL
TOKEN_X_ACCEPTED_AUDIENCE
For å kjøre mot f.eks Q1 kan man enten sette riktige miljøvariabler (manuelt eller ved hjelp av bat/bash script) eller midlertidig skrive disse inn i Environment.kt i stedet for defaultValue'er.
For eksempel, for å bruke ORDS i Q1 må man erstatte
```
Expand All @@ -23,7 +29,7 @@ val ordsClientId: String = getEnvVar("CLIENT_ID", "%CLIENT_ID_FRA_VAULT%"),
val ordsClientSecret: String = getEnvVar("CLIENT_SECRET", "%CLIENT_SECRET_FRA_VAULT%"),
```
Appen starter på http://localhost:8090. Sjekk for eksempel at ping svarer på http://localhost:8090/meldekortservice/internal/ping.
Swagger er tilgjengelig på http://localhost:8090/meldekortservice/internal/apidocs/index.html?url=swagger.json
Swagger er tilgjengelig på http://localhost:8090/meldekortservice/internal/apidocs/index.html

## Feilsøking
For å være sikker på at man får en ny tom database, kan man kjøre kommandoen: `docker-compose down -v`.
Expand Down
160 changes: 82 additions & 78 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,56 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

val flywayVersion = "8.4.0"
val h2Version = "1.4.200"
val jacksonVersion = "2.13.1"
val flywayVersion = "9.0.1"
val h2Version = "2.1.214"
val jacksonVersion = "2.13.3"
val javaxActivationVersion = "1.1.1"
val javaxAnnotationApiVersion = "1.3.2"
val javaxJaxwsApiVersion = "2.3.1"
val jaxbApiVersion = "2.4.0-b180830.0359"
val jaxbRuntimeVersion = "3.0.2"
val jaxbRuntimeVersion = "4.0.0"
val jaxwsApiVersion = "2.3.1"
val jaxwsToolsVersion = "2.3.5"
val junitVersion = "5.8.2"
val kluentVersion = "1.68"
val kotestVersion = "5.0.3"
val kotlinLoggerVersion = "2.1.21"
val ktorVersion = "1.6.7"
val logbackVersion = "1.2.7"
val logstashVersion = "7.0.1"
val mockOauthVersion = "0.4.1"
val mockitoVersion = "4.2.0"
val mockkVersion = "1.12.2"
val jaxwsToolsVersion = "2.3.5" // Senere versjoner har ikke javax.jws.WebService
val kotlinLoggerVersion = "2.1.23"
val ktorVersion = "2.0.3"
val logbackVersion = "1.2.11"
val logstashVersion = "7.2"
val micrometerVersion = "1.9.2"
val mockOauthVersion = "0.5.1"
val mockkVersion = "1.12.4"
val navCommonCacheVersion = "2.2020.03.18_12.19-ac82e907ebc9"
val navCommonVersion = "1.2021.07.07_10.18-72bd65c546f6"
val navCommonVersion = "2.2022.07.01_07.12-6a0864fa6938"
val ojdbc8Version = "19.3.0.0"
val postgresVersion = "42.3.1"
val prometheusVersion = "0.14.1"
val slf4jVersion = "1.7.32"
val swaggerVersion = "3.23.8"
val tjenestespecVersion = "1.2019.09.25-00.21-49b69f0625e0"
val tokenValidationVersion = "2.0.17"
val postgresVersion = "42.4.0"
val swaggerVersion = "4.11.1" // Husk å endre versjonen også i SwaggerUi.kt
val tjenestespecVersion = "2589.e85bf84"
val tokenValidationVersion = "2.1.2"
val vaultJdbcVersion = "1.3.9"
val vaultVersion = "5.1.0"
val cxfVersion = "3.5.2"
val cxfVersion = "3.5.3"


project.setProperty("mainClassName", "io.ktor.server.netty.EngineMain")

repositories {
mavenCentral()
jcenter()
maven("https://plugins.gradle.org/m2/")
maven("https://jitpack.io")
}

plugins {

id("com.github.ManifestClasspath") version "0.1.0-RELEASE"

id("org.jetbrains.kotlin.jvm") version "1.6.21"
id("org.jetbrains.kotlin.plugin.allopen") version "1.6.21"
id("org.jetbrains.kotlin.jvm") version "1.7.10"
id("org.jetbrains.kotlin.plugin.allopen") version "1.7.10"

id("com.github.johnrengelman.shadow") version "7.1.2"

id("org.flywaydb.flyway") version ("8.4.0")
id("org.flywaydb.flyway") version ("9.0.1")

id("org.sonarqube") version "3.3"
id("org.sonarqube") version "3.4.0.2513"

id("com.github.ben-manes.versions") version "0.41.0"
id("com.github.ben-manes.versions") version "0.42.0"

jacoco

Expand All @@ -75,68 +69,66 @@ application {
dependencies {

implementation(kotlin("stdlib"))
implementation("no.nav:vault-jdbc:$vaultJdbcVersion")
implementation("ch.qos.logback:logback-classic:$logbackVersion")
implementation("ch.qos.logback:logback-core:$logbackVersion")
implementation("net.logstash.logback:logstash-logback-encoder:$logstashVersion")
implementation("io.prometheus:simpleclient_common:$prometheusVersion")
implementation("io.prometheus:simpleclient_hotspot:$prometheusVersion")
implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("io.ktor:ktor-auth:$ktorVersion")
implementation("io.ktor:ktor-auth-jwt:$ktorVersion")
implementation("io.ktor:ktor-client-apache:$ktorVersion")
implementation("io.ktor:ktor-client-json:$ktorVersion")
implementation("io.ktor:ktor-client-jackson:$ktorVersion")
implementation("io.ktor:ktor-locations:$ktorVersion")
implementation("com.bettercloud:vault-java-driver:$vaultVersion")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:$jacksonVersion")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names:$jacksonVersion")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:$jacksonVersion")
implementation("io.ktor:ktor-jackson:$ktorVersion")
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names:$jacksonVersion")
implementation("com.oracle.ojdbc:ojdbc8:$ojdbc8Version")
implementation("com.sun.xml.ws:jaxws-tools:$jaxwsToolsVersion") {
exclude(group = "com.sun.xml.ws", module = "policy")
}
implementation("io.github.microutils:kotlin-logging:$kotlinLoggerVersion")
implementation("io.ktor:ktor-client-apache:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-client-jackson:$ktorVersion")
implementation("io.ktor:ktor-client-jackson:$ktorVersion")
implementation("io.ktor:ktor-client-json:$ktorVersion")
implementation("io.ktor:ktor-serialization-jackson:$ktorVersion")
implementation("io.ktor:ktor-server-auth-jvm:$ktorVersion")
implementation("io.ktor:ktor-server-auth-jwt:$ktorVersion")
implementation("io.ktor:ktor-server-call-logging:$ktorVersion")
implementation("io.ktor:ktor-server-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-server-default-headers:$ktorVersion")
implementation("io.ktor:ktor-server-locations:$ktorVersion")
implementation("io.ktor:ktor-server-metrics-micrometer:$ktorVersion")
implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("io.micrometer:micrometer-registry-prometheus:$micrometerVersion")
implementation("javax.activation:activation:$javaxActivationVersion")
implementation("javax.annotation:javax.annotation-api:$javaxAnnotationApiVersion")
implementation("javax.xml.bind:jaxb-api:$jaxbApiVersion")
implementation("javax.xml.ws:jaxws-api:$jaxwsApiVersion")
implementation("net.logstash.logback:logstash-logback-encoder:$logstashVersion")
implementation("no.nav.common:cache:$navCommonCacheVersion")
implementation("no.nav.common:cxf:$navCommonVersion")
implementation("no.nav.common:log:$navCommonVersion")
implementation("no.nav.common:types:$navCommonVersion")
implementation("org.slf4j:slf4j-api:$slf4jVersion")
implementation("io.github.microutils:kotlin-logging:$kotlinLoggerVersion")
implementation("com.bettercloud:vault-java-driver:$vaultVersion")
implementation("no.nav.tjenestespesifikasjoner:arena-sakOgAktivitet_v1:$tjenestespecVersion")
implementation("no.nav.security:token-validation-ktor-v2:$tokenValidationVersion")
implementation("com.github.navikt.tjenestespesifikasjoner:arena-sakogaktivitet_v1:$tjenestespecVersion")
implementation("org.apache.cxf:cxf-core:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-bindings-soap:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-features-logging:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-frontend-jaxws:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-frontend-simple:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-transports-http:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-ws-policy:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-ws-security:$cxfVersion")
implementation("org.flywaydb:flyway-core:$flywayVersion")
implementation("org.glassfish.jaxb:jaxb-runtime:$jaxbRuntimeVersion")
implementation("org.postgresql:postgresql:$postgresVersion")
implementation("no.nav.security:token-validation-ktor:$tokenValidationVersion")
implementation("no.nav.security:token-client-spring:$tokenValidationVersion")
implementation("com.oracle.ojdbc:ojdbc8:$ojdbc8Version")
implementation("org.webjars:swagger-ui:$swaggerVersion")
implementation("no.nav:vault-jdbc:$vaultJdbcVersion")

testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testImplementation(kotlin("test-junit5"))
testImplementation("org.mockito:mockito-core:$mockitoVersion")
testImplementation("io.ktor:ktor-server-test-host:$ktorVersion")
testImplementation("io.ktor:ktor-client-mock:$ktorVersion")
testImplementation("io.ktor:ktor-client-mock-jvm:$ktorVersion")
testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")

testImplementation("com.h2database:h2:$h2Version")
testImplementation("org.amshove.kluent:kluent:$kluentVersion")
testImplementation("io.ktor:ktor-client-mock:$ktorVersion")
testImplementation("io.ktor:ktor-server-test-host:$ktorVersion")
testImplementation("io.mockk:mockk:$mockkVersion")
testImplementation("no.nav.security:mock-oauth2-server:$mockOauthVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")

implementation("org.webjars:swagger-ui:$swaggerVersion")
implementation("javax.xml.ws:jaxws-api:$jaxwsApiVersion")
implementation("javax.annotation:javax.annotation-api:$javaxAnnotationApiVersion")
implementation("javax.xml.bind:jaxb-api:$jaxbApiVersion")
implementation("org.glassfish.jaxb:jaxb-runtime:$jaxbRuntimeVersion")
implementation("javax.activation:activation:$javaxActivationVersion")
implementation("com.sun.xml.ws:jaxws-tools:$jaxwsToolsVersion") {
exclude(group = "com.sun.xml.ws", module = "policy")
}

implementation("org.apache.cxf:cxf-rt-frontend-jaxws:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-transports-http:$cxfVersion")
implementation("org.apache.cxf:cxf-rt-ws-security:$cxfVersion")
}

configure<JavaPluginConvention> {
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
Expand All @@ -156,6 +148,8 @@ tasks {
}

withType<ShadowJar> {
isZip64 = true

transform(ServiceFileTransformer::class.java) {
setPath("META-INF/cxf")
}
Expand All @@ -171,7 +165,7 @@ tasks {

jacocoTestReport {
reports {
xml.isEnabled = true
xml.required.set(true)
}
}

Expand All @@ -180,7 +174,17 @@ tasks {
}

register("runServer", JavaExec::class) {
main = project.property("mainClassName").toString()
mainClass.set(project.property("mainClassName").toString())
classpath = sourceSets["main"].runtimeClasspath
}

register("runServerTest", JavaExec::class) {
systemProperties["IDPORTEN_WELL_KNOWN_URL"] = "idporten.dev.nav.no"
systemProperties["IDPORTEN_ACCEPTED_AUDIENCE"] = "nav.no"
systemProperties["TOKEN_X_WELL_KNOWN_URL"] = "tokenx.dev.nav.no"
systemProperties["TOKEN_X_ACCEPTED_AUDIENCE"] = "nav.no"

mainClass.set(project.property("mainClassName").toString())
classpath = sourceSets["main"].runtimeClasspath
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
74 changes: 55 additions & 19 deletions src/main/kotlin/no/nav/meldeplikt/meldekortservice/api/Internal.kt
Original file line number Diff line number Diff line change
@@ -1,47 +1,83 @@
package no.nav.meldeplikt.meldekortservice.api

import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.application.*
import io.ktor.server.locations.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.micrometer.prometheus.PrometheusMeterRegistry
import no.nav.meldeplikt.meldekortservice.config.SWAGGER_URL_V1
import no.nav.meldeplikt.meldekortservice.config.swagger
import no.nav.meldeplikt.meldekortservice.utils.API_PATH
import no.nav.meldeplikt.meldekortservice.utils.BASE_PATH
import no.nav.meldeplikt.meldekortservice.utils.INTERNAL_PATH
import no.nav.meldeplikt.meldekortservice.utils.swagger.SwaggerUi

fun Route.healthApi() {
@KtorExperimentalLocationsAPI
fun Route.healthApi(appMicrometerRegistry: PrometheusMeterRegistry) {

route(INTERNAL_PATH) {
val swaggerUI = SwaggerUi()

get("") {
call.respondRedirect(SWAGGER_URL_V1)
}

get("/") {
call.respondRedirect(SWAGGER_URL_V1)
}

get("/apidocs") {
call.respondRedirect(SWAGGER_URL_V1)
}

get("/apidocs/") {
call.respondRedirect(SWAGGER_URL_V1)
}

get("/apidocs/{fileName}") {
val fileName = call.parameters["fileName"]
if (fileName == "swagger.json") {
call.respond(swagger)
} else {
swaggerUI.serve(fileName, call)
}
}

get("/isAlive") {
call.respondText(text = "Alive", contentType = ContentType.Text.Plain)
call.respondText("Alive")
}

get("/isReady") {
call.respondText(text = "Ready", contentType = ContentType.Text.Plain)
call.respondText("Ready")
}

get("/ping") {
val pingJsonResponse = """{"ping": "pong"}"""
call.respondText(text = pingJsonResponse, contentType = ContentType.Application.Json)
}

get("/metrics") {
call.respondText(appMicrometerRegistry.scrape())
}
}
}

fun Routing.swaggerRoutes() {
val swaggerUI = SwaggerUi()

get(BASE_PATH) { call.respondRedirect(SWAGGER_URL_V1) }
get(API_PATH) { call.respondRedirect(SWAGGER_URL_V1) }
get("$INTERNAL_PATH/apidocs") { call.respondRedirect(SWAGGER_URL_V1) }
get("$INTERNAL_PATH/apidocs/{fileName}") {
val fileName = call.parameters["fileName"]
if (fileName == "swagger.json") {
call.respond(swagger)
} else {
swaggerUI.serve(fileName, call)
route(BASE_PATH) {
get("") {
call.respondRedirect(SWAGGER_URL_V1)
}

get("/") {
call.respondRedirect(SWAGGER_URL_V1)
}

get("/api") {
call.respondRedirect(SWAGGER_URL_V1)
}

get("/api/") {
call.respondRedirect(SWAGGER_URL_V1)
}
}
}
}
Loading

0 comments on commit 740560b

Please sign in to comment.