@Test public void makeSureTheDocSaysWeAreNotFineIfTheCountersDontChangeBetweenTwoSamples() throws Exception { Counting externalWorkCounter = mock(Counting.class); Counting interestingEventsObservedCounter = mock(Counting.class); when(externalWorkCounter.getCount()).thenReturn((long)0).thenReturn((long)500).thenReturn((long) 500); when(interestingEventsObservedCounter.getCount()).thenReturn((long)0).thenReturn((long)500).thenReturn((long)1500); ReplicatorDoctor doc = new ReplicatorDoctor(externalWorkCounter, "", mock(Logger.class), interestingEventsObservedCounter); // first reading is always OK, because there is no data for comparison assertTrue(doc.makeHealthAssessment().isOk()); assertTrue(doc.makeHealthAssessment().isOk()); assertFalse(doc.makeHealthAssessment().isOk()); }
@Test public void makeSureTheDocSaysWeAreFineIfWeHaventSeenAnythingInterestingAndHaventPushedNothingOutsideBetweenTwoSamples() throws Exception { Counting externalWorkCounter = mock(Counting.class); Counting interestingEventsObservedCounter = mock(Counting.class); when(externalWorkCounter.getCount()).thenReturn((long) 0).thenReturn((long)500).thenReturn((long) 500); when(interestingEventsObservedCounter.getCount()).thenReturn((long)0).thenReturn((long)500).thenReturn((long)500); ReplicatorDoctor doc = new ReplicatorDoctor(externalWorkCounter, "", mock(Logger.class), interestingEventsObservedCounter); // first reading is always OK, because there is no data for comparison assertTrue(doc.makeHealthAssessment().isOk()); assertTrue(doc.makeHealthAssessment().isOk()); assertTrue(doc.makeHealthAssessment().isOk()); }
/** * Creates a {@link DropwizardMetricRegistry} with an {@link Slf4jReporter}. Only non-zero metrics * will be logged to the {@link Slf4jReporter}. * * @param registry The registry on which to add the reporter. * @param logger The {@link Logger} to report to * @param period the amount of time between polls * @param unit the unit for {@code period} */ public static void createSlf4jReporter(DropwizardMetricRegistry registry, Logger logger, long period, TimeUnit unit) { MetricFilter nonZeroMatcher = new MetricFilter() { @Override public boolean matches(String name, Metric metric) { if (metric instanceof Counting) { Counting counter = (Counting) metric; return counter.getCount() > 0; } return true; } }; Slf4jReporter.forRegistry(registry.getRegistry()) .outputTo(logger) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .filter(nonZeroMatcher) .build() .start(period, unit); }
void reportCounter(Map.Entry<String, ? extends Counting> entry, String typeDimValue, List<MetricDatum> data) { Counting metric = entry.getValue(); final long diff = diffLast(metric); if (diff == 0) { // Don't submit metrics that have not changed. No reason to keep these alive. Also saves on CloudWatch // costs. return; } DemuxedKey key = new DemuxedKey(appendGlobalDimensions(entry.getKey())); Iterables.addAll(data, key.newDatums(typeDimName, typeDimValue, new Function<MetricDatum, MetricDatum>() { @Override public MetricDatum apply(MetricDatum datum) { return datum.withValue((double) diff).withUnit(StandardUnit.Count); } })); }
private void register(Object[] tagValues, Function<TagEncodedMetricName, Counting> countingSupplier) { String key = getKey(tagValues); Map<String, String> tagMap = new HashMap<>(tagValues.length); for (int i = 0; i < tags.length; i++) { tagMap.put(tags[i], String.valueOf(tagValues[i])); } counters.put(key, countingSupplier.apply(rootMetric.withTags(tagMap))); }
@Override public boolean matches(MetricName metricName, Metric metric) { if (metric instanceof Gauge) { final Object value = ((Gauge) metric).getValue(); // Java, I hate you :( if (value instanceof Long) return (Long) value != 0; if (value instanceof Integer) return (Integer) value != 0; if (value instanceof Float) return (Float) value != 0; if (value instanceof Double) return (Double) value != 0; else return true; } else if (metric instanceof Counting) { return ((Counting)metric).getCount() != 0; } return false; }
private void reportCounter(MetricId key, Counting value) { key = MetricId.join(prefix, key); final Metric m = FastForward .metric(key.getKey()) .attributes(key.getTags()) .attribute(METRIC_TYPE, "counter"); send(m.value(value.getCount())); }
private void processCounter(final String metricName, final Counting counter, final List<MetricDatum> metricData) { long currentCount = counter.getCount(); Long lastCount = lastPolledCounts.get(counter); lastPolledCounts.put(counter, currentCount); if (lastCount == null) { lastCount = 0L; } // Only submit metrics that have changed - let's save some money! final long delta = currentCount - lastCount; stageMetricDatum(true, metricName, delta, StandardUnit.Count, DIMENSION_COUNT, metricData); }
/** * @param eventsPushedToWorldCounter - the counter which helps measure the work the Replicator pushes to the outside world * @param counterDescription - counterDescription * @param logger - logger * @param interestingEventsObservedCounter - the counter which shows how many interesting events the Replicator has observed (the # of events * that have reached the Applier) in the databases being monitored */ public ReplicatorDoctor( Counting eventsPushedToWorldCounter, String counterDescription, Logger logger, Counting interestingEventsObservedCounter) { if (eventsPushedToWorldCounter == null) { throw new IllegalArgumentException("eventsPushedToWorldCounter must be not null"); } if (interestingEventsObservedCounter == null) { throw new IllegalArgumentException("interestingEventsObservedCounter must be not null"); } if (logger == null) { throw new IllegalArgumentException("logger must be not null"); } this.counterDescription = counterDescription; this.globallyImpactingEventsCounter = eventsPushedToWorldCounter; this.logger = logger; this.numberOfInterestingEventsObserved = interestingEventsObservedCounter; }
@Test public void makeSureTheDocSaysWeAreFineIfTheCountersChangeBetweenTwoSamples() throws Exception { Counting externalWorkCounter = mock(Counting.class); Counting interestingEventsObservedCounter = mock(Counting.class); when(externalWorkCounter.getCount()).thenReturn((long) 0).thenReturn((long)500); when(interestingEventsObservedCounter.getCount()).thenReturn((long)500).thenReturn((long)1500); ReplicatorDoctor doc = new ReplicatorDoctor(externalWorkCounter, "", mock(Logger.class), interestingEventsObservedCounter); assertTrue(doc.makeHealthAssessment().isOk()); assertTrue(doc.makeHealthAssessment().isOk()); }
private boolean canSkipMetric(String name, Counting counting) { boolean isIdle = (calculateDelta(name, counting.getCount()) == 0); if (skipIdleMetrics && !isIdle) { previousValues.put(name, counting.getCount()); } return skipIdleMetrics && isIdle; }
private static Snapshot toSnapshot( String name, Metric value ) { Snapshot snapshot = new Snapshot( name ); if( value instanceof Sampling ) snapshot.mean = ( ( Sampling ) value ).getSnapshot().getMean(); if( value instanceof Metered ) snapshot.meanRate = ( ( Metered ) value ).getMeanRate(); if( value instanceof Counting ) snapshot.count = ( ( Counting ) value ).getCount(); if( value instanceof Gauge ) snapshot.count = ( ( Number ) ( ( Gauge ) value ).getValue() ).longValue(); return snapshot; }
/** * Returns true if this metric is idle and should be skipped. * * @param name * @param counting * @return true if the metric should be skipped */ protected boolean canSkipMetric(String name, Counting counting) { boolean isIdle = calculateDelta(name, counting.getCount()) == 0L; if (skipIdleMetrics && !isIdle) { previousValues.put(name, counting.getCount()); } return skipIdleMetrics && isIdle; }
@Override public String getIndex() { TemplateCall call = templater.template(PREFIX + "index.html"); call.set("gauges", registry.getGauges().entrySet()); call.set("counters", this.<Counting>combine(registry.getCounters(), registry.getMeters(), registry.getTimers(), registry.getHistograms()).entrySet()); call.set("meters", this.<Metered>combine(registry.getMeters(), registry.getTimers()).entrySet()); call.set("histograms", this.<Sampling>combine(registry.getHistograms(), registry.getTimers()).entrySet()); return call.process(); }
private long diffLast(Counting metric) { long count = metric.getCount(); Long lastCount = lastPolledCounts.get(metric); lastPolledCounts.put(metric, count); if (lastCount == null) { lastCount = 0L; } return count - lastCount; }
private void reportCounter( final BatchBuilder builder, final Counting value ) { builder.buildPoint("count", value.getCount()); }
/** * {@inheritDoc} * @see com.heliosapm.streams.jmx.metrics.MetricType.AttributeAdapter#invoke(com.codahale.metrics.Metric) */ @Override public Object invoke(final Metric metric) { return ((Counting)metric).getCount(); }
Stream<MetricPart<Counting, Long>> countings() { return COUNTINGS.stream() .filter(metricPart -> metricPredicate.test(metricPart.getSuffix())); }
public CountingAdapter(Counting counter, String name, String description, ValueSemantics valueSemantics) { this.counter = counter; this.monitoredValue = new NonSelfRegisteringSettableValue<>(name, description, ONE, counter.getCount(), valueSemantics); }
private void addCounting(String metricName, Counting value, long scale) { context.getCounter("morphline", metricName).increment(value.getCount() / scale); }
private static void assertSummary(Map<String, ?> map, String property, int expectedCount) { assertThat(((Counting) map.get(clientMetricName(property))).getCount()).isEqualTo(expectedCount); assertThat(((Sampling) map.get(clientMetricName(property))).getSnapshot().getMean()).isPositive(); assertThat(((Counting) map.get(serverMetricName(property))).getCount()).isEqualTo(expectedCount); assertThat(((Sampling) map.get(serverMetricName(property))).getSnapshot().getMean()).isPositive(); }
/** * Returns the appropriate function depending on configuration sent from the user interface. * * @param dto an object describing the type of function to create * @return a function that yields a number */ public Supplier<Number> convert(MetricDto dto) { Metric metric = getMetric(dto); Supplier<Number> ret = null; switch (dto.getValue()) { case COUNT: ret = new Supplier<Number>() { @Override public Long get() { return ((Counting) metric).getCount(); } }; break; case MEAN: ret = new Supplier<Number>() { @Override public Double get() { return ((Metered) metric).getMeanRate(); } }; break; case ONE_MINUTE: ret = new Supplier<Number>() { @Override public Double get() { return ((Metered) metric).getOneMinuteRate(); } }; break; case FIVE_MINUTE: ret = new Supplier<Number>() { @Override public Double get() { return ((Metered) metric).getFiveMinuteRate(); } }; break; case FIFTEEN_MINUTE: ret = new Supplier<Number>() { @Override public Double get() { return ((Metered) metric).getFifteenMinuteRate(); } }; break; } return ret; }
private void reportCounter(List<Point> points, String prefix, String name, Counting counter, long timestamp) throws IOException { String metricName = getKey(prefix, name, COUNT.getName()); points.add(buildMetricAsPoint(metricName, counter.getCount(), false, timestamp)); }
private void reportCounter(String prefix, String name, Counting counter, long timestamp) throws IOException { String metricName = getKey(prefix, name, COUNT.getName()); pushMetric(metricName, counter.getCount(), false, timestamp); }
private void addCounting(Context context, String metricName, Counting value, long scale) { final String COUNTER_GROUP = "HBase Indexer Metrics"; context.getCounter(COUNTER_GROUP, metricName).increment(value.getCount() / scale); }
/** * Extracts metrics from {@link com.codahale.metrics.Counter}. * @param name name of the {@link com.codahale.metrics.Counter}. * @param counter instance of {@link com.codahale.metrics.Counter} to serialize. * @return a list of {@link gobblin.metrics.Metric}. */ protected List<Metric> serializeCounter(String name, Counting counter) { return Lists.newArrayList( serializeValue(name, counter.getCount(), Measurements.COUNT.name()) ); }
/** * Extracts metrics from {@link com.codahale.metrics.Counter}. * * @param name name of the {@link com.codahale.metrics.Counter}. * @param counter instance of {@link com.codahale.metrics.Counter} to serialize. * @return a list of {@link org.apache.gobblin.metrics.Metric}. */ protected List<Metric> serializeCounter(String name, Counting counter) { return Lists.newArrayList( serializeValue(name, counter.getCount(), Measurements.COUNT.name()) ); }