feat:support webclient and gateway report call metrics (#942)
Co-authored-by: Haotian Zhang <928016560@qq.com>pull/956/head
parent
afe8178d65
commit
47a5f73286
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.circuitbreaker.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.polaris.circuitbreaker.common.PolarisCircuitBreakerConfigBuilder;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
|
||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||
import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
||||
public class PolarisCircuitBreakerUtilsTests {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReportStatus() {
|
||||
ConsumerAPI consumerAPI = Mockito.mock(ConsumerAPI.class);
|
||||
Mockito.doNothing().when(consumerAPI).updateServiceCallResult(new ServiceCallResult());
|
||||
PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration conf = new PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration();
|
||||
PolarisCircuitBreakerUtils.reportStatus(consumerAPI, conf, new CallAbortedException("mock", new CircuitBreakerStatus.FallbackInfo(0, new HashMap<>(), "")));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.loadbalancer.reactive;
|
||||
|
||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerClientRequestTransformer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
|
||||
public class PolarisLoadBalancerClientRequestTransformer implements LoadBalancerClientRequestTransformer {
|
||||
|
||||
private final ConsumerAPI consumerAPI;
|
||||
|
||||
public PolarisLoadBalancerClientRequestTransformer(ConsumerAPI consumerAPI) {
|
||||
this.consumerAPI = consumerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientRequest transformRequest(ClientRequest request, ServiceInstance instance) {
|
||||
if (instance != null) {
|
||||
HttpHeaders headers = request.headers();
|
||||
headers.add(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID, instance.getServiceId());
|
||||
}
|
||||
return request;
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.constant;
|
||||
|
||||
/**
|
||||
* Built-in system http header fields.
|
||||
*/
|
||||
public final class HeaderConstant {
|
||||
|
||||
/**
|
||||
* The called service returns the real call result of its own processing request.
|
||||
*/
|
||||
public static final String INTERNAL_CALLEE_RET_STATUS = "internal-callee-retstatus";
|
||||
|
||||
/**
|
||||
* The name of the rule that the current limit/circiutbreaker rule takes effect.
|
||||
*/
|
||||
public static final String INTERNAL_ACTIVE_RULE_NAME = "internal-callee-activerule";
|
||||
|
||||
/**
|
||||
* The name information of the called service.
|
||||
*/
|
||||
public static final String INTERNAL_CALLEE_SERVICE_ID = "internal-callee-serviceid";
|
||||
|
||||
/**
|
||||
* The name information of the called instance host.
|
||||
*/
|
||||
public static final String INTERNAL_CALLEE_INSTANCE_HOST = "internal-callee-instance-host";
|
||||
|
||||
/**
|
||||
* The name information of the called instance port.
|
||||
*/
|
||||
public static final String INTERNAL_CALLEE_INSTANCE_PORT = "internal-callee-instance-port";
|
||||
|
||||
private HeaderConstant() {
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.util;
|
||||
|
||||
/**
|
||||
* Request Label Utils.
|
||||
*/
|
||||
public final class RequestLabelUtils {
|
||||
|
||||
private RequestLabelUtils() {
|
||||
}
|
||||
|
||||
public static String convertLabel(String label) {
|
||||
label = label.replaceAll("\"|\\{|\\}", "")
|
||||
.replaceAll(",", "|");
|
||||
return label;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.rpc.enhancement.scg;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
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.client.api.SDKContext;
|
||||
import io.netty.handler.codec.http.HttpHeaders;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.netty.http.client.HttpClientConfig;
|
||||
import reactor.netty.http.client.HttpClientResponse;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class EnhancedPolarisHttpClient extends HttpClient {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EnhancedPolarisHttpClient.class);
|
||||
|
||||
private final RpcEnhancementReporterProperties properties;
|
||||
private final SDKContext context;
|
||||
private final ConsumerAPI consumerAPI;
|
||||
private final Reporter adapter;
|
||||
private final BiConsumer<? super HttpClientResponse, ? super Throwable> handler = new BiConsumer<HttpClientResponse, Throwable>() {
|
||||
@Override
|
||||
public void accept(HttpClientResponse httpClientResponse, Throwable throwable) {
|
||||
if (Objects.isNull(consumerAPI)) {
|
||||
return;
|
||||
}
|
||||
HttpHeaders responseHeaders = httpClientResponse.responseHeaders();
|
||||
|
||||
ServiceCallResult result = new ServiceCallResult();
|
||||
result.setCallerService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE));
|
||||
result.setNamespace(MetadataContext.LOCAL_NAMESPACE);
|
||||
|
||||
Map<String, String> metadata = MetadataContextHolder.get().getLoadbalancerMetadata();
|
||||
result.setDelay(System.currentTimeMillis() - Long.parseLong(metadata.get("startTime")));
|
||||
result.setService(metadata.get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID));
|
||||
result.setHost(metadata.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST));
|
||||
result.setPort(Integer.parseInt(metadata.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT)));
|
||||
RetStatus status = RetStatus.RetSuccess;
|
||||
if (Objects.isNull(throwable)) {
|
||||
if (EnhancedPolarisHttpClient.this.adapter.apply(HttpStatus.valueOf(httpClientResponse.status()
|
||||
.code()))) {
|
||||
status = RetStatus.RetFail;
|
||||
}
|
||||
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
|
||||
responseHeaders.forEach(entry -> headers.add(entry.getKey(), entry.getValue()));
|
||||
status = adapter.getRetStatusFromRequest(headers, status);
|
||||
result.setRuleName(adapter.getActiveRuleNameFromRequest(headers));
|
||||
}
|
||||
else {
|
||||
if (throwable instanceof SocketTimeoutException) {
|
||||
status = RetStatus.RetTimeout;
|
||||
}
|
||||
}
|
||||
result.setMethod(httpClientResponse.uri());
|
||||
result.setRetCode(httpClientResponse.status().code());
|
||||
result.setRetStatus(status);
|
||||
if (Objects.nonNull(context)) {
|
||||
result.setCallerIp(context.getConfig().getGlobal().getAPI().getBindIP());
|
||||
}
|
||||
try {
|
||||
consumerAPI.updateServiceCallResult(result);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
LOG.error("update service call result fail", ex);
|
||||
}
|
||||
}
|
||||
};
|
||||
private HttpClient target;
|
||||
|
||||
public EnhancedPolarisHttpClient(
|
||||
HttpClient client,
|
||||
RpcEnhancementReporterProperties properties,
|
||||
SDKContext context,
|
||||
ConsumerAPI consumerAPI) {
|
||||
this.properties = properties;
|
||||
this.context = context;
|
||||
this.consumerAPI = consumerAPI;
|
||||
this.target = client;
|
||||
this.adapter = new Reporter(properties);
|
||||
this.registerReportHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClientConfig configuration() {
|
||||
return target.configuration();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HttpClient duplicate() {
|
||||
return new EnhancedPolarisHttpClient(target, properties, context, consumerAPI);
|
||||
}
|
||||
|
||||
private void registerReportHandler() {
|
||||
target = target.doOnRequest((request, connection) -> {
|
||||
String serviceId = request.requestHeaders().get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID);
|
||||
String host = request.requestHeaders().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST);
|
||||
String port = request.requestHeaders().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT);
|
||||
if (StringUtils.isNotBlank(serviceId)) {
|
||||
MetadataContextHolder.get().setLoadbalancer(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID, serviceId);
|
||||
MetadataContextHolder.get().setLoadbalancer(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST, host);
|
||||
MetadataContextHolder.get().setLoadbalancer(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT, port);
|
||||
MetadataContextHolder.get().setLoadbalancer("startTime", System.currentTimeMillis() + "");
|
||||
}
|
||||
|
||||
request.requestHeaders().remove(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID);
|
||||
request.requestHeaders().remove(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST);
|
||||
request.requestHeaders().remove(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT);
|
||||
});
|
||||
target = target.doOnResponse((httpClientResponse, connection) -> handler.accept(httpClientResponse, null));
|
||||
target = target.doOnResponseError(handler);
|
||||
}
|
||||
|
||||
|
||||
private static class Reporter extends AbstractPolarisReporterAdapter {
|
||||
|
||||
/**
|
||||
* Constructor With {@link RpcEnhancementReporterProperties} .
|
||||
*
|
||||
* @param reportProperties instance of {@link RpcEnhancementReporterProperties}.
|
||||
*/
|
||||
protected Reporter(RpcEnhancementReporterProperties reportProperties) {
|
||||
super(reportProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(HttpStatus httpStatus) {
|
||||
return super.apply(httpStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetStatus getRetStatusFromRequest(org.springframework.http.HttpHeaders headers, RetStatus defaultVal) {
|
||||
return super.getRetStatusFromRequest(headers, defaultVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getActiveRuleNameFromRequest(org.springframework.http.HttpHeaders headers) {
|
||||
return super.getActiveRuleNameFromRequest(headers);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.rpc.enhancement.scg;
|
||||
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
|
||||
import org.springframework.cloud.gateway.config.HttpClientCustomizer;
|
||||
|
||||
public class EnhancedPolarisHttpClientCustomizer implements HttpClientCustomizer {
|
||||
|
||||
private final RpcEnhancementReporterProperties properties;
|
||||
private final SDKContext context;
|
||||
private final ConsumerAPI consumerAPI;
|
||||
|
||||
public EnhancedPolarisHttpClientCustomizer(RpcEnhancementReporterProperties properties, SDKContext context, ConsumerAPI consumerAPI) {
|
||||
this.properties = properties;
|
||||
this.context = context;
|
||||
this.consumerAPI = consumerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClient customize(HttpClient httpClient) {
|
||||
return new EnhancedPolarisHttpClient(httpClient, properties, context, consumerAPI);
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.rpc.enhancement.scg;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.Response;
|
||||
import org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
|
||||
|
||||
public class EnhancedPolarisHttpHeadersFilter implements HttpHeadersFilter {
|
||||
|
||||
public EnhancedPolarisHttpHeadersFilter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
|
||||
Response<ServiceInstance> serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
||||
if (serviceInstanceResponse == null || !serviceInstanceResponse.hasServer()) {
|
||||
return input;
|
||||
}
|
||||
ServiceInstance instance = serviceInstanceResponse.getServer();
|
||||
write(input, HeaderConstant.INTERNAL_CALLEE_SERVICE_ID, instance.getServiceId(), true);
|
||||
write(input, HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST, instance.getHost(), true);
|
||||
write(input, HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT, instance.getPort() + "", true);
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Type type) {
|
||||
return Type.REQUEST.equals(type);
|
||||
}
|
||||
|
||||
private void write(HttpHeaders headers, String name, String value, boolean append) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
if (append) {
|
||||
headers.add(name, value);
|
||||
// these headers should be treated as a single comma separated header
|
||||
List<String> values = headers.get(name);
|
||||
String delimitedValue = StringUtils.collectionToCommaDelimitedString(values);
|
||||
headers.set(name, delimitedValue);
|
||||
}
|
||||
else {
|
||||
headers.set(name, value);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.rpc.enhancement.webclient;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||
import com.tencent.cloud.common.constant.RouterConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.RequestLabelUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
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 com.tencent.polaris.client.api.SDKContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.util.context.Context;
|
||||
import reactor.util.context.ContextView;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
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 static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
|
||||
|
||||
public class EnhancedWebClientReporter extends AbstractPolarisReporterAdapter implements ExchangeFilterFunction {
|
||||
|
||||
protected static final String METRICS_WEBCLIENT_START_TIME = EnhancedWebClientReporter.class.getName()
|
||||
+ ".START_TIME";
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedWebClientReporter.class);
|
||||
private final ConsumerAPI consumerAPI;
|
||||
|
||||
private final SDKContext context;
|
||||
|
||||
public EnhancedWebClientReporter(RpcEnhancementReporterProperties reportProperties, SDKContext context, ConsumerAPI consumerAPI) {
|
||||
super(reportProperties);
|
||||
this.context = context;
|
||||
this.consumerAPI = consumerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
|
||||
return next.exchange(request).as((responseMono) -> instrumentResponse(request, responseMono))
|
||||
.contextWrite(this::putStartTime);
|
||||
}
|
||||
|
||||
Mono<ClientResponse> instrumentResponse(ClientRequest request, Mono<ClientResponse> responseMono) {
|
||||
return Mono.deferContextual((ctx) -> responseMono.doOnEach((signal) -> {
|
||||
// report result to polaris
|
||||
if (!reportProperties.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
ServiceCallResult callResult = new ServiceCallResult();
|
||||
Long startTime = getStartTime(ctx);
|
||||
callResult.setDelay(System.currentTimeMillis() - startTime);
|
||||
|
||||
callResult.setNamespace(MetadataContext.LOCAL_NAMESPACE);
|
||||
callResult.setService(request.headers().getFirst(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID));
|
||||
String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
|
||||
String sourceService = MetadataContext.LOCAL_SERVICE;
|
||||
if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
|
||||
callResult.setCallerService(new ServiceKey(sourceNamespace, sourceService));
|
||||
}
|
||||
|
||||
Collection<String> labels = request.headers().get(RouterConstant.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);
|
||||
}
|
||||
callResult.setLabels(RequestLabelUtils.convertLabel(label));
|
||||
}
|
||||
|
||||
URI uri = request.url();
|
||||
callResult.setMethod(uri.getPath());
|
||||
callResult.setHost(uri.getHost());
|
||||
// -1 means access directly by url, and use http default port number 80
|
||||
callResult.setPort(uri.getPort() == -1 ? 80 : uri.getPort());
|
||||
if (Objects.nonNull(context)) {
|
||||
callResult.setCallerIp(context.getConfig().getGlobal().getAPI().getBindIP());
|
||||
}
|
||||
|
||||
RetStatus retStatus = RetStatus.RetSuccess;
|
||||
ClientResponse response = signal.get();
|
||||
if (Objects.nonNull(response)) {
|
||||
HttpHeaders headers = response.headers().asHttpHeaders();
|
||||
|
||||
callResult.setRuleName(getActiveRuleNameFromRequest(headers));
|
||||
if (apply(response.statusCode())) {
|
||||
retStatus = RetStatus.RetFail;
|
||||
}
|
||||
retStatus = getRetStatusFromRequest(headers, retStatus);
|
||||
}
|
||||
if (signal.isOnError()) {
|
||||
Throwable throwable = signal.getThrowable();
|
||||
if (throwable instanceof SocketTimeoutException) {
|
||||
retStatus = RetStatus.RetTimeout;
|
||||
}
|
||||
}
|
||||
callResult.setRetStatus(retStatus);
|
||||
|
||||
consumerAPI.updateServiceCallResult(callResult);
|
||||
}));
|
||||
}
|
||||
|
||||
private Long getStartTime(ContextView context) {
|
||||
return context.get(METRICS_WEBCLIENT_START_TIME);
|
||||
}
|
||||
|
||||
private Context putStartTime(Context context) {
|
||||
return context.put(METRICS_WEBCLIENT_START_TIME, System.currentTimeMillis());
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.rpc.enhancement.scg;
|
||||
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
|
||||
public class EnhancedPolarisHttpClientCustomizerTest {
|
||||
|
||||
@Test
|
||||
public void testCustomize() {
|
||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||
properties.setEnabled(true);
|
||||
properties.getStatuses().clear();
|
||||
properties.getSeries().clear();
|
||||
|
||||
SDKContext context = Mockito.mock(SDKContext.class);
|
||||
ConsumerAPI consumerAPI = Mockito.mock(ConsumerAPI.class);
|
||||
|
||||
EnhancedPolarisHttpClientCustomizer clientCustomizer = new EnhancedPolarisHttpClientCustomizer(properties, context, consumerAPI);
|
||||
|
||||
HttpClient client = HttpClient.create();
|
||||
HttpClient proxyClient = clientCustomizer.customize(client);
|
||||
|
||||
Assertions.assertNotNull(proxyClient);
|
||||
Assertions.assertEquals(EnhancedPolarisHttpClient.class.getName(), proxyClient.getClass().getName());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.rpc.enhancement.scg;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
|
||||
|
||||
public class EnhancedPolarisHttpHeadersFilterTest {
|
||||
|
||||
@Test
|
||||
public void testFilter() {
|
||||
EnhancedPolarisHttpHeadersFilter filter = new EnhancedPolarisHttpHeadersFilter();
|
||||
ServiceInstance instance = Mockito.mock(ServiceInstance.class);
|
||||
Mockito.doReturn("mock_service").when(instance).getServiceId();
|
||||
Mockito.doReturn("127.0.0.1").when(instance).getHost();
|
||||
Mockito.doReturn(8080).when(instance).getPort();
|
||||
DefaultResponse response = new DefaultResponse(instance);
|
||||
ServerWebExchange exchange = Mockito.mock(ServerWebExchange.class);
|
||||
Mockito.doReturn(response).when(exchange).getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
||||
HttpHeaders input = new HttpHeaders();
|
||||
HttpHeaders headers = filter.filter(input, exchange);
|
||||
Assertions.assertEquals(Collections.singletonList("mock_service"), headers.get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID));
|
||||
Assertions.assertEquals(Collections.singletonList("127.0.0.1"), headers.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST));
|
||||
Assertions.assertEquals(Collections.singletonList("8080"), headers.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT));
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.rpc.enhancement.webclient;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.webclient.EnhancedWebClientReporter.METRICS_WEBCLIENT_START_TIME;
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
||||
public class EnhancedWebClientReporterTest {
|
||||
|
||||
private static final String URI_TEMPLATE_ATTRIBUTE = EnhancedWebClientReporterTest.class.getName() + ".uriTemplate";
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstrumentResponse() {
|
||||
ClientResponse response = Mockito.mock(ClientResponse.class);
|
||||
ClientResponse.Headers headers = Mockito.mock(ClientResponse.Headers.class);
|
||||
Mockito.doReturn(headers).when(response).headers();
|
||||
Mockito.doReturn(new HttpHeaders()).when(headers).asHttpHeaders();
|
||||
Mono<ClientResponse> responseMono = Mono.just(response);
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.org/projects/spring-boot"))
|
||||
.attribute(URI_TEMPLATE_ATTRIBUTE, "https://example.org/projects/{project}")
|
||||
.build();
|
||||
|
||||
ConsumerAPI consumerAPI = Mockito.mock(ConsumerAPI.class);
|
||||
Mockito.doAnswer(invocationOnMock -> {
|
||||
ServiceCallResult result = invocationOnMock.getArgument(0, ServiceCallResult.class);
|
||||
Assertions.assertTrue(result.getDelay() > 0);
|
||||
return null;
|
||||
}).when(consumerAPI)
|
||||
.updateServiceCallResult(Mockito.any(ServiceCallResult.class));
|
||||
|
||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||
properties.setEnabled(true);
|
||||
properties.getStatuses().clear();
|
||||
properties.getSeries().clear();
|
||||
EnhancedWebClientReporter reporter = new EnhancedWebClientReporter(properties, null, consumerAPI);
|
||||
|
||||
reporter.instrumentResponse(request, responseMono)
|
||||
.contextWrite(context -> context.put(METRICS_WEBCLIENT_START_TIME, System.currentTimeMillis()))
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue