Skip to content

Commit

Permalink
add ExchangeFilterFunction logger webclient service info
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyouling committed Jul 18, 2024
1 parent 3325517 commit 12b5f6c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class GovernanceProperties {
public static final String WEBCLIENT_FAULT_INJECTION_ENABLED =
PREFIX + "." + "webclient.faultInjection.enabled";

public static final String WEBCLIENT_REQUEST_SERVICE_LOGGER_ENABLED =
PREFIX + "." + "webclient.requestServiceLogger.enabled";

public static final String GATEWAY_GOVERNANCE_ENABLED = PREFIX + "." + "gateway.governance.enabled";

public static final String GATEWAY_RETRY_ENABLED = PREFIX + "." + "gateway.retry.enabled";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.huaweicloud.governance.adapters.webclient;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFunction;

import com.huaweicloud.governance.adapters.loadbalancer.RetryContext;

import reactor.core.publisher.Mono;

public class ServiceInfoLoggerExchangeFilterFunction implements ExchangeFilterFunction, Ordered {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInfoLoggerExchangeFilterFunction.class);

@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
return next.exchange(request).map(response -> logServiceInfo(response, request));
}

private ClientResponse logServiceInfo(ClientResponse response, ClientRequest request) {
if (response.statusCode() != HttpStatusCode.valueOf(200)) {
Optional<Object> invocationContext = request.attribute(RetryContext.RETRY_SERVICE_INSTANCE);
if (invocationContext.isPresent() && invocationContext.get() instanceof ServiceInstance instance) {
LOGGER.error("request >>>>>>>>>>>>>> service {}[{}:{}] failed", instance.getServiceId(), instance.getHost(),
instance.getPort());
}
}
return response;
}

@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ public ExchangeFilterFunction faultInjectionExchangeFilterFunction(FaultInjectio
public StatusCodeExtractor clientResponseStatusCodeExtractor(Environment environment) {
return new ClientResponseStatusCodeExtractor(environment);
}

@Bean
@ConditionalOnProperty(value = GovernanceProperties.WEBCLIENT_REQUEST_SERVICE_LOGGER_ENABLED,
havingValue = "true", matchIfMissing = true)
public ServiceInfoLoggerExchangeFilterFunction serviceInfoLoggerExchangeFilterFunction() {
return new ServiceInfoLoggerExchangeFilterFunction();
}
}

0 comments on commit 12b5f6c

Please sign in to comment.