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

jmx allow boolean and enum for metric attributes #12231

Merged
merged 2 commits into from
Sep 13, 2024
Merged
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
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");
}
}
Loading