Skip to content

Commit

Permalink
replace pattern matching with string comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Karina Calma authored and Karina Calma committed Nov 20, 2024
1 parent d180e07 commit 136d2dc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 51 deletions.
45 changes: 35 additions & 10 deletions collector/src/main/java/io/prometheus/jmx/JmxCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ static class Rule {
String type = "UNKNOWN";
ArrayList<String> labelNames;
ArrayList<String> labelValues;
ArrayList<String> attributesAsLabels;
}

public static class MetricCustomizer {
MBeanFilter mbeanFilter;
List<String> attributesAsLabels;
}

public static class MBeanFilter {
String domain;
Map<String, String> properties;
}

private static class Config {
Expand All @@ -87,7 +96,7 @@ private static class Config {
ObjectNameAttributeFilter objectNameAttributeFilter;
List<Rule> rules = new ArrayList<>();
long lastUpdate = 0L;

List<MetricCustomizer> metricCustomizers = new ArrayList<>();
MatchedRulesCache rulesCache;
}

Expand Down Expand Up @@ -297,6 +306,29 @@ private Config loadConfig(Map<String, Object> yamlConfig) throws MalformedObject
}
}

if (yamlConfig.containsKey("metricCustomizers")) {
List<Map<String, Object>> metricCustomizersYaml =
(List<Map<String, Object>>) yamlConfig.get("metricCustomizers");
for (Map<String, Object> metricCustomizerYaml : metricCustomizersYaml) {
Map<String, Object> mbeanFilterYaml =
(Map<String, Object>) metricCustomizerYaml.get("mbeanFilter");
MBeanFilter mbeanFilter = new MBeanFilter();
mbeanFilter.domain = (String) mbeanFilterYaml.get("domain");
mbeanFilter.properties = (Map<String, String>) mbeanFilterYaml.get("properties");

List<String> attributesAsLabels =
(List<String>) metricCustomizerYaml.get("attributesAsLabels");
if (attributesAsLabels == null) {
attributesAsLabels = new ArrayList<>();
}

MetricCustomizer metricCustomizer = new MetricCustomizer();
metricCustomizer.mbeanFilter = mbeanFilter;
metricCustomizer.attributesAsLabels = attributesAsLabels;
cfg.metricCustomizers.add(metricCustomizer);
}
}

if (yamlConfig.containsKey("rules")) {
List<Map<String, Object>> configRules =
(List<Map<String, Object>>) yamlConfig.get("rules");
Expand Down Expand Up @@ -349,13 +381,6 @@ private Config loadConfig(Map<String, Object> yamlConfig) throws MalformedObject
}
}

if (yamlRule.containsKey("attributesAsLabels")) {
List<String> attributes = (List<String>) yamlRule.get("attributesAsLabels");
rule.attributesAsLabels = new ArrayList<>();
if (attributes != null) {
rule.attributesAsLabels.addAll(attributes);
}
}
// Validation.
if ((rule.labelNames != null || rule.help != null) && rule.name == null) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -750,7 +775,7 @@ public MetricSnapshots collect() {
config.includeObjectNames,
config.excludeObjectNames,
config.objectNameAttributeFilter,
config.rules,
config.metricCustomizers,
receiver,
jmxMBeanPropertyCache);

Expand Down
71 changes: 30 additions & 41 deletions collector/src/main/java/io/prometheus/jmx/JmxScraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import javax.management.Attribute;
import javax.management.AttributeList;
Expand Down Expand Up @@ -74,7 +73,7 @@ void recordBean(
private final String password;
private final boolean ssl;
private final List<ObjectName> includeObjectNames, excludeObjectNames;
private final List<JmxCollector.Rule> rules;
private final List<JmxCollector.MetricCustomizer> metricCustomizers;
private final ObjectNameAttributeFilter objectNameAttributeFilter;
private final JmxMBeanPropertyCache jmxMBeanPropertyCache;

Expand All @@ -86,7 +85,7 @@ public JmxScraper(
List<ObjectName> includeObjectNames,
List<ObjectName> excludeObjectNames,
ObjectNameAttributeFilter objectNameAttributeFilter,
List<JmxCollector.Rule> rules,
List<JmxCollector.MetricCustomizer> metricCustomizers,
MBeanReceiver receiver,
JmxMBeanPropertyCache jmxMBeanPropertyCache) {
this.jmxUrl = jmxUrl;
Expand All @@ -96,7 +95,7 @@ public JmxScraper(
this.ssl = ssl;
this.includeObjectNames = includeObjectNames;
this.excludeObjectNames = excludeObjectNames;
this.rules = rules;
this.metricCustomizers = metricCustomizers;
this.objectNameAttributeFilter = objectNameAttributeFilter;
this.jmxMBeanPropertyCache = jmxMBeanPropertyCache;
}
Expand Down Expand Up @@ -225,7 +224,12 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mBeanName) {

final String mBeanNameString = mBeanName.toString();
final String mBeanDomain = mBeanName.getDomain();
Map<String, Object> attributeMap = attributes.asList().stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValue));
JmxCollector.MetricCustomizer metricCustomizer = getMetricCustomizer(mBeanName);
Map<String, String> attributesAsLabelsWithValues = new HashMap<>();
if (metricCustomizer != null) {
attributesAsLabelsWithValues =
getAttributesAsLabelsWithValues(metricCustomizer, attributes);
}

