Skip to content

Commit 89e688a

Browse files
committed
Legg til callId på logg kontekst
1 parent 2c6fd02 commit 89e688a

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

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

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.prometheus.client.Counter
99
import io.prometheus.client.Summary
1010
import java.time.YearMonth
1111
import mu.KotlinLogging
12+
import mu.withLoggingContext
1213
import no.nav.dagpenger.inntekt.moshiInstance
1314
import no.nav.dagpenger.oidc.OidcClient
1415

@@ -61,41 +62,47 @@ class InntektskomponentHttpClient(
6162

6263
val jsonBody = jsonRequestRequestAdapter.toJson(requestBody)
6364
val timer = clientLatencyStats.startTimer()
64-
try {
65-
val (_, response, result) = with(hentInntektlisteUrl.httpPost()) {
66-
timeout(timeouts.connectionTimeout.toMillis().toInt())
67-
timeoutRead(timeouts.readTimeout.toMillis().toInt())
68-
69-
authentication().bearer(oidcClient.oidcToken().access_token)
70-
header("Nav-Consumer-Id" to "dp-inntekt-api")
71-
header("Nav-Call-Id" to ulid.nextULID())
72-
body(jsonBody)
73-
awaitResponseResult(moshiDeserializerOf(jsonResponseAdapter))
74-
}
75-
76-
inntektskomponentStatusCodesCounter.labels(response.statusCode.toString()).inc()
77-
78-
return result.fold({
79-
it
80-
}, { error ->
81-
val resp = error.response.body().asString("application/json")
82-
val detail = runCatching {
8365

84-
jsonMapAdapter.fromJson(resp)
85-
}.let {
86-
it.getOrNull()?.get("message")?.toString() ?: error.message
66+
val callId = ulid.nextULID()
67+
withLoggingContext(
68+
"callId" to callId
69+
) {
70+
try {
71+
val (_, response, result) = with(hentInntektlisteUrl.httpPost()) {
72+
timeout(timeouts.connectionTimeout.toMillis().toInt())
73+
timeoutRead(timeouts.readTimeout.toMillis().toInt())
74+
75+
authentication().bearer(oidcClient.oidcToken().access_token)
76+
header("Nav-Consumer-Id" to "dp-inntekt-api")
77+
header("Nav-Call-Id" to callId)
78+
body(jsonBody)
79+
awaitResponseResult(moshiDeserializerOf(jsonResponseAdapter))
8780
}
8881

89-
clientFetchErrors.inc()
90-
91-
throw InntektskomponentenHttpClientException(
92-
if (response.statusCode == -1) 500 else response.statusCode, // we did not get a response status code, ie timeout/network issues
93-
"Failed to fetch inntekt. Status code ${response.statusCode}. Response message: ${response.responseMessage}. Problem message: $detail",
94-
detail
95-
)
96-
})
97-
} finally {
98-
timer.observeDuration()
82+
inntektskomponentStatusCodesCounter.labels(response.statusCode.toString()).inc()
83+
84+
return result.fold({
85+
it
86+
}, { error ->
87+
val resp = error.response.body().asString("application/json")
88+
val detail = runCatching {
89+
90+
jsonMapAdapter.fromJson(resp)
91+
}.let {
92+
it.getOrNull()?.get("message")?.toString() ?: error.message
93+
}
94+
95+
clientFetchErrors.inc()
96+
97+
throw InntektskomponentenHttpClientException(
98+
if (response.statusCode == -1) 500 else response.statusCode, // we did not get a response status code, ie timeout/network issues
99+
"Failed to fetch inntekt. Status code ${response.statusCode}. Response message: ${response.responseMessage}. Problem message: $detail",
100+
detail
101+
)
102+
})
103+
} finally {
104+
timer.observeDuration()
105+
}
99106
}
100107
}
101108
}

0 commit comments

Comments
 (0)