Skip to content

Commit

Permalink
[#noissue] Cleanup ThreadDump serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Nov 19, 2024
1 parent e184ef3 commit 8e43f7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static String createDumpMessage(TThreadDump threadDump) {
TThreadState threadState = getThreadState(threadDump.getThreadState());

// set threadName
StringBuilder message = new StringBuilder("\"" + threadDump.getThreadName() + "\"");
StringBuilder message = new StringBuilder(64);
message.append("\"").append(threadDump.getThreadName()).append("\"");

// set threadId
String hexStringThreadId = Long.toHexString(threadDump.getThreadId());
Expand Down Expand Up @@ -69,17 +70,17 @@ public static String createDumpMessage(TThreadDump threadDump) {
// set StackTrace
for (int i = 0; i < threadDump.getStackTraceSize(); i++) {
String stackTrace = threadDump.getStackTrace().get(i);
message.append(TAB_SEPARATOR + "at ").append(stackTrace);
message.append(TAB_SEPARATOR).append("at ").append(stackTrace);
message.append(LINE_SEPARATOR);

if (i == 0 && !StringUtils.isBlank(threadDump.getLockName())) {
switch (threadState) {
case BLOCKED -> {
message.append(TAB_SEPARATOR + "- blocked on ").append(threadDump.getLockName());
message.append(TAB_SEPARATOR).append("- blocked on ").append(threadDump.getLockName());
message.append(LINE_SEPARATOR);
}
case WAITING, TIMED_WAITING -> {
message.append(TAB_SEPARATOR + "- waiting on ").append(threadDump.getLockName());
message.append(TAB_SEPARATOR).append("- waiting on ").append(threadDump.getLockName());
message.append(LINE_SEPARATOR);
}
}
Expand All @@ -88,7 +89,7 @@ public static String createDumpMessage(TThreadDump threadDump) {
if (threadDump.getLockedMonitors() != null) {
for (TMonitorInfo lockedMonitor : threadDump.getLockedMonitors()) {
if (lockedMonitor.getStackDepth() == i) {
message.append(TAB_SEPARATOR + "- locked ").append(lockedMonitor.getStackFrame());
message.append(TAB_SEPARATOR).append("- locked ").append(lockedMonitor.getStackFrame());
message.append(LINE_SEPARATOR);
}
}
Expand All @@ -101,7 +102,7 @@ public static String createDumpMessage(TThreadDump threadDump) {
message.append(LINE_SEPARATOR).append(TAB_SEPARATOR).append("Number of locked synchronizers = ").append(lockedSynchronizers.size());
message.append(LINE_SEPARATOR);
for (String lockedSynchronizer : lockedSynchronizers) {
message.append(TAB_SEPARATOR + "- ").append(lockedSynchronizer);
message.append(TAB_SEPARATOR).append("- ").append(lockedSynchronizer);
message.append(LINE_SEPARATOR);
}
}
Expand All @@ -114,7 +115,8 @@ public static String createDumpMessage(ThreadDumpBo threadDump) {
ThreadState threadState = getThreadState(threadDump.getThreadState());

// set threadName
StringBuilder message = new StringBuilder("\"" + threadDump.getThreadName() + "\"");
StringBuilder message = new StringBuilder(64);
message.append("\"").append(threadDump.getThreadName()).append("\"");

// set threadId
String hexStringThreadId = Long.toHexString(threadDump.getThreadId());
Expand All @@ -140,19 +142,20 @@ public static String createDumpMessage(ThreadDumpBo threadDump) {
message.append(LINE_SEPARATOR);

// set StackTrace
for (int i = 0; i < threadDump.getStackTraceList().size(); i++) {
String stackTrace = threadDump.getStackTraceList().get(i);
message.append(TAB_SEPARATOR + "at ").append(stackTrace);
final List<String> stackTraceList = threadDump.getStackTraceList();
for (int i = 0; i < stackTraceList.size(); i++) {
String stackTrace = stackTraceList.get(i);
message.append(TAB_SEPARATOR).append("at ").append(stackTrace);
message.append(LINE_SEPARATOR);

if (i == 0 && !StringUtils.isBlank(threadDump.getLockName())) {
switch (threadState) {
case BLOCKED -> {
message.append(TAB_SEPARATOR + "- blocked on ").append(threadDump.getLockName());
message.append(TAB_SEPARATOR).append("- blocked on ").append(threadDump.getLockName());
message.append(LINE_SEPARATOR);
}
case WAITING, TIMED_WAITING -> {
message.append(TAB_SEPARATOR + "- waiting on ").append(threadDump.getLockName());
message.append(TAB_SEPARATOR).append("- waiting on ").append(threadDump.getLockName());
message.append(LINE_SEPARATOR);
}
default -> {
Expand All @@ -162,7 +165,7 @@ public static String createDumpMessage(ThreadDumpBo threadDump) {

for (MonitorInfoBo lockedMonitor : threadDump.getLockedMonitorInfoList()) {
if (lockedMonitor.getStackDepth() == i) {
message.append(TAB_SEPARATOR + "- locked ").append(lockedMonitor.getStackFrame());
message.append(TAB_SEPARATOR).append("- locked ").append(lockedMonitor.getStackFrame());
message.append(LINE_SEPARATOR);
}
}
Expand All @@ -174,7 +177,7 @@ public static String createDumpMessage(ThreadDumpBo threadDump) {
message.append(LINE_SEPARATOR).append(TAB_SEPARATOR).append("Number of locked synchronizers = ").append(lockedSynchronizers.size());
message.append(LINE_SEPARATOR);
for (String lockedSynchronizer : lockedSynchronizers) {
message.append(TAB_SEPARATOR + "- ").append(lockedSynchronizer);
message.append(TAB_SEPARATOR).append("- ").append(lockedSynchronizer);
message.append(LINE_SEPARATOR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.navercorp.pinpoint.web.vo.AgentEvent;

import java.io.IOException;
import java.util.List;

/**
* @author Taejin Koo
Expand Down Expand Up @@ -54,12 +53,8 @@ public void serialize(AgentEvent agentEvent, JsonGenerator jgen, SerializerProvi
final Object eventMessage = agentEvent.getEventMessage();
if (eventMessage != null) {
if (eventMessage instanceof DeadlockBo deadlock) {
final StringBuilder message = new StringBuilder();
final List<ThreadDumpBo> threadDumpBoList = deadlock.getThreadDumpBoList();
for (ThreadDumpBo threadDump : threadDumpBoList) {
message.append(ThreadDumpUtils.createDumpMessage(threadDump));
}
jgen.writeObjectField("eventMessage", message.toString());
final String deadLockEvent = deadLockEvent(deadlock);
jgen.writeObjectField("eventMessage", deadLockEvent);
} else {
jgen.writeObjectField("eventMessage", eventMessage);
}
Expand All @@ -68,4 +63,12 @@ public void serialize(AgentEvent agentEvent, JsonGenerator jgen, SerializerProvi
jgen.writeEndObject();
}

private String deadLockEvent(DeadlockBo deadlock) {
final StringBuilder message = new StringBuilder(128);
for (ThreadDumpBo threadDump : deadlock.getThreadDumpBoList()) {
message.append(ThreadDumpUtils.createDumpMessage(threadDump));
}
return message.toString();
}

}

0 comments on commit 8e43f7e

Please sign in to comment.