Skip to content

Commit

Permalink
Merge pull request #83 from KUTEJiang/main_traceid_dev_mingcao
Browse files Browse the repository at this point in the history
[feat](log)generate traceId for each http request(#36)
  • Loading branch information
renmao authored Nov 22, 2024
2 parents ed1f5b9 + 7e9cebd commit e15a4fb
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.alipay.muagent.model.trace;

import lombok.Data;

/**
* @author Joshua
* @version TraceContext.java v1.0 2024-11-20 20:21
**/
@Data
public class TraceContext {

private String traceId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.alipay.muagent.model.trace;

import java.util.EmptyStackException;

/**
* @author Joshua
* @version TraceThreadLocalContext.java v1.0 2024-11-20 20:24
**/
public class TraceThreadLocalContext {

private final ThreadLocal<TraceContext> traceContextThreadLocal = new ThreadLocal<>();

public void push(TraceContext traceContext) {
if (traceContext == null) {
return;
}
traceContextThreadLocal.set(traceContext);
}

public TraceContext pop() throws EmptyStackException {
if (this.isEmpty()) {
return null;
}
TraceContext traceContext = traceContextThreadLocal.get();
this.clear();
return traceContext;
}

public TraceContext getCurrentTraceContext() throws EmptyStackException {
if (this.isEmpty()) {
return null;
}
return traceContextThreadLocal.get();
}

public boolean isEmpty() {
TraceContext traceContext = traceContextThreadLocal.get();
return traceContext == null;
}

public void clear() {
traceContextThreadLocal.remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alipay.muagent.model.trace;

/**
* @author Joshua
* @version TraceThreadLocalContextHolder.java v1.0 2024-11-20 20:28
**/
public class TraceThreadLocalContextHolder {

/**
* singleton SofaTraceContext
*/
private static final TraceThreadLocalContext SINGLE_TRACE_CONTEXT = new TraceThreadLocalContext();

/**
* Get threadlocal trace context
*
* @return TraceThreadLocalContext
*/
public static TraceThreadLocalContext getTraceThreadLocalContext() {
return SINGLE_TRACE_CONTEXT;
}

}
6 changes: 6 additions & 0 deletions runtime/util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
</properties>

<dependencies>
<dependency>
<groupId>com.alipay.muagent</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
14 changes: 11 additions & 3 deletions runtime/util/src/main/java/com/alipay/muagent/util/LoggerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.alipay.muagent.util;

import com.alipay.muagent.model.trace.TraceThreadLocalContext;
import com.alipay.muagent.model.trace.TraceThreadLocalContextHolder;
import org.slf4j.Logger;
import org.slf4j.helpers.MessageFormatter;

Expand All @@ -13,10 +15,16 @@
*/
public class LoggerUtil {

/**
* 获取当前请求上下文的traceId
* @return traceId
*/
private static String getTraceId() {
// todo
return "";

TraceThreadLocalContext traceThreadLocalContext = TraceThreadLocalContextHolder.getTraceThreadLocalContext();
if (!traceThreadLocalContext.isEmpty()) {
return traceThreadLocalContext.getCurrentTraceContext().getTraceId();
}
return null;
}

/**
Expand Down
101 changes: 101 additions & 0 deletions runtime/util/src/main/java/com/alipay/muagent/util/TraceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.alipay.muagent.util;

import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @author Joshua
* @version TraceUtil.java v1.0 2024-11-20 20:34
**/
public class TraceUtil {

private static final String EMPTY_STRING = "";

private static final AtomicInteger COUNT = new AtomicInteger(1000);

private static String processId = null;

private static String hexIpAddress = "ffffffff";

static {
try {
String ipAddressStr = getInetAddress();
if (null != ipAddressStr) {
hexIpAddress = getHexAddress(ipAddressStr);
}
} catch (Exception e) {
/*
* empty catch
*/
}
}

public static String generateTraceId() {
return getTraceId(hexIpAddress, System.currentTimeMillis(), getNextId());
}

private static String getTraceId(String ip, long timeStamp, int nextId) {
return ip + timeStamp + nextId + getProcessId();
}

private static String getProcessId() {
if (StringUtils.isNotBlank(processId)) {
return processId;
}

String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();

if (StringUtils.isBlank(processName)) {
return EMPTY_STRING;
}

String[] processSplitName = processName.split("@");

if (processSplitName.length == 0) {
return EMPTY_STRING;
}

String pid = processSplitName[0];

if (StringUtils.isBlank(pid)) {
return EMPTY_STRING;
}

processId = pid;
return pid;
}

private static String getHexAddress(String ipAddressStr) {
final String[] ips = ipAddressStr.split("\\.");
StringBuilder stringBuilder = new StringBuilder();
for (String partOfAddress : ips) {
String hex = Integer.toHexString(Integer.parseInt(partOfAddress));
if (hex.length() == 1) {
stringBuilder.append('0').append(hex);
} else {
stringBuilder.append(hex);
}
}
return stringBuilder.toString();
}

private static String getInetAddress() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (Exception e) {
return null;
}
}

private static int getNextId() {
while (true) {
int current = COUNT.get();
int next = (current > 9000) ? 1000 : current + 1;
if (COUNT.compareAndSet(current, next)) {
return next;
}
}
}


}
5 changes: 5 additions & 0 deletions runtime/web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.alipay.muagent.web.interceptor;

import com.alipay.muagent.model.trace.TraceContext;
import com.alipay.muagent.model.trace.TraceThreadLocalContext;
import com.alipay.muagent.model.trace.TraceThreadLocalContextHolder;
import com.alipay.muagent.util.TraceUtil;
import com.alipay.muagent.web.model.Result;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
* @author Joshua
* @version RequestInterceptorAspect.java v1.0 2024-11-20 20:52
**/
@Aspect
@Component
public class RequestInterceptorAspect {

@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) || " +
"@annotation(org.springframework.web.bind.annotation.GetMapping) || " +
"@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
"@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
"@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void requestMappingMethods() {
}

@Before("requestMappingMethods()")
public void doBeforeRequestHandle() {
TraceContext traceContext = new TraceContext();
traceContext.setTraceId(TraceUtil.generateTraceId());
TraceThreadLocalContext traceThreadLocalContext = TraceThreadLocalContextHolder.getTraceThreadLocalContext();
traceThreadLocalContext.push(traceContext);
}

@AfterReturning(pointcut = "requestMappingMethods()", returning = "result")
public void handleAfterReturning(JoinPoint joinPoint, Object result) {
try {
TraceContext traceContext = TraceThreadLocalContextHolder.getTraceThreadLocalContext().getCurrentTraceContext();
((Result<?>) result).setTraceId(traceContext.getTraceId());
} catch (Exception e) {
/*
* empty catch
*/
} finally {
TraceThreadLocalContextHolder.getTraceThreadLocalContext().clear();
}
}

@AfterThrowing(pointcut = "requestMappingMethods()", throwing = "error")
public void handleAfterThrowing(JoinPoint joinPoint, Throwable error) {
TraceThreadLocalContextHolder.getTraceThreadLocalContext().clear();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Result<T> {

private String host = getLocalHost();

private String traceId;

/**
* 创建一个方法执行成功的Result对象
*
Expand Down

0 comments on commit e15a4fb

Please sign in to comment.