启用 GlobalMethodSecurity 时未显示 Spring Actuator JVM 指标
Posted
技术标签:
【中文标题】启用 GlobalMethodSecurity 时未显示 Spring Actuator JVM 指标【英文标题】:Spring Actuator JVM metrics not showing when GlobalMethodSecurity is enabled 【发布时间】:2019-12-27 15:59:37 【问题描述】:我正在现有的 Spring Boot 应用程序中设置 Spring Actuator。一切正常,除了/metrics
端点返回的指标中缺少 JVM 指标(CPU、内存、GC、..)。
我正在使用 Spring Boot 2.1.4。我明确配置了GlobalMethodSecurity
并排除了SecurityAutoConfiguration
。
这是我的安全配置:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration
private final IAuthService authService;
@Autowired
public MethodSecurityConfig(IAuthService authService)
this.authService = authService;
@Override
protected MethodSecurityExpressionHandler createExpressionHandler()
return new CustomMethodSecurityExpressionHandler(authService);
虽然这些是我正在使用的指标属性:
management:
security:
enabled: false
server:
address: 0.0.0.0
port: 8080
endpoints:
web:
exposure.include: metrics,prometheus
exposure.exclude:
metrics:
enable.jvm: true
使用此配置,可用的指标是:
"names": [
"jdbc.connections.active",
"jdbc.connections.max",
"jdbc.connections.min",
"tomcat.global.sent",
"tomcat.sessions.created",
"tomcat.global.request.max",
"hikaricp.connections.idle",
"hikaricp.connections.pending",
"tomcat.global.request",
"tomcat.sessions.expired",
"hikaricp.connections",
"tomcat.global.received",
"hikaricp.connections.active",
"hikaricp.connections.creation",
"tomcat.sessions.rejected",
"tomcat.threads.config.max",
"hikaricp.connections.max",
"hikaricp.connections.min",
"tomcat.global.error",
"tomcat.sessions.active.current",
"tomcat.sessions.alive.max",
"hikaricp.connections.usage",
"tomcat.threads.current",
"hikaricp.connections.timeout",
"tomcat.sessions.active.max",
"hikaricp.connections.acquire",
"tomcat.threads.busy"
]
如果我暂时删除 @EnableGlobalMethodSecurity
,可用指标变为:
"names": [
"jvm.threads.states",
"jdbc.connections.active",
"process.files.max",
"jvm.memory.used",
"jvm.gc.memory.promoted",
"jvm.memory.max",
"system.load.average.1m",
"jvm.gc.max.data.size",
"jdbc.connections.max",
"jdbc.connections.min",
"jvm.memory.committed",
"system.cpu.count",
"tomcat.global.sent",
"jvm.buffer.memory.used",
"tomcat.sessions.created",
"jvm.threads.daemon",
"system.cpu.usage",
"jvm.gc.memory.allocated",
"tomcat.global.request.max",
"hikaricp.connections.idle",
"hikaricp.connections.pending",
"tomcat.global.request",
"tomcat.sessions.expired",
"hikaricp.connections",
"jvm.threads.live",
"jvm.threads.peak",
"tomcat.global.received",
"hikaricp.connections.active",
"hikaricp.connections.creation",
"process.uptime",
"tomcat.sessions.rejected",
"process.cpu.usage",
"tomcat.threads.config.max",
"jvm.classes.loaded",
"hikaricp.connections.max",
"hikaricp.connections.min",
"jvm.classes.unloaded",
"tomcat.global.error",
"tomcat.sessions.active.current",
"tomcat.sessions.alive.max",
"jvm.gc.live.data.size",
"log4j2.events",
"hikaricp.connections.usage",
"tomcat.threads.current",
"jvm.gc.pause",
"hikaricp.connections.timeout",
"process.files.open",
"jvm.buffer.count",
"jvm.buffer.total.capacity",
"tomcat.sessions.active.max",
"hikaricp.connections.acquire",
"tomcat.threads.busy",
"process.start.time"
]
我希望方法的安全性不会影响 JVM 指标的发布。 我应该如何配置 Actuator/Security 以获得所有指标?
提前感谢您的帮助!
【问题讨论】:
【参考方案1】:我设法通过添加此配置使其工作:
@Configuration
public class ActuatorMetricsConfig
@Bean
InitializingBean forcePrometheusPostProcessor(BeanPostProcessor meterRegistryPostProcessor, PrometheusMeterRegistry registry)
return () -> meterRegistryPostProcessor.postProcessAfterInitialization(registry, "");
【讨论】:
谢谢!您节省了我的一天,我使用 MeterRegistry 包含了不同库的复杂设置,并且在我认为是一个小变化之后 jvm 指标停止暴露后,我花了一个多小时尝试了许多不同的事情。 也为我工作,所以我提交了一份报告,希望能解决这个问题github.com/spring-projects/spring-boot/issues/26684 在github上的issue中,它说从2.5.1开始修复,但是如果我升级到2.5.1,我有错误Caused by: java.lang.IllegalStateException: No ServletContext set
,请问你有什么想法吗?
也为我工作,但是当我运行自动化测试时,我得到了 prometheusRegistry 的 beanNotFoundException以上是关于启用 GlobalMethodSecurity 时未显示 Spring Actuator JVM 指标的主要内容,如果未能解决你的问题,请参考以下文章