for (Object object : attributes) {
// The contents of an AttributeList should all be Attribute instances, but we'll verify
Expand All @@ -246,8 +250,6 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mBeanName) {
continue;
}

Map<String, String> attributesAsLabelsWithValues = getAttributesAsLabelsWithValues(mBeanName, attribute, attributeMap);

MBeanAttributeInfo mBeanAttributeInfo =
name2MBeanAttributeInfo.get(attribute.getName());
LOGGER.log(FINE, "%s_%s process", mBeanName, mBeanAttributeInfo.getName());
Expand Down Expand Up @@ -276,46 +278,33 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mBeanName) {
}
}

private Map<String, String> getAttributesAsLabelsWithValues(ObjectName mBeanName, Attribute attribute, Map<String, Object> attributeMap) {
JmxCollector.Rule matchedRule = null;
for (JmxCollector.Rule rule : rules) {
if (rule.attributesAsLabels != null && !rule.attributesAsLabels.isEmpty()) {
if (rule.pattern != null) {
Object matchBeanValue = rule.cache ? "<cache>" : attribute.getValue();
List<String> attrKeys = new LinkedList<>();
if (attribute.getValue() instanceof TabularData || attribute.getValue() instanceof CompositeData) {
attrKeys.add(attribute.getName());
}
String beanName = mBeanName.getDomain()
+ angleBrackets(jmxMBeanPropertyCache.getKeyPropertyList(mBeanName).toString())
+ angleBrackets(attrKeys.toString());
String matchName = beanName + attribute.getName() + ": " + matchBeanValue;
Matcher matcher = rule.pattern.matcher(matchName);
if (matcher.matches()) {
matchedRule = rule;
}
} else if (rule.name == null) {
matchedRule = rule;
}
}
}

private Map<String, String> getAttributesAsLabelsWithValues(JmxCollector.MetricCustomizer metricCustomizer, AttributeList attributes) {
Map<String, Object> attributeMap =
attributes.asList().stream()
.collect(Collectors.toMap(Attribute::getName, Attribute::getValue));
Map<String, String> attributesAsLabelsWithValues = new HashMap<>();
if (matchedRule != null) {
for (String attributeAsLabel : matchedRule.attributesAsLabels) {
Object attrValue = attributeMap.get(attributeAsLabel);
if (attrValue != null) {
attributesAsLabelsWithValues.put(
attributeAsLabel,
attrValue.toString());
}
for (String attributeAsLabel : metricCustomizer.attributesAsLabels) {
Object attrValue = attributeMap.get(attributeAsLabel);
if (attrValue != null) {
attributesAsLabelsWithValues.put(attributeAsLabel, attrValue.toString());
}
}
return attributesAsLabelsWithValues;
}

private String angleBrackets(String s) {
return "<" + s.substring(1, s.length() - 1) + ">";
private JmxCollector.MetricCustomizer getMetricCustomizer(ObjectName mBeanName) {
if (!metricCustomizers.isEmpty()) {
for (JmxCollector.MetricCustomizer metricCustomizer : metricCustomizers) {
if (metricCustomizer.mbeanFilter.domain.equals(mBeanName.getDomain())
&& mBeanName
.getKeyPropertyList()
.entrySet()
.containsAll(metricCustomizer.mbeanFilter.properties.entrySet())) {
return metricCustomizer;
}
}
}
return null;
}

private void processAttributesOneByOne(
Expand Down

0 comments on commit 136d2dc

Please sign in to comment.