【发布时间】:2020-08-18 15:23:37
【问题描述】:
我是 Golang 的新手,我想做的是查询 Prometheus 并将查询结果保存在具有所有时间戳及其度量值的对象(例如地图)中。 我从这个示例代码开始,只做了一些更改 (https://github.com/prometheus/client_golang/blob/master/api/prometheus/v1/example_test.go)
func getFromPromRange(start time.Time, end time.Time, metric string) model.Value {
client, err := api.NewClient(api.Config{
Address: "http://localhost:9090",
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
os.Exit(1)
}
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
r := v1.Range{
Start: start,
End: end,
Step: time.Second,
}
result, warnings, err := v1api.QueryRange(ctx, metric, r)
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
os.Exit(1)
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
fmt.Printf("Result:\n%v\n", result)
return result
}
打印的结果例如: "TEST{instance="localhost:4321", job="realtime"} =>\n21 @[1597758502.337]\n22 @[1597758503.337]...
这些实际上是 Prometheus 上的正确值和时间戳。如何将这些时间戳和值插入到地图对象(或我可以在代码中使用的其他类型的对象)中?
【问题讨论】:
-
没有“地图对象”之类的东西,它只是一张地图,Tour of Go 中介绍了使用地图,我建议您完整阅读以熟悉基础知识语言。
标签: go prometheus