Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 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);
     }
   }
 }
Exemplo n.º 3
0
  @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());
  }
Exemplo n.º 4
0
  /**
   * 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();
  }
Exemplo n.º 5
0
 @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());
 }
Exemplo n.º 6
0
  public RequestData start() {
    RequestData req = new RequestData();
    req = dao.init(req);
    REQUEST.set(req);

    if (config.getMode() != Mode.HISTORY) {
      dao.add(req);
    }

    return req;
  }
Exemplo n.º 7
0
    @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;
    }
Exemplo n.º 8
0
 /** 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());
   }
 }
Exemplo n.º 9
0
 /** 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);
 }
Exemplo n.º 10
0
 /** 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);
 }
Exemplo n.º 11
0
 @Override
 public int hashCode() {
   int result = config.hashCode();
   result = 31 * result + unit.hashCode();
   return result;
 }
Exemplo n.º 12
0
 @Override
 public Monitor<?> newInstance(String name) {
   return new DurationTimer(MonitorConfig.builder(name).build());
 }
Exemplo n.º 13
0
 public void update() {
   if (config.getMode() != Mode.HISTORY) {
     dao.update(REQUEST.get());
   }
 }
Exemplo n.º 14
0
 public boolean isEnabled() {
   return config.isEnabled();
 }
Exemplo n.º 15
0
 public Monitor(MonitorConfig config) {
   this.config = config;
   this.dao = config.createDAO();
 }
Exemplo n.º 16
0
 static {
   LoggerInit.initTddlLog();
   MonitorConfig.initConfig();
 }