feat:support webclient and gateway report call metrics

pull/1080/head
Haotian Zhang 2 years ago
parent 099508ea04
commit 2e8074b163

@ -15,4 +15,5 @@
- [fix:fix instance circuit breaker not working bug.](https://github.com/Tencent/spring-cloud-tencent/commit/ca65902a7d65bc5e4e2ccbd68b6f7cb22a346fa9) - [fix:fix instance circuit breaker not working bug.](https://github.com/Tencent/spring-cloud-tencent/commit/ca65902a7d65bc5e4e2ccbd68b6f7cb22a346fa9)
- [feature: optimize polaris-discovery-example/discovery-callee-service, add client-ip return.](https://github.com/Tencent/spring-cloud-tencent/commit/d2d66dccbef4b9b858885ae365e8f90e0e5555af) - [feature: optimize polaris-discovery-example/discovery-callee-service, add client-ip return.](https://github.com/Tencent/spring-cloud-tencent/commit/d2d66dccbef4b9b858885ae365e8f90e0e5555af)
- [docs:prevent the release of the final version of the sdk.](https://github.com/Tencent/spring-cloud-tencent/commit/be4dc8063bf8feb45d1430de979e9a85339b53c8) - [docs:prevent the release of the final version of the sdk.](https://github.com/Tencent/spring-cloud-tencent/commit/be4dc8063bf8feb45d1430de979e9a85339b53c8)
- [feature: add config for customized local port.]() - [feature: add config for customized local port.](https://github.com/Tencent/spring-cloud-tencent/commit/099508ea04687094e7ede8e5bb39eccc43287e5a)
- [feat:support webclient and gateway report call metrics]()

@ -25,6 +25,7 @@ import java.util.Set;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import com.tencent.cloud.common.constant.HeaderConstant;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties; import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties;
import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant;
@ -32,6 +33,7 @@ import com.tencent.cloud.polaris.ratelimit.resolver.RateLimitRuleArgumentReactiv
import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLimitedFallback; import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLimitedFallback;
import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils; import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils;
import com.tencent.cloud.polaris.ratelimit.utils.RateLimitUtils; import com.tencent.cloud.polaris.ratelimit.utils.RateLimitUtils;
import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.ratelimit.api.core.LimitAPI; import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.Argument; import com.tencent.polaris.ratelimit.api.rpc.Argument;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
@ -70,9 +72,9 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
private String rejectTips; private String rejectTips;
public QuotaCheckReactiveFilter(LimitAPI limitAPI, public QuotaCheckReactiveFilter(LimitAPI limitAPI,
PolarisRateLimitProperties polarisRateLimitProperties, PolarisRateLimitProperties polarisRateLimitProperties,
RateLimitRuleArgumentReactiveResolver rateLimitRuleArgumentResolver, RateLimitRuleArgumentReactiveResolver rateLimitRuleArgumentResolver,
@Nullable PolarisRateLimiterLimitedFallback polarisRateLimiterLimitedFallback) { @Nullable PolarisRateLimiterLimitedFallback polarisRateLimiterLimitedFallback) {
this.limitAPI = limitAPI; this.limitAPI = limitAPI;
this.polarisRateLimitProperties = polarisRateLimitProperties; this.polarisRateLimitProperties = polarisRateLimitProperties;
this.rateLimitRuleArgumentResolver = rateLimitRuleArgumentResolver; this.rateLimitRuleArgumentResolver = rateLimitRuleArgumentResolver;
@ -108,8 +110,7 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
response.setRawStatusCode(polarisRateLimiterLimitedFallback.rejectHttpCode()); response.setRawStatusCode(polarisRateLimiterLimitedFallback.rejectHttpCode());
response.getHeaders().setContentType(polarisRateLimiterLimitedFallback.mediaType()); response.getHeaders().setContentType(polarisRateLimiterLimitedFallback.mediaType());
dataBuffer = response.bufferFactory().allocateBuffer() dataBuffer = response.bufferFactory().allocateBuffer()
.write(polarisRateLimiterLimitedFallback.rejectTips() .write(polarisRateLimiterLimitedFallback.rejectTips().getBytes(polarisRateLimiterLimitedFallback.charset()));
.getBytes(polarisRateLimiterLimitedFallback.charset()));
} }
else { else {
response.setRawStatusCode(polarisRateLimitProperties.getRejectHttpCode()); response.setRawStatusCode(polarisRateLimitProperties.getRejectHttpCode());
@ -117,6 +118,10 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
dataBuffer = response.bufferFactory().allocateBuffer() dataBuffer = response.bufferFactory().allocateBuffer()
.write(rejectTips.getBytes(StandardCharsets.UTF_8)); .write(rejectTips.getBytes(StandardCharsets.UTF_8));
} }
response.getHeaders().add(HeaderConstant.INTERNAL_CALLEE_RET_STATUS, RetStatus.RetFlowControl.getDesc());
if (Objects.nonNull(quotaResponse.getActiveRule())) {
response.getHeaders().add(HeaderConstant.INTERNAL_ACTIVE_RULE_NAME, quotaResponse.getActiveRule().getName().getValue());
}
return response.writeWith(Mono.just(dataBuffer)); return response.writeWith(Mono.just(dataBuffer));
} }
// Unirate // Unirate

@ -115,8 +115,7 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
} }
response.addHeader(HeaderConstant.INTERNAL_CALLEE_RET_STATUS, RetStatus.RetFlowControl.getDesc()); response.addHeader(HeaderConstant.INTERNAL_CALLEE_RET_STATUS, RetStatus.RetFlowControl.getDesc());
if (Objects.nonNull(quotaResponse.getActiveRule())) { if (Objects.nonNull(quotaResponse.getActiveRule())) {
response.addHeader(HeaderConstant.INTERNAL_ACTIVE_RULE_NAME, quotaResponse.getActiveRule().getName() response.addHeader(HeaderConstant.INTERNAL_ACTIVE_RULE_NAME, quotaResponse.getActiveRule().getName().getValue());
.getValue());
} }
return; return;
} }

