Skip to content

Commit 3f3e4a4

Browse files
committed
Add callId to logging
1 parent 50fd827 commit 3f3e4a4

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import io.ktor.application.install
1111
import io.ktor.auth.Authentication
1212
import io.ktor.auth.jwt.JWTPrincipal
1313
import io.ktor.auth.jwt.jwt
14+
import io.ktor.features.CallId
1415
import io.ktor.features.CallLogging
1516
import io.ktor.features.ContentNegotiation
1617
import io.ktor.features.DefaultHeaders
1718
import io.ktor.features.StatusPages
19+
import io.ktor.features.callIdMdc
20+
import io.ktor.http.HttpHeaders
1821
import io.ktor.http.HttpStatusCode
1922
import io.ktor.http.auth.HttpAuthHeader
2023
import io.ktor.http.isSuccess
@@ -33,6 +36,7 @@ import io.prometheus.client.hotspot.DefaultExports
3336
import java.net.URI
3437
import java.net.URL
3538
import java.util.concurrent.TimeUnit
39+
import java.util.concurrent.atomic.AtomicLong
3640
import kotlin.concurrent.fixedRateTimer
3741
import kotlinx.coroutines.launch
3842
import kotlinx.coroutines.runBlocking
@@ -72,7 +76,8 @@ fun main() = runBlocking {
7276
.rateLimited(10, 1, TimeUnit.MINUTES)
7377
.build()
7478

75-
val authApiKeyVerifier = AuthApiKeyVerifier(ApiKeyVerifier(config.application.apiSecret), config.application.allowedApiKeys)
79+
val authApiKeyVerifier =
80+
AuthApiKeyVerifier(ApiKeyVerifier(config.application.apiSecret), config.application.allowedApiKeys)
7681

7782
val dataSource = dataSourceFrom(config)
7883
val postgresInntektStore = PostgresInntektStore(dataSource)
@@ -83,7 +88,8 @@ fun main() = runBlocking {
8388
listen()
8489
}
8590

86-
val gRpcServer = InntektGrpcServer(port = 50051, inntektStore = postgresInntektStore, apiKeyVerifier = authApiKeyVerifier)
91+
val gRpcServer =
92+
InntektGrpcServer(port = 50051, inntektStore = postgresInntektStore, apiKeyVerifier = authApiKeyVerifier)
8793

8894
launch {
8995
gRpcServer.start()
@@ -204,7 +210,9 @@ fun Application.inntektApi(
204210
}
205211
exception<InntektskomponentenHttpClientException> { cause ->
206212
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(
208216
cause.status
209217
)
210218
sikkerLogg.error(cause) { "Request failed against inntektskomponenten" }
@@ -256,15 +264,25 @@ fun Application.inntektApi(
256264
call.respond(statusCode, error)
257265
}
258266
}
267+
268+
install(CallId) {
269+
retrieveFromHeader(HttpHeaders.XRequestId)
270+
generate { newRequestId() }
271+
verify { it.isNotEmpty() }
272+
}
273+
259274
install(CallLogging) {
260275
level = Level.INFO
261276

277+
callIdMdc("x-call-id")
278+
262279
filter { call ->
263280
!call.request.path().startsWith("/isAlive") &&
264281
!call.request.path().startsWith("/isReady") &&
265282
!call.request.path().startsWith("/metrics")
266283
}
267284
}
285+
268286
install(ContentNegotiation) {
269287
moshi(moshiInstance)
270288
}
@@ -286,3 +304,6 @@ data class AuthApiKeyVerifier(private val apiKeyVerifier: ApiKeyVerifier, privat
286304
return clients.map { apiKeyVerifier.verify(payload, it) }.firstOrNull { it } ?: false
287305
}
288306
}
307+
308+
private val lastIncrement = AtomicLong()
309+
private fun newRequestId(): String = "dp-inntekt-api-${lastIncrement.incrementAndGet()}"

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ fun Route.inntekt(behandlingsInntektsGetter: BehandlingsInntektsGetter) {
2525

2626
val spesifisertInntekt = withContext(api) {
2727
behandlingsInntektsGetter.getSpesifisertInntekt(
28-
Inntektparametre(aktørId = request.aktørId, vedtakId = request.vedtakId, beregningsdato = request.beregningsDato, fødselnummer = request.fødselsnummer)
28+
Inntektparametre(
29+
aktørId = request.aktørId,
30+
vedtakId = request.vedtakId,
31+
beregningsdato = request.beregningsDato,
32+
fødselnummer = request.fødselsnummer
33+
)
2934
)
3035
}
3136

@@ -38,7 +43,12 @@ fun Route.inntekt(behandlingsInntektsGetter: BehandlingsInntektsGetter) {
3843

3944
val klassifisertInntekt = withContext(api) {
4045
behandlingsInntektsGetter.getKlassifisertInntekt(
41-
Inntektparametre(aktørId = request.aktørId, vedtakId = request.vedtakId, beregningsdato = request.beregningsDato, fødselnummer = request.fødselsnummer)
46+
Inntektparametre(
47+
aktørId = request.aktørId,
48+
vedtakId = request.vedtakId,
49+
beregningsdato = request.beregningsDato,
50+
fødselnummer = request.fødselsnummer
51+
)
4252
)
4353
}
4454

0 commit comments

Comments
 (0)