启用 GlobalMethodSecurity 时,Spring Actuator JVM 指标不显示

And*_*dre 2 java spring spring-security spring-boot spring-boot-actuator

我正在现有的 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然这些是我正在使用的指标属性:

management:
  security:
    enabled: false
  server:
    address: 0.0.0.0
    port: 8080
  endpoints:
    web:
      exposure.include: metrics,prometheus
      exposure.exclude:
  metrics:
    enable.jvm: true
Run Code Online (Sandbox Code Playgroud)

使用此配置,可用指标为:

{
  "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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

如果我暂时删除@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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我希望该方法的安全性不应该影响 JVM 指标的发布。我应该如何配置 Actuator/Security 以获得所有指标?

在此先感谢您的帮助!

And*_*dre 11

我设法通过添加此配置使其工作:

@Configuration
public class ActuatorMetricsConfig {

    @Bean
    InitializingBean forcePrometheusPostProcessor(BeanPostProcessor meterRegistryPostProcessor, PrometheusMeterRegistry registry) {
        return () -> meterRegistryPostProcessor.postProcessAfterInitialization(registry, "");
    }

}
Run Code Online (Sandbox Code Playgroud)