下列 YAML 檔案顯示 MonitoringRule 自訂資源的範本。詳情請參閱 API 參考說明文件。
# Configures either an alert or a target record for precomputation.apiVersion:monitoring.gdc.goog/v1kind:MonitoringRulemetadata:# Choose a namespace that matches the project namespace.# The alert or record is produced in the same namespace.namespace:PROJECT_NAMESPACEname:MONITORING_RULE_NAMEspec:# Rule evaluation interval.interval:60s# Configure the limit for the number of alerts.# A value of '0' means no limit.# Optional.# Default value: '0'limit:0# Configure recording rules to generate new metrics based on existing metrics.# Recording rules precompute expressions that are frequently needed or computationally expensive.# Results are saved as a new set of time series.recordRules:# Define the time series where you want to write the recording rule.# The value must be a valid metric name.-record:MyMetricsName# Define the PromQL expression to evaluate for this rule.expr:rate({service_name="bob-service"} [1m])# Define labels to add or overwrite.# Map of key-value pairs.# Optional.labels:verb:read
更改下列內容:
PROJECT_NAMESPACE:您的專案命名空間。
MONITORING_RULE_NAME:MonitoringRule 定義檔案的名稱。
LoggingRule 記錄規則
LoggingRule CR 包含記錄規則,可說明根據現有記錄計算新指標的條件。
以下 YAML 檔案顯示 LoggingRule CR 的 recordRules 欄位範本。
# Configures either an alert or a target record for precomputationapiVersion:logging.gdc.goog/v1alpha1kind:LoggingRulemetadata:# Choose namespace that matches the project's namespace# Note: The alert or record will be produced in the same namespacenamespace:g-fleetns-aname:alerting-configspec:# Choose which log source to base alerts on (Operational/Audit/Security Logs)# Optional, Default: Operationalsource:<string>
# Rule evaluation intervalinterval:<duration>
# Configure limit for number of alerts (0: no limit)# Optional, Default: 0 (no limit)limit:<int>
# Configure record rules to generate new metrics based on pre-existing logs.# Record rules generate metrics based on logs.# Use record rules for complex alerts, which query the same expression repeatedly every time they are evaluated.recordRules:# Define which timeseries to write to (must be a valid metric name)-record:<string>
# Define LogQL expression to evaluate for this rule# https://grafana.com/docs/loki/latest/rules/expr:<string>
# Define labels to add or overwrite# Optional, Map of {key, value} pairslabels:<labelname>:<labelvalue>
...
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Create new metrics from existing metrics\n\nThis page describes how to generate new metrics from existing metrics by\ndeploying a `MonitoringRule` custom resource in\nGoogle Distributed Cloud (GDC) air-gapped appliance.\n\nRecording rules allow you to precompute frequently used or computationally\nexpensive expressions, improving performance for dashboards and alerts. Defined\nwithin `MonitoringRule` custom resources, these rules generate new time series\nmetrics from existing metrics, enhancing data observability.\n\nBy storing precomputed results, recording rules eliminate redundant calculations\neach time an expression is needed. This method significantly speeds up query\ntimes, particularly for complex dashboards and alerts that require repeated\nevaluations of the same expression. Essentially, `MonitoringRule` resources\nlet you derive new metrics from existing ones by specifying the necessary\ncalculations within their recording rules.\n\nBefore you begin\n----------------\n\nTo get the permissions that you need to manage `MonitoringRule` custom\nresources, ask your Organization IAM Admin or Project IAM Admin to grant you one\nof the associated `MonitoringRule` roles.\n\nDepending on the level of access and permissions you need, you might obtain\ncreator, editor, or viewer roles for this resource in an organization or a\nproject. For more information, see [Prepare IAM permissions](/distributed-cloud/hosted/docs/latest/appliance/platform/pa-user/obs-iam-permissions).\n\nCreate recording rules\n----------------------\n\nTo create a recording rule, define a name for the recorded metric and a valid expression that produces a numeric value. This expression, when evaluated,\ngenerates the new metric. Deploy the custom resource in your project namespace on the Management API server to create the recording rules.\n\nFor more information about recording rules, see\n\u003chttps://grafana.com/docs/loki/latest/rules/\u003e.\n| **Important:** Before creating recording rules, ensure you have a deployed project with active workloads and configured metric scraping. In this way, the rules have data to operate on. For more information, see [Collect metrics](/distributed-cloud/hosted/docs/latest/appliance/platform/pa-user/collect-metrics).\n\nFollow these steps to create recording rules in your project namespace:\n\n1. Determine the GDC project from which you want to create recording rules.\n2. Create a YAML file defining the `MonitoringRule` custom resource.\n\n [The complete `MonitoringRule` specification](#monitoringrule-recording-rules)\n shows an example for metric-based recording rules. For more information, see\n the [API reference documentation](/distributed-cloud/hosted/docs/latest/appliance/apis/service/monitoring/v1/monitoring-v1#monitoringrule).\n3. Replace the following values in the YAML file according to your needs:\n\n4. Save the YAML file.\n\n5. Apply the `MonitoringRule` configuration to the Management API server within\n the same namespace as your recording rules:\n\n kubectl --kubeconfig \u003cvar translate=\"no\"\u003eKUBECONFIG_PATH\u003c/var\u003e apply -f \u003cvar translate=\"no\"\u003eMONITORING_RULE_NAME\u003c/var\u003e.yaml\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eKUBECONFIG_PATH\u003c/var\u003e: the path to the kubeconfig file for the Management API server.\n - \u003cvar translate=\"no\"\u003eMONITORING_RULE_NAME\u003c/var\u003e: the name of the `MonitoringRule` definition file.\n\nComplete `MonitoringRule` specification\n---------------------------------------\n\nA `MonitoringRule` custom resource contains recording rules that describe the\nconditions to create new metrics based on existing metrics for observability.\n\nThe following YAML file shows a template for the `MonitoringRule` custom\nresource. For more information, see the\n[API reference documentation](/distributed-cloud/hosted/docs/latest/appliance/apis/service/monitoring/v1/monitoring-v1#monitoringrule). \n\n # Configures either an alert or a target record for precomputation.\n apiVersion: monitoring.gdc.goog/v1\n kind: MonitoringRule\n metadata:\n # Choose a namespace that matches the project namespace.\n # The alert or record is produced in the same namespace.\n namespace: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003ePROJECT_NAMESPACE\u003c/span\u003e\u003c/var\u003e\n name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eMONITORING_RULE_NAME\u003c/span\u003e\u003c/var\u003e\n spec:\n # Rule evaluation interval.\n interval: 60s\n\n # Configure the limit for the number of alerts.\n # A value of '0' means no limit.\n # Optional.\n # Default value: '0'\n limit: 0\n\n # Configure recording rules to generate new metrics based on existing metrics.\n # Recording rules precompute expressions that are frequently needed or computationally expensive.\n # Results are saved as a new set of time series.\n recordRules:\n # Define the time series where you want to write the recording rule.\n # The value must be a valid metric name.\n - record: MyMetricsName\n\n # Define the PromQL expression to evaluate for this rule.\n expr: rate({service_name=\"bob-service\"} [1m])\n\n # Define labels to add or overwrite.\n # Map of key-value pairs.\n # Optional.\n labels:\n verb: read\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_NAMESPACE\u003c/var\u003e: your project namespace.\n- \u003cvar translate=\"no\"\u003eMONITORING_RULE_NAME\u003c/var\u003e: the name of the `MonitoringRule` definition file.\n\nThe `LoggingRule` record rules\n------------------------------\n\nA `LoggingRule` CR contains record rules that describe the conditions to calculate new metrics based on pre-existing logs.\n\nThe following YAML file shows a template for the `recordRules` field of the `LoggingRule` CR. \n\n # Configures either an alert or a target record for precomputation\n apiVersion: logging.gdc.goog/v1alpha1\n kind: LoggingRule\n metadata:\n # Choose namespace that matches the project's namespace\n # Note: The alert or record will be produced in the same namespace\n namespace: g-fleetns-a\n name: alerting-config\n spec:\n # Choose which log source to base alerts on (Operational/Audit/Security Logs)\n # Optional, Default: Operational\n source: \u003cstring\u003e\n\n # Rule evaluation interval\n interval: \u003cduration\u003e\n\n # Configure limit for number of alerts (0: no limit)\n # Optional, Default: 0 (no limit)\n limit: \u003cint\u003e\n\n # Configure record rules to generate new metrics based on pre-existing logs.\n # Record rules generate metrics based on logs.\n # Use record rules for complex alerts, which query the same expression repeatedly every time they are evaluated.\n recordRules:\n # Define which timeseries to write to (must be a valid metric name)\n - record: \u003cstring\u003e\n\n # Define LogQL expression to evaluate for this rule\n # https://grafana.com/docs/loki/latest/rules/\n expr: \u003cstring\u003e\n\n # Define labels to add or overwrite\n # Optional, Map of {key, value} pairs\n labels:\n \u003clabelname\u003e: \u003clabelvalue\u003e\n ..."]]