提交 75a23219 编写于 作者: F Frankie Wu

add cat-thread-count to heartbeat and fix getSessionToken() bug

上级 31bdeca8
......@@ -8,6 +8,7 @@ import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import com.dianping.cat.Cat;
......@@ -22,17 +23,15 @@ public class CatFilter implements Filter {
public void destroy() {
}
protected String getOriginalUrl(ServletRequest request) {
return ((HttpServletRequest) request).getRequestURI();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String sessionToken = req.getSession().getId();
String sessionToken = getSessionIdFromCookie(req);
// setup for thread local data
Cat.setup(sessionToken);
MessageProducer cat = Cat.getProducer();
Transaction t = cat.newTransaction(CatConstants.TYPE_URL, getOriginalUrl(request));
......@@ -70,6 +69,24 @@ public class CatFilter implements Filter {
}
}
protected String getOriginalUrl(ServletRequest request) {
return ((HttpServletRequest) request).getRequestURI();
}
private String getSessionIdFromCookie(HttpServletRequest req) {
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
return cookie.getValue();
}
}
}
return null;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
......
......@@ -6,9 +6,11 @@ import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;
import com.dianping.cat.message.spi.MessageStatistics;
import com.dianping.cat.status.model.entity.DiskSpaceInfo;
......@@ -18,7 +20,7 @@ import com.dianping.cat.status.model.entity.MessageInfo;
import com.dianping.cat.status.model.entity.OsInfo;
import com.dianping.cat.status.model.entity.RuntimeInfo;
import com.dianping.cat.status.model.entity.StatusInfo;
import com.dianping.cat.status.model.entity.ThreadInfo;
import com.dianping.cat.status.model.entity.ThreadsInfo;
import com.dianping.cat.status.model.transform.BaseVisitor;
class StatusInfoCollector extends BaseVisitor {
......@@ -28,6 +30,18 @@ class StatusInfoCollector extends BaseVisitor {
m_statistics = statistics;
}
int countThreadsByPrefix(ThreadInfo[] threads, String prefix) {
int count = 0;
for (ThreadInfo thread : threads) {
if (thread.getThreadName().startsWith(prefix)) {
count++;
}
}
return count;
}
long getGcCount(List<GarbageCollectorMXBean> mxbeans) {
long count = 0;
......@@ -52,6 +66,23 @@ class StatusInfoCollector extends BaseVisitor {
return time;
}
String getThreadDump(ThreadInfo[] threads) {
StringBuilder sb = new StringBuilder(32768);
int index = 1;
TreeMap<String, ThreadInfo> sortedThreads = new TreeMap<String, ThreadInfo>();
for (ThreadInfo thread : threads) {
sortedThreads.put(thread.getThreadName(), thread);
}
for (ThreadInfo thread : sortedThreads.values()) {
sb.append(index++).append(": ").append(thread);
}
return sb.toString();
}
boolean isInstanceOfInterface(Class<?> clazz, String interfaceName) {
if (clazz == Object.class) {
return false;
......@@ -148,22 +179,23 @@ class StatusInfoCollector extends BaseVisitor {
status.setDiskSpace(new DiskSpaceInfo());
status.setRuntime(new RuntimeInfo());
status.setMemory(new MemoryInfo());
status.setThread(new ThreadInfo());
status.setThread(new ThreadsInfo());
status.setMessage(new MessageInfo());
super.visitStatus(status);
}
@Override
public void visitThread(ThreadInfo thread) {
public void visitThread(ThreadsInfo thread) {
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threads = bean.dumpAllThreads(true, true);
thread.setCount(bean.getThreadCount());
thread.setDaemonCount(bean.getDaemonThreadCount());
thread.setPeekCount(bean.getPeakThreadCount());
thread.setTotalStartedCount(bean.getTotalStartedThreadCount());
// TODO remove below
// System.out.println(Arrays.asList(bean.dumpAllThreads(true, true)));
thread.setCatThreadCount(countThreadsByPrefix(threads, "Cat-"));
thread.setPigeonThreadCount(countThreadsByPrefix(threads, "Pigeon-"));
thread.setDump(getThreadDump(threads));
}
}
\ No newline at end of file
......@@ -47,6 +47,9 @@
<attribute name="daemon-count" value-type="int" />
<attribute name="peek-count" value-type="int" />
<attribute name="total-started-count" value-type="int" />
<attribute name="cat-thread-count" value-type="int" />
<attribute name="pigeon-thread-count" value-type="int" />
<element name="dump" value-type="String"/>
</entity>
<entity name="message">
<attribute name="produced" value-type="int" />
......
......@@ -32,10 +32,12 @@
<attribute name="count" value-type="long" primitive="true" />
<attribute name="time" value-type="long" primitive="true" />
</entity>
<entity name="thread" class-name="ThreadInfo">
<entity name="thread" class-name="ThreadsInfo">
<attribute name="count" value-type="int" primitive="true" />
<attribute name="daemon-count" value-type="int" primitive="true" />
<attribute name="peek-count" value-type="int" primitive="true" />
<attribute name="cat-thread-count" value-type="int" primitive="true" />
<attribute name="pigeon-thread-count" value-type="int" primitive="true" />
<attribute name="total-started-count" value-type="long" primitive="true" />
</entity>
<entity name="message" class-name="MessageInfo">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册