|
1 | 1 | package no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver
|
2 | 2 |
|
3 |
| -import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde |
4 |
| -import io.ktor.server.application.* |
5 |
| -import io.ktor.server.engine.* |
6 |
| -import io.ktor.server.metrics.micrometer.* |
7 |
| -import io.ktor.server.netty.* |
8 |
| -import io.ktor.server.routing.* |
9 |
| -import io.micrometer.core.instrument.binder.MeterBinder |
10 |
| -import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics |
11 |
| -import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics |
| 3 | +import io.ktor.server.application.Application |
| 4 | +import io.ktor.server.engine.addShutdownHook |
| 5 | +import io.ktor.server.engine.embeddedServer |
| 6 | +import io.ktor.server.netty.Netty |
| 7 | +import io.ktor.server.routing.routing |
12 | 8 | import io.micrometer.core.instrument.binder.kafka.KafkaStreamsMetrics
|
13 |
| -import io.micrometer.core.instrument.binder.system.ProcessorMetrics |
14 |
| -import io.micrometer.prometheusmetrics.PrometheusConfig |
15 |
| -import io.micrometer.prometheusmetrics.PrometheusMeterRegistry |
16 |
| -import no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver.applogic.applicationTopology |
17 |
| -import no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver.applogic.varselbygger.VarselMeldingBygger |
18 |
| -import no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver.config.kafkaTopics |
19 |
| -import no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver.config.minSideVarselKonfigurasjon |
20 |
| -import no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver.vo.InternTilstand |
21 |
| -import no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver.vo.StateStoreName |
22 |
| -import no.nav.paw.config.env.currentRuntimeEnvironment |
23 |
| -import no.nav.paw.config.hoplite.loadNaisOrLocalConfiguration |
24 |
| -import no.nav.paw.kafka.config.KAFKA_STREAMS_CONFIG_WITH_SCHEME_REG |
25 |
| -import no.nav.paw.kafka.config.KafkaConfig |
26 |
| -import no.nav.paw.kafka.factory.KafkaStreamsFactory |
27 |
| -import no.nav.paw.error.handler.withApplicationTerminatingExceptionHandler |
28 |
| -import no.nav.paw.health.listener.withHealthIndicatorStateListener |
29 |
| -import no.nav.paw.health.model.HealthStatus |
30 |
| -import no.nav.paw.health.model.LivenessHealthIndicator |
31 |
| -import no.nav.paw.health.model.ReadinessHealthIndicator |
32 |
| -import no.nav.paw.health.repository.HealthIndicatorRepository |
| 9 | +import no.nav.paw.arbeidssoekerregisteret.bekreftelse.minsideoppgaver.context.ApplicationContext |
| 10 | +import no.nav.paw.config.env.appNameOrDefaultForLocal |
33 | 11 | import no.nav.paw.health.route.healthRoutes
|
34 |
| -import org.apache.kafka.common.serialization.Serdes |
35 |
| -import org.apache.kafka.streams.KafkaStreams |
36 |
| -import org.apache.kafka.streams.StreamsBuilder |
37 |
| -import org.apache.kafka.streams.state.Stores |
38 |
| -import org.slf4j.LoggerFactory |
| 12 | +import no.nav.paw.kafka.plugin.installKafkaStreamsPlugins |
| 13 | +import no.nav.paw.logging.logger.buildApplicationLogger |
| 14 | +import no.nav.paw.metrics.plugin.installMetricsPlugin |
| 15 | +import no.nav.paw.metrics.route.metricsRoutes |
39 | 16 |
|
40 |
| -const val APP_SUFFIX = "beta-v2" |
41 |
| -val STATE_STORE_NAME: StateStoreName = StateStoreName("internal_state") |
42 |
| - |
43 |
| -val appLogger = LoggerFactory.getLogger("main") |
44 | 17 | fun main() {
|
45 |
| - appLogger.info("Starter...") |
46 |
| - val kafkaTopics = kafkaTopics() |
47 |
| - val streamsBuilder = StreamsBuilder() |
48 |
| - .addStateStore( |
49 |
| - Stores.keyValueStoreBuilder( |
50 |
| - Stores.persistentKeyValueStore(STATE_STORE_NAME.value), |
51 |
| - Serdes.UUID(), |
52 |
| - jacksonSerde<InternTilstand>() |
53 |
| - ) |
54 |
| - ) |
55 |
| - val kafkaConfig = loadNaisOrLocalConfiguration<KafkaConfig>(KAFKA_STREAMS_CONFIG_WITH_SCHEME_REG) |
56 |
| - val streamsFactory = KafkaStreamsFactory(APP_SUFFIX, kafkaConfig) |
57 |
| - .withDefaultKeySerde(Serdes.Long()::class) |
58 |
| - .withDefaultValueSerde(SpecificAvroSerde::class) |
59 |
| - .withExactlyOnce() |
60 |
| - val stream = KafkaStreams(streamsBuilder.applicationTopology( |
61 |
| - varselMeldingBygger = VarselMeldingBygger( |
62 |
| - runtimeEnvironment = currentRuntimeEnvironment, |
63 |
| - minSideVarselKonfigurasjon = minSideVarselKonfigurasjon() |
64 |
| - ), |
65 |
| - kafkaTopics = kafkaTopics, |
66 |
| - stateStoreName = STATE_STORE_NAME), streamsFactory.properties |
67 |
| - ) |
68 |
| - stream.withApplicationTerminatingExceptionHandler() |
| 18 | + val logger = buildApplicationLogger |
| 19 | + |
| 20 | + val applicationContext = ApplicationContext.build() |
| 21 | + val appName = applicationContext.serverConfig.runtimeEnvironment.appNameOrDefaultForLocal() |
69 | 22 |
|
70 |
| - val registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT) |
71 |
| - val healthIndicatorRepository = HealthIndicatorRepository() |
72 |
| - val livenessHealthIndicator = healthIndicatorRepository.addLivenessIndicator(LivenessHealthIndicator(initialStatus = HealthStatus.UNKNOWN)) |
73 |
| - val readinessHealthIndicator = healthIndicatorRepository.addReadinessIndicator(ReadinessHealthIndicator(initialStatus = HealthStatus.UNKNOWN)) |
74 |
| - stream.withHealthIndicatorStateListener( |
75 |
| - livenessIndicator = livenessHealthIndicator, |
76 |
| - readinessIndicator = readinessHealthIndicator |
77 |
| - ) |
78 |
| - appLogger.info("Starter KafkaStreams...") |
79 |
| - stream.start() |
80 |
| - appLogger.info("Starter Ktor...") |
81 |
| - embeddedServer(Netty, port = 8080) { |
82 |
| - configureMetrics( |
83 |
| - registry, |
84 |
| - KafkaStreamsMetrics(stream) |
| 23 | + with(applicationContext.serverConfig) { |
| 24 | + logger.info("Starter $appName med hostname $host og port $port") |
| 25 | + |
| 26 | + embeddedServer(factory = Netty, port = port) { |
| 27 | + module(applicationContext) |
| 28 | + }.apply { |
| 29 | + addShutdownHook { |
| 30 | + logger.info("Avslutter $appName") |
| 31 | + stop(gracePeriodMillis, timeoutMillis) |
| 32 | + } |
| 33 | + start(wait = true) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fun Application.module(applicationContext: ApplicationContext) { |
| 39 | + with(applicationContext) { |
| 40 | + installMetricsPlugin( |
| 41 | + meterRegistry = prometheusMeterRegistry, |
| 42 | + additionalMeterBinders = kafkaStreamsList.map { KafkaStreamsMetrics(it) } |
| 43 | + ) |
| 44 | + installKafkaStreamsPlugins( |
| 45 | + kafkaStreamsList = kafkaStreamsList, |
85 | 46 | )
|
86 | 47 | routing {
|
87 | 48 | healthRoutes(healthIndicatorRepository)
|
| 49 | + metricsRoutes(prometheusMeterRegistry) |
88 | 50 | }
|
89 |
| - }.start(wait = true) |
90 |
| - appLogger.info("Avslutter...") |
91 |
| -} |
92 |
| - |
93 |
| -fun Application.configureMetrics( |
94 |
| - registry: PrometheusMeterRegistry, |
95 |
| - vararg additionalBinders: MeterBinder |
96 |
| -) { |
97 |
| - install(MicrometerMetrics) { |
98 |
| - this.registry = registry |
99 |
| - meterBinders = listOf( |
100 |
| - JvmMemoryMetrics(), |
101 |
| - JvmGcMetrics(), |
102 |
| - ProcessorMetrics() |
103 |
| - ) + additionalBinders |
104 | 51 | }
|
105 | 52 | }
|
0 commit comments