@Test public void testCounters() { JsonExporter exporter = new JsonExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Counter counterWithoutTags = registry.counter("mycounter"); Counter counterRed = registry.counter("mycounter", new Tag("color", "red")); Counter counterBlue = registry.counter("mycounter", new Tag("color", "blue"), new Tag("foo", "bar")); counterWithoutTags.inc(1); counterRed.inc(2); counterBlue.inc(3); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mycounter").toString(); JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject(); assertEquals(1, json.getInt("mycounter")); assertEquals(2, json.getInt("mycounter;color=red")); assertEquals(3, json.getInt("mycounter;color=blue;foo=bar")); }
@Override public StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name) { MetricRegistry registry = MetricRegistries.get(scope); Map<MetricID, Metric> metricMap = registry.getMetrics() .entrySet() .stream() .filter(e -> e.getKey().getName().equals(name)) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue)); Map<String, Metadata> metadataMap = registry.getMetadata(); JsonObjectBuilder root = JsonProviderHolder.get().createObjectBuilder(); exportMetricsForMap(metricMap, metadataMap) .forEach(root::add); return stringify(root.build()); }
JsonExporter implements Exporter { @Override public StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name) { MetricRegistry registry = MetricRegistries.get(scope); Map<MetricID, Metric> metricMap = registry.getMetrics() .entrySet() .stream() .filter(e -> e.getKey().getName().equals(name)) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue)); Map<String, Metadata> metadataMap = registry.getMetadata(); JsonObjectBuilder root = JsonProviderHolder.get().createObjectBuilder(); exportMetricsForMap(metricMap, metadataMap) .forEach(root::add); return stringify(root.build()); } }
JsonExporter implements Exporter { @Override public StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name) { MetricRegistry registry = MetricRegistries.get(scope); Map<MetricID, Metric> metricMap = registry.getMetrics() .entrySet() .stream() .filter(e -> e.getKey().getName().equals(name)) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue)); Map<String, Metadata> metadataMap = registry.getMetadata(); JsonObjectBuilder root = JsonProviderHolder.get().createObjectBuilder(); exportMetricsForMap(metricMap, metadataMap) .forEach(root::add); return stringify(root.build()); } JsonExporter(); }
JsonExporter implements Exporter { @Override public StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name) { MetricRegistry registry = MetricRegistries.get(scope); Map<MetricID, Metric> metricMap = registry.getMetrics() .entrySet() .stream() .filter(e -> e.getKey().getName().equals(name)) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue)); Map<String, Metadata> metadataMap = registry.getMetadata(); JsonObjectBuilder root = JsonProviderHolder.get().createObjectBuilder(); exportMetricsForMap(metricMap, metadataMap) .forEach(root::add); return stringify(root.build()); } JsonExporter(); @Override StringBuilder exportOneScope(MetricRegistry.Type scope); @Override StringBuilder exportAllScopes(); @Override StringBuilder exportOneMetric(MetricRegistry.Type scope, MetricID metricID); @Override StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name); @Override String getContentType(); }
JsonExporter implements Exporter { @Override public StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name) { MetricRegistry registry = MetricRegistries.get(scope); Map<MetricID, Metric> metricMap = registry.getMetrics() .entrySet() .stream() .filter(e -> e.getKey().getName().equals(name)) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue)); Map<String, Metadata> metadataMap = registry.getMetadata(); JsonObjectBuilder root = JsonProviderHolder.get().createObjectBuilder(); exportMetricsForMap(metricMap, metadataMap) .forEach(root::add); return stringify(root.build()); } JsonExporter(); @Override StringBuilder exportOneScope(MetricRegistry.Type scope); @Override StringBuilder exportAllScopes(); @Override StringBuilder exportOneMetric(MetricRegistry.Type scope, MetricID metricID); @Override StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name); @Override String getContentType();  }