SpringMvcSofaTracerFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) { if (this.springMvcTracer == null) { this.springMvcTracer = SpringMvcTracer.getSpringMvcTracerSingleton(); } SofaTracerSpan springMvcSpan = null; long responseSize = -1; int httpStatus = -1; try { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; SofaTracerSpanContext spanContext = getSpanContextFromRequest(request); springMvcSpan = springMvcTracer.serverReceive(spanContext); if (StringUtils.isBlank(this.appName)) { this.appName = SofaTracerConfiguration .getProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY); } springMvcSpan.setOperationName(request.getRequestURL().toString()); springMvcSpan.setTag(CommonSpanTags.LOCAL_APP, this.appName); springMvcSpan.setTag(CommonSpanTags.REQUEST_URL, request.getRequestURL().toString()); springMvcSpan.setTag(CommonSpanTags.METHOD, request.getMethod()); springMvcSpan.setTag(CommonSpanTags.REQ_SIZE, request.getContentLength()); ResponseWrapper responseWrapper = new ResponseWrapper(response); filterChain.doFilter(servletRequest, responseWrapper); httpStatus = responseWrapper.getStatus(); responseSize = responseWrapper.getContentLength(); } catch (Throwable t) { httpStatus = 500; throw new RuntimeException(t); } finally { if (springMvcSpan != null) { springMvcSpan.setTag(CommonSpanTags.RESP_SIZE, responseSize); springMvcTracer.serverSend(String.valueOf(httpStatus)); } } }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain); @Override void destroy(); void setAppName(String appName); String getFilterName(); SofaTracerSpanContext getSpanContextFromRequest(HttpServletRequest request);  }
@Test public void testFilterFail() throws Exception { checkFile(); final SpringMvcSofaTracerFilter filter = new SpringMvcSofaTracerFilter(); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain mockFilterChain = new FilterChain() { public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException { throw new RuntimeException("Has got exception..."); } public void init(FilterConfig theConfig) { } public void destroy() { } }; try { filter.doFilter(request, response, mockFilterChain); } catch (Exception e) { String message = e.getMessage(); Assert.assertTrue(message.contains("Has got exception...")); } Thread.sleep(500); File file = new File(logDirectoryPath + File.separator + "tracer-self.log"); if (file.exists()) { List<String> result = FileUtils.readLines(new File(logDirectoryPath + File.separator + "tracer-self.log")); Assert.assertTrue(result.size() == 1); } Assert.assertTrue(!file.exists()); }
@Test public void testFilterSuccess() throws Exception { checkFile(); final SpringMvcSofaTracerFilter filter = new SpringMvcSofaTracerFilter(); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain mockFilterChain = new FilterChain() { public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException { } public void init(FilterConfig theConfig) { } public void destroy() { } }; try { filter.doFilter(request, response, mockFilterChain); } catch (Exception e) { Assert.fail("Failed to execute SpringMvcSofaTracerFilter"); } Thread.sleep(500); List<String> contents = FileUtils.readLines(new File(logDirectoryPath + File.separator + SpringMvcLogEnum.SPRING_MVC_DIGEST .getDefaultLogName())); Assert.assertTrue(contents.size() == 1); List<String> result = FileUtils.readLines(new File(logDirectoryPath + File.separator + "tracer-self.log")); Assert.assertTrue(result.size() == 2); }