public void add(String[] params) { Map<String, String> map = paramsToProps(params); Monitor monitor = mapToMonitor(map); final MonitorConfig monitorConfig = new MonitorConfig(); monitorConfig.setName(map.get("name")); monitorConfig.setMonitor(monitor); monitorDao.save(monitorConfig); }
public void list() { final List<MonitorConfig> list = monitorDao.list(); System.out.println("\nListing monitors"); for (MonitorConfig monitorConfig : list) { System.out.println("\n " + monitorConfig.getId() + " " + monitorConfig.getName()); try { final Map props = new TreeMap(BeanUtils.describe(monitorConfig.getMonitor())); for (Object key : props.keySet()) { System.out.println(" " + key + " = " + props.get(key)); } } catch (Exception ex) { log.error(ex); } } }
@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()); }
/** * Returns a stopwatch that has been started and will automatically record its result to the * dynamic timer specified by the given name, and sequence of (key, value) pairs. The timer uses a * TimeUnit of milliseconds. */ public static Stopwatch start(String name, String... tags) { final MonitorConfig.Builder configBuilder = MonitorConfig.builder(name); Preconditions.checkArgument( tags.length % 2 == 0, "The sequence of (key, value) pairs must have even size: one key, one value"); for (int i = 0; i < tags.length; i += 2) { configBuilder.withTag(tags[i], tags[i + 1]); } return INSTANCE.get(configBuilder.build(), TimeUnit.MILLISECONDS).start(); }
@Test public void testValue() throws Exception { ManualClock clock = new ManualClock(0L); DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock); assertEquals(0L, timer.getValue().longValue()); Stopwatch s = timer.start(); clock.set(10 * 1000L); assertEquals(10L, timer.getValue().longValue()); s.stop(); assertEquals(0L, timer.getValue().longValue()); }
public RequestData start() { RequestData req = new RequestData(); req = dao.init(req); REQUEST.set(req); if (config.getMode() != Mode.HISTORY) { dao.add(req); } return req; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final ConfigUnit that = (ConfigUnit) o; return config.equals(that.config) && unit == that.unit; }
/** Increment a counter specified by a name, and a sequence of (key, value) pairs. */ public static void increment(String name, String... tags) { final MonitorConfig.Builder configBuilder = MonitorConfig.builder(name); Preconditions.checkArgument( tags.length % 2 == 0, "The sequence of (key, value) pairs must have even size: one key, one value"); try { for (int i = 0; i < tags.length; i += 2) { configBuilder.withTag(tags[i], tags[i + 1]); } increment(configBuilder.build()); } catch (IllegalArgumentException e) { LOGGER.warn("Failed to get a counter to increment: {}", e.getMessage()); } }
/** Increment the counter for a given name, tagList by a given delta. */ public static void increment(String name, TagList list, long delta) { final MonitorConfig config = MonitorConfig.builder(name).withTags(list).build(); increment(config, delta); }
/** Creates a new instance of the gauge using a specific Clock. Useful for unit testing. */ MinGauge(MonitorConfig config, Clock clock) { super(config.withAdditionalTag(DataSourceType.GAUGE)); min = new StepLong(Long.MAX_VALUE, clock); }
@Override public int hashCode() { int result = config.hashCode(); result = 31 * result + unit.hashCode(); return result; }
@Override public Monitor<?> newInstance(String name) { return new DurationTimer(MonitorConfig.builder(name).build()); }
public void update() { if (config.getMode() != Mode.HISTORY) { dao.update(REQUEST.get()); } }
public boolean isEnabled() { return config.isEnabled(); }
public Monitor(MonitorConfig config) { this.config = config; this.dao = config.createDAO(); }
static { LoggerInit.initTddlLog(); MonitorConfig.initConfig(); }