29 lines
1.4 KiB
Markdown
29 lines
1.4 KiB
Markdown
|
|
|||
|
### 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)的指南。
|