如何暴露 Vaadin 的会话数、连接数等指标供 Prometheus 抓取?

解读

国内金融、运营商、政务云等项目上线前必须通过 Prometheus+Grafana 做运行时监控,否则无法进入运维“白名单”。
Vaadin 本身只提供 HttpSession 与 Atmosphere 长连接抽象,没有现成指标端点;因此面试官真正想确认的是:

  1. 你是否理解 Vaadin 的运行时模型(UI≈Session、Push≈Atmosphere 长连接)。
  2. 能否用“Spring Boot 生态+Micrometer”这一国内主流方案,零侵入地把指标推到 Prometheus,而不是写一堆 JMX 或 Servlet。
  3. 是否知道在高并发场景下如何防止“指标收集”成为性能瓶颈(如会话遍历、锁竞争)。

知识点

  1. VaadinSession 与 HttpSession 一一映射,可通过 VaadinService.getAllSessions() 统计活跃会话。
  2. 使用 Atmosphere 推送时,框架内部维护 Bayeux 连接,连接数 ≈ AtmosphereResource 数量,可通过 AtmosphereResourceFactory.getAtmosphereResources() 获取。
  3. Spring Boot Actuator 已内置 Micrometer,只要注册自定义 MeterBinder,指标会自动暴露在 /actuator/prometheus。
  4. 指标命名必须符合国内 Prometheus 规范:vaadin_session_total、vaadin_push_connection_total,标签至少带 app、instance。
  5. 遍历会话/连接时必须用 try-finally 减少锁占用,且使用 Iterable.size() 缓存避免重复计算。
  6. 生产环境必须开启 actuator 安全:management.endpoints.web.exposure.include=prometheus,并通过 Spring Security 只允许运维网段访问。

答案

步骤一:引入依赖

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

步骤二:注册 MeterBinder

@Component
public class VaadinMetrics implements MeterBinder {
    @Override
    public void bindTo(MeterRegistry registry) {
        // 1. 会话数
        Gauge.builder("vaadin_session_total", this, VaadinMetrics::sessionCount)
             .description("当前 Vaadin 活跃会话数")
             .tag("app", "order-center")
             .register(registry);

        // 2. 推送连接数
        Gauge.builder("vaadin_push_connection_total", this, VaadinMetrics::pushConnectionCount)
             .description("当前 Atmosphere 推送连接数")
             .tag("app", "order-center")
             .register(registry);
    }

    private double sessionCount() {
        return VaadinService.getCurrent() == null ? 0 :
               VaadinService.getCurrent().getAllSessions().size();
    }

    private double pushConnectionCount() {
        AtmosphereResourceFactory factory = AtmosphereResourceFactory.getDefault();
        return factory == null ? 0 : factory.getAtmosphereResources().size();
    }
}

步骤三:配置暴露端点

management:
  endpoints:
    web:
      exposure:
        include: prometheus,health
  metrics:
    tags:
      instance: ${spring.application.name}_${server.port}

步骤四:验证
启动后访问 http://ip:port/actuator/prometheus,应能看到
vaadin_session_total{app="order-center",instance="order-center_8080"} 123
vaadin_push_connection_total{app="order-center",instance="order-center_8080"} 456

步骤五:Grafana 侧导入官方 JVM 面板,再自建 Vaadin 行:

  • vaadin_session_total 反映在线用户
  • vaadin_push_connection_total 反映长连接压力
    设置告警:session_total > 5000 或 push_connection_total > 3000 即触发扩容。

拓展思考

  1. 集群场景下会话可能存储在 Redis,需替换统计逻辑为“Redis key 前缀计数”,否则每个节点只报告本地值。
  2. 如果开启 Spring Session,建议直接复用 spring_session 表的过期时间字段,用 SQL 统计代替内存遍历,降低 GC 压力。
  3. 对推送连接可进一步细分指标:
    vaadin_push_connection_transport{type="websocket"}
    vaadin_push_connection_transport{type="long-polling"}
    方便排查“部分用户降级为长轮询”导致的性能抖动。
  4. 考虑业务维度,可在用户登录后将“tenantId”写入 VaadinSession 属性,再通过 VaadinService.getAllSessions().stream().filter(s -> s.getAttribute("tenantId").equals("A")) 统计租户级会话,实现多租户隔离告警。