@Test @InSequence(1) public void countedMethodNotCalledYet() { assertThat("Counter is not registered correctly", registry.getCounters(), hasKey(COUNTER_NAME)); Counter counter = registry.getCounters().get(COUNTER_NAME); // Make sure that the counter hasn't been called yet assertThat("Counter count is incorrect", counter.getCount(), is(equalTo(COUNTER_COUNT.get()))); }
/** * Update record-level metrics. * * @param recordsWritten number of records written by the writer * @param branchIndex fork branch index */ public void updateRecordMetrics(long recordsWritten, int branchIndex) { TaskMetrics metrics = TaskMetrics.get(this); String forkBranchId = ForkOperatorUtils.getForkId(this.taskId, branchIndex); Counter taskRecordCounter = metrics.getCounter(gobblin.runtime.util.MetricGroup.TASK.name(), forkBranchId, RECORDS); long inc = recordsWritten - taskRecordCounter.getCount(); taskRecordCounter.inc(inc); metrics.getMeter(MetricGroup.TASK.name(), forkBranchId, RECORDS_PER_SECOND).mark(inc); metrics.getCounter(MetricGroup.JOB.name(), this.jobId, RECORDS).inc(inc); metrics.getMeter(MetricGroup.JOB.name(), this.jobId, RECORDS_PER_SECOND).mark(inc); }
public void printMetrics() { logger.info("Metrics"); Metrics metrics = session.getCluster().getMetrics(); Gauge<Integer> gauge = metrics.getConnectedToHosts(); Integer numberOfHosts = gauge.getValue(); logger.info("Number of hosts: " + numberOfHosts); Metrics.Errors errors = metrics.getErrorMetrics(); Counter counter = errors.getReadTimeouts(); logger.info("Number of read timeouts:" + counter.getCount()); com.codahale.metrics.Timer timer = metrics.getRequestsTimer(); Timer.Context context = timer.time(); try { long numberUserRequests = timer.getCount(); logger.info("Number of user requests:" + numberUserRequests); } finally { context.stop(); } }
@Test @InSequence(4) public void removeCounterFromRegistry() { assertThat("Counter is not registered correctly", registry.getCounters(), hasKey(COUNTER_NAME)); Counter counter = registry.getCounters().get(COUNTER_NAME); // Remove the counter from metrics registry registry.remove(COUNTER_NAME); try { // Call the counted method and assert an exception is thrown bean.countedMethod( new Callable<Long>() { @Override public Long call() throws Exception { return null; } }); } catch (Exception cause) { assertThat(cause, is(instanceOf(IllegalStateException.class))); assertThat( cause.getMessage(), is( equalTo( "No counter with name [" + COUNTER_NAME + "] found in registry [" + registry + "]"))); // Make sure that the counter hasn't been called assertThat( "Counter count is incorrect", counter.getCount(), is(equalTo(COUNTER_COUNT.get()))); return; } fail("No exception has been re-thrown!"); }
@Override public boolean matchesSafely(JsonObject jsonObject) { JsonObject jsonMetric = jsonObject.get("metric").asObject(); JsonObject jsonCounter; JsonObject jsonMeter; JsonObject jsonTimer; Counter counter; Meter meter; Timer timer; // check counter metric if (jsonMetric.get("counter") != null) { jsonCounter = jsonMetric.get("counter").asObject(); counter = (Counter) metric; if (jsonCounter.get("counter").asLong() != counter.getCount()) { reason = "counter " + counter.getCount(); return false; } } // check meter metric if (jsonMetric.get("meter") != null) { jsonMeter = jsonMetric.get("meter").asObject(); meter = (Meter) metric; if (jsonMeter.get("counter").asLong() != meter.getCount()) { reason = "counter " + meter.getCount(); return false; } if (jsonMeter.get("1_min_rate").asDouble() != meter.getOneMinuteRate()) { reason = "1 minute rate " + meter.getOneMinuteRate(); return false; } if (jsonMeter.get("5_min_rate").asDouble() != meter.getOneMinuteRate()) { reason = "5 minute rate " + meter.getFiveMinuteRate(); return false; } if (jsonMeter.get("15_min_rate").asDouble() != meter.getFifteenMinuteRate()) { reason = "15 minute rate " + meter.getFifteenMinuteRate(); return false; } } if (jsonMetric.get("timer") != null) { jsonTimer = jsonMetric.get("timer").asObject(); timer = (Timer) metric; if (jsonTimer.get("counter").asLong() != timer.getCount()) { reason = "counter " + timer.getCount(); return false; } if (jsonTimer.get("1_min_rate").asDouble() != timer.getOneMinuteRate()) { reason = "1 minute rate " + timer.getOneMinuteRate(); return false; } if (jsonTimer.get("5_min_rate").asDouble() != timer.getOneMinuteRate()) { reason = "5 minute rate " + timer.getFiveMinuteRate(); return false; } if (jsonTimer.get("15_min_rate").asDouble() != timer.getFifteenMinuteRate()) { reason = "15 minute rate " + timer.getFifteenMinuteRate(); return false; } if (jsonTimer.get("mean").asDouble() != nanoToMs(timer.getSnapshot().getMean())) { reason = "mean " + timer.getSnapshot().getMean(); return false; } if (jsonTimer.get("min").asDouble() != nanoToMs(timer.getSnapshot().getMin())) { reason = "min " + timer.getSnapshot().getMin(); return false; } if (jsonTimer.get("max").asDouble() != nanoToMs(timer.getSnapshot().getMax())) { reason = "max " + timer.getSnapshot().getMax(); return false; } if (jsonTimer.get("stddev").asDouble() != nanoToMs(timer.getSnapshot().getStdDev())) { reason = "stddev " + timer.getSnapshot().getStdDev(); return false; } } return true; }
@Test @InSequence(3) public void callCountedMethodOnce() throws InterruptedException, TimeoutException { assertThat("Counter is not registered correctly", registry.getCounters(), hasKey(COUNTER_NAME)); Counter counter = registry.getCounters().get(COUNTER_NAME); // Call the counted method, block and assert it's been counted final Exchanger<Long> exchanger = new Exchanger<>(); Thread thread = new Thread( new Runnable() { @Override public void run() { try { exchanger.exchange( bean.countedMethod( new Callable<Long>() { @Override public Long call() throws Exception { exchanger.exchange(0L); return exchanger.exchange(0L); } })); } catch (InterruptedException cause) { throw new RuntimeException(cause); } } }); final AtomicInteger uncaught = new AtomicInteger(); thread.setUncaughtExceptionHandler( new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { uncaught.incrementAndGet(); } }); thread.start(); // Wait until the method is executing and make sure that the counter has been incremented exchanger.exchange(0L, 5L, TimeUnit.SECONDS); assertThat( "Counter count is incorrect", counter.getCount(), is(equalTo(COUNTER_COUNT.incrementAndGet()))); // Exchange the result and unblock the method execution Long random = 1 + Math.round(Math.random() * (Long.MAX_VALUE - 1)); exchanger.exchange(random, 5L, TimeUnit.SECONDS); // Wait until the method has returned assertThat( "Counted method return value is incorrect", exchanger.exchange(0L), is(equalTo(random))); // Then make sure that the counter has been decremented assertThat( "Counter count is incorrect", counter.getCount(), is(equalTo(COUNTER_COUNT.decrementAndGet()))); // Finally make sure calling thread is returns correctly thread.join(); assertThat("Exception thrown in method call thread", uncaught.get(), is(equalTo(0))); }
public void addCounter(String name, Counter counter) { addGaugeMeasurement(name, counter.getCount()); }