

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 查詢 Prometheus 指標
<a name="AMP-query"></a>

既然已將指標擷取至工作區，便可對其進行查詢。

若要建立具有指標視覺化呈現的儀表板，您可以使用 Amazon Managed Grafana 等服務。Amazon Managed Grafana （或 Grafana 的獨立執行個體） 可以建置圖形界面，以各種顯示呈現樣式顯示您的指標。如需 Amazon Managed Grafana 的詳細資訊，請參閱《[Amazon Managed Grafana 使用者指南》](https://docs.aws.amazon.com/grafana/latest/userguide/)。

您也可以建立一次性查詢、探索資料，或使用直接查詢撰寫使用指標的自有應用程式。直接查詢使用 Amazon Managed Service for Prometheus API 和標準 Prometheus 查詢語言 PromQL，從您的 Prometheus 工作區取得資料。如需有關 PromQL 和其語法的詳細資訊，請參閱 Prometheus 說明文件中的[查詢 Prometheus](https://prometheus.io/docs/prometheus/latest/querying/basics/)。

**Topics**
+ [PromQL 欺騙工作表](#promql-cheat-sheet)
+ [基本選擇器](#promql-basic-selectors)
+ [範圍向量選擇器](#promql-range-selectors)
+ [彙總運算子](#promql-aggregation-operators)
+ [常見的函數](#promql-functions)
+ [二元運算子](#promql-operators)
+ [實際查詢範例](#promql-practical-examples)
+ [保護您的指標查詢](AMP-secure-querying.md)
+ [設定 Amazon Managed Grafana，以搭配 Amazon Managed Service for Prometheus 使用](AMP-amg.md)
+ [設定 Grafana 開放原始碼或 Grafana 企業版，以搭配 Amazon Managed Service for Prometheus 使用](AMP-onboard-query-standalone-grafana.md)
+ [使用 Amazon EKS 叢集中執行的 Grafana 查詢](AMP-onboard-query-grafana-7.3.md)
+ [使用與 Prometheus 相容的 API 查詢](AMP-onboard-query-APIs.md)
+ [取得每個查詢的查詢用量統計資料](AMP-stats.md)

## PromQL 欺騙工作表
<a name="promql-cheat-sheet"></a>

在 Amazon Managed Service for Prometheus 工作區中查詢指標時，請使用此 PromQL (Prometheus 查詢語言） 備忘單作為快速參考。使用 PromQL，您可以透過其功能查詢語言即時選取和彙總時間序列資料。

如需 PromQL 的詳細資訊，請參閱 [PromLabs 網站上的 PromQL Cheat Sheet](https://promlabs.com/promql-cheat-sheet/)。 *PromLabs* 

## 基本選擇器
<a name="promql-basic-selectors"></a>

依指標名稱和標籤比對器選取時間序列：

```
# Select all time series with the metric name http_requests_total
http_requests_total

# Select time series with specific label values
http_requests_total{job="prometheus", method="GET"}

# Use label matchers
http_requests_total{status_code!="200"}          # Not equal
http_requests_total{status_code=~"2.."}          # Regex match
http_requests_total{status_code!~"4.."}          # Negative regex match
```

## 範圍向量選擇器
<a name="promql-range-selectors"></a>

選取一段時間內的範例範圍：

```
# Select 5 minutes of data
http_requests_total[5m]

# Time units: s (seconds), m (minutes), h (hours), d (days), w (weeks), y (years)
cpu_usage[1h]
memory_usage[30s]
```

## 彙總運算子
<a name="promql-aggregation-operators"></a>

跨多個時間序列彙總資料：

```
# Sum all values
sum(http_requests_total)

# Sum by specific labels
sum by (job) (http_requests_total)
sum without (instance) (http_requests_total)

# Other aggregation operators
avg(cpu_usage)                    # Average
min(response_time)               # Minimum
max(response_time)               # Maximum
count(up)                        # Count of series
stddev(cpu_usage)               # Standard deviation
```

## 常見的函數
<a name="promql-functions"></a>

套用函數來轉換您的資料：

```
# Rate of increase per second (for counters)
rate(http_requests_total[5m])

# Increase over time range
increase(http_requests_total[1h])

# Derivative (for gauges)
deriv(cpu_temperature[5m])

# Mathematical functions
abs(cpu_usage - 50)              # Absolute value
round(cpu_usage, 0.1)           # Round to nearest 0.1
sqrt(memory_usage)              # Square root

# Time functions
time()                          # Current Unix timestamp
hour()                          # Hour of day (0-23)
day_of_week()                   # Day of week (0-6, Sunday=0)
```

## 二元運算子
<a name="promql-operators"></a>

執行算術和邏輯操作：

```
# Arithmetic operators
cpu_usage + 10
memory_total - memory_available
disk_usage / disk_total * 100

# Comparison operators (return 0 or 1)
cpu_usage > 80
memory_usage < 1000
response_time >= 0.5

# Logical operators
(cpu_usage > 80) and (memory_usage > 1000)
(status_code == 200) or (status_code == 201)
```

## 實際查詢範例
<a name="promql-practical-examples"></a>

您可以在 Amazon Managed Service for Prometheus 工作區中使用的常見監控查詢：

```
# CPU usage percentage
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

# Memory usage percentage
(1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100

# Request rate per second
sum(rate(http_requests_total[5m])) by (job)

# Error rate percentage
sum(rate(http_requests_total{status_code=~"5.."}[5m])) / 
sum(rate(http_requests_total[5m])) * 100

# 95th percentile response time
histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))

# Top 5 instances by CPU usage
topk(5, avg by (instance) (cpu_usage))
```