StagemonitorMetricsServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json"); if (servletPlugin.getMetricsServletAllowedOrigin() != null) { resp.setHeader("Access-Control-Allow-Origin", servletPlugin.getMetricsServletAllowedOrigin()); } resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store"); resp.setStatus(HttpServletResponse.SC_OK); try (OutputStream output = resp.getOutputStream()) { String jsonpParamName = servletPlugin.getMetricsServletJsonpParamName(); if (jsonpParamName != null && req.getParameter(jsonpParamName) != null) { getWriter(req).writeValue(output, new JSONPObject(req.getParameter(jsonpParamName), registry)); } else { getWriter(req).writeValue(output, registry); } } } StagemonitorMetricsServlet();  StagemonitorMetricsServlet(Metric2Registry registry, ServletPlugin servletPlugin, ObjectMapper mapper);   }
@Test public void getAllMetrics() throws Exception { registry.counter(name("foo").tag("bar", "baz").build()).inc(); registry.counter(name("qux").tag("quux", "foo").build()).inc(); final MockHttpServletResponse resp = new MockHttpServletResponse(); servlet.doGet(new MockHttpServletRequest(), resp); assertEquals("[" + "{\"name\":\"foo\",\"tags\":{\"bar\":\"baz\"},\"values\":{\"count\":1}}," + "{\"name\":\"qux\",\"tags\":{\"quux\":\"foo\"},\"values\":{\"count\":1}}" + "]", resp.getContentAsString()); }
@Test public void getFilteredMetrics() throws Exception { registry.counter(name("foo").tag("bar", "baz").build()).inc(); registry.counter(name("qux").tag("quux", "foo").build()).inc(); final MockHttpServletResponse resp = new MockHttpServletResponse(); final MockHttpServletRequest req = new MockHttpServletRequest(); req.addParameter("metricNames[]", "foo"); servlet.doGet(req, resp); assertEquals("[{\"name\":\"foo\",\"tags\":{\"bar\":\"baz\"},\"values\":{\"count\":1}}]", resp.getContentAsString()); }
@Test public void getMeter() throws Exception { registry.meter(name("foo").tag("bar", "baz").build()).mark(); final MockHttpServletResponse resp = new MockHttpServletResponse(); servlet.doGet(new MockHttpServletRequest(), resp); final double mean_rate = JsonUtils.getMapper().readTree(resp.getContentAsString()).get(0).get("values").get("mean_rate").doubleValue(); assertTrue("Expected m1 rate of > 0, but got " + mean_rate, mean_rate > 0); }