/** * Enables the CSV reporter for the given metrics registry, and causes it to write to files in * {@code outputDir} with the specified period. * * @param metricsRegistry the metrics registry * @param outputDir the directory in which {@code .csv} files will be created * @param period the period between successive outputs * @param unit the time unit of {@code period} */ public static void enable(MetricsRegistry metricsRegistry, File outputDir, long period, TimeUnit unit) { final CsvReporter reporter = new CsvReporter(metricsRegistry, outputDir); reporter.start(period, unit); }
/** * Enables the CSV reporter for the default metrics registry, and causes it to write to files in * {@code outputDir} with the specified period. * * @param outputDir the directory in which {@code .csv} files will be created * @param period the period between successive outputs * @param unit the time unit of {@code period} */ public static void enable(File outputDir, long period, TimeUnit unit) { enable(Metrics.defaultRegistry(), outputDir, period, unit); }
private PrintStream getPrintStream(MetricName metricName, String header) throws IOException { PrintStream stream; synchronized (streamMap) { stream = streamMap.get(metricName); if (stream == null) { stream = createStreamForMetric(metricName); streamMap.put(metricName, stream); stream.println(header); } } return stream; } }
CsvReporter csvReporter = new CsvReporter(csvOut, Metrics.defaultRegistry()); csvReporter.start(updateInterval, TimeUnit.SECONDS); csvReporter.shutdown();
@Override public void run() { final long time = TimeUnit.MILLISECONDS.toSeconds(clock.time() - startTime); final Set<Entry<MetricName, Metric>> metrics = getMetricsRegistry().allMetrics().entrySet(); try { for (Entry<MetricName, Metric> entry : metrics) { final MetricName metricName = entry.getKey(); final Metric metric = entry.getValue(); if (predicate.matches(metricName, metric)) { final Context context = new Context() { @Override public PrintStream getStream(String header) throws IOException { final PrintStream stream = getPrintStream(metricName, header); stream.print(time); stream.print(','); return stream; } }; metric.processWith(this, entry.getKey(), context); } } } catch (Exception e) { e.printStackTrace(); } }
@Override public boolean enable() { log.info("Enabling CsvReporter to {}", outdir); try { File foutDir = new File(outdir); boolean success = foutDir.mkdirs(); if (!success) { log.error("Failed to create directory {} for CsvReporter", outdir); return false; } // static enable() methods omit the option of specifying a // predicate. Calling constructor and starting manually // instead final CsvReporter reporter = new CsvReporter(Metrics.defaultRegistry(), getMetricPredicate(), foutDir); reporter.start(getPeriod(), getRealTimeunit()); } catch (Exception e) { log.error("Failure while Enabling CsvReporter", e); return false; } return true; }
/** Process csv options */ private void processCsv() { if (csvEnabled()) { File dir = new File(csvDirectory); try { FileUtils.deleteDirectory(dir); // CHECKSTYLE: stop EmptyBlock } catch (IOException e) { } // CHECKSTYLE: resume EmptyBlock if (!dir.mkdirs()) { LOG.error("Failed to create CSV directory " + dir); } CsvReporter.enable(dir, csvPrintSecs, TimeUnit.SECONDS); } }
@Override public boolean enable() { log.info("Enabling CsvReporter to {}", outdir); try { File foutDir = createFile(); if (foutDir == null) { log.error("Failed to create directory {} for CsvReporter", outdir); return false; } // static enable() methods omit the option of specifying a // predicate. Calling constructor and starting manually // instead final CsvReporter reporter = new CsvReporter( Metrics.defaultRegistry(), MetricPredicateTransformer.generatePredicate(getPredicate()), foutDir); reporter.start(getPeriod(), getRealTimeunit()); } catch (Exception e) { log.error("Failure while Enabling CsvReporter", e); return false; } return true; }
break; case FILE: CsvReporter.enable(outputDir, 1, TimeUnit.SECONDS); break;
break; case FILE: CsvReporter.enable(outputDir, 1, TimeUnit.SECONDS); break;