Skip to content

Commit 1696208

Browse files
authored
Enabled auto object name attribute filtering by default (#871)
Signed-off-by: Doug Hoard <[email protected]>
1 parent 7bdf29b commit 1696208

File tree

12 files changed

+54
-26
lines changed

12 files changed

+54
-26
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ lowercaseOutputName: false
8383
lowercaseOutputLabelNames: false
8484
includeObjectNames: ["org.apache.cassandra.metrics:*"]
8585
excludeObjectNames: ["org.apache.cassandra.metrics:type=ColumnFamily,*"]
86+
autoExcludeObjectNameAttributes: true
8687
excludeObjectNameAttributes:
8788
"java.lang:type=OperatingSystem":
8889
- "ObjectName"
@@ -112,7 +113,7 @@ lowercaseOutputName | Lowercase the output metric name. Applies to default forma
112113
lowercaseOutputLabelNames | Lowercase the output metric label names. Applies to default format and `labels`. Defaults to false.
113114
includeObjectNames | A list of [ObjectNames](http://docs.oracle.com/javase/6/docs/api/javax/management/ObjectName.html) to query. Defaults to all mBeans.
114115
excludeObjectNames | A list of [ObjectNames](http://docs.oracle.com/javase/6/docs/api/javax/management/ObjectName.html) to not query. Takes precedence over `includeObjectNames`. Defaults to none.
115-
excludeObjectNameAttributesDynamic | Whether to use dynamic [ObjectName](http://docs.oracle.com/javase/6/docs/api/javax/management/ObjectName.html) attribute filtering. Defaults to false.
116+
autoExcludeObjectNameAttributes | Whether to use auto add [ObjectName](http://docs.oracle.com/javase/6/docs/api/javax/management/ObjectName.html) attributes that can't be converted to standard metrics types. Defaults to `true`.
116117
excludeObjectNameAttributes | Optional a map of [ObjectNames](http://docs.oracle.com/javase/6/docs/api/javax/management/ObjectName.html) with a list of attribute names.
117118
rules | A list of rules to apply in order, processing stops at the first matching rule. Attributes that aren't matched aren't collected. If not specified, defaults to collecting everything in the default format.
118119
pattern | Regex pattern to match against each bean attribute. The pattern is not anchored. Capture groups can be used in other options. Defaults to matching everything.

collector/src/main/java/io/prometheus/jmx/JmxScraper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mBeanName) {
217217
Attribute attribute = (Attribute) object;
218218
String attributeName = attribute.getName();
219219
if (mBeanName.toString().equals("java.lang:type=Runtime")
220-
&& (attributeName.equalsIgnoreCase("SystemProperties")
221-
|| attributeName.equalsIgnoreCase("ClassPath")
222-
|| attributeName.equalsIgnoreCase("BootClassPath"))
223-
|| attributeName.equalsIgnoreCase("LibraryPath")) {
224-
// Skip attributes for the "java.lang:type=Runtime" MBean because
220+
&& (attributeName.equalsIgnoreCase("SystemProperties")
221+
|| attributeName.equalsIgnoreCase("ClassPath")
222+
|| attributeName.equalsIgnoreCase("BootClassPath")
223+
|| attributeName.equalsIgnoreCase("LibraryPath"))) {
224+
// Skip this attributes for the "java.lang:type=Runtime" MBean because
225225
// getting the values is expensive and the values are ultimately ignored
226226
continue;
227227
} else if (mBeanName.toString().equals("jdk.management.jfr:type=FlightRecorder")) {

collector/src/main/java/io/prometheus/jmx/ObjectNameAttributeFilter.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@
1616

1717
package io.prometheus.jmx;
1818

19+
import io.prometheus.jmx.logger.Logger;
20+
import io.prometheus.jmx.logger.LoggerFactory;
1921
import java.util.Collections;
2022
import java.util.HashSet;
2123
import java.util.List;
2224
import java.util.Map;
2325
import java.util.Set;
2426
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.logging.Level;
2528
import javax.management.MalformedObjectNameException;
2629
import javax.management.ObjectName;
2730

2831
/** Class to implement filtering of an MBean's attributes based on the attribute's name */
2932
@SuppressWarnings("unchecked")
3033
public class ObjectNameAttributeFilter {
3134

35+
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectNameAttributeFilter.class);
36+
3237
/** Configuration constant to define a mapping of ObjectNames to attribute names */
3338
public static final String EXCLUDE_OBJECT_NAME_ATTRIBUTES = "excludeObjectNameAttributes";
3439

35-
/** Configuration constant to enable dynamic support of ObjectName attributes filtering */
36-
public static final String EXCLUDE_OBJECT_NAME_ATTRIBUTES_DYNAMIC =
37-
EXCLUDE_OBJECT_NAME_ATTRIBUTES + "Dynamic";
40+
/** Configuration constant to enable auto ObjectName attributes filtering */
41+
public static final String AUTO_EXCLUDE_OBJECT_NAME_ATTRIBUTES =
42+
"autoExcludeObjectNameAttributes";
3843

3944
private final Map<ObjectName, Set<String>> excludeObjectNameAttributesMap;
4045

41-
private boolean dynamicExclusion;
46+
private boolean autoExcludeObjectNameAttributes;
4247

4348
/** Constructor */
4449
private ObjectNameAttributeFilter() {
@@ -68,17 +73,27 @@ private ObjectNameAttributeFilter initialize(Map<String, Object> yamlConfig)
6873
objectName, o -> Collections.synchronizedSet(new HashSet<>()));
6974

7075
for (String attributeName : attributeNames) {
76+
LOGGER.log(
77+
Level.FINE,
78+
"excluding object name [%d] attribute name [%s]",
79+
objectName.getCanonicalName(),
80+
attributeName);
7181
attributeNameSet.add(attributeName);
7282
}
7383

7484
excludeObjectNameAttributesMap.put(objectName, attributeNameSet);
7585
}
7686
}
7787

78-
if (yamlConfig.containsKey(EXCLUDE_OBJECT_NAME_ATTRIBUTES_DYNAMIC)) {
79-
dynamicExclusion = (Boolean) yamlConfig.get(EXCLUDE_OBJECT_NAME_ATTRIBUTES_DYNAMIC);
88+
if (yamlConfig.containsKey(AUTO_EXCLUDE_OBJECT_NAME_ATTRIBUTES)) {
89+
autoExcludeObjectNameAttributes =
90+
(Boolean) yamlConfig.get(AUTO_EXCLUDE_OBJECT_NAME_ATTRIBUTES);
91+
} else {
92+
autoExcludeObjectNameAttributes = true;
8093
}
8194

95+
LOGGER.log(Level.FINE, "dynamicExclusion [%b]", autoExcludeObjectNameAttributes);
96+
8297
return this;
8398
}
8499

@@ -89,11 +104,17 @@ private ObjectNameAttributeFilter initialize(Map<String, Object> yamlConfig)
89104
* @param attributeName the attribute name
90105
*/
91106
public void add(ObjectName objectName, String attributeName) {
92-
if (dynamicExclusion) {
107+
if (autoExcludeObjectNameAttributes) {
93108
Set<String> attribteNameSet =
94109
excludeObjectNameAttributesMap.computeIfAbsent(
95110
objectName, o -> Collections.synchronizedSet(new HashSet<>()));
96111

112+
LOGGER.log(
113+
Level.FINE,
114+
"auto adding exclusion of object name [%s] attribute name [%s]",
115+
objectName.getCanonicalName(),
116+
attributeName);
117+
97118
attribteNameSet.add(attributeName);
98119
}
99120
}

collector/src/main/java/io/prometheus/jmx/logger/Logger.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public class Logger {
2323

2424
private final java.util.logging.Logger LOGGER;
2525

26+
private final boolean JMX_PROMETHEUS_EXPORTER_DEVELOPER_DEBUG =
27+
"true".equals(System.getenv("JMX_PROMETHEUS_EXPORTER_DEVELOPER_DEBUG"))
28+
|| "true".equals(System.getProperty("jmx.prometheus.exporter.developer.debug"));
29+
2630
/**
2731
* Constructor
2832
*
@@ -53,5 +57,11 @@ public void log(Level level, String message, Object... objects) {
5357
if (LOGGER.isLoggable(level)) {
5458
LOGGER.log(level, String.format(message, objects));
5559
}
60+
61+
if (JMX_PROMETHEUS_EXPORTER_DEVELOPER_DEBUG) {
62+
System.out
63+
.format("[%s] %s %s", level, LOGGER.getName(), String.format(message, objects))
64+
.println();
65+
}
5666
}
5767
}

integration_test_suite/integration_tests/src/test/java/io/prometheus/jmx/test/DynamicExcludeObjectNameAttributesTest.java renamed to integration_test_suite/integration_tests/src/test/java/io/prometheus/jmx/test/DisableAutoExcludeObjectNameAttributesTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
import java.util.Set;
3434
import org.antublue.test.engine.api.TestEngine;
3535

36-
public class DynamicExcludeObjectNameAttributesTest extends BaseTest implements ContentConsumer {
36+
public class DisableAutoExcludeObjectNameAttributesTest extends BaseTest
37+
implements ContentConsumer {
3738

3839
@TestEngine.Test
3940
public void testHealthy() {

integration_test_suite/integration_tests/src/test/resources/io/prometheus/jmx/test/DynamicExcludeObjectNameAttributesTest/JavaAgent/application.sh renamed to integration_test_suite/integration_tests/src/test/resources/io/prometheus/jmx/test/DisableAutoExcludeObjectNameAttributesTest/JavaAgent/application.sh

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
autoExcludeObjectNameAttributes: false
2+
rules:
3+
- pattern: ".*"

integration_test_suite/integration_tests/src/test/resources/io/prometheus/jmx/test/DynamicExcludeObjectNameAttributesTest/Standalone/application.sh renamed to integration_test_suite/integration_tests/src/test/resources/io/prometheus/jmx/test/DisableAutoExcludeObjectNameAttributesTest/Standalone/application.sh

File renamed without changes.

integration_test_suite/integration_tests/src/test/resources/io/prometheus/jmx/test/DynamicExcludeObjectNameAttributesTest/Standalone/exporter.sh renamed to integration_test_suite/integration_tests/src/test/resources/io/prometheus/jmx/test/DisableAutoExcludeObjectNameAttributesTest/Standalone/exporter.sh

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
hostPort: application:9999
2+
autoExcludeObjectNameAttributes: false
3+
rules:
4+
- pattern: ".*"

0 commit comments

Comments
 (0)