You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Micrometer:如何获取标签相关统计信息

问题解决方案

1. 按租户标签查询指标的方法

默认访问/actuator/metrics/company.publishing_errors返回的是所有租户的聚合值,要查看特定租户的统计数据,只需在请求中添加tag参数过滤:

http://127.0.0.1:8080/actuator/metrics/company.publishing_errors?tag=tenant:你的租户ID

返回的JSON中,measurements字段就是该租户的独立计数,同时availableTags会列出所有已存在的租户标签值,方便确认可用的维度。

2. 是否需要设置commonTags?

不需要。commonTags的作用是给所有指标统一添加全局标签(比如应用环境、服务名称),而你已经通过代码为每个Counter单独绑定了tenant标签,完全满足按租户区分的需求,无需额外配置commonTags。

3. 是否需要引入Micrometer-Prometheus?

如果只是在Actuator端点中按标签查询指标,不需要额外引入。Spring Boot默认的micrometer-core配合Actuator就能实现这个功能。

但如果后续需要对接Prometheus做监控数据持久化、或用Grafana做可视化分析,就需要引入micrometer-registry-prometheus依赖,此时访问/actuator/prometheus端点会输出带标签维度的原生Prometheus格式指标,方便Prometheus抓取。

内容的提问来源于stack exchange,提问作者Mathias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 00:05:18