diff --git a/build.gradle.kts b/build.gradle.kts index 5f24d090..836d2906 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,6 +19,7 @@ val mockitoInlineVersion = "5.2.0" val testContainersVersion = "1.19.8" val mockkVersion = "1.13.10" val springMockkVersion = "4.0.2" +val otelVersion = "1.42.1" val githubUser: String by project val githubPassword: String by project @@ -52,6 +53,8 @@ dependencies { implementation("io.micrometer:micrometer-registry-prometheus") + implementation("io.opentelemetry:opentelemetry-api:$otelVersion") + implementation("org.projectreactor:reactor-spring:1.0.1.RELEASE") implementation("org.flywaydb:flyway-core") diff --git a/src/main/kotlin/no/nav/klage/config/FileClientConfiguration.kt b/src/main/kotlin/no/nav/klage/config/FileClientConfiguration.kt index e7e40bfa..f532087a 100644 --- a/src/main/kotlin/no/nav/klage/config/FileClientConfiguration.kt +++ b/src/main/kotlin/no/nav/klage/config/FileClientConfiguration.kt @@ -17,6 +17,5 @@ class FileClientConfiguration(private val webClientBuilder: WebClient.Builder) { fun fileWebClient(): WebClient = webClientBuilder .baseUrl(url) - .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) .build() } diff --git a/src/main/kotlin/no/nav/klage/config/PdlClientConfiguration.kt b/src/main/kotlin/no/nav/klage/config/PdlClientConfiguration.kt index daea8d7c..7dce0b05 100644 --- a/src/main/kotlin/no/nav/klage/config/PdlClientConfiguration.kt +++ b/src/main/kotlin/no/nav/klage/config/PdlClientConfiguration.kt @@ -23,8 +23,6 @@ class PdlClientConfiguration(private val webClientBuilder: WebClient.Builder) { fun pdlWebClient(): WebClient { return webClientBuilder .baseUrl(pdlUrl) - .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) .defaultHeader("Nav-Consumer-Id", username) .defaultHeader("TEMA", "KLA") //Fra behandlingskatalogen diff --git a/src/main/kotlin/no/nav/klage/config/SafselvbetjeningClientConfiguration.kt b/src/main/kotlin/no/nav/klage/config/SafselvbetjeningClientConfiguration.kt index fb30f252..0e106051 100644 --- a/src/main/kotlin/no/nav/klage/config/SafselvbetjeningClientConfiguration.kt +++ b/src/main/kotlin/no/nav/klage/config/SafselvbetjeningClientConfiguration.kt @@ -17,7 +17,5 @@ class SafselvbetjeningClientConfiguration(private val webClientBuilder: WebClien fun safselvbetjeningWebClient(): WebClient = webClientBuilder .baseUrl(url) - .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) .build() } diff --git a/src/main/kotlin/no/nav/klage/config/WebClientCustomizer.kt b/src/main/kotlin/no/nav/klage/config/WebClientCustomizer.kt new file mode 100644 index 00000000..ef3c9ddc --- /dev/null +++ b/src/main/kotlin/no/nav/klage/config/WebClientCustomizer.kt @@ -0,0 +1,47 @@ +package no.nav.klage.config + +import io.opentelemetry.api.trace.Span +import org.springframework.boot.web.reactive.function.client.WebClientCustomizer +import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import org.springframework.stereotype.Component +import org.springframework.web.reactive.function.client.ClientRequest +import org.springframework.web.reactive.function.client.ExchangeFilterFunction +import org.springframework.web.reactive.function.client.WebClient +import reactor.core.publisher.Mono +import reactor.netty.http.client.HttpClient + +/** + * Common configuration for all web clients. + */ +@Component +class WebClientCustomizer : WebClientCustomizer { + + override fun customize(webClientBuilder: WebClient.Builder) { + val headersWithTraceId = listOf( + "Nav-Call-Id", + "Nav-Callid", + "X-Correlation-ID", + ) + + webClientBuilder + .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .filter( + ExchangeFilterFunction.ofRequestProcessor { request -> + val traceId = Span.current().spanContext.traceId + Mono.just( + ClientRequest.from(request) + .headers { headers -> + headersWithTraceId.forEach { headerName -> + headers[headerName] = traceId + } + } + .build() + ) + } + ) + } +} \ No newline at end of file