Search in sources :

Example 1 with JmxResultProcessor

use of com.googlecode.jmxtrans.model.JmxResultProcessor in project jmxtrans by jmxtrans.

the class JmxResultProcessorTest method canReadFieldsOfTabularData.

@Test(timeout = 1000)
public void canReadFieldsOfTabularData() throws MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
    // Need to induce a GC for the attribute below to be populated
    Runtime.getRuntime().gc();
    ObjectInstance runtime = null;
    try {
        runtime = getG1YoungGen();
    } catch (InstanceNotFoundException e) {
        // ignore test if G1 not enabled
        assumeNoException("G1 GC in Java 7/8 needs to be enabled with -XX:+UseG1GC", e);
    }
    AttributeList attr;
    // but takes a non-deterministic amount of time for LastGcInfo to get populated
    while (true) {
        // but bounded by Test timeout
        attr = ManagementFactory.getPlatformMBeanServer().getAttributes(runtime.getObjectName(), new String[] { "LastGcInfo" });
        if (((Attribute) attr.get(0)).getValue() != null) {
            break;
        }
    }
    List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), runtime, attr.asList(), runtime.getClassName(), TEST_DOMAIN_NAME).getResults();
    assertThat(results.size()).isGreaterThan(2);
    // Should have primitive typed fields
    assertThat(firstMatch(results, "LastGcInfo", "duration").get()).isNotNull();
    // assert tabular fields are excluded
    assertThat(firstMatch(results, "LastGcInfo", "memoryUsageBeforeGc").get()).isNull();
    assertThat(firstMatch(results, "LastGcInfo", "memoryUsageAfterGc").get()).isNull();
}
Also used : AttributeList(javax.management.AttributeList) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance) JmxResultProcessor(com.googlecode.jmxtrans.model.JmxResultProcessor) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Example 2 with JmxResultProcessor

use of com.googlecode.jmxtrans.model.JmxResultProcessor in project jmxtrans by jmxtrans.

the class JmxResultProcessorTest method canReadMapData.

@Test
public void canReadMapData() throws MalformedObjectNameException {
    Attribute mapAttribute = new Attribute("map", ImmutableMap.of("key1", "value1", "key2", "value2"));
    List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), new ObjectInstance("java.lang:type=Memory", "java.lang.SomeClass"), ImmutableList.of(mapAttribute), "java.lang.SomeClass", TEST_DOMAIN_NAME).getResults();
    assertThat(results).isNotNull();
    assertThat(results).hasSize(2);
    for (Result result : results) {
        assertThat(result.getAttributeName()).isEqualTo("map");
        assertThat(result.getTypeName()).isEqualTo("type=Memory");
    }
    assertThat(firstMatch(results, "map", "key1").get().getValue()).isEqualTo("value1");
    assertThat(firstMatch(results, "map", "key2").get().getValue()).isEqualTo("value2");
}
Also used : Attribute(javax.management.Attribute) JmxResultProcessor(com.googlecode.jmxtrans.model.JmxResultProcessor) ObjectInstance(javax.management.ObjectInstance) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Example 3 with JmxResultProcessor

use of com.googlecode.jmxtrans.model.JmxResultProcessor in project jmxtrans by jmxtrans.

the class JmxResultProcessorTest method canReadMapDataWithNonStringKeys.

@Test
public void canReadMapDataWithNonStringKeys() throws MalformedObjectNameException {
    Attribute mapAttribute = new Attribute("map", ImmutableMap.of(1, "value1", 2, "value2"));
    List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), new ObjectInstance("java.lang:type=Memory", "java.lang.SomeClass"), ImmutableList.of(mapAttribute), "java.lang.SomeClass", TEST_DOMAIN_NAME).getResults();
    assertThat(results).isNotNull();
    for (Result result : results) {
        assertThat(result.getAttributeName()).isEqualTo("map");
        assertThat(result.getTypeName()).isEqualTo("type=Memory");
    }
    assertThat(firstMatch(results, "map", "1").get().getValue()).isEqualTo("value1");
    assertThat(firstMatch(results, "map", "2").get().getValue()).isEqualTo("value2");
}
Also used : Attribute(javax.management.Attribute) JmxResultProcessor(com.googlecode.jmxtrans.model.JmxResultProcessor) ObjectInstance(javax.management.ObjectInstance) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Example 4 with JmxResultProcessor

use of com.googlecode.jmxtrans.model.JmxResultProcessor in project jmxtrans by jmxtrans.

the class JmxResultProcessorTest method canReadCompositeDataWithFilteringKeys.

@Test
public void canReadCompositeDataWithFilteringKeys() throws MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
    ObjectInstance memory = getMemory();
    AttributeList attr = ManagementFactory.getPlatformMBeanServer().getAttributes(memory.getObjectName(), new String[] { "HeapMemoryUsage" });
    List<Result> results = new JmxResultProcessor(Query.builder().setObj("myQuery:key=val").setResultAlias("resultAlias").addKey("init").build(), memory, attr.asList(), memory.getClassName(), TEST_DOMAIN_NAME).getResults();
    assertThat(results).hasSize(1);
    for (Result result : results) {
        assertThat(result.getAttributeName()).isEqualTo("HeapMemoryUsage");
        assertThat(result.getTypeName()).isEqualTo("type=Memory");
    }
    Optional<Result> optionalResult = firstMatch(results, "HeapMemoryUsage", "init");
    assertThat(optionalResult.isPresent()).isTrue();
    Object objectValue = optionalResult.get().getValue();
    assertThat(objectValue).isInstanceOf(Long.class);
}
Also used : AttributeList(javax.management.AttributeList) ObjectInstance(javax.management.ObjectInstance) JmxResultProcessor(com.googlecode.jmxtrans.model.JmxResultProcessor) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Example 5 with JmxResultProcessor

use of com.googlecode.jmxtrans.model.JmxResultProcessor in project jmxtrans by jmxtrans.

the class JmxResultProcessorTest method canReadTabularData.

@Test
public void canReadTabularData() throws MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
    ObjectInstance runtime = getRuntime();
    AttributeList attr = ManagementFactory.getPlatformMBeanServer().getAttributes(runtime.getObjectName(), new String[] { "SystemProperties" });
    List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), runtime, attr.asList(), runtime.getClassName(), TEST_DOMAIN_NAME).getResults();
    assertThat(results.size()).isGreaterThan(2);
    Optional<Result> result = firstMatch(results, "SystemProperties", "java.version", "value");
    assertThat(result.isPresent()).isTrue();
    assertThat(result.get().getAttributeName()).isEqualTo("SystemProperties");
    assertThat(result.get().getValuePath()).isEqualTo(ImmutableList.of("java.version", "value"));
    assertThat(result.get().getValue()).isEqualTo(System.getProperty("java.version"));
}
Also used : AttributeList(javax.management.AttributeList) ObjectInstance(javax.management.ObjectInstance) JmxResultProcessor(com.googlecode.jmxtrans.model.JmxResultProcessor) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Aggregations

JmxResultProcessor (com.googlecode.jmxtrans.model.JmxResultProcessor)11 Result (com.googlecode.jmxtrans.model.Result)11 ObjectInstance (javax.management.ObjectInstance)11 Test (org.junit.Test)11 Attribute (javax.management.Attribute)7 AttributeList (javax.management.AttributeList)4 InstanceNotFoundException (javax.management.InstanceNotFoundException)1