Skip to content

Commit f80df66

Browse files
committed
Bruke io.prometheus:client_java
1 parent d99fc2f commit f80df66

File tree

11 files changed

+32
-66
lines changed

11 files changed

+32
-66
lines changed

dp-inntekt-api/build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ dependencies {
3939

4040
implementation("io.micrometer:micrometer-registry-prometheus:1.14.4")
4141

42+
implementation("io.prometheus:client_java:1.3.6")
43+
implementation("io.prometheus:prometheus-metrics-core:1.3.6")
44+
implementation("io.prometheus:prometheus-metrics-instrumentation-jvm:1.3.6")
45+
4246
implementation("com.expediagroup:graphql-kotlin-client:$expediaGraphqlVersion")
4347
implementation("com.expediagroup:graphql-kotlin-ktor-client:$expediaGraphqlVersion")
4448
implementation("com.expediagroup:graphql-kotlin-client-jackson:$expediaGraphqlVersion")
@@ -61,9 +65,6 @@ dependencies {
6165
implementation(libs.konfig)
6266
implementation("org.slf4j:slf4j-api:2.0.17")
6367

64-
implementation("io.prometheus:simpleclient_common:0.16.0")
65-
implementation("io.prometheus:simpleclient_hotspot:0.16.0")
66-
implementation("io.prometheus:simpleclient_log4j2:0.16.0")
6768
implementation("no.bekk.bekkopen:nocommons:0.16.0")
6869

6970
implementation("com.uchuhimo:kotlinx-bimap:1.2")

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/Application.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package no.nav.dagpenger.inntekt
22

33
import io.ktor.server.engine.embeddedServer
4-
import io.ktor.server.engine.stop
54
import io.ktor.server.netty.Netty
6-
import io.prometheus.client.hotspot.DefaultExports
75
import kotlinx.coroutines.runBlocking
86
import mu.KotlinLogging
97
import no.nav.dagpenger.inntekt.Config.inntektApiConfig
@@ -26,7 +24,6 @@ fun main() {
2624
runBlocking {
2725
val config = configuration.inntektApiConfig
2826
PostgresDataSourceBuilder.runMigration()
29-
DefaultExports.initialize()
3027
val dataSource = PostgresDataSourceBuilder.dataSource
3128
val postgresInntektStore = PostgresInntektStore(dataSource)
3229
val pdlPersonOppslag =

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/InntektApi.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ internal fun Application.inntektApi(
6060
enhetsregisterClient: EnhetsregisterClient,
6161
healthChecks: List<HealthCheck>,
6262
collectorRegistry: PrometheusRegistry = PrometheusRegistry.defaultRegistry,
63+
meterRegistry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT, collectorRegistry, Clock.SYSTEM),
6364
) {
6465
install(DefaultHeaders)
6566
install(MicrometerMetrics) {
66-
registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT, collectorRegistry, Clock.SYSTEM)
67+
registry = meterRegistry
6768
}
6869

6970
install(Authentication) {
@@ -229,7 +230,7 @@ internal fun Application.inntektApi(
229230
}
230231
}
231232
}
232-
naischecks(healthChecks)
233+
naischecks(healthChecks, meterRegistry)
233234
}
234235
}
235236

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/NaisChecks.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ package no.nav.dagpenger.inntekt
22

33
import io.ktor.http.ContentType
44
import io.ktor.http.HttpStatusCode
5-
import io.ktor.server.application.call
65
import io.ktor.server.response.respondText
7-
import io.ktor.server.response.respondTextWriter
86
import io.ktor.server.routing.Routing
97
import io.ktor.server.routing.get
10-
import io.prometheus.client.CollectorRegistry
11-
import io.prometheus.client.exporter.common.TextFormat
8+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
129
import mu.KotlinLogging
1310

1411
private val LOGGER = KotlinLogging.logger {}
15-
private val collectorRegistry: CollectorRegistry = CollectorRegistry.defaultRegistry
1612

