MetricsResource { @GET @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) @Path("/{group}/{metric}") public Response getMetric(final @PathParam("group") String group, final @PathParam("metric") String metricName, final @QueryParam("tagKey") List<String> tagKeys, final @QueryParam("tagValue") List<String> tagValues) { if (!manager.isEnabled()) { return Response.status(Response.Status.FORBIDDEN).build(); } List<String> groups = manager.listMetricGroups(); if (!groups.contains(group)) { return buildResult(null, false, "The specified group is not found!"); } MetricRegistry registry = manager.getMetricRegistryByGroup(group); List<MetricObject> metricObjects = buildMetricRegistry(registry, new MetricNameSetFilter(metricName)); if (metricObjects.isEmpty()) { return buildResult(null, false, "The specified metric is not found!"); } return buildResult(metricObjects); }  @Path("/list") @GET @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) Response listMetrics(); @GET @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) Response getMetrics(); @GET @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) @Path("/{group}") Response getMetrics(@DefaultValue("all") @PathParam("group") String group); @GET @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) @Path("/{group}/level/{level}") Response getMetricByLevel(final @PathParam("group") String group,
                                     final @PathParam("level") String level,
                                     final @QueryParam("above") boolean above); @GET @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) @Path("/{group}/{metric}") Response getMetric(final @PathParam("group") String group,
                              final @PathParam("metric") String metricName,
                              final @QueryParam("tagKey") List<String> tagKeys,
                              final @QueryParam("tagValue") List<String> tagValues); @GET @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) @Path("/specific") Response getMetric(final @QueryParam("metric") Set<String> metricNames, @QueryParam("zeroIgnore") boolean zeroIgnore); @POST @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) @Path("/specific") Response getMetricsByPost(MultivaluedMap<String, String> params, @QueryParam("zeroIgnore") boolean zeroIgnore); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces({Constants.PRODUCE_JSON_WITH_QUALITY_SOURCE, MediaType.TEXT_HTML}) @Path("/search") Response searchMetrics(String metricsSearch, @HeaderParam("REMOTE_ADDR") String remoteAddr,
            @HeaderParam("X-Forwarded-For") String xForwardedFor, @Context HttpServletRequest httpServletRequest);  }
@Test public void testCollectCompassMetric() { MetricsCollector collector = MetricsCollectorFactory.createNew(TimeUnit.SECONDS.toSeconds(1), 1.0 / TimeUnit.MILLISECONDS.toNanos(1), null); Assert.assertTrue(collector instanceof NormalMetricsCollector); ManualClock clock = new ManualClock(); Compass compass = new CompassImpl(ReservoirType.EXPONENTIALLY_DECAYING, clock, 10, 1, 10, 10); Compass.Context context = compass.time(); context.markAddon("hit"); clock.addSeconds(1); context.success(); context.stop(); collector.collect(MetricName.build("TEST"), compass, System.currentTimeMillis()); List<MetricObject> metricObjects = collector.build(); Assert.assertEquals(21, metricObjects.size()); MetricObject found = null; for (MetricObject object: metricObjects) { if (object.getMetric().equals("TEST.hit.count")) { found = object; } } Assert.assertNotNull("Should have the .hit.count metric", found); found = null; for (MetricObject object: metricObjects) { if (object.getMetric().equals("TEST.bucket_count")) { found = object; } } Assert.assertNotNull("Should have the .bucket_count metric", found); Assert.assertEquals(found.getMetricType(), MetricObject.MetricType.DELTA); found = null; for (MetricObject object: metricObjects) { if (object.getMetric().equals("TEST.success_bucket_count")) { found = object; } } Assert.assertNotNull("Should have the .success_bucket_count metric", found); Assert.assertEquals(found.getMetricType(), MetricObject.MetricType.DELTA); found = null; for (MetricObject object: metricObjects) { if (object.getMetric().equals("TEST.hit_bucket_count")) { found = object; } } Assert.assertNotNull("Should have the .hit_bucket_count metric", found); Assert.assertEquals(found.getMetricType(), MetricObject.MetricType.DELTA); }
@SuppressWarnings("unchecked") @Test public void testFastCompassCustomizedMetric() { FastCompass c1 = MetricManager.getFastCompass("zzz", MetricName.build("test.abc.fc")); c1.record(10, "pushSuccess"); c1.record(20, "fail"); MetricsResource resource = new MetricsResource(); Set<String> metrics = new HashSet<String>(); metrics.add("test.abc.fc.pushSuccess_bucket_count"); Response response = resource.getMetric(metrics, false); Object dataList = ((Map<String, Object>)response.getEntity()).get("data"); Assert.assertEquals(1, ((List<Object>)dataList).size()); }