68d9ea71bf306c27296bb7611246143a01a4938e
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
21
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;
32
33 import reactor.core.publisher.Mono;
34 import reactor.netty.http.client.HttpClient;
35
36 import java.lang.invoke.MethodHandles;
37 import java.util.concurrent.atomic.AtomicInteger;
38
39 @Service
40 @DependsOn({"otelConfig"})
41 public class WebClientUtil {
42
43     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44
45     private static OtelConfig otelConfig;
46
47     private static SpringWebfluxTelemetry springWebfluxTelemetry;
48
49     public WebClientUtil(OtelConfig otelConfig, @Autowired(required = false) SpringWebfluxTelemetry springWebfluxTelemetry) {
50         WebClientUtil.otelConfig = otelConfig;
51         if (otelConfig.isTracingEnabled()) {
52             WebClientUtil.springWebfluxTelemetry = springWebfluxTelemetry;
53         }
54     }
55
56     public static WebClient buildWebClient(String baseURL, final HttpClient httpClient) {
57
58         Object traceTag = new AtomicInteger().incrementAndGet();
59
60         ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder() //
61                 .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)) //
62                 .build();
63
64         ExchangeFilterFunction reqLogger = ExchangeFilterFunction.ofRequestProcessor(req -> {
65             logger.debug("{} {} uri = '{}''", traceTag, req.method(), req.url());
66             return Mono.just(req);
67         });
68
69         ExchangeFilterFunction respLogger = ExchangeFilterFunction.ofResponseProcessor(resp -> {
70             logger.debug("{} resp: {}", traceTag, resp.statusCode());
71             return Mono.just(resp);
72         });
73
74         WebClient.Builder webClientBuilder = WebClient.builder()
75                 .clientConnector(new ReactorClientHttpConnector(httpClient))
76                 .baseUrl(baseURL)
77                 .exchangeStrategies(exchangeStrategies)
78                 .filter(reqLogger)
79                 .filter(respLogger);
80
81         if (otelConfig.isSouthTracingEnabled()) {
82             webClientBuilder.filters(springWebfluxTelemetry::addClientTracingFilter);
83         }
84
85         return webClientBuilder.build();
86     }
87 }