@Override public void startImpl() { this.executor = Executors.newSingleThreadScheduledExecutor( ExecutorsUtils.newDaemonThreadFactory(Optional.of(log), Optional.of("metrics-" + name + "-scheduler"))); this.reportingPeriodSeconds = parsePeriodToSeconds( config.hasPath(REPORTING_INTERVAL) ? config.getString(REPORTING_INTERVAL) : DEFAULT_REPORTING_INTERVAL_PERIOD); ensureMetricFilterIsInitialized(config); this.scheduledTask = Optional.<ScheduledFuture>of(this.executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { report(); } }, 0, this.reportingPeriodSeconds, TimeUnit.SECONDS)); }
@Override public void close() throws IOException { try { this.closer.close(); } catch(Exception e) { LOGGER.warn("Exception when closing ConfiguredScheduledReporter", e); } finally { super.close(); } } }
private synchronized void ensureMetricFilterIsInitialized(Config config) { if (this.metricFilter == null) { this.metricFilter = createMetricFilter(config); } }
/*** * @param isFinal true if this is the final time report will be called for this reporter, false otherwise * @see #report() */ protected void report(boolean isFinal) { for (ReportableContext metricContext : getMetricContextsToReport()) { report(metricContext, isFinal); } }
@Override protected void removedMetricContext(InnerMetricContext context) { if (shouldReportInnerMetricContext(context)) { report(context, true); } super.removedMetricContext(context); }
/** * @see #report(SortedMap, SortedMap, SortedMap, SortedMap, SortedMap, Map) */ protected abstract void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers, Map<String, Object> tags); }
long reportInterval = Long.parseLong(properties .getProperty(ConfigurationKeys.METRICS_REPORT_INTERVAL_KEY, ConfigurationKeys.DEFAULT_METRICS_REPORT_INTERVAL)); ScheduledReporter.setReportingInterval(properties, reportInterval, reportTimeUnit);
@Test public void testPeriodParser() { Assert.assertEquals(ScheduledReporter.parsePeriodToSeconds("1s"), 1); Assert.assertEquals(ScheduledReporter.parsePeriodToSeconds("2m"), 120); Assert.assertEquals(ScheduledReporter.parsePeriodToSeconds("3h"), 3 * 3600); Assert.assertEquals(ScheduledReporter.parsePeriodToSeconds("1m2s"), 62); Assert.assertEquals(ScheduledReporter.parsePeriodToSeconds("1h1s"), 3601); Assert.assertEquals(ScheduledReporter.parsePeriodToSeconds("1h2m3s"), 3600 + 120 + 3); try { ScheduledReporter.parsePeriodToSeconds("1000000h"); Assert.fail(); } catch (RuntimeException re) { // fail unless exception is thrown } }
public ScheduledReporter(String name, Config config) { super(name, config); ensureMetricFilterIsInitialized(config); }
/** * Trigger emission of a report. */ public void report() { report(false); }
@Override protected void removedMetricContext(InnerMetricContext context) { if (shouldReportInnerMetricContext(context)) { report(context, true); } super.removedMetricContext(context); }
/*** * @param isFinal true if this is the final time report will be called for this reporter, false otherwise * @see #report() */ protected void report(boolean isFinal) { for (ReportableContext metricContext : getMetricContextsToReport()) { report(metricContext, isFinal); } }
ScheduledReporter.setReportingInterval(props, reportingIntervalMillis, TimeUnit.MILLISECONDS); Config config = ConfigUtils.propertiesToConfig(props); config = PrefixContextFilter.setPrefixString(config, ScheduledReporterTest.class.getSimpleName());
public ScheduledReporter(String name, Config config) { super(name, config); ensureMetricFilterIsInitialized(config); }
@Override public void startImpl() { this.executor = Executors.newSingleThreadScheduledExecutor( ExecutorsUtils.newDaemonThreadFactory(Optional.of(log), Optional.of("metrics-" + name + "-scheduler"))); this.reportingPeriodSeconds = parsePeriodToSeconds( config.hasPath(REPORTING_INTERVAL) ? config.getString(REPORTING_INTERVAL) : DEFAULT_REPORTING_INTERVAL_PERIOD); ensureMetricFilterIsInitialized(config); this.scheduledTask = Optional.<ScheduledFuture>of(this.executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { report(); } }, 0, this.reportingPeriodSeconds, TimeUnit.SECONDS)); }
@Override public void run() { report(); } }, 0, this.reportingPeriodSeconds, TimeUnit.SECONDS));
long reportInterval = Long.parseLong(properties .getProperty(ConfigurationKeys.METRICS_REPORT_INTERVAL_KEY, ConfigurationKeys.DEFAULT_METRICS_REPORT_INTERVAL)); ScheduledReporter.setReportingInterval(properties, reportInterval, reportTimeUnit);
@Override public void close() throws IOException { try { this.closer.close(); } catch(Exception e) { LOGGER.warn("Exception when closing ConfiguredScheduledReporter", e); } finally { super.close(); } } }
private synchronized void ensureMetricFilterIsInitialized(Config config) { if (this.metricFilter == null) { this.metricFilter = createMetricFilter(config); } }
/** * Report as {@link InnerMetricContext}. * * <p> * This method is marked as final because it is not directly invoked from the framework, so this method should not * be overloaded. Overload {@link #report(ReportableContext, boolean)} instead. * </p> * * @param context {@link InnerMetricContext} to report. * @see #report(ReportableContext, boolean) */ protected final void report(ReportableContext context) { report(context, false); }