Chapter 14. Configure log-based alerts
You can configure log-based alerts for Loki by creating an AlertingRule custom resource (CR).
14.1. Log-based alerts for Loki Copy linkLink copied to clipboard!
Configure log-based alerts for Loki by creating AlertingRule custom resources. Log-based alerting enables you to trigger alerts based on log patterns and volumes, complementing metric-based alerting to provide comprehensive observability. These alerts integrate with Red Hat OpenShift Logging monitoring and can route to external alerting systems.
14.1.1. Create a log-based alerting rule with Loki Copy linkLink copied to clipboard!
Define and validate alerting rules for Loki log analysis in your LokiStack instance.
The AlertingRule CR contains a set of specifications and webhook validation definitions to declare groups of alerting rules for a single LokiStack instance. In addition, the webhook validation definition provides support for rule validation conditions:
-
If an
AlertingRuleCR includes an invalidintervalperiod, it is an invalid alerting rule -
If an
AlertingRuleCR includes an invalidforperiod, it is an invalid alerting rule. -
If an
AlertingRuleCR includes an invalid LogQLexpr, it is an invalid alerting rule. -
If an
AlertingRuleCR includes two groups with the same name, it is an invalid alerting rule. - If none of the above applies, an alerting rule is considered valid.
| Tenant type | Valid namespaces for AlertingRule CRs |
|---|---|
| application |
|
| audit |
|
| infrastructure |
|
Procedure
Create an
AlertingRulecustom resource (CR):The following example shows an infrastructure
AlertingRuleCR:apiVersion: loki.grafana.com/v1 kind: AlertingRule metadata: name: loki-operator-alerts namespace: openshift-operators-redhat labels: openshift.io/<label_name>: "true" spec: tenantID: "infrastructure" groups: - name: LokiOperatorHighReconciliationError rules: - alert: HighPercentageError expr: | sum(rate({kubernetes_namespace_name="openshift-operators-redhat", kubernetes_pod_name=~"loki-operator-controller-manager.*"} |= "error" [1m])) by (job) / sum(rate({kubernetes_namespace_name="openshift-operators-redhat", kubernetes_pod_name=~"loki-operator-controller-manager.*"}[1m])) by (job) > 0.01 for: 10s labels: severity: critical annotations: summary: High Loki Operator Reconciliation Errors description: High Loki Operator Reconciliation Errors- metadata.namespace
-
The namespace where this
AlertingRuleCR is created must have a label matching the LokiStackspec.rules.namespaceSelectordefinition. - metadata.labels
-
The
labelsblock must match the LokiStackspec.rules.selectordefinition. - spec.tenantID
-
AlertingRuleCRs forinfrastructuretenants are only supported in theopenshift-*,kube-\*, ordefaultnamespaces. - expr
-
The value for
kubernetes_namespace_name:must match the value formetadata.namespace. - labels.severity
-
The value of this mandatory field must be
critical,warning, orinfo. - annotations.summary
- This field is mandatory.
- annotations.description
- This field is mandatory.
Example application
AlertingRuleCRapiVersion: loki.grafana.com/v1 kind: AlertingRule metadata: name: app-user-workload namespace: app-ns labels: openshift.io/<label_name>: "true" spec: tenantID: "application" groups: - name: AppUserWorkloadHighError rules: - alert: expr: | sum(rate({kubernetes_namespace_name="app-ns", kubernetes_pod_name=~"podName.*"} |= "error" [1m])) by (job) for: 10s labels: severity: critical annotations: summary: description:- metadata.namespace
-
The namespace where this
AlertingRuleCR is created must have a label matching the LokiStackspec.rules.namespaceSelectordefinition. - metadata.labels
-
The
labelsblock must match the LokiStackspec.rules.selectordefinition. - expr
-
Value for
kubernetes_namespace_name:must match the value formetadata.namespace. - labels.severity
-
The value of this mandatory field must be
critical,warning, orinfo. - annotations.summary
- The value of this mandatory field is a summary of the rule.
- annotations.description
- The value of this mandatory field is a detailed description of the rule.
Apply the
AlertingRuleCR:$ oc apply -f <filename>.yaml