Skip to content

Commit

Permalink
small upgrde for sprnigside-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin committed Dec 11, 2014
1 parent eb12098 commit 66940a0
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public void reset() {
counter = new AtomicLong(0);
totalCount = 0L;
startTime = clock.getCurrentTime();
lastReportTime = 0L;
lastReportTime = startTime;
}

@Override
public String toString() {
return "Counter [counter=" + counter + ", totalCount=" + totalCount + ", startTime=" + startTime
+ ", lastReportTime=" + lastReportTime + "]";
return "Counter [latestMetric=" + latestMetric + ", counter=" + counter + ", totalCount=" + totalCount
+ ", startTime=" + startTime + ", lastReportTime=" + lastReportTime + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
package org.springside.modules.metrics;

public abstract class Gauge {

public Number snapshot;

public void calculateMetric(){
snapshot = getValue();
}

protected abstract Number getValue();

@Override
public String toString() {
return "Gauge [snapshot=" + snapshot + "]";
}
public Number latestMetric;

public void calculateMetric() {
latestMetric = getValue();
}

protected abstract Number getValue();

@Override
public String toString() {
return "Gauge [latestMetric=" + latestMetric + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,41 @@ public HistogramMetric calculateMetric() {
return createEmptyMetric();
}

// 按数值大小排序,以快速支持百分比过滤
Collections.sort(snapshotList);

int count = snapshotList.size();

HistogramMetric metric = new HistogramMetric();
metric.min = snapshotList.get(0);
metric.max = snapshotList.get(count - 1);

int count = snapshotList.size();
double sum = 0;
for (long value : snapshotList) {
sum += value;
}
metric.mean = sum / count;

for (Double pct : pcts) {
metric.pcts.put(pct, getPercent(snapshotList, count, pct));
if ((pcts != null) && (pcts.length > 0)) {
// 按数值大小排序,以快速支持百分比过滤
Collections.sort(snapshotList);

metric.min = snapshotList.get(0);
metric.max = snapshotList.get(count - 1);

for (long value : snapshotList) {
sum += value;
}

for (Double pct : pcts) {
metric.pcts.put(pct, getPercent(snapshotList, count, pct));
}
} else {
// 不排序的算法,因为不需要支持百分比过滤
metric.min = snapshotList.get(0);
metric.max = snapshotList.get(0);

for (long value : snapshotList) {
if (value < metric.min) {
metric.min = value;
}
if (value > metric.max) {
metric.max = value;
}
sum += value;
}
}

metric.mean = sum / count;
latestMetric = metric;
return metric;
}
Expand Down Expand Up @@ -93,6 +109,7 @@ private Long getPercent(List<Long> snapshotList, int count, double pct) {

@Override
public String toString() {
return "Histogram [measurements=" + measurements + ", pcts=" + Arrays.toString(pcts) + "]";
return "Histogram [latestMetric=" + latestMetric + ", measurements=" + measurements + ", pcts="
+ Arrays.toString(pcts) + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public TimerMetric calculateMetric() {

@Override
public String toString() {
return "Timer [counter=" + counter + ", histogram=" + histogram + "]";
return "Timer [latestMetric=" + latestMetric + ", counter=" + counter + ", histogram=" + histogram + "]";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public JmxGauge(Gauge gauge, ObjectName objectName) {

@Override
public Number getValue() {
return metric.snapshot;
return metric.latestMetric;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void printWithBanner(String s, char c) {
}

private void printGauge(Gauge gauge) {
output.printf(" value = %s%n", gauge.snapshot);
output.printf(" value = %s%n", gauge.latestMetric);
}

private void printCounter(CounterMetric counter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void report(Map<String, Gauge> gauges, Map<String, Counter> counters, Map
long timestamp = System.currentTimeMillis() / 1000;

for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
reportGauge(entry.getKey(), entry.getValue().snapshot, timestamp);
reportGauge(entry.getKey(), entry.getValue().latestMetric, timestamp);
}

for (Map.Entry<String, Counter> entry : counters.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void report(Map<String, Gauge> gauges, Map<String, Counter> counters, Map
Map<String, Timer> timers) {

for (Entry<String, Gauge> entry : getSortedMetrics(gauges).entrySet()) {
logGauge(entry.getKey(), entry.getValue().snapshot);
logGauge(entry.getKey(), entry.getValue().latestMetric);
}

for (Entry<String, Counter> entry : getSortedMetrics(counters).entrySet()) {
Expand Down

0 comments on commit 66940a0

Please sign in to comment.