Search in sources :

Example 1 with Result

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

the class BaseOutputWriterTests method booleanValuesAreNotTransformedToNumber.

@Test
public void booleanValuesAreNotTransformedToNumber() throws Exception {
    final ArrayList<Result> processedResults = Lists.newArrayList();
    BaseOutputWriter outputWriter = new BaseOutputWriter(ImmutableList.<String>of(), false, false, Maps.<String, Object>newHashMap()) {

        @Override
        protected void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception {
            processedResults.addAll(results);
        }

        @Override
        public void validateSetup(Server server, Query query) throws ValidationException {
        }
    };
    Result result = new Result(0, "", "", "", "", "", ImmutableMap.<String, Object>of("true", true, "false", false));
    outputWriter.doWrite(null, null, ImmutableList.of(result));
    assertThat(processedResults).hasSize(1);
    Result processedResult = processedResults.get(0);
    assertThat(processedResult.getValues().get("true")).isEqualTo(true);
    assertThat(processedResult.getValues().get("false")).isEqualTo(false);
}
Also used : Server(com.googlecode.jmxtrans.model.Server) Query(com.googlecode.jmxtrans.model.Query) ImmutableList(com.google.common.collect.ImmutableList) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Example 2 with Result

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

the class TreeWalker3 method walkTree.

public void walkTree(MBeanServerConnection connection, Server server) throws Exception {
    // key here is null, null returns everything!
    Set<ObjectName> mbeans = connection.queryNames(null, null);
    Map<String, String> output = newHashMap();
    for (ObjectName name : mbeans) {
        MBeanInfo info = connection.getMBeanInfo(name);
        MBeanAttributeInfo[] attrs = info.getAttributes();
        Query.Builder queryBuilder = Query.builder().setObj(name.getCanonicalName());
        ResultCapture resultCapture = new ResultCapture();
        queryBuilder.addOutputWriterFactory(resultCapture);
        for (MBeanAttributeInfo attrInfo : attrs) {
            queryBuilder.addAttr(attrInfo.getName());
        }
        Query query = queryBuilder.build();
        try {
            Iterable<Result> results = server.execute(query);
            query.runOutputWritersForQuery(server, results);
        } catch (AttributeNotFoundException anfe) {
            log.error("Error", anfe);
        }
        for (Result result : resultCapture.results) {
            output.put(result.getTypeName(), query.getAttr().toString());
        }
    }
    for (Entry<String, String> entry : output.entrySet()) {
        log.debug(entry.getKey());
        log.debug(entry.getValue());
        log.debug("-----------------------------------------");
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) Query(com.googlecode.jmxtrans.model.Query) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) Result(com.googlecode.jmxtrans.model.Result)

Example 3 with Result

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

the class GraphiteWriter method internalWrite.

@Override
public void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception {
    Socket socket = null;
    PrintWriter writer = null;
    try {
        socket = pool.borrowObject(address);
        writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), UTF_8), true);
        List<String> typeNames = this.getTypeNames();
        for (Result result : results) {
            log.debug("Query result: {}", result);
            Object value = result.getValue();
            if (isValidNumber(value)) {
                String line = KeyUtils.getKeyString(server, query, result, typeNames, rootPrefix).replaceAll("[()]", "_") + " " + value.toString() + " " + result.getEpoch() / 1000 + "\n";
                log.debug("Graphite Message: {}", line);
                writer.write(line);
            } else {
                onlyOnceLogger.infoOnce("Unable to submit non-numeric value to Graphite: [{}] from result [{}]", value, result);
            }
        }
    } finally {
        if (writer != null && writer.checkError()) {
            log.error("Error writing to Graphite, clearing Graphite socket pool");
            pool.invalidateObject(address, socket);
        } else {
            pool.returnObject(address, socket);
        }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) ToString(lombok.ToString) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Result(com.googlecode.jmxtrans.model.Result)

Example 4 with Result

use of com.googlecode.jmxtrans.model.Result 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 5 with Result

use of com.googlecode.jmxtrans.model.Result 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)

Aggregations

Result (com.googlecode.jmxtrans.model.Result)45 Test (org.junit.Test)21 JmxResultProcessor (com.googlecode.jmxtrans.model.JmxResultProcessor)11 ObjectInstance (javax.management.ObjectInstance)11 Query (com.googlecode.jmxtrans.model.Query)8 Server (com.googlecode.jmxtrans.model.Server)7 Attribute (javax.management.Attribute)7 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5 JestResult (io.searchbox.client.JestResult)4 ArrayList (java.util.ArrayList)4 AttributeList (javax.management.AttributeList)4 ToString (lombok.ToString)4 OutputWriter (com.googlecode.jmxtrans.model.OutputWriter)3 ServerFixtures.dummyServer (com.googlecode.jmxtrans.model.ServerFixtures.dummyServer)3 DocumentResult (io.searchbox.core.DocumentResult)3 Map (java.util.Map)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 QueryFixtures.dummyQuery (com.googlecode.jmxtrans.model.QueryFixtures.dummyQuery)2 ResultFixtures.singleFalseResult (com.googlecode.jmxtrans.model.ResultFixtures.singleFalseResult)2