只有干货哦!
目录
支持的api列表
代码调用
库
k8s集群下的prom
非容器部署的prom调用
curl调用
示例1:查询数据
示例2:热加载配置
示例3:主动删除数据
支持的api列表
源码位置:github.com\prometheus\client_golang@v1.11.0\api\prometheus\v1\api.go
代码调用
库
"github.com/prometheus/client_golang/api"
Pro "github.com/prometheus/client_golang/api/prometheus/v1"
go get -t github.com/prometheus/client_golang/prometheus
k8s集群下的prom
获取平台已部署的prometheus的client
var systemPublicEndpoint api.Client
// 连接prometheus,无多集群管理时无需传入集群ID
func ConnectPro(clusterID string) api.Client {
ns := "System" //部署在哪个命名空间
name := "prometheus:prometheus-server" //prometheus-server 组件地址(对应k8s集群中prometheus应用组件的id)
if nil != systemPublicEndpoint {
return systemPublicEndpoint
}
cli, _ := GetPrometheusPublicCli(clusterID, ns, name)
systemPublicEndpoint = cli
return cli
}
func GetPrometheusPublicCli(clusterID, namespace, name string) (api.Client, error) {
//省略获取普罗米修斯server暴露的访问地址逻辑,获取地址addr后组成Config传入
cfg := api.Config{Address: "http://" + addr} //地址示例:http://192.168.104.65:30870
c, err := api.NewClient(cfg)
return c, err
}
以节点cpu的平均使用率为例调api查询:
c1 := ConnectPro(s.Get(consts.KeyCluster).(string))
//mon.Start与mon.End为起始、终止时间,mon为前端传入的对象
sta, _ := strconv.Atoi(mon.Start)
en, _ := strconv.Atoi(mon.End)
rat := en - sta
rat = rat + 60
if rat < 120 {
rat = 120
}
//分子/分母=比率,mon.Node为节点名称
q1 := "sum(rate (container_cpu_usage_seconds_total{id='/',kubernetes_io_hostname=~'" + mon.Node + "$'" + "}[" + strconv.Itoa(rat) + "s]))"
q2 := "/sum (machine_cpu_cores{kubernetes_io_hostname=~'" + mon.Node + "$'})*100"
q := q1 + q2
c, err := pro.NewAPI(c1).Query(context.Background(), q, mon.End)
if err != nil {
errMsg := fmt.Sprint("failed to Query NodeCpuRate:", err.Error())
logError(errMsg)
return c,err
}
return c,nil
返回的数据:
{"message":"","code":200,"data":[{"metric":{},"value":[1572431847,"15.910989253603638"]}]}
[{"metric":{},"value":[1572431847,"15.910989253603638"]}]
即15.9%
非容器部署的prom调用
获取client:
func GetPromCli() (api.Client, error) {
addr := "172.18.1.118:9090"
cfg := api.Config{Address: "http://" + addr}
c, err := api.NewClient(cfg)
return c, err
}
筛选全部实例,以节点cpu使用率查询:
func NodeCpuUseAllNode() (model.Value, error) { //
client, err := GetPromCli()
if err != nil {
fmt.Println("get prom cli err:", err.Error())
return nil, err
}
q := "(1 - avg(rate(node_cpu_seconds_total{instance=~\".*\",mode=\"idle\"}[2m])) by (instance)) * 100"
v, warnings, err := pro.NewAPI(client).Query(context.Background(), q, time.Unix(time.Now().Unix(), 0)) // time.Unix(time.Now().Unix(), 0)
if err != nil {
fmt.Println(fmt.Sprintf("exec NodeCpuUse err:%v, warnings:%v", err.Error(), warnings))
return nil, err
}
fmt.Println(fmt.Sprintf("NodeCpuUseAllNode result :%+v", v.String())) //
fmt.Println("NodeCpuUseAllNode type :", v.Type())
fmt.Println("NodeCpuUseAllNode warnings :", warnings)
return v, nil
}
返回数据:
[
{
"metric": {
"instance": "172.18.1.110:9100"
},
"value": [
1572449707,
"8.597826086916028"
]
},
{
"metric": {
"instance": "172.18.1.118:9100"
},
"value": [
1572449707,
"1.801086956452902"
]
}
]
更多api都有方法支持:
curl调用
示例1:查询数据
curl 'http://localhost:9090/api/v1/query_range?query=up&start=2020-03-21T09:37:30.781Z&end=2020-03-21T09:38:00.781Z&step=15s'
返回:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"__name__": "up",
"instance": "172.18.1.110:9100",
"job": "nodes_list",
"node_group": "nodes_exporter_02"
},
"values": [
[
1626860250.781,
"0"
],
[
1626860265.781,
"0"
]
]
},
{
"metric": {
"__name__": "up",
"instance": "172.18.1.118:9100",
"job": "nodes_list",
"node_group": "nodes_exporter_02"
},
"values": [
[
1626860250.781,
"1"
],
[
1626860265.781,
"1"
]
]
}
]
}
}
1就是active,0就是down掉了
示例2:热加载配置
该方式需要启动server时增加--web.enable-lifecycle参数:
curl -X POST http://localhost:9090/-/reload
执行后在不重启server的情况下自动使用最新配置:
示例3:主动删除数据
curl -X POST -g 'http://127.0.0.1:9090/api/v1/admin/tsdb/delete_series?match[]=node_cpu_seconds_total{mode="idle"}&start=2020-03-10T16:10:00Z&end=2020-03-12T16:20:00Z'
delete_series删除并不会立即释放空间,如果要立即释放空间则需要使用clean_tombstones:
curl -XPOST http://localhost:9090/api/v1/admin/tsdb/clean_tombstones
更多api可见官网:HTTP API | Prometheus
注意:大多数api的调用都需要开启--web.enable-admin-api参数(启动时加上)