Skip to content

Commit

Permalink
jmx allow boolean and enum for metric attributes (#12231)
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Sep 13, 2024
1 parent 19ae57b commit 32fd44e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ private String extractStringAttribute(MBeanServer server, ObjectName objectName)
if (value instanceof String) {
return (String) value;
}
if (value instanceof Boolean) {
return value.toString();
}
if (value instanceof Enum) {
return ((Enum<?>) value).name();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public interface MetricAttributeExtractor {
String extractValue(@Nullable MBeanServer server, @Nullable ObjectName objectName);

static MetricAttributeExtractor fromConstant(String constantValue) {
return (a, b) -> {
return constantValue;
};
return (a, b) -> constantValue;
}

static MetricAttributeExtractor fromObjectNameParameter(String parameterKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public interface Test1MBean {
double getDoubleAttribute();

String getStringAttribute();

boolean getBooleanAttribute();

Enum<?> getEnumAttribute();
}

private static class Test1 implements Test1MBean {
Expand Down Expand Up @@ -71,6 +75,20 @@ public double getDoubleAttribute() {
public String getStringAttribute() {
return "";
}

@Override
public boolean getBooleanAttribute() {
return true;
}

@Override
public Enum<?> getEnumAttribute() {
return DummyEnum.ENUM_VALUE;
}

private enum DummyEnum {
ENUM_VALUE
}
}

private static final String DOMAIN = "otel.jmx.test";
Expand Down Expand Up @@ -202,4 +220,20 @@ void testStringAttribute() throws Exception {
AttributeInfo info = extractor.getAttributeInfo(theServer, objectName);
assertThat(info == null).isTrue();
}

@Test
void testBooleanAttribute() throws Exception {
BeanAttributeExtractor extractor = BeanAttributeExtractor.fromName("BooleanAttribute");
AttributeInfo info = extractor.getAttributeInfo(theServer, objectName);
assertThat(info).isNull();
assertThat(extractor.extractValue(theServer, objectName)).isEqualTo("true");
}

@Test
void testEnumAttribute() throws Exception {
BeanAttributeExtractor extractor = BeanAttributeExtractor.fromName("EnumAttribute");
AttributeInfo info = extractor.getAttributeInfo(theServer, objectName);
assertThat(info).isNull();
assertThat(extractor.extractValue(theServer, objectName)).isEqualTo("ENUM_VALUE");
}
}

0 comments on commit 32fd44e

Please sign in to comment.