From 2d979952293181809b42fa5b3d1919074faf8729 Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Tue, 14 Jul 2015 17:49:38 +0300 Subject: [PATCH] Adding the APITimer The Timer object in the yammer library is used to regularly check a histogram. The APITimer is a Timer that uses the APIHistogram instead that in it self calls the API to get its values. Signed-off-by: Amnon Heiman --- .../com/yammer/metrics/core/APITimer.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/com/yammer/metrics/core/APITimer.java diff --git a/src/main/java/com/yammer/metrics/core/APITimer.java b/src/main/java/com/yammer/metrics/core/APITimer.java new file mode 100644 index 0000000..b750f56 --- /dev/null +++ b/src/main/java/com/yammer/metrics/core/APITimer.java @@ -0,0 +1,44 @@ +/* + * Copyright 2015 Cloudius Systems + * + */ +package com.yammer.metrics.core; + +import java.lang.reflect.Field; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import com.yammer.metrics.core.Histogram.SampleType; + +/** + * A timer metric which aggregates timing durations and provides duration + * statistics, plus throughput statistics via {@link Meter}. + */ +public class APITimer extends Timer { + + public APITimer(String url, ScheduledExecutorService tickThread, + TimeUnit durationUnit, TimeUnit rateUnit) { + super(tickThread, durationUnit, rateUnit); + setHistogram(url); + } + + public APITimer(String url, ScheduledExecutorService tickThread, + TimeUnit durationUnit, TimeUnit rateUnit, Clock clock) { + super(tickThread, durationUnit, rateUnit, clock); + setHistogram(url); + } + + private void setHistogram(String url) { + Field histogram; + try { + histogram = Timer.class.getDeclaredField("histogram"); + histogram.setAccessible(true); + histogram.set(this, new APIHistogram(url, SampleType.BIASED)); + } catch (NoSuchFieldException | SecurityException + | IllegalArgumentException | IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +}