Supporting RequestRateLimiterGatewayFilterFactory (#3538)
This commit is contained in:
parent
9996fbb5e0
commit
26b1d4156a
|
|
@ -34,6 +34,7 @@
|
|||
|
||||
<properties>
|
||||
<spring-cloud-starter-gateway.version>2.1.1.RELEASE</spring-cloud-starter-gateway.version>
|
||||
<compiler.version>1.8</compiler.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x;
|
||||
|
||||
import org.apache.skywalking.apm.agent.core.context.ContextManager;
|
||||
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
|
||||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
|
||||
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
|
||||
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.context.SWTransmitter;
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
|
||||
|
||||
/**
|
||||
* @author songxiaoyue
|
||||
*/
|
||||
public class FilteringWebHandlerInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
|
||||
private static final String SPRING_CLOUD_GATEWAY_ROUTE_PREFIX = "GATEWAY/";
|
||||
@Override
|
||||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
MethodInterceptResult result) throws Throwable {
|
||||
EnhancedInstance instance = NettyRoutingFilterInterceptor.getInstance(allArguments[0]);
|
||||
if (instance == null) {
|
||||
return;
|
||||
}
|
||||
AbstractSpan span = (AbstractSpan) instance.getSkyWalkingDynamicField();
|
||||
if (span == null) {
|
||||
return;
|
||||
}
|
||||
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
|
||||
String operationName = SPRING_CLOUD_GATEWAY_ROUTE_PREFIX;
|
||||
Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);
|
||||
operationName = operationName + route.getId();
|
||||
span.setOperationName(operationName);
|
||||
SWTransmitter transmitter = new SWTransmitter(span.prepareForAsync(),ContextManager.capture(),operationName);
|
||||
instance.setSkyWalkingDynamicField(transmitter);
|
||||
ContextManager.stopSpan(span);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
|
||||
Class<?>[] argumentsTypes, Object ret) throws Throwable {
|
||||
EnhancedInstance instance = NettyRoutingFilterInterceptor.getInstance(allArguments[0]);
|
||||
if (instance == null) {
|
||||
return ret;
|
||||
}
|
||||
SWTransmitter swTransmitter = (SWTransmitter) instance.getSkyWalkingDynamicField();
|
||||
if (swTransmitter == null) {
|
||||
return ret;
|
||||
}
|
||||
Mono<Void> mono = (Mono) ret;
|
||||
return mono.doFinally(d -> {
|
||||
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
|
||||
HttpStatus statusCode = exchange.getResponse().getStatusCode();
|
||||
if (statusCode == HttpStatus.TOO_MANY_REQUESTS) {
|
||||
AbstractSpan localSpan = ContextManager.createLocalSpan(swTransmitter.getOperationName());
|
||||
Tags.STATUS_CODE.set(localSpan,statusCode.toString());
|
||||
SpanLayer.asHttp(localSpan);
|
||||
localSpan.setComponent(ComponentsDefine.SPRING_CLOUD_GATEWAY);
|
||||
ContextManager.continued(swTransmitter.getSnapshot());
|
||||
ContextManager.stopSpan(localSpan);
|
||||
AbstractSpan spanWebflux = swTransmitter.getSpanWebflux();
|
||||
spanWebflux.asyncFinish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
|
||||
Class<?>[] argumentsTypes, Throwable t) {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -19,20 +19,15 @@
|
|||
package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x;
|
||||
|
||||
import org.apache.skywalking.apm.agent.core.context.ContextManager;
|
||||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.context.Constants;
|
||||
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.context.SWTransmitter;
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchangeDecorator;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -40,24 +35,14 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.G
|
|||
*/
|
||||
public class NettyRoutingFilterInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
|
||||
private static final String SPRING_CLOUD_GATEWAY_ROUTE_PREFIX = "GATEWAY/";
|
||||
|
||||
@Override
|
||||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
MethodInterceptResult result) throws Throwable {
|
||||
EnhancedInstance instance = NettyRoutingFilterInterceptor.getInstance(allArguments[0]);
|
||||
if (instance != null) {
|
||||
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
|
||||
AbstractSpan span = (AbstractSpan) instance.getSkyWalkingDynamicField();
|
||||
String operationName = SPRING_CLOUD_GATEWAY_ROUTE_PREFIX;
|
||||
if (span != null) {
|
||||
Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);
|
||||
operationName = operationName + route.getId();
|
||||
span.setOperationName(operationName);
|
||||
SWTransmitter transmitter = new SWTransmitter(span.prepareForAsync(), ContextManager.capture(), operationName);
|
||||
ContextManager.stopSpan(span);
|
||||
ContextManager.getRuntimeContext().put(Constants.SPRING_CLOUD_GATEWAY_TRANSMITTER, transmitter);
|
||||
}
|
||||
SWTransmitter swTransmitter = (SWTransmitter) instance.getSkyWalkingDynamicField();
|
||||
ContextManager.getRuntimeContext().put(Constants.SPRING_CLOUD_GATEWAY_TRANSMITTER, swTransmitter);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -99,4 +84,4 @@ public class NettyRoutingFilterInterceptor implements InstanceMethodsAroundInter
|
|||
throw new RuntimeException("Unknown parameter types:" + o.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.define;
|
||||
|
||||
import net.bytebuddy.description.method.MethodDescription;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
|
||||
|
||||
/**
|
||||
* @author songxiaoyue
|
||||
*/
|
||||
public class FilteringWebHandlerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
|
||||
|
||||
@Override
|
||||
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
|
||||
return new ConstructorInterceptPoint[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
|
||||
return new InstanceMethodsInterceptPoint[]{
|
||||
new InstanceMethodsInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named("handle");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return "org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.FilteringWebHandlerInterceptor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOverrideArgs() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassMatch enhanceClass() {
|
||||
return byName("org.springframework.cloud.gateway.handler.FilteringWebHandler");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -16,4 +16,5 @@
|
|||
|
||||
spring-cloud-gateway-2.1.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.define.DefaultHttpHeadersInstrumentation
|
||||
spring-cloud-gateway-2.1.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.define.HttpClientOperationsInstrumentation
|
||||
spring-cloud-gateway-2.1.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.define.NettyRoutingFilterInstrumentation
|
||||
spring-cloud-gateway-2.1.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.define.NettyRoutingFilterInstrumentation
|
||||
spring-cloud-gateway-2.1.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.define.FilteringWebHandlerInstrumentation
|
||||
|
|
@ -34,6 +34,11 @@
|
|||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
<version>${test.framework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<version>2.1.2.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 org.apache.skywalking.apm.testcase.sc.gateway.projectA;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author songxiaoyue
|
||||
*/
|
||||
@Component
|
||||
public class ApiKeyResolver implements KeyResolver{
|
||||
|
||||
public Mono<String> resolve(ServerWebExchange exchange) {
|
||||
return Mono.just(exchange.getRequest().getPath().value());
|
||||
}
|
||||
}
|
||||
|
|
@ -24,4 +24,13 @@ spring:
|
|||
- id: provider_route
|
||||
uri: http://localhost:18070
|
||||
predicates:
|
||||
- Path=/provider/b/*
|
||||
- Path=/provider/b/*
|
||||
filters:
|
||||
- name: RequestRateLimiter
|
||||
args:
|
||||
redis-rate-limiter.replenishRate: 1
|
||||
redis-rate-limiter.burstCapacity: 1
|
||||
key-resolver: "#{@apiKeyResolver}"
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
Loading…
Reference in New Issue