-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for patterns in object names
If the object name is a pattern, perform a query for matching object names and return the value of the given attribute if the pattern yields a unique match. If the match is ambiguous, return null to avoid returning the value from a wrong match. The object name of an MBean consists of a domain part and a list of key/value pairs that together form a unique identifier. The values, however, often depend on current configuration parameters (i.e. the hostname). By using an object name pattern it is possible to avoid the need of considering configuration parameters when passing the object name to the JmxAttributeGauge.
- Loading branch information
Showing
2 changed files
with
94 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 82 additions & 17 deletions
99
metrics-core/src/test/java/com/codahale/metrics/JmxAttributeGaugeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,99 @@ | ||
package com.codahale.metrics; | ||
|
||
import org.junit.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.management.AttributeNotFoundException; | ||
import java.lang.management.ManagementFactory; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.management.JMException; | ||
import javax.management.MBeanServer; | ||
import javax.management.ObjectInstance; | ||
import javax.management.ObjectName; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class JmxAttributeGaugeTest { | ||
private final MBeanServer mBeanServer = mock(MBeanServer.class); | ||
private final ObjectName objectName = mock(ObjectName.class); | ||
private final JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "attr"); | ||
private final Object value = mock(Object.class); | ||
|
||
private static MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); | ||
|
||
private static List<ObjectName> registeredMBeans = new ArrayList<ObjectName>(); | ||
|
||
public interface JmxTestMBean { | ||
Long getValue(); | ||
} | ||
|
||
private static class JmxTest implements JmxTestMBean { | ||
@Override | ||
public Long getValue() { | ||
return Long.MAX_VALUE; | ||
} | ||
} | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
registerMBean(new ObjectName("JmxAttributeGaugeTest:type=test,name=test1")); | ||
registerMBean(new ObjectName("JmxAttributeGaugeTest:type=test,name=test2")); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() { | ||
for (ObjectName objectName : registeredMBeans) { | ||
try { | ||
mBeanServer.unregisterMBean(objectName); | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void returnsAJmxAttribute() throws Exception { | ||
when(mBeanServer.getAttribute(objectName, "attr")).thenReturn(value); | ||
public void returnsJmxAttribute() throws Exception { | ||
ObjectName objectName = new ObjectName("java.lang:type=ClassLoading"); | ||
JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "LoadedClassCount"); | ||
|
||
assertThat(gauge.getValue()) | ||
.isEqualTo(value); | ||
assertThat(gauge.getValue()).isInstanceOf(Integer.class); | ||
assertThat((Integer) gauge.getValue()).isGreaterThan(0); | ||
} | ||
|
||
@Test | ||
public void returnsNullIfThereIsAnException() throws Exception { | ||
when(mBeanServer.getAttribute(objectName, "attr")).thenThrow(new AttributeNotFoundException()); | ||
public void returnsNullIfAttributeDoesNotExist() throws Exception { | ||
ObjectName objectName = new ObjectName("java.lang:type=ClassLoading"); | ||
JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "DoesNotExist"); | ||
|
||
assertThat(gauge.getValue()) | ||
.isNull(); | ||
assertThat(gauge.getValue()).isNull(); | ||
} | ||
|
||
@Test | ||
public void returnsNullIfMBeanNotFound() throws Exception { | ||
ObjectName objectName = new ObjectName("foo.bar:type=NoSuchMBean"); | ||
JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "LoadedClassCount"); | ||
|
||
assertThat(gauge.getValue()).isNull(); | ||
} | ||
|
||
@Test | ||
public void returnsAttributeForObjectNamePattern() throws Exception { | ||
ObjectName objectName = new ObjectName("JmxAttributeGaugeTest:name=test1,*"); | ||
JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "Value"); | ||
|
||
assertThat(gauge.getValue()).isInstanceOf(Long.class); | ||
assertThat((Long) gauge.getValue()).isEqualTo(Long.MAX_VALUE); | ||
} | ||
|
||
@Test | ||
public void returnsNullIfObjectNamePatternAmbiguous() throws Exception { | ||
ObjectName objectName = new ObjectName("JmxAttributeGaugeTest:type=test,*"); | ||
JmxAttributeGauge gauge = new JmxAttributeGauge(mBeanServer, objectName, "Value"); | ||
|
||
assertThat(gauge.getValue()).isNull(); | ||
} | ||
|
||
private static void registerMBean(ObjectName objectName) throws JMException { | ||
ObjectInstance objectInstance = mBeanServer.registerMBean(new JmxTest(), objectName); | ||
registeredMBeans.add(objectInstance.getObjectName()); | ||
} | ||
|
||
} |