// 构造器创建Countercounter=Counter.builder("http.request").baseUnit("num")// optional.description("a description of what this counter does")// optional.tags("uri","/order/create")// optional.register(registry);counter.increment();// 直接从Registry创建MeterRegistrymeterRegistry=newSimpleMeterRegistry();Countercounter=meterRegistry.counter("http.request","uri","/order/create");counter.increment();
// 构造器创建publicclassFunctionCounterMain{publicstaticvoidmain(String[]args)throwsException{MeterRegistryregistry=newSimpleMeterRegistry();AtomicIntegern=newAtomicInteger(0);//这里ToDoubleFunction匿名实现其实可以使用Lambda表达式简化为AtomicInteger::getFunctionCounter.builder("functionCounter",n,newToDoubleFunction<AtomicInteger>(){@OverridepublicdoubleapplyAsDouble(AtomicIntegervalue){returnvalue.get();}}).baseUnit("function").description("functionCounter").tag("createOrder","CHANNEL-A").register(registry);//下面模拟三次计数 n.incrementAndGet();n.incrementAndGet();n.incrementAndGet();}}// 直接从Registry创建Cachecache=...;// suppose we have a Guava cache with stats recording onregistry.more().counter("evictions",tags,cache,c->c.stats().evictionCount());
// 构造器创建Gaugegauge=Gauge.builder("gauge",myObj,myObj::gaugeValue).description("a description of what this gauge does")// optional.tags("region","test")// optional.register(registry);// 直接从registry创建registry.gauge("listGauge",Collections.emptyList(),newArrayList<>(),List::size);
// 构造器创建Timertimer=Timer.builder("my.timer").description("a description of what this timer does")// optional.tags("region","test")// optional.register(registry);// 直接从Registry创建registry.timer("my.timer","region","test");// record directlytimer.record(Duration.of(60L,ChronoUnit.SECONDS));// record functiontimer.record(()->{// some operation ...});// wrap functiontimer.wrap(()->yourFunction());// use sampleTimer.Samplesample=Timer.start();// do somethingsample.stop(timer);
Long Task Timer同样也是Timer的特殊类型。统计的是当前有多少正在执行的任务,以及这些这任务已经耗费了多少时间,
适用于监控长时间执行的任务和方法,统计类似当前负载量的相关指标。Long Task Timer在事件开始时记录,
在事件结束后将事件移除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 构造器创建LongTaskTimerlongTaskTimer=LongTaskTimer.builder("long.task.timer").description("a description of what this timer does")// optional.tags("region","test")// optional.register(registry);// 直接从Registry创建LongTaskTimerscrapeTimer=registry.more().longTaskTimer("scrape");// record functionscrapeTimer.record(()->{// some operation ...});// record by sampleLongTaskTimer.Samplestart=scrapeTimer.start();// do somethingstart.stop();
publicvoidsummary(){DistributionSummarysummary=DistributionSummary.builder("simple").description("simple distribution summary").minimumExpectedValue(1L).maximumExpectedValue(10L).publishPercentiles(0.5,0.75,0.9).register(registry);summary.record(1);summary.record(1.3);summary.record(2.4);summary.record(3.5);summary.record(4.1);System.out.println(summary.takeSnapshot());}
使用场景:不依赖于时间单位的记录值的测量,例如服务器有效负载值,缓存的命中率等。
1
2
3
4
5
6
7
8
9
10
11
// 构造器创建DistributionSummarysummary=DistributionSummary.builder("response.size").description("a description of what this summary does")// optional.baseUnit("bytes")// optional (1).tags("region","test")// optional.scale(100)// optional (2).register(registry);// 直接从registry创建DistributionSummarysummary=registry.summary("response.size");
Spring Boot 工程集成 Micrometer
我们一般说 Spring Boot 集成 Micrometer 值得时 Spring 2.x 版本,
因为在该版本 spring-boot-actuator 使用了 Micrometer 来实现监控。
@RestController@RequestMapping("/v1")publicclassIndexController{privatefinalMeterRegistryregistry;privateCountercounter_core;privateCountercounter_index;publicIndexController(MeterRegistryregistry){this.registry=registry;}@PostConstructprivatevoidinit(){counter_core=registry.counter("app_requests_method_count","method","IndexController.core");counter_index=registry.counter("app_requests_method_count","method","IndexController.index");}@RequestMapping(value="/index")publicObjectindex(){try{counter_index.increment();}catch(Exceptione){returne;}returncounter_index.count()+" index of springboot2-prometheus.";}@RequestMapping(value="/core")publicObjectcoreUrl(){try{counter_core.increment();}catch(Exceptione){returne;}returncounter_core.count()+" coreUrl Monitor by Prometheus.";}}