APIHistogram: Moving the histogram data collection to the API
This patch replace the Histogram data and statistic calculation from the JMX proxy to the API. This way the count, sum, min, max variance and square sum are calculated always on the server. When an update is perform, the API would return the statistic with a sample of the last n elements as a sample. This implementation insure that the counters are correct. The implementation also allows to set the minimal update interval. The implementation adds a mutable getter for each of the parameters so they could be set to their new value. Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This commit is contained in:
parent
e02313923b
commit
e2cdc95b81
@ -6,24 +6,93 @@ package com.yammer.metrics.core;
|
||||
* Modified by Cloudius Systems
|
||||
*/
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import com.cloudius.urchin.api.APIClient;
|
||||
import com.yammer.metrics.stats.Sample;
|
||||
import com.yammer.metrics.stats.Snapshot;
|
||||
|
||||
public class APIHistogram extends Histogram {
|
||||
Field countField;
|
||||
Field minField;
|
||||
Field maxField;
|
||||
Field sumField;
|
||||
Field varianceField;
|
||||
Field sampleField;
|
||||
|
||||
long last_update = 0;
|
||||
static final long UPDATE_INTERVAL = 50;
|
||||
long updateInterval;
|
||||
String url;
|
||||
private APIClient c = new APIClient();
|
||||
|
||||
public APIHistogram(String _url, Sample sample) {
|
||||
super(sample);
|
||||
url = _url;
|
||||
private void setFields() {
|
||||
try {
|
||||
minField = Histogram.class.getDeclaredField("min");
|
||||
minField.setAccessible(true);
|
||||
maxField = Histogram.class.getDeclaredField("max");
|
||||
maxField.setAccessible(true);
|
||||
sumField = Histogram.class.getDeclaredField("sum");
|
||||
sumField.setAccessible(true);
|
||||
varianceField = Histogram.class.getDeclaredField("variance");
|
||||
varianceField.setAccessible(true);
|
||||
sampleField = Histogram.class.getDeclaredField("sample");
|
||||
sampleField.setAccessible(true);
|
||||
countField = Histogram.class.getDeclaredField("count");
|
||||
countField.setAccessible(true);
|
||||
} catch (NoSuchFieldException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public APIHistogram(String _url, SampleType type) {
|
||||
public AtomicLong getMin() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
return (AtomicLong) minField.get(this);
|
||||
}
|
||||
|
||||
public AtomicLong getMax() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
return (AtomicLong) maxField.get(this);
|
||||
}
|
||||
|
||||
public AtomicLong getSum() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
return (AtomicLong) sumField.get(this);
|
||||
}
|
||||
|
||||
public AtomicLong getCount() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
return (AtomicLong) countField.get(this);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public AtomicReference<double[]> getVariance()
|
||||
throws IllegalArgumentException, IllegalAccessException {
|
||||
return (AtomicReference<double[]>) varianceField.get(this);
|
||||
}
|
||||
|
||||
public Sample getSample() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
return (Sample) sampleField.get(this);
|
||||
}
|
||||
|
||||
public APIHistogram(String url, Sample sample) {
|
||||
super(sample);
|
||||
setFields();
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public APIHistogram(String url, SampleType type, long updateInterval) {
|
||||
super(type);
|
||||
url = _url;
|
||||
setFields();
|
||||
this.url = url;
|
||||
this.updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
public APIHistogram(String url, SampleType type) {
|
||||
this(url, type, UPDATE_INTERVAL);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
@ -33,9 +102,21 @@ public class APIHistogram extends Histogram {
|
||||
}
|
||||
last_update = now;
|
||||
clear();
|
||||
long[] vals = c.getLongArrValue(url);
|
||||
for (long v : vals) {
|
||||
update(v);
|
||||
HistogramValues vals = c.getHistogramValue(url);
|
||||
try {
|
||||
for (long v : vals.sample) {
|
||||
getSample().update(v);
|
||||
}
|
||||
getCount().set(vals.count);
|
||||
getMax().set(vals.max);
|
||||
getMin().set(vals.min);
|
||||
getSum().set(vals.sum);
|
||||
double[] newValue = new double[2];
|
||||
newValue[0] = vals.variance;
|
||||
newValue[1] = vals.svariance;
|
||||
getVariance().getAndSet(newValue);
|
||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user