本页面概述了根据 Google Distributed Cloud (GDC) 气隙环境中的日志和指标创建提醒规则的流程,以帮助您主动监控并更快地响应突发事件。
借助 GDC,您可以根据项目的指标或日志定义提醒规则。当满足特定条件时,这些规则会自动触发提醒。您可以定义以下类型的提醒规则:
- 基于指标的规则:根据从应用或基础架构收集的数值数据触发提醒。例如,您可以创建一条规则,以便在 CPU 使用率超过 80% 时触发提醒。使用 GDC 控制台或
MonitoringRule
自定义资源定义创建基于指标的规则。 - 基于日志的规则:根据日志数据分析结果触发提醒。借助这些提醒,您可以识别并响应日志中的特定事件或模式,例如错误消息或异常活动。使用 GDC 控制台或
LoggingRule
自定义资源定义创建基于日志的规则。
基于指标的规则和基于日志的规则都依赖于查询语言表达式来定义触发提醒的条件。此表达式用于过滤和分析传入的数据,以评估是否满足定义的条件。
首次满足条件时,提醒会转换为待处理状态。 如果在您定义的以下持续时间内条件为 true,则提醒会转到 open 状态。此时,系统会发送提醒。
为了提供更多背景信息并帮助您高效管理提醒,您可以向提醒规则添加标签和注释:
标签:用于对提醒进行分类和标识的键值对。使用标签来标记以下类型的信息:
- 严重程度级别(错误、严重、警告)
- 提醒代码
- 资源名称
注释:提供其他非身份识别信息,以丰富提醒内容。注释可以包含以下信息:
- 详细的错误消息
- 相关表达式
- 指向 runbook 或问题排查指南的链接
准备工作
如需获得管理基于指标的规则所需的权限,请让组织 IAM 管理员或项目 IAM 管理员向您授予关联的MonitoringRule
资源角色之一。
另一方面,如需获得管理基于日志的规则所需的权限,请让组织 IAM 管理员或项目 IAM 管理员向您授予关联的 LoggingRule
资源角色之一。
根据您需要的访问权限级别,您可以在组织或项目中获得这些资源的创建者、编辑者或查看者角色。如需了解详情,请参阅准备 IAM 权限。
定义提醒规则
您可以使用 GDC 控制台(首选)或监控和日志记录 API 在项目命名空间中定义提醒规则,以应用自定义资源。
选择以下方法之一,根据指标或日志定义提醒规则:
控制台
在 GDC 控制台的规则组中创建提醒规则:
- 在 GDC 控制台中,选择一个项目。
- 在导航菜单中,依次选择操作 > 提醒。
- 点击提醒政策标签页。
- 点击 创建规则组。
选择提醒规则类型:
- 对于基于指标的提醒规则,请选择指标。
- 对于基于日志的提醒规则,请选择日志。
配置提醒规则组:
- 在提醒规则组名称字段中,输入规则组的名称。
- 在规则评估间隔字段中,输入每个间隔的秒数。
在限制字段中,输入提醒数量上限。
在提醒规则部分中,点击
添加规则。在创建提醒规则窗口中,输入以下详细信息:
点击保存以创建此规则。
点击创建以创建规则组。
相应规则组会显示在提醒规则组列表中。您可以在此规则组中添加更多提醒规则。
API
通过监控或日志记录 API 创建提醒规则:
在 YAML 文件中定义
MonitoringRule
(基于指标的规则)或LoggingRule
(基于日志的规则)自定义资源。完整的资源规范展示了基于指标的规则和基于日志的规则的示例。
根据需要替换 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
可选。以键值对形式向提醒添加非标识性元数据。 保存 YAML 文件。
将资源配置应用于与基于指标或基于日志的提醒规则位于同一命名空间中的 Management API 服务器:
kubectl --kubeconfig KUBECONFIG_PATH apply -f ALERT_RULE_NAME.yaml
替换以下内容:
KUBECONFIG_PATH
:管理 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
定义文件的名称。