英文:
How to query all time series of a given metrics in prometheus?
问题
如何在Prometheus中查询指定指标的原始时间序列值而不使用步长?例如,我想查询在1分钟内实际可用的数据点数量,但是当我指定步长为15秒时,无论数据点的数量如何,都会生成4个结果。
package main
import (
"context"
"fmt"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"log"
"time"
)
var (
prometheus string = "http://10.22.5.54:8481/select/0/prometheus"
timeout time.Duration = time.Second * 30
)
func main() {
client, err := api.NewClient(api.Config{Address: prometheus})
if err != nil {
log.Fatalln("连接到Prometheus时出错:", err)
}
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
result, warnings, err := v1api.QueryRange(ctx, "up{app=\"activity-api\"}", v1.Range{Start: time.Now().Add(-time.Minute * 1), End: time.Now(), Step: time.Second * 15})
if err != nil {
log.Printf("查询Prometheus时出错:%s\n", err)
}
if len(warnings) > 0 {
log.Printf("警告:%s", warnings)
}
fmt.Println(result)
}
英文:
How to query the raw time series values of a specified metrics in prometheus without step? For example, I want to query how many datapoints are actually available in 1 minute, but when I specify step as 15s, 4 results will be generated regardless of the number of datapoints
package main
import (
"context"
"fmt"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"log"
"time"
)
var (
prometheus string = "http://10.22.5.54:8481/select/0/prometheus"
timeout time.Duration = time.Second * 30
)
func main() {
client, err := api.NewClient(api.Config{Address: prometheus})
if err != nil {
log.Fatalln("Error connect to the prometheus: ", err)
}
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
result, warnings, err := v1api.QueryRange(ctx, "up{app=\"activity-api\"}", v1.Range{Start: time.Now().Add(-time.Minute * 1), End: time.Now(), Step: time.Second * 15})
if err != nil {
log.Printf("Error querying Prometheus: %s\n", err)
}
if len(warnings) > 0 {
log.Printf("Warnings: %s", warnings)
}
fmt.Println(result)
}
答案1
得分: 2
你需要使用v1api.Query而不是v1api.QueryRange,并在系列选择器的末尾添加所需的时间间隔,放在方括号中。例如,以下查询应该返回与up{app="activity-api"}匹配的时间序列的原始样本,时间范围为过去15秒,以给定的time结束。
t := time.Now()
result, warnings, err := v1api.Query(ctx, `up{app="activity-api"}[15s]`, t)
更多详细信息请参见这里。
在内部,github.com/prometheus/client_golang包只是查询/api/v1/query HTTP端点,因此可能更容易直接向该端点发出HTTP请求,而无需弄清楚如何使用github.com/prometheus/client_golang。例如,以下命令将从VictoriaMetrics集群返回原始样本:
curl http://10.22.5.54:8481/select/0/prometheus/api/v1/query -d 'up{app="activity-api"}[15s]'
VictoriaMetrics还提供了导出原始样本的API。
另请参阅https://valyala.medium.com/analyzing-prometheus-data-with-external-tools-5f3e5e147639。
英文:
You need to use v1api.Query instead of v1api.QueryRange, and add the needed interval in square brackets at the end of series selector. For example, the following query should return raw samples for time series matching up{app="activity-api"} for the last 15 seconds ending at the given time.
t := time.Now()
result, warnings, err := v1api.Query(ctx, `up{app="activity-api"}[15s]`, t)
See more details here.
Internally the github.com/prometheus/client_golang package just queries /api/v1/query HTTP endpoint, so it may be easier to just issue HTTP request to this endpoint without the need to figure out how to use github.com/prometheus/client_golang. For example, the following command will return raw samples from VictoriaMetrics cluster:
curl http://10.22.5.54:8481/select/0/prometheus/api/v1/query -d 'up{app="activity-api"}[15s]'
VictoriaMetrics also provides APIs for exporting raw samples.
See also https://valyala.medium.com/analyzing-prometheus-data-with-external-tools-5f3e5e147639 .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论