创建提醒规则

本页面概述了如何根据 Google Distributed Cloud (GDC) 网闸隔离环境中的日志和指标创建提醒规则,以便进行主动监控和更快地响应突发事件。

GDC 允许您根据项目的指标或日志定义提醒规则。当满足特定条件时,这些规则会自动触发提醒。您可以定义以下类型的提醒规则:

  • 基于指标的规则:根据从应用或基础架构收集的数值数据触发提醒。例如,您可以创建一条规则,在 CPU 使用率超过 80% 时触发提醒。使用 GDC 控制台或 MonitoringRule 自定义资源定义创建基于指标的规则。
  • 基于日志的规则:根据日志数据分析触发提醒。借助这些提醒,您可以识别并响应日志中的特定事件或模式,例如出错提示或异常活动。使用 GDC 控制台或 LoggingRule 自定义资源定义创建基于日志的规则。

基于指标和基于日志的规则都依赖于查询语言表达式来定义触发提醒的条件。此表达式会过滤和分析传入的数据,评估是否满足定义的条件。

首次满足条件时,提醒会转换为待处理状态。如果条件在您定义的以下持续时间内为 true,则提醒会进入未结状态。此时,系统会发送提醒。

为了提供更多背景信息并促进高效的提醒管理,您可以向提醒规则添加标签和注解:

  • 标签:对提醒进行分类和标识的键值对。使用标签来表示以下信息:

    • 严重级别(错误、严重、警告)
    • 提醒代码
    • 资源名称
  • 注解:提供额外的非身份信息,以丰富提醒内容。注释可以包含以下信息:

    • 详细的出错提示
    • 相关表达式
    • 指向 Runbook 或问题排查指南的链接

准备工作

如需管理基于指标或基于日志的规则,您必须拥有必要的权限。

### 为基于指标的规则请求 IAM 组织角色

请与您的 IAM 组织管理员联系,申请获得以下角色:

  • MonitoringRule PA Creator (monitoringrule-pa-creator):创建 MonitoringRule 自定义资源。
  • MonitoringRule PA Editor (monitoringrule-pa-editor):修改 MonitoringRule 自定义资源。
  • MonitoringRule PA Viewer (monitoringrule-pa-viewer):查看 MonitoringRule 自定义资源。

    为基于指标的规则请求 IAM 项目角色

请与您的 IAM 项目管理员联系,申请获得以下角色:

  • MonitoringRule Editor (monitoringrule-editor):修改项目命名空间中的 MonitoringRule 自定义资源。
  • MonitoringRule Viewer (monitoringrule-viewer):查看项目命名空间中的 MonitoringRule 自定义资源。

请求基于日志的规则的 IAM 组织角色

请与您的 IAM 组织管理员联系,申请获得以下角色:

  • LoggingRule PA Creator (loggingrule-pa-creator):创建 LoggingRule 自定义资源。
  • LoggingRule PA Editor (loggingrule-pa-editor):修改或更改 LoggingRule 自定义资源。
  • LoggingRule PA Viewer (loggingrule-pa-viewer):查看 LoggingRule 自定义资源。

请求基于日志的规则的 IAM 项目角色

请与您的 IAM 项目管理员联系,申请获得以下角色:

  • LoggingRule Editor (loggingrule-editor):修改项目命名空间中的 LoggingRule 自定义资源。
  • LoggingRule Viewer (loggingrule-viewer):查看项目命名空间中的 LoggingRule 自定义资源。

定义提醒规则

您可以使用 GDC 控制台(首选)或监控和 Logging API 在项目命名空间中定义提醒规则,以应用自定义资源。

选择以下方法之一,根据指标或日志定义提醒规则:

控制台

通过 GDC 控制台在规则组中创建提醒规则:

  1. 在 GDC 控制台中,选择一个项目。
  2. 在导航菜单中,依次选择运维 > 提醒。
  3. 点击提醒政策标签页。
  4. 点击 创建规则组。
  5. 选择提醒规则类型:

    • 对于基于指标的提醒规则,请选择指标。
    • 对于基于日志的提醒规则,请选择日志。
  6. 配置提醒规则组:

    • 在提醒规则组名称字段中,输入规则组的名称。
    • 在规则评估间隔字段中,输入每个间隔的秒数。
    • 在上限字段中,输入提醒数量上限。

  7. 在提醒规则部分中,点击 添加规则。

  8. 在创建提醒规则窗口中,输入以下详细信息:

    • 提醒规则的名称。
    • 提醒规则的表达式(对于基于日志的规则,使用 LogQL;对于基于指标的规则,使用 PromQL)。

    • 提醒从待处理转换为打开之前的时长(以秒为单位)。

    • 严重级别,例如错误或警告。

    • 用于标识相关资源的简称。

    • 用于标识提醒的提醒代码。

    • Runbook 网址或问题排查信息。

    • 提醒消息或说明。

    • 可选:以键值对的形式添加标签和注释。

  9. 点击保存以创建此规则。

  10. 点击创建以创建规则组。

该规则组会显示在提醒规则组列表中。您可以在此规则组中对更多提醒规则进行分组。

