private Delta<?> calculateDelta(Metric<?> value) { long delta = value.getValue().longValue(); Long old = this.counts.replace(value.getName(), delta); if (old != null) { delta = delta - old; } else { this.counts.putIfAbsent(value.getName(), delta); } return new Delta<Long>(value.getName(), delta, value.getTimestamp()); }
@Override public void set(String group, Collection<Metric<?>> values) { String prefix = group; if (!prefix.endsWith(".")) { prefix = prefix + "."; } for (Metric<?> metric : values) { if (!metric.getName().startsWith(prefix)) { metric = new Metric<Number>(prefix + metric.getName(), metric.getValue(), metric.getTimestamp()); } set(metric); } this.groups.add(group); }
@Test public void testMetrics() throws Exception { InMemoryRichGaugeRepository repository = new InMemoryRichGaugeRepository(); repository.set(new Metric<Double>("a", 0.d, new Date())); repository.set(new Metric<Double>("a", 0.5d, new Date())); RichGaugeReaderPublicMetrics metrics = new RichGaugeReaderPublicMetrics(repository); Map<String, Metric<?>> results = new HashMap<String, Metric<?>>(); for (Metric<?> metric : metrics.metrics()) { results.put(metric.getName(), metric); } assertThat(results.containsKey("a.val")).isTrue(); assertThat(results.get("a.val").getValue().doubleValue()).isEqualTo(0.5d); assertThat(results.containsKey("a.avg")).isTrue(); assertThat(results.get("a.avg").getValue().doubleValue()).isEqualTo(0.25d); assertThat(results.containsKey("a.min")).isTrue(); assertThat(results.get("a.min").getValue().doubleValue()).isEqualTo(0.0d); assertThat(results.containsKey("a.max")).isTrue(); assertThat(results.get("a.max").getValue().doubleValue()).isEqualTo(0.5d); assertThat(results.containsKey("a.count")).isTrue(); assertThat(results.get("a.count").getValue().longValue()).isEqualTo(2L); assertThat(results.containsKey("a.alpha")).isTrue(); assertThat(results.get("a.alpha").getValue().doubleValue()).isEqualTo(-1.d); }
private boolean isMatch(Metric<?> metric) { String[] includes = MetricCopyExporter.this.includes; String[] excludes = MetricCopyExporter.this.excludes; String name = metric.getName(); if (ObjectUtils.isEmpty(includes) || PatternMatchUtils.simpleMatch(includes, name)) { return !PatternMatchUtils.simpleMatch(excludes, name); } return false; }
@Test public void prefixWithPeriod() { this.repository.increment(new Delta<Number>("foo.bar", 1)); Set<String> names = new HashSet<String>(); for (Metric<?> metric : this.repository.findAll("foo.")) { names.add(metric.getName()); } assertThat(names).hasSize(1); assertThat(names.contains("foo.bar")).isTrue(); }
@Override protected void write(String group, Collection<Metric<?>> values) { for (Metric<?> value : values) { if (value.getName().startsWith("counter.") && this.counter != null) { this.counter.increment(calculateDelta(value)); } else { this.writer.set(value); } } }
@Test public void onlyRegisteredPrefixCounted() { this.repository.increment(new Delta<Number>("foo.bar", 1)); this.repository.increment(new Delta<Number>("foobar.spam", 1)); Set<String> names = new HashSet<String>(); for (Metric<?> metric : this.repository.findAll("foo")) { names.add(metric.getName()); } assertThat(names).hasSize(1); assertThat(names.contains("foo.bar")).isTrue(); }
@Test public void testMetrics() throws Exception { InMemoryMetricRepository repository = new InMemoryMetricRepository(); repository.set(new Metric<Double>("a", 0.5, new Date())); VanillaPublicMetrics publicMetrics = new VanillaPublicMetrics(repository); Map<String, Metric<?>> results = new HashMap<String, Metric<?>>(); for (Metric<?> metric : publicMetrics.metrics()) { results.put(metric.getName(), metric); } assertThat(results.get("a").getValue().doubleValue(), equalTo(0.5)); }
@Test public void incrementGroup() { this.repository.increment("foo", new Delta<Number>("foo.bar", 1)); this.repository.increment("foo", new Delta<Number>("foo.bar", 2)); this.repository.increment("foo", new Delta<Number>("foo.spam", 1)); Set<String> names = new HashSet<String>(); for (Metric<?> metric : this.repository.findAll("foo")) { names.add(metric.getName()); } assertThat(names).hasSize(2); assertThat(names.contains("foo.bar")).isTrue(); assertThat(this.repository.findOne("foo.bar").getValue()).isEqualTo(3L); }
@Test public void testAdditionalMetrics() throws Exception { InMemoryMetricRepository repository = new InMemoryMetricRepository(); Collection<PublicMetrics> allMetrics = new ArrayList<PublicMetrics>(); allMetrics.add(new ImmutablePublicMetrics(new Metric<Number>("first", 2L))); allMetrics.add(new ImmutablePublicMetrics(new Metric<Number>("second", 4L))); VanillaPublicMetrics publicMetrics = new VanillaPublicMetrics(repository, allMetrics); Map<String, Metric<?>> results = new HashMap<String, Metric<?>>(); for (Metric<?> metric : publicMetrics.metrics()) { results.put(metric.getName(), metric); } assertTrue(results.containsKey("first")); assertTrue(results.containsKey("second")); assertEquals(2, results.size()); }
@Override public void set(Metric<?> value) { this.metrics.set(value.getName(), value); }