@ -113,11 +113,9 @@ public class QuotaCheckReactiveFilterTest {
ServiceRuleManager serviceRuleManager = mock(ServiceRuleManager.class); ServiceRuleManager serviceRuleManager = mock(ServiceRuleManager.class);
RateLimitProto.Rule.Builder ratelimitRuleBuilder = RateLimitProto.Rule.newBuilder(); RateLimitProto.Rule.Builder ratelimitRuleBuilder = RateLimitProto.Rule.newBuilder();
InputStream inputStream = QuotaCheckServletFilterTest.class.getClassLoader() InputStream inputStream = QuotaCheckServletFilterTest.class.getClassLoader().getResourceAsStream("ratelimit.json");
.getResourceAsStream("ratelimit.json"); String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining(""));
String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining(""));
JsonFormat.parser().ignoringUnknownFields().merge(json, ratelimitRuleBuilder); JsonFormat.parser().ignoringUnknownFields().merge(json, ratelimitRuleBuilder);
RateLimitProto.Rule rateLimitRule = ratelimitRuleBuilder.build(); RateLimitProto.Rule rateLimitRule = ratelimitRuleBuilder.build();
RateLimitProto.RateLimit rateLimit = RateLimitProto.RateLimit.newBuilder().addRules(rateLimitRule).build(); RateLimitProto.RateLimit rateLimit = RateLimitProto.RateLimit.newBuilder().addRules(rateLimitRule).build();

@ -69,8 +69,8 @@ import static org.mockito.Mockito.when;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = QuotaCheckServletFilterTest.TestApplication.class, classes = QuotaCheckServletFilterTest.TestApplication.class,
properties = { properties = {
"spring.cloud.polaris.namespace=Test", "spring.cloud.polaris.service=TestApp" "spring.cloud.polaris.namespace=Test", "spring.cloud.polaris.service=TestApp"
}) })
public class QuotaCheckServletFilterTest { public class QuotaCheckServletFilterTest {
private final PolarisRateLimiterLabelServletResolver labelResolver = private final PolarisRateLimiterLabelServletResolver labelResolver =
@ -113,11 +113,9 @@ public class QuotaCheckServletFilterTest {
ServiceRuleManager serviceRuleManager = mock(ServiceRuleManager.class); ServiceRuleManager serviceRuleManager = mock(ServiceRuleManager.class);
RateLimitProto.Rule.Builder ratelimitRuleBuilder = RateLimitProto.Rule.newBuilder(); RateLimitProto.Rule.Builder ratelimitRuleBuilder = RateLimitProto.Rule.newBuilder();
InputStream inputStream = QuotaCheckServletFilterTest.class.getClassLoader() InputStream inputStream = QuotaCheckServletFilterTest.class.getClassLoader().getResourceAsStream("ratelimit.json");
.getResourceAsStream("ratelimit.json"); String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining(""));
String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining(""));
JsonFormat.parser().ignoringUnknownFields().merge(json, ratelimitRuleBuilder); JsonFormat.parser().ignoringUnknownFields().merge(json, ratelimitRuleBuilder);
RateLimitProto.Rule rateLimitRule = ratelimitRuleBuilder.build(); RateLimitProto.Rule rateLimitRule = ratelimitRuleBuilder.build();
RateLimitProto.RateLimit rateLimit = RateLimitProto.RateLimit.newBuilder().addRules(rateLimitRule).build(); RateLimitProto.RateLimit rateLimit = RateLimitProto.RateLimit.newBuilder().addRules(rateLimitRule).build();

@ -38,7 +38,13 @@
"name": "spring.cloud.polaris.local-ip-address", "name": "spring.cloud.polaris.local-ip-address",
"type": "java.lang.String", "type": "java.lang.String",
"defaultValue": "", "defaultValue": "",
"description": "current server local ip address." "description": "current server local ip address to be registered."
},
{
"name": "spring.cloud.polaris.local-port",
"type": "java.lang.Integer",
"defaultValue": "",
"description": "current server local port to be registered."
} }
], ],
"hints": [] "hints": []

Loading…
Cancel
Save