API

通过监控或 Logging API 创建提醒规则:

  1. 在 YAML 文件中定义 MonitoringRule(基于指标的规则)或 LoggingRule(基于日志的规则)自定义资源。

    完整的资源规范展示了基于指标和基于日志的规则的示例。

  2. 根据需要替换 YAML 文件中的以下值:

    字段 说明
    namespace 项目命名空间。
    name 提醒规则配置的名称。
    source 提醒规则的日志来源。有效选项包括 operational 和 audit。仅适用于 LoggingRule 资源。
    interval 规则评估间隔的时长(以秒为单位)。
    limit 可选。提醒数量上限。设置为 0 可接收无限量提醒。
    alertRules 创建提醒规则的定义。
    alertRules.alert 提醒的名称。
    alertRules.expr LogQL 表达式(适用于基于日志的规则)或 PromQL 表达式(适用于基于指标的规则)。表达式的计算结果必须为 true 或 false,以确定提醒是否转换为待处理状态。
    alertRules.for 可选。提醒从待处理转换为打开之前的时长(以秒为单位)。默认值为 0 秒(立即触发)。
    alertRules.labels 用于对提醒进行分类和标识的键值对。它需要以下标签:severity、code 和 resource。
    alertRules.annotations 可选。提醒的非标识性元数据(以键值对形式表示)。
  3. 保存 YAML 文件。

  4. 将资源配置应用于与基于指标或基于日志的提醒规则位于同一命名空间中的 Management API 服务器:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f ALERT_RULE_NAME.yaml
    

    替换以下内容:

    • KUBECONFIG_PATH:Management API 服务器的 kubeconfig 文件的路径。
    • ALERT_RULE_NAME:MonitoringRule 或 LoggingRule 定义文件的名称。

完整资源规范

本部分包含 YAML 模板,您可以使用这些模板通过应用自定义资源来创建基于指标和基于日志的提醒规则。如果您通过 GDC 控制台创建提醒,则可以跳过此部分。

在以下自定义资源中定义提醒规则:

  • MonitoringRule:基于指标的规则。
  • LoggingRule:基于日志的规则。

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 metric-based alert rules.
  alertRules:
    # Define an alert name.
  - alert: my-metric-based-alert

    # Define the PromQL expression to evaluate for this rule.
    expr: rate({service_name="bob-service"} [1m])

    # The duration in seconds before an alert transitions from pending to open.
    # Optional.
    # Default value: '0s'
    for: 0s

    # Define labels to add or overwrite.
    # Map of key-value pairs.
    # Required labels:
    #     severity: [error, critical, warning, info]
    #     code:
    #     resource: component/service/hardware related to the alert
    # Additional labels are optional.
    labels:
      severity: error
      code: 202
      resource: AIS
      another-label: another-value

    # Define annotations to add.
    # Map of key-value pairs.
    # Optional.
    # Recommended annotations:
    #     message: value of the Message field in the user interface.
    #     expression: value of the Rule field in the user interface.
    #     runbookurl: URL of the Actions to take field in the user interface.
    annotations:
      message: my-alert-message

替换以下内容:

  • PROJECT_NAMESPACE:您的项目命名空间。
  • MONITORING_RULE_NAME:MonitoringRule 定义文件的名称。

LoggingRule

以下 YAML 文件显示了 LoggingRule 自定义资源的模板。如需了解详情,请参阅 API 参考文档。

# Configures either an alert or a target record for precomputation.
apiVersion: logging.gdc.goog/v1
kind: LoggingRule
metadata:
  # Choose a namespace that matches the project namespace.
  # The alert or record is produced in the same namespace.
  namespace: PROJECT_NAMESPACE
  name: LOGGING_RULE_NAME
spec:
  # Choose the log source to base alerts on (operational or audit logs).
  # Optional.
  # Valid options: 'operational' and 'audit'
  # Default value: 'operational'
  source: operational

  # 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 log-based alert rules.
  alertRules:
    # Define an alert name.
  - alert: my-log-based-alert

    # Define the LogQL expression to evaluate for this rule.
    expr: rate({service_name="bob-service"} [1m])

    # The duration in seconds before an alert transitions from pending to open.
    # Optional.
    # Default value: '0s'
    for: 0s

    # Define labels to add or overwrite.
    # Map of key-value pairs.
    # Required labels:
    #     severity: [error, critical, warning, info]
    #     code:
    #     resource: component/service/hardware related to the alert
    # Additional labels are optional.
    labels:
      severity: warning
      code: 202
      resource: AIS
      another-label: another-value

    # Define annotations to add.
    # Map of key-value pairs.
    # Optional.
    # Recommended annotations:
    #     message: value of the Message field in the user interface.
    #     expression: value of the Rule field in the user interface.
    #     runbookurl: URL of the Actions to take field in the user interface.
    annotations:
      message: my-alert-message

替换以下内容:

  • PROJECT_NAMESPACE:您的项目命名空间。
  • LOGGING_RULE_NAME:LoggingRule 定义文件的名称。