2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
6 * ======================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
22 import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemetry;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.context.annotation.DependsOn;
27 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
28 import org.springframework.stereotype.Service;
29 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
30 import org.springframework.web.reactive.function.client.ExchangeStrategies;
31 import org.springframework.web.reactive.function.client.WebClient;
33 import reactor.core.publisher.Mono;
34 import reactor.netty.http.client.HttpClient;
36 import java.lang.invoke.MethodHandles;
37 import java.util.concurrent.atomic.AtomicInteger;
40 @DependsOn({"otelConfig"})
41 public class WebClientUtil {
43 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45 private static OtelConfig otelConfig;
47 private static SpringWebfluxTelemetry springWebfluxTelemetry;
49 public WebClientUtil(OtelConfig otelConfig, @Autowired(required = false) SpringWebfluxTelemetry springWebfluxTelemetry) {
50 WebClientUtil.otelConfig = otelConfig;
51 if (otelConfig.isTracingEnabled()) {
52 WebClientUtil.springWebfluxTelemetry = springWebfluxTelemetry;
56 public static WebClient buildWebClient(String baseURL, final HttpClient httpClient) {
58 Object traceTag = new AtomicInteger().incrementAndGet();
60 ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder() //
61 .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)) //
64 ExchangeFilterFunction reqLogger = ExchangeFilterFunction.ofRequestProcessor(req -> {
65 logger.debug("{} {} uri = '{}''", traceTag, req.method(), req.url());
66 return Mono.just(req);
69 ExchangeFilterFunction respLogger = ExchangeFilterFunction.ofResponseProcessor(resp -> {
70 logger.debug("{} resp: {}", traceTag, resp.statusCode());
71 return Mono.just(resp);
74 WebClient.Builder webClientBuilder = WebClient.builder()
75 .clientConnector(new ReactorClientHttpConnector(httpClient))
77 .exchangeStrategies(exchangeStrategies)
81 if (otelConfig.isSouthTracingEnabled()) {
82 webClientBuilder.filters(springWebfluxTelemetry::addClientTracingFilter);
85 return webClientBuilder.build();