Java Examples for org.datadog.jmxfetch.reporter.Reporter

The following java examples will help you to understand the usage of org.datadog.jmxfetch.reporter.Reporter. These source code samples are taken from different open source projects.

Example 1
Project: jmxfetch-master  File: App.java View source code
public void doIteration() {
    loopCounter++;
    Reporter reporter = appConfig.getReporter();
    Iterator<Instance> it = instances.iterator();
    while (it.hasNext()) {
        Instance instance = it.next();
        LinkedList<HashMap<String, Object>> metrics;
        String instanceStatus = Status.STATUS_OK;
        String scStatus = Status.STATUS_OK;
        String instanceMessage = null;
        int numberOfMetrics = 0;
        try {
            if (!instance.timeToCollect()) {
                LOGGER.debug("it is not time to collect, skipping run for " + instance.getName());
                continue;
            }
            metrics = instance.getMetrics();
            numberOfMetrics = metrics.size();
            if (numberOfMetrics == 0) {
                instanceMessage = "Instance " + instance + " didn't return any metrics";
                LOGGER.warn(instanceMessage);
                instanceStatus = Status.STATUS_ERROR;
                scStatus = Status.STATUS_ERROR;
                brokenInstances.add(instance);
            } else if (instance.isLimitReached()) {
                instanceMessage = "Number of returned metrics is too high for instance: " + instance.getName() + ". Please read http://docs.datadoghq.com/integrations/java/ or get in touch with Datadog " + "Support for more details. Truncating to " + instance.getMaxNumberOfMetrics() + " metrics.";
                instanceStatus = Status.STATUS_WARNING;
                // We don't want to log the warning at every iteration so we use this custom logger.
                CustomLogger.laconic(LOGGER, Level.WARN, instanceMessage, 0);
            }
            if (numberOfMetrics > 0)
                reporter.sendMetrics(metrics, instance.getName());
        } catch (IOException e) {
            instanceMessage = "Unable to refresh bean list for instance " + instance;
            LOGGER.warn(instanceMessage, e);
            instanceStatus = Status.STATUS_ERROR;
            scStatus = Status.STATUS_ERROR;
            brokenInstances.add(instance);
        }
        this.reportStatus(appConfig, reporter, instance, numberOfMetrics, instanceMessage, instanceStatus);
        this.sendServiceCheck(reporter, instance, instanceMessage, scStatus);
    }
    // Iterate over broken" instances to fix them by resetting them
    it = brokenInstances.iterator();
    while (it.hasNext()) {
        Instance instance = it.next();
        // Clearing rates aggregator so we won't compute wrong rates if we can reconnect
        reporter.clearRatesAggregator(instance.getName());
        LOGGER.warn("Instance " + instance + " didn't return any metrics." + "Maybe the server got disconnected ? Trying to reconnect.");
        // Remove the broken instance from the good instance list so jmxfetch won't try to collect metrics from this broken instance during next collection
        instance.cleanUp();
        instances.remove(instance);
        // Resetting the instance
        Instance newInstance = new Instance(instance, appConfig);
        try {
            // Try to reinit the connection and force to renew it
            LOGGER.info("Trying to reconnect to: " + newInstance);
            newInstance.init(true);
            // If we are here, the connection succeeded, the instance is fixed. It can be readded to the good instances list
            instances.add(newInstance);
            it.remove();
        } catch (Exception e) {
            String warning = null;
            if (e instanceof IOException) {
                warning = CANNOT_CONNECT_TO_INSTANCE + instance + ". Is a JMX Server running at this address?";
                LOGGER.warn(warning);
            } else if (e instanceof SecurityException) {
                warning = CANNOT_CONNECT_TO_INSTANCE + instance + " because of bad credentials. Please check your credentials";
                LOGGER.warn(warning);
            } else if (e instanceof FailedLoginException) {
                warning = CANNOT_CONNECT_TO_INSTANCE + instance + " because of bad credentials. Please check your credentials";
                LOGGER.warn(warning);
            } else {
                warning = CANNOT_CONNECT_TO_INSTANCE + instance + " for an unknown reason." + e.getMessage();
                LOGGER.fatal(warning, e);
            }
            this.reportStatus(appConfig, reporter, instance, 0, warning, Status.STATUS_ERROR);
            this.sendServiceCheck(reporter, instance, warning, Status.STATUS_ERROR);
        }
    }
    try {
        appConfig.getStatus().flush();
    } catch (Exception e) {
        LOGGER.error("Unable to flush stats.", e);
    }
}