Repository: incubator-edgent
Updated Branches:
  refs/heads/master e440dad54 -> bdb2cd815


Implemented support for csv. Which periodically appends to a set of .csv files 
in a given directory


Project: http://git-wip-us.apache.org/repos/asf/incubator-edgent/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-edgent/commit/fe7c3c21
Tree: http://git-wip-us.apache.org/repos/asf/incubator-edgent/tree/fe7c3c21
Diff: http://git-wip-us.apache.org/repos/asf/incubator-edgent/diff/fe7c3c21

Branch: refs/heads/master
Commit: fe7c3c21fbb168fd0159e8bbf8848f1beba757c6
Parents: e440dad
Author: Thomas Cristanis <t...@cin.ufpe.br>
Authored: Wed Jul 19 20:35:56 2017 -0300
Committer: Thomas Cristanis <t...@cin.ufpe.br>
Committed: Wed Jul 19 20:35:56 2017 -0300

----------------------------------------------------------------------
 .../org/apache/edgent/metrics/MetricsSetup.java | 51 ++++++++++++++++++++
 1 file changed, 51 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/fe7c3c21/utils/metrics/src/main/java/org/apache/edgent/metrics/MetricsSetup.java
----------------------------------------------------------------------
diff --git 
a/utils/metrics/src/main/java/org/apache/edgent/metrics/MetricsSetup.java 
b/utils/metrics/src/main/java/org/apache/edgent/metrics/MetricsSetup.java
index 5ab4564..2d421bc 100644
--- a/utils/metrics/src/main/java/org/apache/edgent/metrics/MetricsSetup.java
+++ b/utils/metrics/src/main/java/org/apache/edgent/metrics/MetricsSetup.java
@@ -18,7 +18,11 @@ under the License.
 */
 package org.apache.edgent.metrics;
 
+import java.io.File;
 import java.lang.management.ManagementFactory;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 
 import javax.management.MBeanServer;
@@ -41,6 +45,7 @@ import com.codahale.metrics.MetricRegistry;
 public class MetricsSetup {
     private static final TimeUnit durationsUnit = TimeUnit.MILLISECONDS;
     private static final TimeUnit ratesUnit = TimeUnit.SECONDS;
+    private static final String FOLDER_METRICS = "/metrics";
 
     private final MetricRegistry metricRegistry;
     private MBeanServer mBeanServer;
@@ -85,6 +90,33 @@ public class MetricsSetup {
         reporter.start();
         return this;
     }
+
+    /**
+     * Starts the metric {@code CsvReporter}. If no MBeanServer was set, use 
the
+     * virtual machine's platform MBeanServer.
+     * 
+     * @param pathMetrics
+     *            pathname where the metric files are stored. If the path does
+     *            not exist or is null is created in a standard directory,
+     *            called metrics
+     * @return this
+     */
+    public MetricsSetup startCSVReporter(String pathMetrics) {
+
+        if (pathMetrics == null) { // pathMetrics is NULL
+            pathMetrics = createDefaultDirectory();
+        } else {
+            File directory = new File(pathMetrics);
+            if (!directory.exists() && !directory.mkdirs()) {
+                pathMetrics = createDefaultDirectory();
+            }
+        }
+
+        final CsvReporter reporter = 
CsvReporter.forRegistry(registry()).formatFor(Locale.US).convertRatesTo(ratesUnit)
+                .convertDurationsTo(durationsUnit).build(new 
File(pathMetrics));
+        reporter.start(1, TimeUnit.SECONDS);
+        return this;
+    }
     
     /**
      * Starts the metric {@code ConsoleReporter} polling every second.
@@ -111,6 +143,25 @@ public class MetricsSetup {
         }
         return mBeanServer;
     }
+
+    /**
+     * Creates a default directory for metrics.
+     * 
+     * @return directory.getPath()
+     */
+    private String createDefaultDirectory() {
+        Path currentRelativePath = Paths.get("");
+        String pathMetrics = currentRelativePath.toAbsolutePath().toString() + 
FOLDER_METRICS;
+        File directory = new File(pathMetrics);
+        
+        if (!directory.mkdirs()) {
+            // Log: "Could not create the directory log"
+        } else {
+            // Log: "The directory was created successfully: "
+        }
+
+        return directory.getPath();
+    }
     
     private class MetricOpletCleaner implements BiConsumer<String, String> {
         

Reply via email to