From b5c386c25a18fafc6e4c3dc2d623ad357397de8b Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Wed, 4 Mar 2015 22:42:07 +0800 Subject: [PATCH] Update and rename 44.4. Recording your own metrics to 44.4. Recording your own metrics.md --- .../44.4. Recording your own metrics | 0 .../44.4. Recording your own metrics.md | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+) delete mode 100644 V. Spring Boot Actuator: Production-ready features/44.4. Recording your own metrics create mode 100644 V. Spring Boot Actuator: Production-ready features/44.4. Recording your own metrics.md diff --git a/V. Spring Boot Actuator: Production-ready features/44.4. Recording your own metrics b/V. Spring Boot Actuator: Production-ready features/44.4. Recording your own metrics deleted file mode 100644 index e69de29..0000000 diff --git a/V. Spring Boot Actuator: Production-ready features/44.4. Recording your own metrics.md b/V. Spring Boot Actuator: Production-ready features/44.4. Recording your own metrics.md new file mode 100644 index 0000000..353d42b --- /dev/null +++ b/V. Spring Boot Actuator: Production-ready features/44.4. Recording your own metrics.md @@ -0,0 +1,28 @@ + +### 44.4. 记录自己的指标 + +想要记录你自己的指标,只需将[CounterService](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/CounterService.java)或[GaugeService](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/GaugeService.java)注入到你的bean中。CounterService暴露increment,decrement和reset方法;GaugeService提供一个submit方法。 + +下面是一个简单的示例,它记录了方法调用的次数: +```java +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.metrics.CounterService; +import org.springframework.stereotype.Service; + +@Service +public class MyService { + + private final CounterService counterService; + + @Autowired + public MyService(CounterService counterService) { + this.counterService = counterService; + } + + public void exampleMethod() { + this.counterService.increment("services.system.myservice.invoked"); + } + +} +``` +**注**:你可以将任何的字符串用作指标的名称,但最好遵循所选存储或图技术的指南。[Matt Aimonetti’s Blog](http://matt.aimonetti.net/posts/2013/06/26/practical-guide-to-graphite-monitoring/)中有一些好的关于图(Graphite)的指南。