ZBLOG

使用 golang 如何调用 prometheus api 查询指标?

可以使用 golang 的 prometheus 客户端库来调用 prometheus api 查询指标。

  1. 首先,需要引入以下两个库:
import (
    "github.com/prometheus/client_golang/api"
    "github.com/prometheus/client_golang/api/prometheus/v1"
)
  1. 创建 prometheus api 客户端:
client, err := api.NewClient(api.Config{
        Address: "http://localhost:9090",
})
if err != nil {
        panic(err)
}
  1. 创建 prometheus api 查询器:
queryAPI := v1.NewAPI(client)
  1. 使用查询器来查询指标:
result, err := queryAPI.Query(context.Background(), "up")
if err != nil {
        panic(err)
}
fmt.Println(result)

上面的代码查询了 up 指标(表示所有已注册的 Exporter 是否处于活动状态)的值,并打印出结果。

完整代码示例:

package main

import (
    "context"
    "fmt"

    "github.com/prometheus/client_golang/api"
    "github.com/prometheus/client_golang/api/prometheus/v1"
)

func main() {
    // 创建 prometheus api 客户端
    client, err := api.NewClient(api.Config{
        Address: "http://localhost:9090",
    })
    if err != nil {
        panic(err)
    }

    // 创建 prometheus api 查询器
    queryAPI := v1.NewAPI(client)

    // 查询指标
    result, err := queryAPI.Query(context.Background(), "up")
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=1628

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?