本页面介绍了如何在 Google Distributed Cloud (GDC) 气隙设备中部署 MonitoringRule 自定义资源,从而根据现有指标生成新指标。
通过记录规则,您可以预先计算常用表达式或计算成本高昂的表达式,从而提高信息中心和提醒的性能。这些规则在 MonitoringRule 自定义资源中定义,可根据现有指标生成新的时序指标,从而增强数据可观测性。
通过存储预先计算的结果,记录规则可在每次需要表达式时消除冗余计算。此方法可显著缩短查询时间,尤其适用于需要重复评估同一表达式的复杂信息中心和提醒。从本质上讲,MonitoringRule 资源可让您通过在记录规则中指定必要的计算来从现有指标中派生出新指标。
准备工作
如需获得管理MonitoringRule自定义资源所需的权限,请让组织 IAM 管理员或项目 IAM 管理员向您授予关联的MonitoringRule角色之一。
根据您需要的访问权限级别,您可以在组织或项目中获得此资源的创建者、编辑者或查看者角色。如需了解详情,请参阅准备 IAM 权限。
创建录制规则
如需创建记录规则,请为记录的指标定义一个名称,并定义一个可生成数值的有效表达式。此表达式在求值时会生成新的指标。在 Management API 服务器上,于项目命名空间中部署自定义资源,以创建记录规则。
如需详细了解记录规则,请参阅 https://grafana.com/docs/loki/latest/rules/。
请按照以下步骤在项目命名空间中创建记录规则:
- 确定要从中创建录制规则的 GDC 项目。
创建一个 YAML 文件,用于定义
MonitoringRule自定义资源。完整的
MonitoringRule规范展示了基于指标的记录规则示例。如需了解详情,请参阅 API 参考文档。根据需要替换 YAML 文件中的以下值:
字段名称 说明 namespace项目命名空间。 name规则配置的名称。 interval规则评估间隔的时长(以秒为单位)。 limit可选。提醒数量上限。设置为 0表示提醒次数不受限制。recordRules用于计算新指标的定义。 recordRules.record新指标的记录名称。该值必须是有效的指标名称,用于定义存储结果的时序。 recordRules.expr指标规则的 PromQL 表达式,必须求值为数值。 recordRules.labels可选。要添加到新指标或覆盖新指标的标签的键值对。 保存 YAML 文件。
将
MonitoringRule配置应用于与记录规则位于同一命名空间中的 Management API 服务器:kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_RULE_NAME.yaml替换以下内容:
KUBECONFIG_PATH:管理 API 服务器的 kubeconfig 文件的路径。MONITORING_RULE_NAME:MonitoringRule定义文件的名称。
完整 MonitoringRule 规范
MonitoringRule 自定义资源包含记录规则,这些规则描述了基于现有指标创建新指标以实现可观测性的条件。
以下 YAML 文件展示了 MonitoringRule 自定义资源的模板。如需了解详情,请参阅 API 参考文档。
# Configures either an alert or a target record for precomputation.
apiVersion: monitoring.gdc.goog/v1
kind: MonitoringRule
metadata:
# Choose a namespace that matches the project namespace.
# The alert or record is produced in the same namespace.
namespace: PROJECT_NAMESPACE
name: MONITORING_RULE_NAME
spec:
# 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 precomputation
apiVersion: logging.gdc.goog/v1alpha1
kind: LoggingRule
metadata:
# Choose namespace that matches the project's namespace
# Note: The alert or record will be produced in the same namespace
namespace: g-fleetns-a
name: alerting-config
spec:
# Choose which log source to base alerts on (Operational/Audit/Security Logs)
# Optional, Default: Operational
source: <string>
# Rule evaluation interval
interval: <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} pairs
labels:
<labelname>: <labelvalue>
...