Update and rename 44.4. Recording your own metrics to 44.4. Recording your own metrics.md

master
qibaoguang 2015-03-04 22:42:07 +08:00
parent 7a7ccc6aa9
commit b5c386c25a
2 changed files with 28 additions and 0 deletions

View File

@ -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暴露incrementdecrement和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 Aimonettis Blog](http://matt.aimonetti.net/posts/2013/06/26/practical-guide-to-graphite-monitoring/)中有一些好的关于图Graphite的指南。