2020import org .springframework .web .method .HandlerMethod ;
2121import org .springframework .web .servlet .HandlerInterceptor ;
2222import org .springframework .web .servlet .ModelAndView ;
23+
2324import javax .servlet .http .HttpServletRequest ;
2425import javax .servlet .http .HttpServletResponse ;
2526import java .util .LinkedHashMap ;
@@ -36,6 +37,7 @@ public class HttpSvrTarsReportInterceptor implements HandlerInterceptor {
3637
3738 public static final String METHOD_START_TIME = "methodStartTime" ;
3839 public static final String ARGU = "argu" ; //argu占位符
40+ private static final String DEFAULT_HOST = "0.0.0.0" ;//
3941
4042 String servantName = StringUtils .EMPTY ;
4143 ServantAdapterConfig servantAdapterConfig = null ;
@@ -48,23 +50,23 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
4850 }
4951
5052 @ Override
51- public void postHandle (HttpServletRequest request , HttpServletResponse response , Object handler , @ Nullable ModelAndView modelAndView ) {
53+ public void postHandle (HttpServletRequest request , HttpServletResponse response , Object handler , @ Nullable ModelAndView modelAndView ) {
5254 }
5355
5456 @ Override
5557 public void afterCompletion (HttpServletRequest request , HttpServletResponse response , Object handler , @ Nullable Exception ex ) {
5658 try {
57- if (!(handler instanceof HandlerMethod )) { //可能会有一些跨域的预请求,不做处理.PreFlightHandler
59+ if (!(handler instanceof HandlerMethod )) { //可能会有一些跨域的预请求,不做处理.PreFlightHandler
5860 return ;
5961 }
60- String funcName = getFuncName (request ,handler );
62+ String funcName = getFuncName (request , handler );
6163 long cost = getCost (request );
6264 int result = getResult (ex );
6365 String masterIp = request .getRemoteAddr (); //主调方的ip(nginx)
6466 String remark = "" ; //暂定为空,实际上应该表示框架异常的错误码.
6567 printFlowLog (masterIp , funcName , cost , result , remark );
6668
67- if (StringUtils .isBlank (funcName )) {
69+ if (StringUtils .isBlank (funcName )) {
6870 return ;
6971 }
7072
@@ -81,7 +83,8 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp
8183 String masterName = Constants .TARS_NOT_CLIENT ;
8284 CommunicatorConfig communicatorConfig = serverConfig .getCommunicatorConfig ();
8385 Endpoint serverEndpoint = servantAdapterConfig .getEndpoint ();
84- InvokeStatHelper .getInstance ().addProxyStat (servantName ).addInvokeTimeByServer (masterName , serverConfig .getApplication (), serverConfig .getServerName (), communicatorConfig .getSetName (), communicatorConfig .getSetArea (), communicatorConfig .getSetID (), funcName , (masterIp == null ? "0.0.0.0" : masterIp ), serverEndpoint .host (), serverEndpoint .port (), result , cost );
86+ InvokeStatHelper .getInstance ().addProxyStat (servantName ).addInvokeTimeByServer (masterName , serverConfig .getApplication (), serverConfig .getServerName (), communicatorConfig .getSetName (),
87+ communicatorConfig .getSetArea (), communicatorConfig .getSetID (), funcName , DEFAULT_HOST , serverEndpoint .host (), serverEndpoint .port (), result , cost );
8588 } catch (Throwable e ) {
8689 log .error ("httpSvr stat report exMsg:{}" , e .getMessage (), e );
8790 }
@@ -122,7 +125,7 @@ private String getFuncName(HttpServletRequest request, Object handler) {
122125
123126 RequestMapping requestMapping = method .getMethodAnnotation (RequestMapping .class );
124127 String [] values = requestMapping .value ();
125- if (requestMapping != null && requestMapping .value () !=null && requestMapping .value ().length > 0 ) {
128+ if (requestMapping != null && requestMapping .value () != null && requestMapping .value ().length > 0 ) {
126129 methodUri = values [0 ];
127130 } else { //一些不存在的接口,会默认转到/error
128131 log .info ("requestMethod not found.requestUri:{}" , request .getRequestURI ());
@@ -136,9 +139,9 @@ private String getFuncName(HttpServletRequest request, Object handler) {
136139 }
137140
138141 public void findReportConstant (ServerConfig serverConfig ) {
139- LinkedHashMap <String , ServantAdapterConfig > servantAdapterConfigLinkedHashMap = serverConfig .getServantAdapterConfMap ();
140- for (Map .Entry <String , ServantAdapterConfig > entry : servantAdapterConfigLinkedHashMap .entrySet ()) { //取servant的ObjName作为上报的servant.
141- if (!entry .getKey ().equalsIgnoreCase (OmConstants .AdminServant )) {
142+ LinkedHashMap <String , ServantAdapterConfig > servantAdapterConfigLinkedHashMap = serverConfig .getServantAdapterConfMap ();
143+ for (Map .Entry <String , ServantAdapterConfig > entry : servantAdapterConfigLinkedHashMap .entrySet ()) { //取servant的ObjName作为上报的servant.
144+ if (!entry .getKey ().equalsIgnoreCase (OmConstants .AdminServant )) {
142145 servantName = entry .getKey ();
143146 servantAdapterConfig = entry .getValue ();
144147 log .info ("report servantName:{}" , servantName );
@@ -148,9 +151,9 @@ public void findReportConstant(ServerConfig serverConfig) {
148151 }
149152
150153 //|主调方ip|接口名|请求参数|状态码|耗时|remark
151- public static void printFlowLog (String masterIp , String funcName , long cost , int result , String remark ) {
154+ public static void printFlowLog (String masterIp , String funcName , long cost , int result , String remark ) {
152155 if (result == TarsHelper .SERVERSUCCESS && (!TarsServantProcessor .isFlowLogEnable ())) return ;
153156 log .info ("|{}|{}|{}|{}|{}|{}|" ,
154- masterIp ,funcName , ARGU , result ,cost ,remark );
157+ masterIp , funcName , ARGU , result , cost , remark );
155158 }
156159}
0 commit comments