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); } }
/** 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); } }
/** * 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); } } }
/** Marshall the given parameter object, and output to a SdkJsonGenerator */ public void marshall(MetricFilter metricFilter, StructuredJsonGenerator jsonGenerator) { if (metricFilter == null) { throw new AmazonClientException("Invalid argument passed to marshall(...)"); } try { jsonGenerator.writeStartObject(); if (metricFilter.getFilterName() != null) { jsonGenerator.writeFieldName("filterName").writeValue(metricFilter.getFilterName()); } if (metricFilter.getFilterPattern() != null) { jsonGenerator.writeFieldName("filterPattern").writeValue(metricFilter.getFilterPattern()); } com.amazonaws.internal.SdkInternalList<MetricTransformation> metricTransformationsList = (com.amazonaws.internal.SdkInternalList<MetricTransformation>) metricFilter.getMetricTransformations(); if (!metricTransformationsList.isEmpty() || !metricTransformationsList.isAutoConstruct()) { jsonGenerator.writeFieldName("metricTransformations"); jsonGenerator.writeStartArray(); for (MetricTransformation metricTransformationsListValue : metricTransformationsList) { if (metricTransformationsListValue != null) { MetricTransformationJsonMarshaller.getInstance() .marshall(metricTransformationsListValue, jsonGenerator); } } jsonGenerator.writeEndArray(); } if (metricFilter.getCreationTime() != null) { jsonGenerator.writeFieldName("creationTime").writeValue(metricFilter.getCreationTime()); } jsonGenerator.writeEndObject(); } catch (Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } }