|
@ -0,0 +1,90 @@
|
|
|
package com.yihu.cat;
|
|
|
|
|
|
import com.dianping.cat.Cat;
|
|
|
import com.dianping.cat.CatConstants;
|
|
|
import com.dianping.cat.message.Transaction;
|
|
|
|
|
|
import javax.servlet.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
* Filter - Cat基础过滤器
|
|
|
* Created by progr1mmer on 2018/9/4.
|
|
|
*/
|
|
|
public class CatServletFilter implements Filter {
|
|
|
|
|
|
@Override
|
|
|
public void init(FilterConfig filterConfig) throws ServletException {
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
|
|
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
|
|
CatHeaderRequestWrapper headerRequestWrapper = new CatHeaderRequestWrapper(request);
|
|
|
if (isRoot(request)) {
|
|
|
Transaction t = Cat.newTransaction(CatConstants.TYPE_CALL, request.getRequestURL().toString());
|
|
|
try {
|
|
|
CatContext catContext = new CatContext();
|
|
|
Cat.logRemoteCallClient(catContext);
|
|
|
|
|
|
headerRequestWrapper.putHeader(Cat.Context.ROOT, catContext.getProperty(Cat.Context.ROOT));
|
|
|
headerRequestWrapper.putHeader(Cat.Context.PARENT, catContext.getProperty(Cat.Context.PARENT));
|
|
|
headerRequestWrapper.putHeader(Cat.Context.CHILD, catContext.getProperty(Cat.Context.CHILD));
|
|
|
filterChain.doFilter(headerRequestWrapper, servletResponse);
|
|
|
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
|
|
if (response.getStatus() == 200) {
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
} else {
|
|
|
t.setStatus("-1");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
Cat.getProducer().logError(e);
|
|
|
t.setStatus(e);
|
|
|
throw e;
|
|
|
} finally {
|
|
|
t.complete();
|
|
|
}
|
|
|
} else {
|
|
|
CatContext catContext = new CatContext();
|
|
|
catContext.addProperty(Cat.Context.ROOT, request.getHeader(Cat.Context.ROOT));
|
|
|
catContext.addProperty(Cat.Context.PARENT, request.getHeader(Cat.Context.PARENT));
|
|
|
catContext.addProperty(Cat.Context.CHILD, request.getHeader(Cat.Context.CHILD));
|
|
|
Cat.logRemoteCallServer(catContext);
|
|
|
|
|
|
Transaction t = Cat.newTransaction(CatConstants.TYPE_SERVICE, request.getRequestURL().toString());
|
|
|
try {
|
|
|
Cat.logEvent("Request.remoteHost", request.getRemoteHost());
|
|
|
filterChain.doFilter(headerRequestWrapper, servletResponse);
|
|
|
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
|
|
if (response.getStatus() == 200) {
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
} else {
|
|
|
t.setStatus("-1");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
t.setStatus(e);
|
|
|
Cat.logError(e);
|
|
|
throw e;
|
|
|
} finally {
|
|
|
t.complete();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void destroy() {
|
|
|
|
|
|
}
|
|
|
|
|
|
private boolean isRoot(HttpServletRequest request) {
|
|
|
/*return request.getHeader(CatHttpConstants.CAT_HTTP_HEADER_ROOT_MESSAGE_ID) != null &&
|
|
|
request.getHeader(CatHttpConstants.CAT_HTTP_HEADER_PARENT_MESSAGE_ID) != null &&
|
|
|
request.getHeader(CatHttpConstants.CAT_HTTP_HEADER_CHILD_MESSAGE_ID) != null;*/
|
|
|
return request.getHeader(Cat.Context.ROOT) == null &&
|
|
|
request.getHeader(Cat.Context.PARENT) == null &&
|
|
|
request.getHeader(Cat.Context.CHILD) == null;
|
|
|
}
|
|
|
}
|