@Test public void testWriterIsClosedOnException() throws IOException, ServletException { HttpServletRequest req = mock(HttpServletRequest.class); HttpServletResponse resp = mock(HttpServletResponse.class); PrintWriter writer = mock(PrintWriter.class); when(resp.getWriter()).thenReturn(writer); doThrow(new RuntimeException()).when(writer).write(any(char[].class), anyInt(), anyInt()); CollectorRegistry registry = new CollectorRegistry(); Gauge a = Gauge.build("a", "a help").register(registry); try { new MetricsServlet(registry).doGet(req, resp); fail("Exception expected"); } catch (Exception e) { } verify(writer).close(); }
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(TextFormat.CONTENT_TYPE_004); Writer writer = new BufferedWriter(resp.getWriter()); try { TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req))); writer.flush(); } finally { writer.close(); } }
MetricsServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(TextFormat.CONTENT_TYPE_004); Writer writer = new BufferedWriter(resp.getWriter()); try { TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req))); writer.flush(); } finally { writer.close(); } } }
MetricsServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(TextFormat.CONTENT_TYPE_004); Writer writer = new BufferedWriter(resp.getWriter()); try { TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req))); writer.flush(); } finally { writer.close(); } } MetricsServlet();  MetricsServlet(CollectorRegistry registry); }
MetricsServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(TextFormat.CONTENT_TYPE_004); Writer writer = new BufferedWriter(resp.getWriter()); try { TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req))); writer.flush(); } finally { writer.close(); } } MetricsServlet();  MetricsServlet(CollectorRegistry registry);  }
MetricsServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(TextFormat.CONTENT_TYPE_004); Writer writer = new BufferedWriter(resp.getWriter()); try { TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req))); writer.flush(); } finally { writer.close(); } } MetricsServlet();  MetricsServlet(CollectorRegistry registry);   }