@Test public void testSimpleTimers() { JsonExporter exporter = new JsonExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = new MetadataBuilder() .withUnit(MetricUnits.SECONDS) .withName("mysimpletimer") .build(); SimpleTimer timerWithoutTags = registry.simpleTimer(metadata); SimpleTimer timerRed = registry.simpleTimer(metadata, new Tag("color", "red")); SimpleTimer timerBlue = registry.simpleTimer(metadata, new Tag("color", "blue"), new Tag("foo", "bar")); timerWithoutTags.update(Duration.ofSeconds(1)); timerRed.update(Duration.ofSeconds(2)); timerBlue.update(Duration.ofSeconds(3)); timerBlue.update(Duration.ofSeconds(4)); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mysimpletimer").toString(); JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject(); JsonObject mytimerObject = json.getJsonObject("mysimpletimer"); assertEquals(1.0, mytimerObject.getJsonNumber("count").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("count;color=red").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("count;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("elapsedTime").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("elapsedTime;color=red").doubleValue(), 1e-10); assertEquals(7.0, mytimerObject.getJsonNumber("elapsedTime;color=blue;foo=bar").doubleValue(), 1e-10); }
@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();  }