Search in sources :

Example 1 with Gauge

use of org.springside.modules.metrics.metric.Gauge in project springside4 by springside.

the class ReportScheduler method report.

/**
 * 定期计算所有Metrics, 并调用所有Reporter进行汇报.
 */
public void report() {
    // 取出所有Metrics, 未按名称排序.
    Map<String, Gauge> gaugeMap = metricRegistry.getGauges();
    Map<String, Counter> counterMap = metricRegistry.getCounters();
    Map<String, Histogram> histogramMap = metricRegistry.getHistograms();
    Map<String, Timer> timerMap = metricRegistry.getTimers();
    // 计算单位时间内的metrics值, 存入该Metrics的Snapshot中,并清零原始数据
    for (Gauge gauge : gaugeMap.values()) {
        gauge.calculateMetric();
    }
    for (Counter counter : counterMap.values()) {
        counter.calculateMetric();
    }
    for (Histogram histogram : histogramMap.values()) {
        histogram.calculateMetric();
    }
    for (Timer timer : timerMap.values()) {
        timer.calculateMetric();
    }
    // 调度所有Reporters 输出 metrics值
    for (Reporter reporter : reporters) {
        reporter.report(gaugeMap, counterMap, histogramMap, timerMap);
    }
}
Also used : Histogram(org.springside.modules.metrics.metric.Histogram) Counter(org.springside.modules.metrics.metric.Counter) Timer(org.springside.modules.metrics.metric.Timer) Gauge(org.springside.modules.metrics.metric.Gauge)

Example 2 with Gauge

use of org.springside.modules.metrics.metric.Gauge in project springside4 by springside.

the class MetricExamples method gaugeExample.

@Test
public void gaugeExample() throws InterruptedException {
    MetricRegistry metricRegistry = new MetricRegistry();
    // 使用ConsoleReporter
    ConsoleReporter consoleReporter = new ConsoleReporter();
    final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    Gauge<Long> usedMemoryGague = new Gauge<Long>() {

        public Long getValue() {
            return memoryMXBean.getHeapMemoryUsage().getUsed();
        }
    };
    Gauge<Long> cachedUsedMemoryGague = new CachedGauge<Long>(10, TimeUnit.SECONDS) {

        public Long loadValue() {
            return memoryMXBean.getHeapMemoryUsage().getUsed();
        }
    };
    metricRegistry.registerGauge(MetricRegistry.name("JVM", "usedMemory"), usedMemoryGague);
    metricRegistry.registerGauge(MetricRegistry.name("JVM", "cachedUsedMemory"), cachedUsedMemoryGague);
    ReportScheduler scheduler = new ReportScheduler(metricRegistry, consoleReporter);
    scheduler.start(1, TimeUnit.SECONDS);
    Thread.sleep(1050);
    // use some memory
    List<Integer> list = new ArrayList<Integer>(200000);
    list.add(1);
    Thread.sleep(1050);
    scheduler.stop();
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) ConsoleReporter(org.springside.modules.metrics.reporter.ConsoleReporter) CachedGauge(org.springside.modules.metrics.metric.CachedGauge) ArrayList(java.util.ArrayList) Gauge(org.springside.modules.metrics.metric.Gauge) CachedGauge(org.springside.modules.metrics.metric.CachedGauge) Test(org.junit.Test)

Aggregations

Gauge (org.springside.modules.metrics.metric.Gauge)2 MemoryMXBean (java.lang.management.MemoryMXBean)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 CachedGauge (org.springside.modules.metrics.metric.CachedGauge)1 Counter (org.springside.modules.metrics.metric.Counter)1 Histogram (org.springside.modules.metrics.metric.Histogram)1 Timer (org.springside.modules.metrics.metric.Timer)1 ConsoleReporter (org.springside.modules.metrics.reporter.ConsoleReporter)1