17-
fun Routing.naischecks(healthChecks: List<HealthCheck>) {
13+
fun Routing.naischecks(
14+
healthChecks: List<HealthCheck>,
15+
meterRegistry: PrometheusMeterRegistry,
16+
) {
1817
get("/isAlive") {
1918
val failingHealthChecks = healthChecks.filter { it.status() == HealthStatus.DOWN }
2019

@@ -31,8 +30,6 @@ fun Routing.naischecks(healthChecks: List<HealthCheck>) {
3130
}
3231
get("/metrics") {
3332
val names = call.request.queryParameters.getAll("name[]")?.toSet() ?: setOf()
34-
call.respondTextWriter(ContentType.parse(TextFormat.CONTENT_TYPE_004)) {
35-
TextFormat.write004(this, collectorRegistry.filteredMetricFamilySamples(names))
36-
}
33+
call.respondText(meterRegistry.scrape(), ContentType.parse("text/plain; version=0.0.4; charset=utf-8"))
3734
}
3835
}

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/db/PostgresInntektStore.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package no.nav.dagpenger.inntekt.db
22

33
import com.fasterxml.jackson.module.kotlin.readValue
44
import de.huxhorn.sulky.ulid.ULID
5-
import io.prometheus.client.Summary
5+
import io.prometheus.metrics.core.metrics.Summary
66
import kotliquery.queryOf
77
import kotliquery.sessionOf
88
import kotliquery.using
@@ -27,7 +27,7 @@ internal class PostgresInntektStore(private val dataSource: DataSource) : Inntek
2727
private val ulidGenerator = ULID()
2828
private val LOGGER = KotlinLogging.logger {}
2929
private val markerInntektTimer =
30-
Summary.build()
30+
Summary.builder()
3131
.name("marker_inntekt_brukt")
3232
.help("Hvor lang tid det tar å markere en inntekt brukt (i sekunder")
3333
.register()

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/inntektskomponenten/v1/InntektskomponentHttpClient.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import io.ktor.client.statement.bodyAsText
1919
import io.ktor.http.ContentType
2020
import io.ktor.http.HttpHeaders
2121
import io.ktor.serialization.jackson.JacksonConverter
22-
import io.prometheus.client.Counter
23-
import io.prometheus.client.Summary
22+
import io.prometheus.metrics.core.metrics.Counter
23+
import io.prometheus.metrics.core.metrics.Summary
2424
import mu.KotlinLogging
2525
import mu.withLoggingContext
2626
import no.nav.dagpenger.inntekt.serder.jacksonObjectMapper
@@ -31,7 +31,7 @@ private val sikkerLogg = KotlinLogging.logger("tjenestekall")
3131
private val ulid = ULID()
3232
const val INNTEKTSKOMPONENT_CLIENT_SECONDS_METRICNAME = "inntektskomponent_client_seconds"
3333
private val clientLatencyStats: Summary =
34-
Summary.build()
34+
Summary.builder()
3535
.name(INNTEKTSKOMPONENT_CLIENT_SECONDS_METRICNAME)
3636
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
3737
.quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
@@ -40,13 +40,13 @@ private val clientLatencyStats: Summary =
4040
.register()
4141
const val INNTEKTSKOMPONENT_FETCH_ERROR = "inntektskomponent_fetch_error"
4242
private val clientFetchErrors =
43-
Counter.build()
43+
Counter.builder()
4444
.name(INNTEKTSKOMPONENT_FETCH_ERROR)
4545
.help("Number of times fetching form inntektskomponenten has failed")
4646
.register()
4747
const val INNTEKTSKOMPONENT_STATUS_CODES = "inntektskomponent_status_codes"
4848
private val inntektskomponentStatusCodesCounter =
49-
Counter.build()
49+
Counter.builder()
5050
.name(INNTEKTSKOMPONENT_STATUS_CODES)
5151
.help("Number of times inntektskomponenten has returned a specific status code")
5252
.labelNames("status_code")
@@ -99,7 +99,7 @@ internal class InntektkomponentKtorClient(
9999
}
100100
} catch (error: ServerResponseException) {
101101
val statusKode = error.response.status.value
102-
inntektskomponentStatusCodesCounter.labels(statusKode.toString()).inc()
102+
inntektskomponentStatusCodesCounter.labelValues(statusKode.toString()).inc()
103103
clientFetchErrors.inc()
104104
val feilmelding =
105105
kotlin.runCatching {
@@ -126,7 +126,7 @@ internal class InntektkomponentKtorClient(
126126
} finally {
127127
timer.observeDuration()
128128
}
129-
inntektskomponentStatusCodesCounter.labels(response.status.value.toString()).inc()
129+
inntektskomponentStatusCodesCounter.labelValues(response.status.value.toString()).inc()
130130

131131
return response.body()
132132
}

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/subsumsjonbrukt/Vaktmester.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package no.nav.dagpenger.inntekt.subsumsjonbrukt
22

3-
import io.prometheus.client.Counter
3+
import io.prometheus.metrics.core.metrics.Counter
44
import kotliquery.queryOf
55
import kotliquery.sessionOf
66
import kotliquery.using
77
import javax.sql.DataSource
88

99
const val INNTEKT_SLETTET = "inntekt_slettet"
1010
private val deleteCounter =
11-
Counter.build()
11+
Counter.builder()
1212
.name(INNTEKT_SLETTET)
1313
.help("Antall inntektsett slettet fra databasen")
1414
.register()

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/v1/UklassifisertInntektRoute.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package no.nav.dagpenger.inntekt.v1
33
import com.auth0.jwt.exceptions.JWTDecodeException
44
import io.ktor.http.HttpStatusCode
55
import io.ktor.server.application.ApplicationCall
6-
import io.ktor.server.application.call
76
import io.ktor.server.auth.authenticate
87
import io.ktor.server.auth.authentication
98
import io.ktor.server.auth.jwt.JWTPrincipal
@@ -14,7 +13,7 @@ import io.ktor.server.routing.Route
1413
import io.ktor.server.routing.get
1514
import io.ktor.server.routing.post
1615
import io.ktor.server.routing.route
17-
import io.prometheus.client.Counter
16+
import io.prometheus.metrics.core.metrics.Counter
1817
import kotlinx.coroutines.Dispatchers
1918
import kotlinx.coroutines.withContext
2019
import mu.KotlinLogging
@@ -41,13 +40,13 @@ import java.time.LocalDate
4140
private val logger = KotlinLogging.logger {}
4241
const val INNTEKT_KORRIGERING = "inntekt_korrigering"
4342
private val inntektKorrigeringCounter =
44-
Counter.build().name(INNTEKT_KORRIGERING).help("Antall ganger saksbehandler har korrigert inntekter").register()
43+
Counter.builder().name(INNTEKT_KORRIGERING).help("Antall ganger saksbehandler har korrigert inntekter").register()
4544
const val INNTEKT_OPPFRISKING = "inntekt_oppfrisking"
4645
private val inntektOppfriskingCounter =
47-
Counter.build().name(INNTEKT_OPPFRISKING).help("Antall ganger saksbehandler har oppdatert inntekter").register()
46+
Counter.builder().name(INNTEKT_OPPFRISKING).help("Antall ganger saksbehandler har oppdatert inntekter").register()
4847
const val INNTEKT_OPPFRISKING_BRUKT = "inntekt_oppfrisking_brukt"
4948
private val inntektOppfriskingBruktCounter =
50-
Counter.build().name(INNTEKT_OPPFRISKING_BRUKT).help("Antall ganger saksbehandler har brukt oppdaterte inntekter")
49+
Counter.builder().name(INNTEKT_OPPFRISKING_BRUKT).help("Antall ganger saksbehandler har brukt oppdaterte inntekter")
5150
.register()
5251

5352
fun Route.uklassifisertInntekt(

dp-inntekt-api/src/test/kotlin/no/nav/dagpenger/inntekt/inntektskomponenten/v1/InntektskomponentHttpClientTest.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import com.github.tomakehurst.wiremock.core.WireMockConfiguration
1111
import com.github.tomakehurst.wiremock.matching.AnythingPattern
1212
import com.github.tomakehurst.wiremock.matching.EqualToPattern
1313
import io.kotest.matchers.booleans.shouldBeTrue
14-
import io.kotest.matchers.doubles.shouldBeGreaterThan
1514
import io.kotest.matchers.shouldBe
16-
import io.kotest.matchers.shouldNotBe
17-
import io.prometheus.client.CollectorRegistry
1815
import kotlinx.coroutines.runBlocking
1916
import org.junit.jupiter.api.AfterAll
2017
import org.junit.jupiter.api.BeforeAll
@@ -83,7 +80,6 @@ internal class InntektskomponentHttpClientTest {
8380
}
8481

8582
assertEquals("99999999999", hentInntektListeResponse.ident.identifikator)
86-
shouldBeCounted(metricName = INNTEKTSKOMPONENT_CLIENT_SECONDS_METRICNAME)
8783
}
8884

8985
@Test
@@ -203,8 +199,6 @@ internal class InntektskomponentHttpClientTest {
203199
status = 500,
204200
message = "Tidsavbrudd mot inntektskomponenten.",
205201
)
206-
207-
shouldBeCounted(metricName = INNTEKTSKOMPONENT_FETCH_ERROR)
208202
}
209203

210204
@Test
@@ -273,11 +267,3 @@ private inline fun <reified T : InntektskomponentenHttpClientException> Result<I
273267
message?.let { exception.message shouldBe message }
274268
detail?.let { exception.detail shouldBe detail }
275269
}
276-
277-
private fun shouldBeCounted(metricName: String) {
278-
CollectorRegistry.defaultRegistry.metricFamilySamples().asSequence().find { it.name == metricName }
279-
?.let { metric ->
280-
metric.samples[0].value shouldNotBe null
281-
metric.samples[0].value shouldBeGreaterThan 0.0
282-
}
283-
}

