將 Cloud Monitoring 指標名稱對應至 PromQL
Cloud Monitoring 指標名稱包含兩個元件:網域 (例如 compute.googleapis.com/
) 和路徑 (例如 instance/disk/max_read_ops_count
)。由於 PromQL 只支援特殊字元 :
和 _
,因此您必須先讓 Monitoring 指標名稱與 PromQL 相容。如要將 Monitoring 指標名稱對應至 PromQL,請套用下列規則:
- 將第一個
/
替換為:
。 - 將所有其他特殊字元 (包括
.
和其他/
字元) 替換為_
。
下表列出部分指標名稱及其 PromQL 等價:
Cloud Monitoring 指標名稱 | PromQL 指標名稱 |
---|---|
compute.googleapis.com/instance/cpu/utilization |
compute_googleapis_com:instance_cpu_utilization |
logging.googleapis.com/log_entry_count |
logging_googleapis_com:log_entry_count |
您可以像查詢 Prometheus 直方圖一樣查詢 Cloud Monitoring 分布值指標,只要在指標名稱後面加上 _count
、_sum
或 _bucket
後綴即可:
Cloud Monitoring 指標名稱 | PromQL 指標名稱 |
---|---|
networking.googleapis.com/vm_flow/rtt |
networking_googleapis_com:vm_flow_rtt_sum networking_googleapis_com:vm_flow_rtt_count networking_googleapis_com:vm_flow_rtt_bucket
|
指定受控資源類型
如果指標只與單一 Cloud Monitoring 監控的資源類型相關聯,PromQL 查詢就能運作,而不需要手動指定資源類型。不過,Cloud Monitoring 中的部分指標 (包括某些系統指標) 會對應至多個資源類型。您可以參閱 Trusted Cloud by S3NS 指標清單,瞭解哪些受控資源類型會對應至指標。說明文件中的每個項目都會在類型下方的第一欄列出相關的受控資源類型。如果沒有列出任何受控資源類型,則指標可以與任何類型相關聯。
如果指標與多種資源類型相關聯,您必須在 PromQL 查詢中指定資源類型。您可以使用特殊標籤 monitored_resource
選取資源類型。
受控資源類型在多數情況下是短字串,例如 gce_instance
,但有時也會顯示為完整的 URI,例如 monitoring.googleapis.com/MetricIngestionAttribution
。格式正確的 PromQL 查詢可能如下所示:
logging_googleapis_com:byte_count{monitored_resource="k8s_container"}
loadbalancing_googleapis_com:l3_external_egress_bytes_count{monitored_resource="loadbalancing.googleapis.com/ExternalNetworkLoadBalancerRule"}
如果您在需要時未使用 monitored_resource
標籤,就會收到以下錯誤:
metric is configured to be used with more than one monitored resource type;
series selector must specify a label matcher on monitored resource name
解決標籤衝突
在 Cloud Monitoring 中,標籤可以屬於指標或資源。如果指標標籤的鍵名稱與資源標籤相同,您可以將前置字串 metric_
新增至查詢中的標籤鍵名稱,即可特別參照指標標籤。
舉例來說,假設您在指標 example.googleapis.com/user/widget_count
中,有一個資源標籤和一個指標標籤,兩者都命名為 pod_name
。
如要篩選資源標籤的值,請使用
example_googleapis_com:user_widget_count{pod_name="RESOURCE_LABEL_VALUE"}
如要篩選指標標籤的值,請使用
example_googleapis_com:user_widget_count{metric_pod_name="METRIC_LABEL_VALUE"}