@Override public void run() { String metricPrefix = ""; if (ApplicationConfiguration.isGlobalMetricNamePrefixEnabled() && (ApplicationConfiguration.getGlobalMetricNamePrefix() != null)) metricPrefix += ApplicationConfiguration.getGlobalMetricNamePrefix(); if (!metricPrefix.isEmpty() && !metricPrefix.endsWith(".")) metricPrefix += "."; if (metricCollectorPrefix_ != null) metricPrefix += metricCollectorPrefix_; if (!metricPrefix.isEmpty() && !metricPrefix.endsWith(".")) metricPrefix += "."; while (true) { long readMetricsTimeStart = System.currentTimeMillis(); oldestValidMetricTimestamp_Seconds_ = Math.round( (double) (((double) System.currentTimeMillis() - (double) ApplicationConfiguration.getMaxMetricAge()) / 1000)); List<GenericMetricFormat> metrics = getMetricsFromFile(ApplicationConfiguration.isAlwaysCheckOutputFiles(), metricPrefix); List<GenericMetricFormat> newMetrics = getNewMetrics(metrics); for (GenericMetricFormat metric : newMetrics) { metric.setMetricHashKey(GlobalVariables.metricHashKeyGenerator.incrementAndGet()); if (metric instanceof GraphiteMetric) GlobalVariables.graphiteMetrics.put(metric.getMetricHashKey(), (GraphiteMetric) metric); else if (metric instanceof OpenTsdbMetric) GlobalVariables.openTsdbMetrics.put(metric.getMetricHashKey(), (OpenTsdbMetric) metric); } cleanupPreviousMetrics(); String filename = (fileToMonitor_ == null) ? null : fileToMonitor_.getName(); String outputStatusString = "Finished reading metrics from file. File=\"" + filename + "\", NewMetricCount=" + newMetrics.size() + ", FileTimestamp=" + previousFileLastModifiedTimestamp_; if (newMetrics.size() > 0) logger.info(outputStatusString); else logger.debug(outputStatusString); long readMetricsTimeElapsed = System.currentTimeMillis() - readMetricsTimeStart; long sleepTime = checkFilesIntervalInMilliseconds_ - readMetricsTimeElapsed; Threads.sleepMilliseconds(sleepTime); } }
protected List<GenericMetricFormat> getNewMetrics(List<GenericMetricFormat> metrics) { if ((metrics == null) || metrics.isEmpty()) { return new ArrayList<>(); } List<GenericMetricFormat> newMetrics = new ArrayList<>(); for (GenericMetricFormat metric : metrics) { if (metric.getMetricTimestampInMilliseconds() >= oldestValidMetricTimestamp_Seconds_) { Long previousGraphiteMetricTimestamp = previousMetricTimestamps_.get(metric.getMetricKey()); if ((previousGraphiteMetricTimestamp == null) || (previousGraphiteMetricTimestamp < metric.getMetricTimestampInMilliseconds())) { newMetrics.add(metric); previousMetricTimestamps_.put( metric.getMetricKey(), metric.getMetricTimestampInMilliseconds()); } } } return newMetrics; }