
# 支持注解形式自定义监控指标
#### 使用场景
为了方便用户更加快速、便捷地使用micrometer定义自己的监控指标，devspore-probe提供了注解的方式去快速地定义监控指标，避免了定义复杂的监控类。
#### 如何使用
1. 项目中引入devspore-probe依赖，在项目的pom.xml中添加下面的配置。 
   ```
   <dependency>
   <groupId>com.huaweicloud.devspore</groupId>
   <artifactId>devspore-probe</artifactId>
   <version>latest</version>
   </dependency>
   ```
   
   
2. 根据业务需要，添加自定义监控指标，devspore-probe根据Prometheus的监控数据类型，提供了Counter、Gauge、Summary、Histogram四种类型的自定义指标类型，使用说明、配置、示例、效果参照[表1]。
   
    表1自定义监控指标 
   | 监控类型                                                                                 | 注解参数                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | 示例                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
   |:---|:---|:---|
   | Counter（计数器） 每次调用被注解函数，监控指标值自动加1。                  | 方法注解@Counter。 - name：String类型，自定义指标名称。  - 方法类型：不限。  - 效果：每次执行该方法，对应指标值+1。   参数注解@Label。 - name：String类型，自定义指标Label Key值。  - 被注解的参数：String类型，自定义指标Label Value值。                                                                                                                      | ``` @Counter(name = "application_name") public Object index(@Label(name = "name") String userName, @Label(name = "age") String age) { // 执行业务代码 } ``` prometheus效果： ``` application_name_total{age="xxx",application="devspore-application",name="xxxxxx",} 1.0 application_name_total{age="yyy",application="devspore-application",name="yyyyyy",} 1.0 ```                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
   | Gauge(测量) 是表示单个数值，可以任意地上升和下降的度量。                  | 方法注解@Gauge。 - name：String类型，自定义指标名称。  - 方法类型：Number。  - 效果：每次执行该方法，对应指标值设置为方法的返回值。   参数注解@Label。 - name：String类型，自定义指标Label Key值。  - 被注解的参数：String类型，自定义指标Label Value值 。                                                                                                     | ``` @Gauge(name = "application_cpu_usage") public double gauge(@Label(name = "name") String userName, @Label(name = "age") String age) { // 执行业务代码 } ``` prometheus效果： ``` application_cpu_usage{age="xxx",application="devspore-application2",name="xxxx",} 0.013787109469182268 application_cpu_usage{age="yyy",application="devspore-application2",name="yyyy",} 0.4807864361390415 ```                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
   | Summary(概要) 提供数值的百分比分布区间，百分比包括0.50，0.75，0.90，0.99。 | 方法注解@Summary。 - name：String类型，自定义指标名称。  - 方法类型：Number。  - 效果：每次执行该方法，对应指标添加该方法的返回值作为统计数据。   参数注解@Label。 - name：String类型，自定义指标Label Key值。  - 被注解的参数：String类型，自定义指标Label Value值。                                                                                                         | ``` @Summary(name = "request_time_summary") public double summary(@Label(name = "name") String userName, @Label(name = "age") String age) { // 执行业务代码 } ``` prometheus效果： ``` request_time_summary{age="xxx",application="devspore-application2",name="xxxx",quantile="0.5",} 0.52734375 request_time_summary{age="xxx",application="devspore-application2",name="xxxx",quantile="0.75",} 0.74609375 request_time_summary{age="xxx",application="devspore-application2",name="xxxx",quantile="0.9",} 0.87109375 request_time_summary{age="xxx",application="devspore-application2",name="xxxx",quantile="0.99",} 0.99609375 request_time_summary_count{age="xxx",application="devspore-application2",name="xxxx",} 100.0 request_time_summary_sum{age="xxx",application="devspore-application2",name="xxxx",} 51.06987916008812 ```                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
   | Histogram（直方图） 不同区间内样本的个数。                        | 方法注解@Histogram。 - name：String类型，自定义指标名称。  - maxValue：double类型，数据采样的最大值。  - 方法类型：Number。  - 效果：每次执行该方法，对应指标添加该方法的返回值作为统计数据。   参数注解@Label。 - name：String类型，自定义指标Label Key值。  - 被注解的参数：String类型，自定义指标Label Value值。   | ``` @Histogram(name = "request_time_histogram", maxValue = 10) public double histogram(@Label(name = "name") String userName, @Label(name = "age") String age) { // 执行业务代码 } ``` prometheus效果： ``` request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="1.0",} 5.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="2.0",} 12.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="3.0",} 17.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="4.0",} 23.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="5.0",} 28.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="6.0",} 34.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="7.0",} 39.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="8.0",} 43.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="9.0",} 47.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="10.0",} 52.0 request_time_histogram_bucket{age="xxx",application="devspore-application2",name="xxxx",le="+Inf",} 52.0 request_time_histogram_count{age="xxx",application="devspore-application2",name="xxxx",} 52.0 request_time_histogram_sum{age="xxx",application="dev ``` |
      
   
   
 
