solon-cloud-metrics
此插件,由社区成员(Bai)贡献
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-cloud-metrics</artifactId>
</dependency>
1、描述
分布式扩展插件。在 solon-cloud 插件的基础上,添加基于 micrometer 度量的支持。此插件类似 slf4j,使用时需要添加具体的记录方案。v2.4.2 后支持
2、观测地址
- "/metrics/registrys" 查看所有注册器
{
"_registrys": ["xxx.xxx.Name1", "xxx.xxx.Name2"]
}
- "/metrics/meters" 查看所有度量
{
"_meters": ["name1", "name2"]
}
- "/metrics/meter/{meterName}" 查看某个度量详情
{
"name": "name1",
"description": "",
"baseUnit": "",
"measurements": {},
"tags": {}
}
- "/metrics/prometheus" 输出普罗米修斯的数据
3、代码使用示例(以 prometheus 为例)
- 添加记录方案包
pom.xml
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>${micrometer.version}</version>
</dependency>
- 添加代码示例
注解模式
@Controller
public class DemoController {
@Mapping("/counter")
@MeterCounter("demo.counter")
public String counter() {
return "counter";
}
@Mapping("/gauge")
@MeterGauge("demo.gauge")
public Long gauge() {
return System.currentTimeMillis() % 100;
}
@Mapping("/summary")
@MeterSummary(value = "demo.summary", maxValue = 88, minValue = 1, percentiles = {10, 20, 50})
public Long summary() {
return System.currentTimeMillis() % 100;
}
@Mapping("/timer")
@MeterTimer("demo.timer")
public String timer() {
return "timer";
}
}
手动模式
@Component
public class DemoRouterInterceptor implements RouterInterceptor {
@Override
public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
long start = System.currentTimeMillis();
try {
chain.doIntercept(ctx, mainHandler);
} finally {
long span = System.currentTimeMillis() - start;
//手动记录时间(使用更自由)
Metrics.timer(ctx.path()).record(span, TimeUnit.MICROSECONDS);
}
}
}
4、定制
全局自动添加的 commandTags
solon.app.name //应用名
solon.app.group //应用组
solon.app.nameSpace //应用命名空间
使用注解时自动添加的 tags
uri //请求 uri
method //请求 method
class //执行类
executable //执行函数
代码定制(如果有需要)
@Configuration
public class DemoConfig {
@Bean
public void custom(MeterRegistry registry){
//添加公共 tag
registry.config().commonTags().commonTags("author","noear")
//添加几个 meter
new JvmMemoryMetrics().bindTo(registry);
new JvmThreadMetrics().bindTo(registry);
new ProcessorMetrics().bindTo(registry);
}
}
5、配置使用示例(以 prometheus 为例)
prometheus.yml
scrape_configs:
- job_name: 'micrometer-example'
scrape_interval: 5s
metrics_path: '/metrics/prometheus'
static_configs:
- targets: ['127.0.0.1:8080']
labels:
instance: 'example1'