-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from KUTEJiang/main_traceid_dev_mingcao
[feat](log)generate traceId for each http request(#36)
- Loading branch information
Showing
9 changed files
with
262 additions
and
3 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
runtime/model/src/main/java/com/alipay/muagent/model/trace/TraceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
runtime/model/src/main/java/com/alipay/muagent/model/trace/TraceThreadLocalContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ime/model/src/main/java/com/alipay/muagent/model/trace/TraceThreadLocalContextHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
runtime/util/src/main/java/com/alipay/muagent/util/TraceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
runtime/web/src/main/java/com/alipay/muagent/web/interceptor/RequestInterceptorAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters