SpringBoot结合prometheus自定义埋点实践
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MetricsConfig {
@Bean
public MeterRegistry meterRegistry() {
return new PrometheusMeterRegistry(PrometheusMeterRegistry.config()
.commonTags("application", "my-spring-boot-application"));
}
@Bean
public JvmThreadMetrics jvmThreadMetrics() {
return new JvmThreadMetrics();
}
@Bean
public ProcessorMetrics processorMetrics() {
return new ProcessorMetrics();
}
// 以下是自定义的埋点示例
@Bean
public MyCustomMetrics myCustomMetrics(MeterRegistry registry) {
return new MyCustomMetrics(registry);
}
}
class MyCustomMetrics {
private final MeterRegistry registry;
public MyCustomMetrics(MeterRegistry registry) {
this.registry = registry;
// 在这里添加自定义的埋点逻辑
}
// 自定义的埋点方法
public void recordMyCustomMetric(String key, double value) {
registry.counter(key, "type", "myCustomMetric").increment(value);
}
}
这个代码示例展示了如何在Spring Boot应用程序中配置Prometheus和Micrometer,并添加了JVM线程和处理器指标的监控以及自定义的埋点逻辑。这是一个很好的实践,可以作为开发者在集成Prometheus和Spring Boot时的参考。
评论已关闭