Clean up code and close BufferedReader in MetricsManager.loadMetrics()
@@ -20,10 +20,11 @@ import com.codahale.metrics.Timer;
public class MetricsManager {
+ private static final Logger logger = LoggerFactory.getLogger(MetricsManager.class);
+
private final MetricRegistry metrics;
+ private final String storageDirectory;
private ConsoleReporter reporter;
- private String storageDirectory;
- private static final Logger logger = LoggerFactory.getLogger(MetricsManager.class);
public MetricsManager() {
this(true, null);
@@ -41,7 +42,8 @@ public class MetricsManager {
this(new MetricRegistry(), startConsoleReporter, directoryPath);
}
- public MetricsManager(MetricRegistry metricsRegistry, boolean startConsoleReporter, String directoryPath) {
+ public MetricsManager(MetricRegistry metricsRegistry, boolean startConsoleReporter,
+ String directoryPath) {
if (directoryPath != null) {
this.metrics = loadMetrics(metricsRegistry, directoryPath);
} else {
@@ -93,22 +95,9 @@ public class MetricsManager {
}
writer.flush();
writer.close();
-
-// outputFile = new File(directoryName + "metrics_timers.data");
-// writer = new BufferedWriter(new FileWriter(outputFile));
-// SortedMap<String, Timer> timers = metrics.getTimers();
-// for (String timer : timers.keySet()) {
-// Timer t = timers.get(timer);
-// writer.write(timer + ":" + t.getCount());
-// writer.write("\n");
-// writer.flush();
-// }
-// writer.flush();
-// writer.close();
} catch (IOException e) {
logger.error("Unable to save metrics to a file." + e.getMessage());
}
-
}
void saveMetrics(String directoryPath) {
@@ -120,7 +109,7 @@ public class MetricsManager {
}
/**
- * Loads metrics from the file at the directry path
+ * Loads metrics from the file at the directory path
*
* @param metricsRegistry
* @param directoryPath
@@ -138,25 +127,11 @@ public class MetricsManager {
Counter counter = metricsRegistry.counter(input[0]);
counter.inc(Integer.parseInt(input[1]));
}
+ reader.close();
} catch (Exception e) {
logger.error("Unable to deserialize counters : " + e.getMessage());
}
}
-
- // metricsFile = new File(directoryPath+"/metrics/metrics_timers.data");
- // if(metricsFile.exists()) {
- // try {
- // BufferedReader reader = new BufferedReader(new FileReader(metricsFile));
- // String line = null;
- // SortedMap<String, Timer> timers = metrics.getTimers();
- // while((line = reader.readLine()) != null) {
- // String[] input = line.split(":");
- // timers.get(input[0]);
- // }
- // }catch(Exception e) {
- // logger.error("Unable to deserialize counters : "+ e.getMessage());
- // }
- // }
return metricsRegistry;
}
