Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to scrape all types of attributes #747

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ username | The username to be used in remote JMX password authentication.
password | The password to be used in remote JMX password authentication.
jmxUrl | A full JMX URL to connect to. Should not be specified if hostPort is.
ssl | Whether JMX connection should be done over SSL. To configure certificates you have to set following system properties:<br/>`-Djavax.net.ssl.keyStore=/home/user/.keystore`<br/>`-Djavax.net.ssl.keyStorePassword=changeit`<br/>`-Djavax.net.ssl.trustStore=/home/user/.truststore`<br/>`-Djavax.net.ssl.trustStorePassword=changeit`
scrapeAllAttributes | Option to scrape everything from JMX, otherwise only Attribute instances will be scraped, default: false
lowercaseOutputName | Lowercase the output metric name. Applies to default format and `name`. Defaults to false.
lowercaseOutputLabelNames | Lowercase the output metric label names. Applies to default format and `labels`. Defaults to false.
whitelistObjectNames | A list of [ObjectNames](http://docs.oracle.com/javase/6/docs/api/javax/management/ObjectName.html) to query. Defaults to all mBeans.
Expand Down
7 changes: 6 additions & 1 deletion collector/src/main/java/io/prometheus/jmx/JmxCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static class Config {
String username = "";
String password = "";
boolean ssl = false;
boolean scrapeAllAttributes = false;
boolean lowercaseOutputName;
boolean lowercaseOutputLabelNames;
List<ObjectName> whitelistObjectNames = new ArrayList<ObjectName>();
Expand Down Expand Up @@ -183,6 +184,10 @@ private Config loadConfig(Map<String, Object> yamlConfig) throws MalformedObject
cfg.ssl = (Boolean)yamlConfig.get("ssl");
}

if (yamlConfig.containsKey("scrapeAllAttributes")) {
cfg.scrapeAllAttributes = (Boolean)yamlConfig.get("scrapeAllAttributes");
}

if (yamlConfig.containsKey("lowercaseOutputName")) {
cfg.lowercaseOutputName = (Boolean)yamlConfig.get("lowercaseOutputName");
}
Expand Down Expand Up @@ -587,7 +592,7 @@ public List<MetricFamilySamples> collect() {

MatchedRulesCache.StalenessTracker stalenessTracker = new MatchedRulesCache.StalenessTracker();
Receiver receiver = new Receiver(config, stalenessTracker);
JmxScraper scraper = new JmxScraper(config.jmxUrl, config.username, config.password, config.ssl,
JmxScraper scraper = new JmxScraper(config.jmxUrl, config.username, config.password, config.ssl, config.scrapeAllAttributes
config.whitelistObjectNames, config.blacklistObjectNames, receiver, jmxMBeanPropertyCache);
long start = System.nanoTime();
double error = 0;
Expand Down
43 changes: 25 additions & 18 deletions collector/src/main/java/io/prometheus/jmx/JmxScraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,20 @@ void recordBean(
private final String username;
private final String password;
private final boolean ssl;
private final boolean scrapeAllAttributes;
private final List<ObjectName> whitelistObjectNames, blacklistObjectNames;
private final JmxMBeanPropertyCache jmxMBeanPropertyCache;
private final OptionalValueExtractor optionalValueExtractor = new OptionalValueExtractor();

public JmxScraper(String jmxUrl, String username, String password, boolean ssl,
public JmxScraper(String jmxUrl, String username, String password, boolean ssl, boolean scrapeAllAttributes,
List<ObjectName> whitelistObjectNames, List<ObjectName> blacklistObjectNames,
MBeanReceiver receiver, JmxMBeanPropertyCache jmxMBeanPropertyCache) {
this.jmxUrl = jmxUrl;
this.receiver = receiver;
this.username = username;
this.password = password;
this.ssl = ssl;
this.scrapeAllAttributes = scrapeAllAttributes;
this.whitelistObjectNames = whitelistObjectNames;
this.blacklistObjectNames = blacklistObjectNames;
this.jmxMBeanPropertyCache = jmxMBeanPropertyCache;
Expand Down Expand Up @@ -160,21 +162,26 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mbeanName) {
processAttributesOneByOne(beanConn, mbeanName, name2AttrInfo);
return;
}
for (Object attributeObj : attributes.asList()) {
if (Attribute.class.isInstance(attributeObj)) {
Attribute attribute = (Attribute)(attributeObj);
MBeanAttributeInfo attr = name2AttrInfo.get(attribute.getName());
logScrape(mbeanName, attr, "process");
processBeanValue(
mbeanName.getDomain(),
jmxMBeanPropertyCache.getKeyPropertyList(mbeanName),
new LinkedList<String>(),
attr.getName(),
attr.getType(),
attr.getDescription(),
attribute.getValue()
);

List<Attribute> attrList = attributes.asList();
for (int i = 0; i < attrList.size(); i++) {
Object value = attrList.get(i);
boolean isMxAttribute = value instanceof Attribute;
if (isMxAttribute) {
value = ((Attribute) (value)).getValue();
}
if (!isMxAttribute && !scrapeAllAttributes) {
return;
}
MBeanAttributeInfo attr = attrInfos[i];
logScrape(mbeanName, attr, "process");
processBeanValue(mbeanName.getDomain(),
jmxMBeanPropertyCache.getKeyPropertyList(mbeanName),
new LinkedList<String>(),
attr.getName(),
attr.getType(),
attr.getDescription(),
value);
}
}

Expand Down Expand Up @@ -358,15 +365,15 @@ public static void main(String[] args) throws Exception {
List<ObjectName> objectNames = new LinkedList<ObjectName>();
objectNames.add(null);
if (args.length >= 3){
new JmxScraper(args[0], args[1], args[2], (args.length >3 && "ssl".equalsIgnoreCase(args[3])), objectNames, new LinkedList<ObjectName>(),
new JmxScraper(args[0], args[1], args[2], (args.length >3 && "ssl".equalsIgnoreCase(args[3])), false, objectNames, new LinkedList<ObjectName>(),
new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape();
}
else if (args.length > 0){
new JmxScraper(args[0], "", "", false, objectNames, new LinkedList<ObjectName>(),
new JmxScraper(args[0], "", "", false, false, objectNames, new LinkedList<ObjectName>(),
new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape();
}
else {
new JmxScraper("", "", "", false, objectNames, new LinkedList<ObjectName>(),
new JmxScraper("", "", "", false, false, objectNames, new LinkedList<ObjectName>(),
new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape();
}
}
Expand Down