/** * Creates a new {@link ExponentiallyDecayingSample}. * * @param reservoirSize the number of samples to keep in the sampling reservoir * @param alpha the exponential decay factor; the higher this is, the more biased the * sample will be towards newer values */ public ExponentiallyDecayingSample(int reservoirSize, double alpha) { this(reservoirSize, alpha, Clock.defaultClock()); }
private long currentTimeInSeconds() { return TimeUnit.MILLISECONDS.toSeconds(clock.time()); }
/** * Creates a new {@link TimerContext} with the current time as its starting value and with the * given {@link Timer}. * * @param timer the {@link Timer} to report the elapsed time to */ TimerContext(Timer timer, Clock clock) { this.timer = timer; this.clock = clock; this.startTime = clock.tick(); }
/** * Creates a new {@link MetricsRegistry}. */ public MetricsRegistry() { this(Clock.defaultClock()); }
@Override public void start(long period, TimeUnit unit) { this.startTime = clock.time(); super.start(period, unit); }
public static void main(String[] args) { Clock myClock = new Clock; // new clock object. while (true) { myClock.tick(); // wait for a second. try { Thread.sleep(1000); } catch (InterruptedException ie) { // Handle the exception } } }
/** * Creates a new {@link Timer}. * * @param tickThread background thread for updating the rates * @param durationUnit the scale unit for this timer's duration metrics * @param rateUnit the scale unit for this timer's rate metrics */ Timer(ScheduledExecutorService tickThread, TimeUnit durationUnit, TimeUnit rateUnit) { this(tickThread, durationUnit, rateUnit, Clock.defaultClock()); }
@Override public void start(long period, TimeUnit unit) { this.startTime = clock.time(); super.start(period, unit); }
/** * Creates a new {@link Meter}. * * @param tickThread background thread for updating the rates * @param eventType the plural name of the event the meter is measuring (e.g., {@code * "requests"}) * @param rateUnit the rate unit of the new meter * @param clock the clock to use for the meter ticks */ Meter(ScheduledExecutorService tickThread, String eventType, TimeUnit rateUnit, Clock clock) { this.rateUnit = rateUnit; this.eventType = eventType; this.future = tickThread.scheduleAtFixedRate(new Runnable() { @Override public void run() { tick(); } }, INTERVAL, INTERVAL, TimeUnit.SECONDS); this.clock = clock; this.startTime = this.clock.tick(); }
/** * Creates a new {@link CsvReporter} which will write metrics from the given * {@link MetricsRegistry} which match the given {@link MetricPredicate} to CSV files in the * given output directory. * * @param metricsRegistry the {@link MetricsRegistry} containing the metrics this reporter * will report * @param predicate the {@link MetricPredicate} which metrics are required to match * before being written to files * @param outputDir the directory to which files will be written */ public CsvReporter(MetricsRegistry metricsRegistry, MetricPredicate predicate, File outputDir) { this(metricsRegistry, predicate, outputDir, Clock.defaultClock()); }
locale); format.setTimeZone(timeZone); final String dateTime = format.format(new Date(clock.time())); out.print(dateTime); out.print(' ');
/** * Times and records the duration of event. * * @param event a {@link Callable} whose {@link Callable#call()} method implements a process * whose duration should be timed * @param <T> the type of the value returned by {@code event} * @return the value returned by {@code event} * @throws Exception if {@code event} throws an {@link Exception} */ public <T> T time(Callable<T> event) throws Exception { final long startTime = clock.tick(); try { return event.call(); } finally { update(clock.tick() - startTime); } }
/** * Creates a new {@link ConsoleReporter} for a given metrics registry. * * @param metricsRegistry the metrics registry * @param out the {@link PrintStream} to which output will be written * @param predicate the {@link MetricPredicate} used to determine whether a metric will be * output */ public ConsoleReporter(MetricsRegistry metricsRegistry, PrintStream out, MetricPredicate predicate) { this(metricsRegistry, out, predicate, Clock.defaultClock(), TimeZone.getDefault()); }
@Override public void run() { try { statsD.connect(); final long epoch = clock.time() / 1000; printRegularMetrics(epoch); } catch (IOException e) { LOG.info("Failed to connect or print metrics to statsd", e); } finally { try { statsD.close(); } catch (IOException e) { LOG.info("Failure when closing statsd connection", e); } } }
private void rescaleIfNeeded() { final long now = clock.tick(); final long next = nextScaleTime.get(); if (now >= next) { rescale(now, next); } }
/** do not instantiate */ private NoOpMeter() { super(new NoOpExecutorService(), "", TimeUnit.SECONDS, Clock.defaultClock()); }
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); final long epoch = clock.time() / 1000; if (this.printVMMetrics) { printVmMetrics(epoch);
/** * Stops recording the elapsed time and updates the timer. */ public void stop() { timer.update(clock.tick() - startTime, TimeUnit.NANOSECONDS); } }
protected X(MetricsRegistry registry) { super(registry, "consumer-groups-reporter"); this.clock = Clock.defaultClock(); this.admin = AdminClient.create(props); }
@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(); } }