【问题标题】:Access Prometheus default Go metrics from within the code从代码中访问 Prometheus 默认 Go 指标
【发布时间】:2021-05-18 01:23:44
【问题描述】:

我已经从我的 Go 应用程序中公开了默认指标和一些自定义指标。 我可以在浏览器中查看指标:

# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes ***********

我希望能够从我的应用程序代码中访问这些指标,也许可以查询它们。 我一直在研究 prometheus 和 prometheus HTTP API 的 client_golang 包,但找不到我的方法。

如何做到这一点?

【问题讨论】:

  • 当你说你使用HTTP API时,你是查询客户端(即localhost,还是收集指标的Prometheus服务器?你需要调用服务器上的API。如果你不想进行外部调用,您可以在本地安装服务器,从 localhost 抓取并调用本地托管服务器上的 API。或者只需在代码中调用端点并解析 go_memstats_alloc_bytes

标签: go prometheus metrics


【解决方案1】:

如果您想从该应用程序中访问有关正在运行的应用程序的内存信息,请考虑引用runtime.ReadMemStats

var m runtime.MemStats
runtime.ReadMemStats(&m)

// reference m.HeapAlloc or m.TotalAlloc, etc.

请注意,当前的实现“停止了世界”,因此对于内部循环来说可能过于昂贵。

【讨论】:

    猜你喜欢
    • 2022-10-19
    • 2020-06-25
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多