@Test public void richGaugePublicMetrics() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( RichGaugeReaderConfig.class, MetricRepositoryAutoConfiguration.class, PublicMetricsAutoConfiguration.class); RichGaugeReader richGaugeReader = context.getBean(RichGaugeReader.class); assertThat(richGaugeReader).isNotNull(); given(richGaugeReader.findAll()) .willReturn(Collections.singletonList(new RichGauge("bar", 3.7d))); RichGaugeReaderPublicMetrics publicMetrics = context .getBean(RichGaugeReaderPublicMetrics.class); assertThat(publicMetrics).isNotNull(); Collection<Metric<?>> metrics = publicMetrics.metrics(); assertThat(metrics).isNotNull(); assertThat(6).isEqualTo(metrics.size()); assertHasMetric(metrics, new Metric<Double>("bar.val", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.avg", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.min", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.max", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.alpha", -1.d)); assertHasMetric(metrics, new Metric<Long>("bar.count", 1L)); context.close(); }
@Test public void richGaugePublicMetrics() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( RichGaugeReaderConfig.class, MetricRepositoryAutoConfiguration.class, PublicMetricsAutoConfiguration.class); RichGaugeReader richGaugeReader = context.getBean(RichGaugeReader.class); assertNotNull(richGaugeReader); given(richGaugeReader.findAll()) .willReturn(Collections.singletonList(new RichGauge("bar", 3.7d))); RichGaugeReaderPublicMetrics publicMetrics = context .getBean(RichGaugeReaderPublicMetrics.class); assertNotNull(publicMetrics); Collection<Metric<?>> metrics = publicMetrics.metrics(); assertNotNull(metrics); assertEquals(metrics.size(), 6); assertHasMetric(metrics, new Metric<Double>("bar.val", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.avg", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.min", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.max", 3.7d)); assertHasMetric(metrics, new Metric<Double>("bar.alpha", -1.d)); assertHasMetric(metrics, new Metric<Long>("bar.count", 1L)); context.close(); }
@Override public String format(List<RichGauge> gauges, List<Metric<?>> metrics) { StringBuilder builder = new StringBuilder("\n########## Metrics Start ##########\n"); if (gauges != null) { for (RichGauge gauge : gauges) { builder.append(gauge.toString() + "\n"); } } if (metrics != null) { for (Metric<?> metric : metrics) { builder.append(metric.toString() + "\n"); } } builder.append("########## Metrics End ############"); return builder.toString(); }
@Bean public MetricsOutputFormatter metricsOutputFormatter() { return new MetricsOutputFormatter() { @Override public String format(List<RichGauge> gauges, List<Metric<?>> metrics) { StringBuilder builder = new StringBuilder("\n########## Personal Header for metrics! #####\n########## Metrics Start ##########\n"); if (gauges != null) { for (RichGauge gauge : gauges) { builder.append(gauge.toString() + "\n"); } } if (metrics != null) { for (Metric<?> metric : metrics) { builder.append(metric.toString() + "\n"); } } builder.append("########## Metrics End ############"); return builder.toString(); } }; }
@Override protected Iterable<Metric<?>> next(String group) { RichGauge rich = this.reader.findOne(group); Collection<Metric<?>> metrics = new ArrayList<Metric<?>>(); metrics.add(new Metric<Number>(group + MIN, rich.getMin())); metrics.add(new Metric<Number>(group + MAX, rich.getMax())); metrics.add(new Metric<Number>(group + COUNT, rich.getCount())); metrics.add(new Metric<Number>(group + VALUE, rich.getValue())); metrics.add(new Metric<Number>(group + AVG, rich.getAverage())); metrics.add(new Metric<Number>(group + ALPHA, rich.getAlpha())); return metrics; }
@Override protected Iterable<String> groups() { Collection<String> names = new HashSet<String>(); for (RichGauge rich : this.reader.findAll()) { names.add(rich.getName()); } return names; }
@Override public Collection<Metric<?>> metrics() { List<Metric<?>> result = new ArrayList<Metric<?>>(); for (RichGauge richGauge : this.richGaugeReader.findAll()) { result.addAll(convert(richGauge)); } return result; }
private List<Metric<?>> convert(RichGauge gauge) { List<Metric<?>> result = new ArrayList<Metric<?>>(6); result.add( new Metric<Double>(gauge.getName() + RichGauge.AVG, gauge.getAverage())); result.add(new Metric<Double>(gauge.getName() + RichGauge.VAL, gauge.getValue())); result.add(new Metric<Double>(gauge.getName() + RichGauge.MIN, gauge.getMin())); result.add(new Metric<Double>(gauge.getName() + RichGauge.MAX, gauge.getMax())); result.add( new Metric<Double>(gauge.getName() + RichGauge.ALPHA, gauge.getAlpha())); result.add(new Metric<Long>(gauge.getName() + RichGauge.COUNT, gauge.getCount())); return result; }
private List<RichGauge> exportBatchRichGauges() { List<RichGauge> gauges = new ArrayList<RichGauge>(); for (RichGauge gauge : richGaugeReader.findAll()) { gauges.add(gauge); } return gauges; }
public String format(List<RichGauge> gauges, List<Metric<?>> metrics);