Skip to content

Commit

Permalink
web trace
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Aug 16, 2024
1 parent 4aba741 commit f5564ef
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 64 deletions.
4 changes: 2 additions & 2 deletions rxlib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<jsch.version>0.1.55</jsch.version>

<spring.boot.version>2.5.15</spring.boot.version>
<spring.version>5.3.33</spring.version>
<spring.version>5.3.27</spring.version>
<aspectj.version>1.9.7</aspectj.version>
</properties>

Expand All @@ -57,7 +57,7 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface EnableTrace {
public @interface EnableLog {
boolean doValidate() default false;
}
22 changes: 2 additions & 20 deletions rxlib/src/main/java/org/rx/spring/BaseInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import io.netty.util.concurrent.FastThreadLocal;
import org.apache.commons.lang3.BooleanUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.rx.core.*;
import org.rx.core.Reflects;
import org.rx.core.Sys;

import static org.rx.core.Extends.as;
import static org.rx.core.Sys.*;
Expand All @@ -15,28 +15,11 @@ public abstract class BaseInterceptor {
static final FastThreadLocal<Boolean> idempotent = new FastThreadLocal<>();
protected CallLogBuilder logBuilder = Sys.DEFAULT_LOG_BUILDER;

protected final void enableTrace(String traceName) {
if (traceName == null) {
traceName = Constants.DEFAULT_TRACE_NAME;
}
RxConfig.ThreadPoolConfig conf = RxConfig.INSTANCE.getThreadPool();
conf.setTraceName(traceName);
ThreadPool.onTraceIdChanged.first((s, e) -> logCtx(conf.getTraceName(), e.getValue()));
}

protected String startTrace(JoinPoint joinPoint, String parentTraceId) {
return ThreadPool.startTrace(parentTraceId);
}

public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
if (BooleanUtils.isTrue(idempotent.get())) {
return joinPoint.proceed();
}
idempotent.set(Boolean.TRUE);
String tn = RxConfig.INSTANCE.getThreadPool().getTraceName();
if (tn != null) {
logCtxIfAbsent(tn, startTrace(joinPoint, null));
}
try {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = as(signature, MethodSignature.class);
Expand All @@ -53,7 +36,6 @@ public Object invoke() throws Throwable {
}
}, logBuilder, null);
} finally {
ThreadPool.endTrace();
clearLogCtx();
idempotent.remove();
}
Expand Down
31 changes: 5 additions & 26 deletions rxlib/src/main/java/org/rx/spring/Interceptors.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.rx.spring;

import lombok.SneakyThrows;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.ConstructorSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.rx.annotation.EnableTrace;
import org.rx.annotation.EnableLog;
import org.rx.bean.Tuple;
import org.rx.core.*;
import org.rx.exception.ApplicationException;
Expand Down Expand Up @@ -63,22 +62,6 @@ protected Object methodAround(ProceedingJoinPoint joinPoint, Tuple<HttpServletRe
return super.doAround(joinPoint);
}

@Override
protected String startTrace(JoinPoint joinPoint, String parentTraceId) {
Tuple<HttpServletRequest, HttpServletResponse> httpEnv = httpEnv();
if (httpEnv == null) {
return super.startTrace(joinPoint, parentTraceId);
}

String tn = RxConfig.INSTANCE.getThreadPool().getTraceName();
if (parentTraceId == null) {
parentTraceId = httpEnv.left.getHeader(tn);
}
String tid = super.startTrace(joinPoint, parentTraceId);
httpEnv.right.setHeader(tn, tid);
return tid;
}

