@@ -11,10 +11,13 @@ import io.ktor.application.install
11
11
import io.ktor.auth.Authentication
12
12
import io.ktor.auth.jwt.JWTPrincipal
13
13
import io.ktor.auth.jwt.jwt
14
+ import io.ktor.features.CallId
14
15
import io.ktor.features.CallLogging
15
16
import io.ktor.features.ContentNegotiation
16
17
import io.ktor.features.DefaultHeaders
17
18
import io.ktor.features.StatusPages
19
+ import io.ktor.features.callIdMdc
20
+ import io.ktor.http.HttpHeaders
18
21
import io.ktor.http.HttpStatusCode
19
22
import io.ktor.http.auth.HttpAuthHeader
20
23
import io.ktor.http.isSuccess
@@ -33,6 +36,7 @@ import io.prometheus.client.hotspot.DefaultExports
33
36
import java.net.URI
34
37
import java.net.URL
35
38
import java.util.concurrent.TimeUnit
39
+ import java.util.concurrent.atomic.AtomicLong
36
40
import kotlin.concurrent.fixedRateTimer
37
41
import kotlinx.coroutines.launch
38
42
import kotlinx.coroutines.runBlocking
@@ -72,7 +76,8 @@ fun main() = runBlocking {
72
76
.rateLimited(10 , 1 , TimeUnit .MINUTES )
73
77
.build()
74
78
75
- val authApiKeyVerifier = AuthApiKeyVerifier (ApiKeyVerifier (config.application.apiSecret), config.application.allowedApiKeys)
79
+ val authApiKeyVerifier =
80
+ AuthApiKeyVerifier (ApiKeyVerifier (config.application.apiSecret), config.application.allowedApiKeys)
76
81
77
82
val dataSource = dataSourceFrom(config)
78
83
val postgresInntektStore = PostgresInntektStore (dataSource)
@@ -83,7 +88,8 @@ fun main() = runBlocking {
83
88
listen()
84
89
}
85
90
86
- val gRpcServer = InntektGrpcServer (port = 50051 , inntektStore = postgresInntektStore, apiKeyVerifier = authApiKeyVerifier)
91
+ val gRpcServer =
92
+ InntektGrpcServer (port = 50051 , inntektStore = postgresInntektStore, apiKeyVerifier = authApiKeyVerifier)
87
93
88
94
launch {
89
95
gRpcServer.start()
@@ -204,7 +210,9 @@ fun Application.inntektApi(
204
210
}
205
211
exception<InntektskomponentenHttpClientException > { cause ->
206
212
val statusCode =
207
- if (HttpStatusCode .fromValue(cause.status).isSuccess()) HttpStatusCode .InternalServerError else HttpStatusCode .fromValue(
213
+ if (HttpStatusCode .fromValue(cause.status)
214
+ .isSuccess()
215
+ ) HttpStatusCode .InternalServerError else HttpStatusCode .fromValue(
208
216
cause.status
209
217
)
210
218
sikkerLogg.error(cause) { " Request failed against inntektskomponenten" }
@@ -256,15 +264,25 @@ fun Application.inntektApi(
256
264
call.respond(statusCode, error)
257
265
}
258
266
}
267
+
268
+ install(CallId ) {
269
+ retrieveFromHeader(HttpHeaders .XRequestId )
270
+ generate { newRequestId() }
271
+ verify { it.isNotEmpty() }
272
+ }
273
+
259
274
install(CallLogging ) {
260
275
level = Level .INFO
261
276
277
+ callIdMdc(" x-call-id" )
278
+
262
279
filter { call ->
263
280
! call.request.path().startsWith(" /isAlive" ) &&
264
281
! call.request.path().startsWith(" /isReady" ) &&
265
282
! call.request.path().startsWith(" /metrics" )
266
283
}
267
284
}
285
+
268
286
install(ContentNegotiation ) {
269
287
moshi(moshiInstance)
270
288
}
@@ -286,3 +304,6 @@ data class AuthApiKeyVerifier(private val apiKeyVerifier: ApiKeyVerifier, privat
286
304
return clients.map { apiKeyVerifier.verify(payload, it) }.firstOrNull { it } ? : false
287
305
}
288
306
}
307
+
308
+ private val lastIncrement = AtomicLong ()
309
+ private fun newRequestId (): String = " dp-inntekt-api-${lastIncrement.incrementAndGet()} "
0 commit comments