@Test public void testTimers() { JsonExporter exporter = new JsonExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = new MetadataBuilder() .withUnit(MetricUnits.SECONDS) .withName("mytimer") .build(); Timer timerWithoutTags = registry.timer(metadata); Timer timerRed = registry.timer(metadata, new Tag("color", "red")); Timer timerBlue = registry.timer(metadata, new Tag("color", "blue"), new Tag("foo", "bar")); timerWithoutTags.update(Duration.ofSeconds(1)); timerRed.update(Duration.ofSeconds(2)); timerBlue.update(Duration.ofSeconds(3)); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mytimer").toString(); JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject(); JsonObject mytimerObject = json.getJsonObject("mytimer"); assertEquals(1.0, mytimerObject.getJsonNumber("count").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("count;color=red").doubleValue(), 1e-10); assertEquals(1.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(3.0, mytimerObject.getJsonNumber("elapsedTime;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("p50").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("p50;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("p50;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("p75").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("p75;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("p75;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("p95").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("p95;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("p95;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("p98").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("p98;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("p98;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("p99").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("p99;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("p99;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("p999").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("p999;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("p999;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("min").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("min;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("min;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("mean").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("mean;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("mean;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(1.0, mytimerObject.getJsonNumber("max").doubleValue(), 1e-10); assertEquals(2.0, mytimerObject.getJsonNumber("max;color=red").doubleValue(), 1e-10); assertEquals(3.0, mytimerObject.getJsonNumber("max;color=blue;foo=bar").doubleValue(), 1e-10); assertEquals(0.0, mytimerObject.getJsonNumber("stddev").doubleValue(), 1e-10); assertEquals(0.0, mytimerObject.getJsonNumber("stddev;color=red").doubleValue(), 1e-10); assertEquals(0.0, mytimerObject.getJsonNumber("stddev;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();  }