根据指标数据计算指标

计算指标是指根据预先存在的日志或指标生成新指标,以实现数据可观测性。您可以在 GDC 中通过在 MonitoringRule 自定义资源中配置记录规则来计算指标。记录规则会预先计算经常需要或计算成本较高的表达式。这些规则会将结果另存为一组新的时序。查询预计算结果比在需要时执行原始表达式更快。这种预计算对于信息中心和复杂提醒尤其有用,因为它们每次刷新或评估时都必须重复查询相同的表达式。

您可以在 MonitoringRule 自定义资源中配置记录规则,以根据其他系统监控指标计算指标。MonitoringRule 自定义资源包含记录规则,用于描述根据预先存在的指标计算新指标的条件。

准备工作

如需获得根据指标计算指标所需的权限,请让项目 IAM 管理员向您授予以下角色之一:

  • 监控规则编辑器:用于修改 MonitoringRule 自定义资源。申请 Monitoring Rule Editor (monitoringrule-editor) 角色。
  • Monitoring Rule Viewer:查看 MonitoringRule 自定义资源。请求 Monitoring Rule Viewer (monitoringrule-viewer) 角色。

创建录制规则

定义记录名称和用于评估规则的有效表达式。表达式必须解析为数值,才能将其记录为新指标。在管理员集群的项目命名空间中部署自定义资源,以创建记录规则。

如需详细了解记录规则,请参阅 https://grafana.com/docs/loki/latest/rules/

请按照以下步骤在项目的命名空间中创建记录规则并计算指标:

  1. 使用 MonitoringRule CR 模板打开或创建用于监控记录规则的 CR 的 YAML 文件。
  2. 在 CR 的 namespace 字段中,输入项目的命名空间。
  3. name 字段中,输入规则配置的名称。
  4. 可选:您可以在 source 字段中选择日志来源。有效选项包括 operationalaudit
  5. interval 字段中,输入规则评估时间间隔的时长(以秒为单位)。
  6. 可选:在 limit 字段中,输入提醒数量上限。输入 0 表示提醒次数不受限制。
  7. recordRules 字段中,输入以下信息以计算指标:

    • record 字段中,输入记录名称。此值用于定义要将记录规则写入的时序,并且必须是有效的指标名称。
    • expr 字段中,输入指标规则的 PromQL 表达式。

      此表达式必须解析为数值,才能记录为新指标。

    • 可选:在 labels 字段中,以键值对形式定义要添加或覆盖的标签。

  8. 保存 CR 的 YAML 文件。

  9. 在管理员集群的项目命名空间中部署 CR 以创建记录规则。

MonitoringRule 条记录规则

MonitoringRule 自定义资源包含记录规则,用于描述根据预先存在的系统监控指标计算新指标的条件。

以下 YAML 文件展示了 MonitoringRule CR 的 recordRules 字段的模板。

# Configures either an alert or a target record for precomputation
apiVersion: monitoring.gdc.goog/v1alpha1
kind: MonitoringRule
metadata:
  # Choose namespace that matches the project's namespace
  # Note: The alert or record will be produced in the same namespace
  namespace: PROJECT_NAMESPACE
  name: alerting-config
spec:
  # 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 metrics.
  # Record rules precompute expressions that are frequently needed or computationally expensive.
  # These rules save their result as a new set of time series.
  recordRules:
    # Define which timeseries to write to (must be a valid metric name)
  - record: <string>

    # Define PromQL expression to evaluate for this rule
    expr: <string>

    # Define labels to add or overwrite
    # Optional, Map of {key, value} pairs
    labels:
      <labelname>: <labelvalue>
...

PROJECT_NAMESPACE 替换为项目的命名空间。

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>
...