计算指标是指根据预先存在的日志或指标生成新指标,以实现数据可观测性。您可以在 GDC 中通过在 LoggingRule 自定义资源中配置记录规则来计算指标。记录规则会预先计算经常需要或计算成本较高的表达式。这些规则会将结果另存为一组新的时序。查询预计算结果比在需要时执行原始表达式更快。这种预计算对于信息中心和复杂提醒尤其有用,因为它们每次刷新或评估时都必须重复查询相同的表达式。
您可以在 LoggingRule 自定义资源中配置记录规则,以根据系统日志记录数据计算指标。LoggingRule 自定义资源包含记录规则,用于描述根据预先存在的日志计算新指标的条件。
准备工作
如需获得根据日志计算指标所需的权限,请让项目 IAM 管理员在项目命名空间中向您授予以下角色之一:
- 日志记录规则创建器:创建
LoggingRule自定义资源。申请 Logging Rule Creator (loggingrule-creator) 角色。 - 日志记录规则编辑器:用于修改
LoggingRule自定义资源。申请 Logging Rule Editor (loggingrule-editor) 角色。 - Logging Rule Viewer:查看
LoggingRule自定义资源。请求 Logging Rule Viewer (loggingrule-viewer) 角色。
创建录制规则
定义记录名称和用于评估规则的有效表达式。表达式必须解析为数值,才能将其记录为新指标。在组织管理员集群上部署项目命名空间中的自定义资源,以创建 Observability 平台的记录规则。
如需详细了解 Observability 的记录规则,请参阅 https://grafana.com/docs/loki/latest/rules/
请按照以下步骤在项目的命名空间中创建记录规则并计算指标:
- 使用日志记录规则的
LoggingRuleCR 模板打开或创建 CR 的 YAML 文件。 - 在 CR 的
namespace字段中,输入项目的命名空间。 - 在
name字段中,输入规则配置的名称。 - 可选:您可以在
source字段中选择日志来源。例如,输入operational或audit等值。 - 在
interval字段中,输入规则评估时间间隔的时长(以秒为单位)。 - 可选:在
limit字段中,输入提醒数量上限。输入0表示提醒次数不受限制。 在
recordRules字段中,输入以下信息以计算指标:- 在
record字段中,输入记录名称。此值用于定义要将记录规则写入的时序,并且必须是有效的指标名称。 在
expr字段中,输入日志规则的 LogQL 表达式。此表达式必须解析为数值,才能记录为新指标。
可选:在
labels字段中,以键值对形式定义要添加或覆盖的标签。
- 在
保存 CR 的 YAML 文件。
在管理员集群的项目命名空间中部署 CR 以创建记录规则。
LoggingRule 条记录规则
LoggingRule 自定义资源包含记录规则,这些规则描述了根据预先存在的系统日志记录数据计算新指标以实现可观测性的条件。
以下 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: PROJECT_NAMESPACE
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>
...
将 PROJECT_NAMESPACE 替换为项目的命名空间。