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

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

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 AlertingRule CR includes an invalid interval period, it is an invalid alerting rule
  • If an AlertingRule CR includes an invalid for period, it is an invalid alerting rule.
  • If an AlertingRule CR includes an invalid LogQL expr, it is an invalid alerting rule.
  • If an AlertingRule CR 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.
Table 14.1. AlertingRule definitions
Tenant typeValid namespaces for AlertingRule CRs

application

<your_application_namespace>

audit

openshift-logging

infrastructure

openshift-/*, kube-/\*, default

Procedure

  1. Create an AlertingRule custom resource (CR):

    The following example shows an infrastructure AlertingRule CR:

      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 AlertingRule CR is created must have a label matching the LokiStack spec.rules.namespaceSelector definition.
    metadata.labels
    The labels block must match the LokiStack spec.rules.selector definition.
    spec.tenantID
    AlertingRule CRs for infrastructure tenants are only supported in the openshift-*, kube-\*, or default namespaces.
    expr
    The value for kubernetes_namespace_name: must match the value for metadata.namespace.
    labels.severity
    The value of this mandatory field must be critical, warning, or info.
    annotations.summary
    This field is mandatory.
    annotations.description
    This field is mandatory.

    Example application AlertingRule CR

      apiVersion: 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 AlertingRule CR is created must have a label matching the LokiStack spec.rules.namespaceSelector definition.
    metadata.labels
    The labels block must match the LokiStack spec.rules.selector definition.
    expr
    Value for kubernetes_namespace_name: must match the value for metadata.namespace.
    labels.severity
    The value of this mandatory field must be critical, warning, or info.
    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.
  2. Apply the AlertingRule CR:

    $ oc apply -f <filename>.yaml
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top