@Test public void testCounter() { System.out.println("******************************* COUNTER *******************************"); counter = registry.counter("counter"); try { for (int i = 0; i < ITER_COUNT; i++) { counter.inc(i); Thread.sleep(SLEEP_MS); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } }
@Test public void testMeter() { System.out.println("******************************* METER *******************************"); meter = registry.meter("meter"); try { for (int i = 0; i < ITER_COUNT; i++) { meter.mark(); Thread.sleep(SLEEP_MS); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } }
@Test public void testHistogram() { System.out.println("******************************* HISTOGRAM *******************************"); histogram = registry.histogram("histogram"); try { for (int i = 0; i < ITER_COUNT; i++) { histogram.update(i); Thread.sleep(SLEEP_MS); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } }
@Test public void testTimer() { System.out.println("******************************* TIMER *******************************"); timer = registry.timer("timer"); try { for (int i = 0; i < ITER_COUNT; i++) { final Timer.Context context = timer.time(); Thread.sleep(SLEEP_MS); context.stop(); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } }
@Test public void testGauge() { System.out.println("******************************* GAUGE *******************************"); gauge = new Gauge<Integer>() { @Override public Integer getValue() { return count++; } }; registry.register("gauge", gauge); try { for (int i = 0; i < ITER_COUNT; i++) { gauge.getValue(); Thread.sleep(SLEEP_MS); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } }
/** Returns an unmodifiable set of the current predefined metrics. */ public static Set<MetricType> getPredefinedMetrics() { return registry.predefinedMetrics(); }
/** * Removes the given metric type from the registry of predefined metrics to be captured at the AWS * SDK level. * * @return true if the set of predefined metric types gets changed as a result of the call */ public static boolean remove(MetricType type) { return type == null ? false : registry.removeMetricType(type); }
/** * Sets the given metric types to replace the registry of predefined metrics to be captured at the * AWS SDK level. */ public static <T extends MetricType> void set(Collection<T> types) { registry.setMetricTypes(types); }
/** * Adds the given metric types to the registry of predefined metrics to be captured at the AWS SDK * level. * * @return true if the set of predefined metric types gets changed as a result of the call */ public static <T extends MetricType> boolean addAll(Collection<T> types) { return types == null || types.size() == 0 ? false : registry.addMetricTypes(types); }
/** * Adds the given metric type to the registry of predefined metrics to be captured at the AWS SDK * level. * * @return true if the set of predefined metric types gets changed as a result of the call */ public static boolean add(MetricType type) { return type == null ? false : registry.addMetricType(type); }
static { Configuration config = Configuration.getInstance(); // register jvm metrics MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); if (!System.getProperty("java.version").split("\\.")[1].equals("6")) { // if not running 1.6 registry.registerAll( new PrefixedMetricSet(new BufferPoolMetricSet(mbs), JVM_PREFIX, "buffer-pool")); } registry.registerAll(new PrefixedMetricSet(new GarbageCollectorMetricSet(), JVM_PREFIX, "gc")); registry.registerAll(new PrefixedMetricSet(new MemoryUsageGaugeSet(), JVM_PREFIX, "memory")); registry.registerAll( new PrefixedMetricSet(new ThreadStatesGaugeSet(), JVM_PREFIX, "thread-states")); // instrument log4j InstrumentedAppender appender = new InstrumentedAppender(registry); appender.activateOptions(); LogManager.getRootLogger().addAppender(appender); if (!config.getStringProperty(CoreConfig.RIEMANN_HOST).equals("")) { RiemannReporter tmpreporter; try { Riemann riemann = new Riemann( config.getStringProperty(CoreConfig.RIEMANN_HOST), config.getIntegerProperty(CoreConfig.RIEMANN_PORT)); RiemannReporter.Builder builder = RiemannReporter.forRegistry(registry) .convertDurationsTo(TimeUnit.MILLISECONDS) .convertRatesTo(TimeUnit.SECONDS); if (!config.getStringProperty(CoreConfig.RIEMANN_SEPARATOR).isEmpty()) { builder.useSeparator(config.getStringProperty(CoreConfig.RIEMANN_SEPARATOR)); } if (!config.getStringProperty(CoreConfig.RIEMANN_TTL).isEmpty()) { builder.withTtl(config.getFloatProperty(CoreConfig.RIEMANN_TTL)); } if (!config.getStringProperty(CoreConfig.RIEMANN_LOCALHOST).isEmpty()) { builder.localHost(config.getStringProperty(CoreConfig.RIEMANN_LOCALHOST)); } if (!config.getStringProperty(CoreConfig.RIEMANN_PREFIX).isEmpty()) { builder.prefixedWith(config.getStringProperty(CoreConfig.RIEMANN_PREFIX)); } if (!config.getStringProperty(CoreConfig.RIEMANN_TAGS).isEmpty()) { builder.tags(config.getListProperty(CoreConfig.RIEMANN_TAGS)); } tmpreporter = builder.build(riemann); tmpreporter.start(30l, TimeUnit.SECONDS); } catch (IOException e) { tmpreporter = null; } reporter1 = tmpreporter; } else { reporter1 = null; } if (!config.getStringProperty(CoreConfig.GRAPHITE_HOST).equals("")) { Graphite graphite = new Graphite( new InetSocketAddress( config.getStringProperty(CoreConfig.GRAPHITE_HOST), config.getIntegerProperty(CoreConfig.GRAPHITE_PORT))); reporter = GraphiteReporter.forRegistry(registry) .convertDurationsTo(TimeUnit.MILLISECONDS) .convertRatesTo(TimeUnit.SECONDS) .prefixedWith(config.getStringProperty(CoreConfig.GRAPHITE_PREFIX)) .build(graphite); reporter.start(30l, TimeUnit.SECONDS); } else { reporter = null; } reporter2 = JmxReporter.forRegistry(registry) .convertDurationsTo(TimeUnit.MILLISECONDS) .convertRatesTo(TimeUnit.SECONDS) .build(); reporter2.start(); }
public static Counter counter(Class kls, String... names) { return getRegistry().counter(MetricRegistry.name(kls, names)); }
public static Histogram histogram(Class kls, String... names) { return getRegistry().histogram(MetricRegistry.name(kls, names)); }
public static Timer timer(Class kls, String... names) { return getRegistry().timer(MetricRegistry.name(kls, names)); }
public static Meter meter(Class kls, String... names) { return getRegistry().meter(MetricRegistry.name(kls, names)); }
public class InstrumentedScheduledExecutorServiceTest { private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(); private final MetricRegistry registry = new MetricRegistry(); private final InstrumentedScheduledExecutorService instrumentedScheduledExecutor = new InstrumentedScheduledExecutorService(scheduledExecutor, registry, "xs"); final Meter submitted = registry.meter("xs.submitted"); final Counter running = registry.counter("xs.running"); final Meter completed = registry.meter("xs.completed"); final Timer duration = registry.timer("xs.duration"); final Meter scheduledOnce = registry.meter("xs.scheduled.once"); final Meter scheduledRepetitively = registry.meter("xs.scheduled.repetitively"); final Counter scheduledOverrun = registry.counter("xs.scheduled.overrun"); final Histogram percentOfPeriod = registry.histogram("xs.scheduled.percent-of-period"); @Test public void testSubmitRunnable() throws Exception { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); Future<?> theFuture = instrumentedScheduledExecutor.submit( new Runnable() { public void run() { assertThat(submitted.getCount()).isEqualTo(1); assertThat(running.getCount()).isEqualTo(1); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); } }); theFuture.get(); assertThat(submitted.getCount()).isEqualTo(1); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isEqualTo(1); assertThat(duration.getCount()).isEqualTo(1); assertThat(duration.getSnapshot().size()).isEqualTo(1); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); } @Test public void testScheduleRunnable() throws Exception { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); ScheduledFuture<?> theFuture = instrumentedScheduledExecutor.schedule( new Runnable() { public void run() { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isEqualTo(1); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isEqualTo(1); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); } }, 10L, TimeUnit.MILLISECONDS); theFuture.get(); assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isEqualTo(1); assertThat(duration.getCount()).isEqualTo(1); assertThat(duration.getSnapshot().size()).isEqualTo(1); assertThat(scheduledOnce.getCount()).isEqualTo(1); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); } @Test public void testSubmitCallable() throws Exception { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); final Object obj = new Object(); Future<Object> theFuture = instrumentedScheduledExecutor.submit( new Callable<Object>() { public Object call() { assertThat(submitted.getCount()).isEqualTo(1); assertThat(running.getCount()).isEqualTo(1); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); return obj; } }); assertThat(theFuture.get()).isEqualTo(obj); assertThat(submitted.getCount()).isEqualTo(1); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isEqualTo(1); assertThat(duration.getCount()).isEqualTo(1); assertThat(duration.getSnapshot().size()).isEqualTo(1); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); } @Test public void testScheduleCallable() throws Exception { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); final Object obj = new Object(); ScheduledFuture<Object> theFuture = instrumentedScheduledExecutor.schedule( new Callable<Object>() { public Object call() { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isEqualTo(1); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isEqualTo(1); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); return obj; } }, 10L, TimeUnit.MILLISECONDS); assertThat(theFuture.get()).isEqualTo(obj); assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isEqualTo(1); assertThat(duration.getCount()).isEqualTo(1); assertThat(duration.getSnapshot().size()).isEqualTo(1); assertThat(scheduledOnce.getCount()).isEqualTo(1); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); } @Test public void testScheduleFixedRateCallable() throws Exception { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isZero(); assertThat(duration.getCount()).isZero(); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isZero(); assertThat(scheduledOverrun.getCount()).isZero(); assertThat(percentOfPeriod.getCount()).isZero(); ScheduledFuture<?> theFuture = instrumentedScheduledExecutor.scheduleAtFixedRate( new Runnable() { public void run() { assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isEqualTo(1); assertThat(scheduledOnce.getCount()).isEqualTo(0); assertThat(scheduledRepetitively.getCount()).isEqualTo(1); try { TimeUnit.MILLISECONDS.sleep(50); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } return; } }, 10L, 10L, TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS.sleep(100); theFuture.cancel(true); TimeUnit.MILLISECONDS.sleep(100); assertThat(submitted.getCount()).isZero(); assertThat(running.getCount()).isZero(); assertThat(completed.getCount()).isNotEqualTo(0); assertThat(duration.getCount()).isNotEqualTo(0); assertThat(duration.getSnapshot().size()).isNotEqualTo(0); assertThat(scheduledOnce.getCount()).isZero(); assertThat(scheduledRepetitively.getCount()).isEqualTo(1); assertThat(scheduledOverrun.getCount()).isNotEqualTo(0); assertThat(percentOfPeriod.getCount()).isNotEqualTo(0); } @After public void tearDown() throws Exception { instrumentedScheduledExecutor.shutdown(); if (!instrumentedScheduledExecutor.awaitTermination(2, TimeUnit.SECONDS)) { System.err.println("InstrumentedScheduledExecutorService did not terminate."); } } }