Skip to content

Commit 776d206

Browse files
author
yuqianwei
committed
[Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway #736
1 parent d8192e5 commit 776d206

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v20x/GatewayFilterInterceptor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class GatewayFilterInterceptor implements InstanceMethodsAroundIntercepto
3939
@Override
4040
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
4141
MethodInterceptResult result) throws Throwable {
42-
STACK_DEEP.get().getAndIncrement();
4342
if (isEntry()) {
4443
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
4544

@@ -67,10 +66,8 @@ public static EnhancedInstance getInstance(Object o) {
6766
@Override
6867
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
6968
Object ret) throws Throwable {
70-
STACK_DEEP.get().getAndDecrement();
71-
if (ContextManager.isActive()) {
72-
if (isExit()) {
73-
STACK_DEEP.remove();
69+
if (isExit()) {
70+
if (ContextManager.isActive()) {
7471
ContextManager.stopSpan();
7572
}
7673
}
@@ -84,10 +81,14 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
8481
}
8582

8683
private boolean isEntry() {
87-
return STACK_DEEP.get().intValue() == 1;
84+
return STACK_DEEP.get().getAndIncrement() == 0;
8885
}
8986

9087
private boolean isExit() {
91-
return STACK_DEEP.get().intValue() <= 0;
88+
boolean isExit = STACK_DEEP.get().decrementAndGet() == 0;
89+
if (isExit) {
90+
STACK_DEEP.remove();
91+
}
92+
return isExit;
9293
}
9394
}

apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v21x/GatewayFilterInterceptor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class GatewayFilterInterceptor implements InstanceMethodsAroundIntercepto
3939
@Override
4040
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
4141
MethodInterceptResult result) throws Throwable {
42-
STACK_DEEP.get().getAndIncrement();
4342
if (isEntry()) {
4443
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
4544

@@ -67,10 +66,8 @@ public static EnhancedInstance getInstance(Object o) {
6766
@Override
6867
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
6968
Object ret) throws Throwable {
70-
STACK_DEEP.get().getAndDecrement();
71-
if (ContextManager.isActive()) {
72-
if (isExit()) {
73-
STACK_DEEP.remove();
69+
if (isExit()) {
70+
if (ContextManager.isActive()) {
7471
ContextManager.stopSpan();
7572
}
7673
}
@@ -84,10 +81,14 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
8481
}
8582

8683
private boolean isEntry() {
87-
return STACK_DEEP.get().intValue() == 1;
84+
return STACK_DEEP.get().getAndIncrement() == 0;
8885
}
8986

9087
private boolean isExit() {
91-
return STACK_DEEP.get().intValue() <= 0;
88+
boolean isExit = STACK_DEEP.get().decrementAndGet() == 0;
89+
if (isExit) {
90+
STACK_DEEP.remove();
91+
}
92+
return isExit;
9293
}
9394
}

apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v3x/GatewayFilterInterceptor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class GatewayFilterInterceptor implements InstanceMethodsAroundIntercepto
3939
@Override
4040
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
4141
MethodInterceptResult result) throws Throwable {
42-
STACK_DEEP.get().getAndIncrement();
4342
if (isEntry()) {
4443
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
4544

@@ -67,10 +66,8 @@ public static EnhancedInstance getInstance(Object o) {
6766
@Override
6867
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
6968
Object ret) throws Throwable {
70-
STACK_DEEP.get().getAndDecrement();
71-
if (ContextManager.isActive()) {
72-
if (isExit()) {
73-
STACK_DEEP.remove();
69+
if (isExit()) {
70+
if (ContextManager.isActive()) {
7471
ContextManager.stopSpan();
7572
}
7673
}
@@ -84,10 +81,14 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
8481
}
8582

8683
private boolean isEntry() {
87-
return STACK_DEEP.get().intValue() == 1;
84+
return STACK_DEEP.get().getAndIncrement() == 0;
8885
}
8986

9087
private boolean isExit() {
91-
return STACK_DEEP.get().intValue() <= 0;
88+
boolean isExit = STACK_DEEP.get().decrementAndGet() == 0;
89+
if (isExit) {
90+
STACK_DEEP.remove();
91+
}
92+
return isExit;
9293
}
9394
}

apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v4x/GatewayFilterInterceptor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class GatewayFilterInterceptor implements InstanceMethodsAroundIntercepto
3939
@Override
4040
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
4141
MethodInterceptResult result) throws Throwable {
42-
STACK_DEEP.get().getAndIncrement();
4342
if (isEntry()) {
4443
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
4544

@@ -67,10 +66,8 @@ public static EnhancedInstance getInstance(Object o) {
6766
@Override
6867
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
6968
Object ret) throws Throwable {
70-
STACK_DEEP.get().getAndDecrement();
71-
if (ContextManager.isActive()) {
72-
if (isExit()) {
73-
STACK_DEEP.remove();
69+
if (isExit()) {
70+
if (ContextManager.isActive()) {
7471
ContextManager.stopSpan();
7572
}
7673
}
@@ -84,10 +81,14 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
8481
}
8582

8683
private boolean isEntry() {
87-
return STACK_DEEP.get().intValue() == 1;
84+
return STACK_DEEP.get().getAndIncrement() == 0;
8885
}
8986

9087
private boolean isExit() {
91-
return STACK_DEEP.get().intValue() <= 0;
88+
boolean isExit = STACK_DEEP.get().decrementAndGet() == 0;
89+
if (isExit) {
90+
STACK_DEEP.remove();
91+
}
92+
return isExit;
9293
}
9394
}

0 commit comments

Comments
 (0)