Spring Actuator端点对应Java类在哪?能否通过JVM直接调用获取指标?
Spring Actuator端点实现类查找与内部调用方案
1. Actuator核心端点实现类定位
Spring Actuator的端点实现分为两层:
- 核心功能层:不依赖web协议,直接处理业务逻辑,所有端点都实现了
Endpoint接口,你要找的Prometheus指标端点核心实现类是PrometheusScrapeEndpoint,位于org.springframework.boot.actuate.metrics.export.prometheus包下,核心方法是scrape(),直接调用即可获取Prometheus格式的指标文本,不需要走HTTP请求。 - Web适配层:负责将核心端点暴露为HTTP接口,对应类是
PrometheusScrapeEndpointWebExtension,这层只做协议适配,不需要关注,直接调用核心层即可。
你提到的直接使用MeterRegistry.forEachMeter()是更优的方案,Actuator的PrometheusScrapeEndpoint底层也是基于MeterRegistry的实例遍历所有指标完成的格式化,你直接调用的话还能省去格式转换的开销,更适合自定义合并的场景。
2. JVM内部调用Actuator端点的实现方式
直接将对应端点类注入到你的自定义组件即可,示例代码:
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MetricMergeService { // 直接注入Prometheus端点核心类 @Autowired private PrometheusScrapeEndpoint prometheusEndpoint; public String getActuatorMetrics() { // 内部直接调用,无HTTP开销 return prometheusEndpoint.scrape(); } }
3. OpenCensus端点处理器查找
如果你的OpenCensus端点是Spring Boot自动配置暴露的,核心实现类是OpenCensusMetricsEndpoint,位于org.springframework.boot.actuate.metrics.opencensus包下,核心方法是metrics(),同样可以直接注入调用获取原始指标数据。如果是你们内部自定义的OpenCensus端点,直接找实现了Endpoint接口的对应类即可,规则和Actuator官方端点一致。
4. 指标合并的最优方案推荐
你不需要自建端点调用两个现有端点,更推荐的实现逻辑是:
- 分别从
MeterRegistry获取Actuator侧的所有指标 - 从OpenCensus的
MetricReader实例中获取遗留指标 - 统一做格式转换后上报,整个流程都在内存完成,性能损耗最低
内容的提问来源于stack exchange,提问作者kane
相关产品推荐
相关产品推荐

