private List<Metric> mkList() {
   return ImmutableList.of(
       new Metric("m1", SortedTagList.EMPTY, 0L, 0),
       new Metric("m2", SortedTagList.builder().withTag("c", "a.b.c.d.M1").build(), 0L, 0),
       new Metric("m3", SortedTagList.builder().withTag("c", "a.b.c.c.M3").build(), 0L, 0),
       new Metric("m4", SortedTagList.builder().withTag("c", "a.b.c.d.M4").build(), 0L, 0),
       new Metric("m5", SortedTagList.builder().withTag("c", "a.a.a.a.M5").build(), 0L, 0));
 }
Example #2
0
 /** Create a new metric object and add it to the list. */
 private void addMetric(List<Metric> metrics, String name, TagList tags, Object value) {
   long now = System.currentTimeMillis();
   Number num = asNumber(value);
   if (num != null) {
     TagList newTags =
         counters.matches(MonitorConfig.builder(name).withTags(tags).build())
             ? SortedTagList.builder().withTags(tags).withTag(DataSourceType.COUNTER).build()
             : SortedTagList.builder().withTags(tags).withTag(DataSourceType.GAUGE).build();
     Metric m = new Metric(name, newTags, now, num);
     metrics.add(m);
   }
 }
Example #3
0
 /** Creates a tag list from an object name. */
 private TagList createTagList(ObjectName name) {
   Map<String, String> props = name.getKeyPropertyList();
   List<Tag> tags = Lists.newArrayList();
   for (Map.Entry<String, String> e : props.entrySet()) {
     String key = PROP_KEY_PREFIX + "." + e.getKey();
     tags.add(Tags.newTag(key, e.getValue()));
   }
   tags.add(Tags.newTag(DOMAIN_KEY, name.getDomain()));
   tags.add(CLASS_TAG);
   return SortedTagList.builder().withTags(tags).build();
 }
Example #4
0
  /**
   * Query the mbean connection and add all metrics that satisfy the filter to the list {@code
   * metrics}.
   */
  private void getMetrics(
      MBeanServerConnection con, MetricFilter filter, List<Metric> metrics, ObjectName name)
      throws JMException, IOException {
    // Create tags from the object name
    TagList tags = createTagList(name);
    MBeanInfo info = con.getMBeanInfo(name);
    MBeanAttributeInfo[] attrInfos = info.getAttributes();

    // Restrict to attributes that match the filter
    List<String> matchingNames = Lists.newArrayList();
    for (MBeanAttributeInfo attrInfo : attrInfos) {
      String attrName = attrInfo.getName();
      if (filter.matches(new MonitorConfig.Builder(attrName).withTags(tags).build())) {
        matchingNames.add(attrName);
      }
    }
    List<Attribute> attributeList = safelyLoadAttributes(con, name, matchingNames);

    for (Attribute attr : attributeList) {
      String attrName = attr.getName();
      Object obj = attr.getValue();
      if (obj instanceof CompositeData) {
        Map<String, Object> values = Maps.newHashMap();
        extractValues(null, values, (CompositeData) obj);
        for (Map.Entry<String, Object> e : values.entrySet()) {
          String key = e.getKey();
          TagList newTags =
              SortedTagList.builder().withTags(tags).withTag(COMPOSITE_PATH_KEY, key).build();
          if (filter.matches(MonitorConfig.builder(attrName).withTags(newTags).build())) {
            addMetric(metrics, attrName, newTags, e.getValue());
          }
        }
      } else {
        addMetric(metrics, attrName, tags, obj);
      }
    }
  }