dp-inntekt-api/src/test/kotlin/no/nav/dagpenger/inntekt/subsumsjonbrukt/VaktmesterTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package no.nav.dagpenger.inntekt.subsumsjonbrukt
22

3-
import io.kotest.matchers.doubles.shouldBeGreaterThan
43
import io.kotest.matchers.shouldBe
54
import io.kotest.matchers.shouldNotBe
6-
import io.prometheus.client.CollectorRegistry
5+
import io.prometheus.metrics.model.registry.PrometheusRegistry
76
import no.nav.dagpenger.inntekt.Postgres.withMigratedDb
87
import no.nav.dagpenger.inntekt.db.InntektNotFoundException
98
import no.nav.dagpenger.inntekt.db.Inntektparametre
@@ -122,10 +121,10 @@ internal class VaktmesterTest {
122121
assertThrows<InntektNotFoundException> { inntektStore.getInntekt(ubruktEldreEnn180Dager.inntektId) }
123122
inntektStore.getInntekt(ubruktYngreEnn180Dager.inntektId) shouldBe ubruktYngreEnn180Dager
124123
}
125-
CollectorRegistry.defaultRegistry.metricFamilySamples().asSequence().find { it.name == "inntekt_slettet" }
124+
PrometheusRegistry.defaultRegistry.scrape().find { it.metadata.name == "inntekt_slettet" }
126125
?.let { metric ->
127-
metric.samples[0].value shouldNotBe null
128-
metric.samples[0].value shouldBeGreaterThan 0.0
126+
metric.dataPoints[0].labels shouldNotBe null
127+
metric.dataPoints[0].scrapeTimestampMillis shouldNotBe null
129128
} ?: AssertionError("Could not find metric")
130129
}
131130

0 commit comments

Comments
 (0)