如何在单个PromQL查询中同时获取两个Prometheus指标值?
Got it, let's work through this problem to get your desired table-style result.
The core trick here is using PromQL's join operator, which lets you combine two metric series based on the shared labels you care about. Here's how to do it step by step:
Basic Query to Combine Metrics
join on (Label1, Label2) ( http_request_duration_seconds_bucket, http_request_duration_seconds_count )
This query matches up series from both metrics where the values of Label1 and Label2 are identical. The result will include:
- Your shared labels
Label1andLabel2 - The value from
http_request_duration_seconds_bucket - The value from
http_request_duration_seconds_count
Rename Metrics for Clearer Output
If you want the result columns to explicitly show as metric1 and metric2 instead of the original metric names, use the alias() function to rename them:
join on (Label1, Label2) ( alias(http_request_duration_seconds_bucket, "metric1"), alias(http_request_duration_seconds_count, "metric2") )
Aggregate First (For Rolled-Up Results)
If your metrics have extra labels you don't need, or if you want to roll up values per Label1/Label2 pair, aggregate each metric before joining:
join on (Label1, Label2) ( sum(http_request_duration_seconds_bucket) by (Label1, Label2), sum(http_request_duration_seconds_count) by (Label1, Label2) )
This ensures each unique Label1 + Label2 combination has a single row in the result, which fits perfectly with your desired table format.
内容的提问来源于stack exchange,提问作者Bharath Kumar




