/** {@inheritDoc} */ public List<Metric> poll(MetricFilter filter, boolean reset) { refreshMonitorCache(filter); List<Monitor<?>> monitors = cachedMonitors.get(); List<Metric> metrics = Lists.newArrayListWithCapacity(monitors.size()); for (Monitor<?> monitor : monitors) { Object v = getValue(monitor, reset); if (v != null) { long now = System.currentTimeMillis(); metrics.add(new Metric(monitor.getConfig(), now, v)); } } return metrics; }
private Object getValue(Monitor<?> monitor, boolean reset) { try { if (limiter != null) { final MonitorValueCallable c = new MonitorValueCallable(monitor); return limiter.callWithTimeout(c, 1, TimeUnit.SECONDS, true); } else { return monitor.getValue(); } } catch (UncheckedTimeoutException e) { LOGGER.warn("timeout trying to get value for {}", monitor.getConfig()); } catch (Exception e) { LOGGER.warn("failed to get value for " + monitor.getConfig(), e); } return null; }
private void getMonitors(List<Monitor<?>> monitors, MetricFilter filter, Monitor<?> monitor) { if (monitor instanceof CompositeMonitor<?>) { for (Monitor<?> m : ((CompositeMonitor<?>) monitor).getMonitors()) { getMonitors(monitors, filter, m); } } else if (filter.matches(monitor.getConfig())) { monitors.add(monitor); } }
private void refreshMonitorCache(MetricFilter filter) { final long age = System.currentTimeMillis() - cacheLastUpdateTime.get(); if (age > cacheTTL) { List<Monitor<?>> monitors = Lists.newArrayList(); for (Monitor<?> monitor : registry.getRegisteredMonitors()) { try { getMonitors(monitors, filter, monitor); } catch (Exception e) { LOGGER.warn("failed to get monitors for composite " + monitor.getConfig(), e); } } cacheLastUpdateTime.set(System.currentTimeMillis()); cachedMonitors.set(monitors); LOGGER.debug( "cache refreshed, {} monitors matched filter, previous age {} seconds", monitors.size(), age / 1000); } else { LOGGER.debug( "cache age of {} seconds is within ttl of {} seconds", age / 1000, cacheTTL / 1000); } }
public Object call() throws Exception { return monitor.getValue(); }