Report the labels in request when report the result of invocation by Feign (#546)

Co-authored-by: Haotian Zhang <928016560@qq.com>
pull/547/head
pandaapo 2 years ago committed by GitHub
parent b0ff775e84
commit 0a2b17271e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,3 +19,4 @@
- [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/535)
- [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/541)
- [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/543)
- [Fix issue: report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/546)

@ -28,11 +28,11 @@ import java.util.Optional;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.reactive.LoadBalancerCommand;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterConstants;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer;

@ -27,11 +27,11 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.polaris.router.RouterConstants;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.FeignRouterLabelResolver;
import feign.RequestInterceptor;

@ -26,12 +26,12 @@ import java.util.Map;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.loadbalancer.ILoadBalancer;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterConstants;
import com.tencent.cloud.polaris.router.SimpleLoadBalancer;
import org.junit.Assert;
import org.junit.Test;

@ -27,12 +27,12 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.polaris.router.RouterConstants;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.FeignRouterLabelResolver;
import feign.RequestTemplate;

@ -17,14 +17,24 @@
package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.Collection;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.rpc.ServiceCallResult;
import com.tencent.polaris.api.utils.CollectionUtils;
import feign.Request;
import feign.RequestTemplate;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
/**
* Util for polaris reporter.
@ -33,6 +43,8 @@ import org.apache.commons.lang.StringUtils;
*/
public final class ReporterUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ReporterUtils.class);
private ReporterUtils() {
}
@ -40,7 +52,19 @@ public final class ReporterUtils {
ServiceCallResult resultRequest = new ServiceCallResult();
resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
String serviceName = request.requestTemplate().feignTarget().name();
RequestTemplate requestTemplate = request.requestTemplate();
String serviceName = requestTemplate.feignTarget().name();
Collection<String> labels = requestTemplate.headers().get(RouterConstants.ROUTER_LABEL_HEADER);
if (CollectionUtils.isNotEmpty(labels) && labels.iterator().hasNext()) {
String label = labels.iterator().next();
try {
label = URLDecoder.decode(label, UTF_8);
}
catch (UnsupportedEncodingException e) {
LOGGER.error("unsupported charset exception " + UTF_8, e);
}
resultRequest.setLabels(convertLabel(label));
}
resultRequest.setService(serviceName);
URI uri = URI.create(request.url());
resultRequest.setMethod(uri.getPath());
@ -52,7 +76,12 @@ public final class ReporterUtils {
}
resultRequest.setHost(uri.getHost());
resultRequest.setPort(uri.getPort());
return resultRequest;
}
private static String convertLabel(String label) {
label = label.replaceAll("\"|\\{|\\}", "")
.replaceAll(",", "|");
return label;
}
}

@ -17,6 +17,10 @@
package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.polaris.api.pojo.RetStatus;
@ -33,6 +37,7 @@ import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
@ -77,6 +82,12 @@ public class ReporterUtilsTest {
// mock RequestTemplate.class
RequestTemplate requestTemplate = new RequestTemplate();
requestTemplate.feignTarget(target);
try {
requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER, URLEncoder.encode("{\"k1\":\"v1\",\"k2\":\"v2\"}", UTF_8));
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported charset exception " + UTF_8);
}
// mock request
Request request = mock(Request.class);
@ -92,5 +103,6 @@ public class ReporterUtilsTest {
assertThat(serviceCallResult.getMethod()).isEqualTo("/path");
assertThat(serviceCallResult.getCallerService().getNamespace()).isEqualTo(NAMESPACE_TEST);
assertThat(serviceCallResult.getCallerService().getService()).isEqualTo(SERVICE_PROVIDER);
assertThat(serviceCallResult.getLabels()).isEqualTo("k1:v1|k2:v2");
}
}

Loading…
Cancel
Save