如何使用 Spring Boot 和 Micrometer 测量 MongoDB 的数据库延迟?

问题描述 投票:0回答:1

我想测量我的 Spring Boot 微服务的数据库延迟,该服务使用 MongoDB 作为数据库。我使用的是 Micrometer,指标由 Prometheus 抓取并由 Grafana 可视化。我是否需要为我的存储库方法创建一个方面,或者 Micrometer 提供开箱即用的这些指标?

mongodb spring-boot prometheus grafana micrometer
1个回答
0
投票

Micrometer 提供以下开箱即用的指标:

# HELP mongodb_driver_commands_seconds_max Timer of mongodb commands
# TYPE mongodb_driver_commands_seconds_max gauge
--
# HELP mongodb_driver_commands_seconds Timer of mongodb commands
# TYPE mongodb_driver_commands_seconds summary
--
# HELP mongodb_driver_pool_waitqueuesize the current size of the wait queue for a connection from the pool
# TYPE mongodb_driver_pool_waitqueuesize gauge
--
# HELP mongodb_driver_pool_checkedout the count of connections that are currently in use
# TYPE mongodb_driver_pool_checkedout gauge
--
# HELP mongodb_driver_pool_size the current size of the connection pool, including idle and and in-use members
# TYPE mongodb_driver_pool_size gauge

重要:当您在本地启动服务并调用指标端点时,这些指标将不可见,除非您的应用程序向 MongoDB 发出请求。要查看它们,您需要使用业务逻辑调用某个端点,该端点使用数据库。之后,您可以调用指标端点并查看 MongoDB 驱动程序指标。

例如,您可以使用以下 PromQL 计算每个分片的平均数据库延迟:

sum by (server_address) ((rate(mongodb_driver_commands_seconds_sum{app="${Application}",pod=~"${PodName}"}[$__rate_interval])))
/
sum by (server_address) ((rate(mongodb_driver_commands_seconds_count{app="${Application}",pod=~"${PodName}"}[$__rate_interval])))

或者通过集合获取平均延迟: 总和(集合)

(rate(mongodb_driver_commands_seconds_sum{app="${Application}",pod=~"${PodName}"}[$__rate_interval]))
/
sum by (collection) (rate(mongodb_driver_commands_seconds_count{app="${Application}",pod=~"${PodName}"}[$__rate_interval]))

© www.soinside.com 2019 - 2024. All rights reserved.