expose report() method in MetricsFileReporter
authorMichael Vorburger <vorburger@redhat.com>
Sun, 23 Sep 2018 11:48:40 +0000 (13:48 +0200)
committerAnil Belur <abelur@linuxfoundation.org>
Thu, 7 Oct 2021 05:19:58 +0000 (15:19 +1000)
so that performance tests (e.g. etc'd) can dump Timer metrics.

Change-Id: I22fb8a7f8f1305bd4bbcba7f74b5faacb8e1c438
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
metrics/impl/src/main/java/org/opendaylight/infrautils/metrics/internal/MetricProviderImpl.java
metrics/impl/src/main/java/org/opendaylight/infrautils/metrics/internal/MetricsFileReporter.java

index db7da44ab4016d817e85525d8aae775acc841f39..dfc50d73d9d6f5d577960e0a4144f4d1bc90b60f 100644 (file)
@@ -23,6 +23,7 @@ import com.codahale.metrics.jvm.FileDescriptorRatioGauge;
 import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
 import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
 import com.codahale.metrics.jvm.ThreadDeadlockDetector;
+import com.google.common.annotations.VisibleForTesting;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.lang.management.ManagementFactory;
 import java.time.Duration;
@@ -127,6 +128,11 @@ public class MetricProviderImpl implements MetricProvider {
         }
     }
 
+    @VisibleForTesting
+    public MetricRegistry getRegistry() {
+        return registry;
+    }
+
     private static void setUpJvmMetrics(MetricRegistry registry) {
         ThreadDeadlockDetector threadDeadlockDetector = new ThreadDeadlockDetector();
         FileDescriptorRatioGauge fileDescriptorRatioGauge = new FileDescriptorRatioGauge();
index 5b44da8c3fd022544c0ee49c4ad4fcef1d3e9b3d..b169b4600420ffcbb77d18145aa898bcf5cd2064 100644 (file)
@@ -47,11 +47,13 @@ public class MetricsFileReporter extends ScheduledReporter {
 
     private final File parentDirectory;
     private final Map<String, Long> oldCounters = new HashMap<>();
+    private final MetricRegistry registry;
     private final Duration interval;
 
     public MetricsFileReporter(MetricRegistry registry, Duration interval) {
         super(registry, "file-reporter", MetricFilter.ALL, TimeUnit.SECONDS, TimeUnit.SECONDS);
         this.parentDirectory = new File(DATA_DIRECTORY, COUNTERS_DIRECTORY);
+        this.registry = registry;
         this.interval = interval;
     }
 
@@ -63,6 +65,59 @@ public class MetricsFileReporter extends ScheduledReporter {
         return interval;
     }
 
+    public void report(PrintWriter pw) {
+        report(pw, registry.getGauges(), registry.getCounters(),
+                registry.getHistograms(), registry.getMeters(), registry.getTimers());
+    }
+
+    private void report(PrintWriter pw, @SuppressWarnings("rawtypes") SortedMap<String, Gauge> gauges,
+            SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms,
+            SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
+        pw.print("date,");
+        pw.print(new Date());
+        pw.println();
+
+        pw.println("Counters:");
+        for (Map.Entry<String, Counter> entry : counters.entrySet()) {
+            Counter newCounter = entry.getValue();
+            // avoid unnecessary write to report file
+            // report the counter only if there is a change
+            Long oldCounterObj = oldCounters.get(entry.getKey());
+            long oldCounter = oldCounterObj != null ? oldCounterObj.longValue() : 0;
+            if (newCounter.getCount() != oldCounter) {
+                pw.print(entry.getKey());
+                printWithSeparator(pw, "count", entry.getValue().getCount());
+                printWithSeparator(pw, "diff",
+                        entry.getValue().getCount() - oldCounter);
+                pw.println();
+            }
+        }
+        pw.println("Gauges:");
+        for (@SuppressWarnings("rawtypes") Map.Entry<String, Gauge> entry : gauges.entrySet()) {
+            pw.print(entry.getKey());
+            pw.println(entry.getValue().getValue());
+        }
+        pw.println("Histograms:");
+        for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
+            pw.print(entry.getKey());
+            printWithSeparator(pw, "count", entry.getValue().getCount());
+            printSampling(pw, entry.getValue());
+            pw.println();
+        }
+        pw.println("Meters:");
+        for (Map.Entry<String, Meter> entry : meters.entrySet()) {
+            pw.print(entry.getKey());
+            printMeter(pw, entry.getValue());
+        }
+        pw.println("Timers:");
+        for (Map.Entry<String, Timer> entry : timers.entrySet()) {
+            pw.print(entry.getKey());
+            printSampling(pw, entry.getValue());
+            printMeter(pw, entry.getValue());
+        }
+        counters.forEach((key, value) -> oldCounters.put(key, value.getCount()));
+    }
+
     @Override
     public void report(@SuppressWarnings("rawtypes")
                        SortedMap<String, Gauge> gauges,
@@ -80,49 +135,7 @@ public class MetricsFileReporter extends ScheduledReporter {
             File file = createFile(dayOfTheWeek, hourOfTheDay);
             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, append),
                     DEFAULT_ENCODING));
-            pw.print("date,");
-            pw.print(new Date());
-            pw.println();
-
-            pw.println("Counters:");
-            for (Map.Entry<String, Counter> entry : counters.entrySet()) {
-                Counter newCounter = entry.getValue();
-                // avoid unnecessary write to report file
-                // report the counter only if there is a change
-                Long oldCounterObj = oldCounters.get(entry.getKey());
-                long oldCounter = oldCounterObj != null ? oldCounterObj.longValue() : 0;
-                if (newCounter.getCount() != oldCounter) {
-                    pw.print(entry.getKey());
-                    printWithSeparator(pw, "count", entry.getValue().getCount());
-                    printWithSeparator(pw, "diff",
-                            entry.getValue().getCount() - oldCounter);
-                    pw.println();
-                }
-            }
-            pw.println("Gauges:");
-            for (@SuppressWarnings("rawtypes") Map.Entry<String, Gauge> entry : gauges.entrySet()) {
-                pw.print(entry.getKey());
-                pw.println(entry.getValue().getValue());
-            }
-            pw.println("Histograms:");
-            for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
-                pw.print(entry.getKey());
-                printWithSeparator(pw, "count", entry.getValue().getCount());
-                printSampling(pw, entry.getValue());
-                pw.println();
-            }
-            pw.println("Meters:");
-            for (Map.Entry<String, Meter> entry : meters.entrySet()) {
-                pw.print(entry.getKey());
-                printMeter(pw, entry.getValue());
-            }
-            pw.println("Timers:");
-            for (Map.Entry<String, Timer> entry : timers.entrySet()) {
-                pw.print(entry.getKey());
-                printSampling(pw, entry.getValue());
-                printMeter(pw, entry.getValue());
-            }
-            counters.forEach((key, value) -> oldCounters.put(key, value.getCount()));
+            report(pw, gauges, counters, histograms, meters, timers);
             pw.close();
         } catch (IOException e) {
             LOG.error("Failed to report counters to files", e);