创建提醒规则

本页面简要介绍了如何根据 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 控制台(首选)或监控和日志记录 API 在项目命名空间中定义提醒规则,以应用自定义资源。

选择以下方法之一来定义基于指标或日志的提醒规则:

控制台

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

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

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

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

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

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

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

    • 提醒从待处理状态转换为 已打开状态之前的持续时间(以秒为单位)。

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

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

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

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

    • 提醒消息或说明。

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

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

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

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

API

通过监控或日志记录 API 创建提醒规则:

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

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

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

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

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

    kubectl --kubeconfig KUBECONFIG_PATH apply -f ALERT_RULE_NAME.yaml
    

    替换以下内容:

    • KUBECONFIG_PATH:Management API 服务器的 kubeconfig 文件的路径。
    • ALERT_RULE_NAMEMonitoringRuleLoggingRule 定义文件的名称。

完整资源规范

本部分包含可用于通过应用自定义资源来创建基于指标的提醒规则和基于日志的提醒规则的 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_NAMEMonitoringRule 定义文件的名称。

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_NAMELoggingRule 定义文件的名称。