@Override
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Tuple<HttpServletRequest, HttpServletResponse> httpEnv = httpEnv();
Expand Down Expand Up @@ -149,12 +132,8 @@ public Object onException(Throwable e, HttpServletRequest request) {

@Aspect
@Component
public static class TraceInterceptor extends BaseInterceptor {
public void setTraceName(String traceName) {
super.enableTrace(traceName);
}

@Around("@annotation(org.rx.annotation.EnableTrace) || @within(org.rx.annotation.EnableTrace)")
public static class LogInterceptor extends BaseInterceptor {
@Around("@annotation(org.rx.annotation.EnableLog) || @within(org.rx.annotation.EnableLog)")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Signature signature = joinPoint.getSignature();
if (signature instanceof ConstructorSignature) {
Expand All @@ -173,9 +152,9 @@ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
}

protected boolean doValidate(Executable r) {
EnableTrace a = r.getAnnotation(EnableTrace.class);
EnableLog a = r.getAnnotation(EnableLog.class);
if (a == null) {
a = r.getDeclaringClass().getAnnotation(EnableTrace.class);
a = r.getDeclaringClass().getAnnotation(EnableLog.class);
}
return a != null && a.doValidate();
}
Expand Down
14 changes: 0 additions & 14 deletions rxlib/src/main/java/org/rx/spring/MxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,6 @@ public Object health(HttpServletRequest request) {
}
}
return target;
// case 4:
// Class<?> ft = Class.forName(request.getParameter("ft"));
// String fn = request.getParameter("fn");
// String fu = request.getParameter("fu");
// Map<Class<?>, Map<String, String>> fms = RxConfig.INSTANCE.getMxHttpForwards();
// if (fu == null) {
// Map<String, String> fts = fms.get(ft);
// if (fts != null) {
// fts.remove(fn);
// }
// } else {
// fms.computeIfAbsent(ft, k -> new ConcurrentHashMap<>(8)).put(fn, fu);
// }
// return fms;
case 5:
return Linq.from(InetAddress.getAllByName(request.getParameter("host"))).select(p -> p.getHostAddress()).toArray();
case 6:
Expand Down
59 changes: 59 additions & 0 deletions rxlib/src/main/java/org/rx/spring/MxWebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.rx.spring;

import lombok.RequiredArgsConstructor;
import org.rx.core.Constants;
import org.rx.core.RxConfig;
import org.rx.core.Strings;
import org.rx.core.ThreadPool;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.rx.core.Sys.logCtx;

@RequiredArgsConstructor
@Configuration
public class MxWebConfig implements WebMvcConfigurer {
@Component
public static class WebTracer implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
RxConfig.ThreadPoolConfig conf = RxConfig.INSTANCE.getThreadPool();
String traceName = conf.getTraceName();
if (Strings.isEmpty(traceName)) {
return true;
}

String parentTraceId = request.getHeader(traceName);
String traceId = ThreadPool.startTrace(parentTraceId);
response.setHeader(traceName, traceId);
return true;
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
ThreadPool.endTrace();
}
}

public static void enableTrace(String traceName) {
if (traceName == null) {
traceName = Constants.DEFAULT_TRACE_NAME;
}
RxConfig.ThreadPoolConfig conf = RxConfig.INSTANCE.getThreadPool();
conf.setTraceName(traceName);
ThreadPool.onTraceIdChanged.first((s, e) -> logCtx(conf.getTraceName(), e.getValue()));
}

final WebTracer tracer;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(tracer).addPathPatterns("/**");
}
}
2 changes: 1 addition & 1 deletion rxlib/src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="springContext" class="org.rx.spring.SpringContext"/>
<!-- <bean id="logInterceptor" class="org.rx.spring.Interceptors.TraceInterceptor"/>-->
<!-- <bean id="logInterceptor" class="org.rx.spring.Interceptors.LogInterceptor"/>-->
<!-- <bean id="controllerInterceptor" class="org.rx.spring.ControllerInterceptor"/>-->
<!-- execution(public * org.rx..*.*(..)) and !execution(* org.rx.util.Validator.*(..))-->
<aop:config proxy-target-class="true" expose-proxy="true">
Expand Down

0 comments on commit f5564ef

Please sign in to comment.