Search in sources :

Example 1 with Slf4jReporter

use of org.springside.modules.metrics.reporter.Slf4jReporter in project springside4 by springside.

the class MetricExamples method timerExample.

@Test
public void timerExample() throws InterruptedException {
    MetricRegistry metricRegistry = new MetricRegistry();
    // 使用slf4j reporter,并使用自定义logger名字
    Slf4jReporter slf4jReporter = new Slf4jReporter("mymetrics");
    ReportScheduler scheduler = new ReportScheduler(metricRegistry);
    scheduler.addReporter(slf4jReporter);
    scheduler.start(1, TimeUnit.SECONDS);
    Timer timer = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.timer"), new Double[] { 99d, 99.99d });
    // 写法1
    TimerContext timerContext = timer.start();
    Thread.sleep(100);
    timerContext.stop();
    timerContext = timer.start();
    Thread.sleep(200);
    timerContext.stop();
    Thread.sleep(750);
    // 用法2
    long start = System.currentTimeMillis();
    Thread.sleep(150);
    timer.update(start);
    start = System.currentTimeMillis();
    Thread.sleep(250);
    timer.update(start);
    Thread.sleep(650);
    scheduler.stop();
}
Also used : Timer(org.springside.modules.metrics.metric.Timer) Slf4jReporter(org.springside.modules.metrics.reporter.Slf4jReporter) TimerContext(org.springside.modules.metrics.metric.Timer.TimerContext) Test(org.junit.Test)

Example 2 with Slf4jReporter

use of org.springside.modules.metrics.reporter.Slf4jReporter in project springside4 by springside.

the class MetricExamples method histogramExample.

@Test
public void histogramExample() throws InterruptedException {
    MetricRegistry metricRegistry = new MetricRegistry();
    // 使用slf4j reporter,并使用默认logger名字
    Slf4jReporter slf4jReporter = new Slf4jReporter();
    ReportScheduler scheduler = new ReportScheduler(metricRegistry, slf4jReporter);
    scheduler.start(1, TimeUnit.SECONDS);
    Histogram histogram = metricRegistry.histogram(MetricRegistry.name("UserService", "getUser.latency"));
    histogram.update(1);
    histogram.update(100);
    Thread.sleep(1050);
    // 增加百分位
    histogram.setPcts(new Double[] { 99d, 99.95d });
    histogram.update(2);
    histogram.update(200);
    Thread.sleep(1050);
    scheduler.stop();
}
Also used : Histogram(org.springside.modules.metrics.metric.Histogram) Slf4jReporter(org.springside.modules.metrics.reporter.Slf4jReporter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Slf4jReporter (org.springside.modules.metrics.reporter.Slf4jReporter)2 Histogram (org.springside.modules.metrics.metric.Histogram)1 Timer (org.springside.modules.metrics.metric.Timer)1 TimerContext (org.springside.modules.metrics.metric.Timer.TimerContext)1