public static <T> boolean register(Monitor<T> monitor) { try { DefaultMonitorRegistry.getInstance().register(monitor); } catch (Exception e) { logger.warn("Cannot register monitor {}", monitor.getConfig().getName()); if (logger.isDebugEnabled()) { logger.debug(e.getMessage(), e); } return false; } return true; }
private Object getValue(Monitor<?> monitor) { try { if (limiter != null) { final MonitorValueCallable c = new MonitorValueCallable(monitor); return limiter.callWithTimeout(c, 1, TimeUnit.SECONDS); } else { return monitor.getValue(); } } catch (TimeLimiter.UncheckedTimeoutException e) { LOGGER.warn("timeout trying to get value for {}", monitor.getConfig()); TIMEOUT_ERROR.increment(); } catch (Exception e) { LOGGER.warn("failed to get value for " + monitor.getConfig(), e); DynamicCounter.increment(GET_VALUE_ERROR, "id", e.getClass().getSimpleName()); } return null; }
/** * {@inheritDoc} */ @Override public Object getAttribute(String name) throws AttributeNotFoundException { return monitor.getValue(); }
/** * {@inheritDoc} */ @Override public T getValue(int pollerIdx) { return monitor.getValue(); }
/** * Creates a new instance of the wrapper. */ MonitorWrapper(TagList tags, Monitor<T> monitor) { super(monitor.getConfig().withAdditionalTags(tags)); this.monitor = monitor; }
private static void monitorMonitonicValues(Monitor<?> monitor) { if (!(monitor instanceof SpectatorMonitor)) { if (monitor instanceof CompositeMonitor<?>) { CompositeMonitor<?> cm = (CompositeMonitor<?>) monitor; for (Monitor<?> m : cm.getMonitors()) { monitorMonitonicValues(m); } } else if (isCounter(monitor.getConfig())) { polledGauge(monitor.getConfig()) .monitorMonotonicCounter(monitor, m -> ((Number) m.getValue()).longValue()); } } }
public Object call() throws Exception { return monitor.getValue(); } }
private MBeanAttributeInfo createAttributeInfo(Monitor<?> m) { final String type = (m instanceof NumericMonitor<?>) ? Number.class.getName() : String.class.getName(); return new MBeanAttributeInfo( "value", type, m.getConfig().toString(), true, // isReadable false, // isWritable false); // isIs } }
private void addMeasurements(Monitor<?> m, List<Measurement> measurements) { // Skip any that will report directly if (!(m instanceof SpectatorMonitor)) { if (m instanceof CompositeMonitor<?>) { CompositeMonitor<?> cm = (CompositeMonitor<?>) m; for (Monitor<?> v : cm.getMonitors()) { addMeasurements(v, measurements); } } else if (!isCounter(m.getConfig())) { try { Object obj = m.getValue(); if (obj instanceof Number) { double value = ((Number) obj).doubleValue(); // timestamp will get ignored as the value will get forwarded to a gauge Measurement v = new Measurement(createId(m.getConfig()), 0L, value); measurements.add(v); } } catch (Throwable t) { LOGGER.warn("Exception while querying user defined gauge ({}), " + "the value will be ignored. The owner of the user defined " + "function should fix it to not propagate an exception.", m.getConfig(), t); } } } }
/** * {@inheritDoc} */ @Override public AttributeList getAttributes(String[] names) { AttributeList list = new AttributeList(); for (String name : names) { list.add(new Attribute(name, monitor.getValue())); } return list; }
ServoMeter(Monitor<?> monitor) { this.id = createId(monitor.getConfig()); this.monitor = monitor; }
private void assertStats(List<Monitor<?>> monitors, Map<String, Number> expectedValues) { for (Monitor<?> monitor : monitors) { final String stat = monitor.getConfig().getTags().getValue("statistic"); final Number actual = (Number) monitor.getValue(); final Number expected = expectedValues.get(stat); if (expected instanceof Double) { double e = expected.doubleValue(); double a = actual.doubleValue(); assertEquals(a, e, 0.5, stat); } else { assertEquals(actual, expected, stat); } } }
@Test public void testTimer() throws Exception { ManualClock clock = new ManualClock(0); DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock); Stopwatch s = timer.start(); clock.set(10 * 1000L); assertEquals(10, s.getDuration()); Monitor<Long> duration = getDuration(timer.getMonitors()); Monitor<Long> activeTasks = getActiveTasks(timer.getMonitors()); assertEquals(10L, duration.getValue().longValue()); assertEquals(1L, activeTasks.getValue().longValue()); clock.set(20 * 1000L); assertEquals(20L, duration.getValue().longValue()); assertEquals(1L, activeTasks.getValue().longValue()); Stopwatch anotherTask = timer.start(); assertEquals(20L, duration.getValue().longValue()); assertEquals(2L, activeTasks.getValue().longValue()); clock.set(30 * 1000L); assertEquals(40L, duration.getValue().longValue()); // 30s for the first, 10s for the second assertEquals(2L, activeTasks.getValue().longValue()); s.stop(); assertEquals(10L, duration.getValue().longValue()); // 30s for the first, 10s for the second assertEquals(1L, activeTasks.getValue().longValue()); anotherTask.stop(); assertEquals(0L, duration.getValue().longValue()); assertEquals(0L, activeTasks.getValue().longValue()); }
/** * Register a new monitor in the registry. */ @Override public void register(Monitor<?> monitor) { try { List<MonitorMBean> beans = MonitorMBean.createMBeans(name, monitor, mapper); for (MonitorMBean bean : beans) { register(bean.getObjectName(), bean); } monitors.put(monitor.getConfig(), monitor); updatePending.set(true); } catch (Exception e) { LOG.warn("Unable to register Monitor:" + monitor.getConfig(), e); } }
private void assertMonitors(List<Monitor<?>> monitors, Map<String, Number> expectedValues) { Set<String> exclude = UnmodifiableSet.of("count", "min", "max"); String[] namespaces = new String[]{"statistic", "servo.bucket"}; for (Monitor<?> monitor : monitors) { for (String namespace : namespaces) { final String tag = monitor.getConfig().getTags().getValue(namespace); if (tag != null && !exclude.contains(tag)) { final Number actual = (Number) monitor.getValue(); final Number expected = expectedValues.get(tag); assertEquals(actual, expected, namespace + "." + tag); } } } }
/** * {@inheritDoc} */ @Override public T getValue(int pollerIdx) { return monitor.getValue(); }
/** * Unregister a Monitor from the registry. */ @Override public void unregister(Monitor<?> monitor) { try { List<MonitorMBean> beans = MonitorMBean.createMBeans(name, monitor, mapper); for (MonitorMBean bean : beans) { try { mBeanServer.unregisterMBean(bean.getObjectName()); locks.remove(bean.getObjectName()); } catch (InstanceNotFoundException ignored) { // ignore errors attempting to unregister a non-registered monitor // a common error is to unregister twice } } monitors.remove(monitor.getConfig()); updatePending.set(true); } catch (Exception e) { LOG.warn("Unable to un-register Monitor:" + monitor.getConfig(), e); } }
@Test public void testGetMonitors() throws Exception { List<Monitor<?>> monitors = ((CompositeMonitor<?>) newInstance("test")).getMonitors(); assertEquals(monitors.size(), 2); Monitor<Long> duration = getDuration(monitors); Monitor<Long> activeTasks = getActiveTasks(monitors); assertEquals(duration.getConfig().getName(), "test.duration"); assertEquals(activeTasks.getConfig().getName(), "test.activeTasks"); assertEquals(duration.getValue().longValue(), 0L); assertEquals(activeTasks.getValue().longValue(), 0L); }
/** * {@inheritDoc} */ @Override public Object getAttribute(String name) throws AttributeNotFoundException { return monitor.getValue(); }
private void refreshMonitorCache(MetricFilter filter) { final long age = System.currentTimeMillis() - cacheLastUpdateTime.get(); if (age > cacheTTL) { List<Monitor<?>> monitors = new ArrayList<>(); 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(clock.now()); 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); } }