Performance optimization with recording rules
In large-scale clusters, recording rules optimize how Prometheus handles network data. Recording rules improve dashboard responsiveness and reduce the computational overhead of complex queries.
Optimization benefits
Recording rules pre-compute complex Prometheus Query Language (PromQL) expressions and save the results as new time series. Unlike alerting rules, recording rules do not monitor thresholds.
Using recording rules provides the following advantages:
- Improved performance
-
Pre-computing Prometheus queries allows dashboards to load faster by avoiding on-demand calculations for long-term trends.
- Resource efficiency
-
Calculating data at fixed intervals reduces CPU load on the Prometheus server compared to recalculating data on every dashboard refresh.
- Simplified queries
-
Using short metric names, such as
cluster:network_traffic:rate_5m, simplifies complex aggregate calculations in custom dashboards.
Comparison of rule modes
The following table compares rule modes based on the expected outcome:
| Feature | Alerting rules | Recording rules |
|---|---|---|
Primary goal |
Issue notification. |
Persistent metric history. |
Data output |
Alerting state. |
New time series metric. |
UI visibility |
Alerting and Network Health views. |
Metrics Explorer and Network Health views. |
Notifications |
Triggers |
Does not trigger notifications. |
Integrating recording rules with the health dashboard
Custom recording rules that contribute to the Network Health dashboard must meet specific metadata requirements.
- Label requirements
-
Include the
netobserv: "true"label in thelabelsfield of the rule and thePrometheusRulemetadata. The Network Observability Operator identifiesPrometheusRuleresources cluster-wide by using this label. - Annotation requirements
-
Include the
netobserv.io/network-healthannotation in thePrometheusRulemetadata. This annotation is required for recording rules to appear in the Network Health dashboard. The value is a JSON object where keys are the metric names (therecordfield of each rule). Each value consists of the following fields:-
summary: An optional short title. This field supports Prometheus template syntax, such as{{ $labels.namespace }}. -
description: An optional description. This field supports Prometheus template syntax. -
netobserv_io_network_health: A required JSON string. For recording rules, use therecordingThresholdsfield instead ofthreshold. This field determines the health score and UI coloring, such as{"info":"10","warning":"25","critical":"50"}.
-
Optimizing dashboard metrics with recording rules
Create custom recording rules to pre-compute metrics for the Network Health dashboard. Recording rules require specific annotations and labels to integrate with the Network Observability Operator.
-
Access to the cluster with
cluster-adminprivileges. -
The Network Observability Operator is installed.
-
Red Hat OpenShift Container Platform 4.16 or later is installed.
-
Familiarity with PromQL.
|
|
Custom |
-
Define a
PrometheusRuleresource in a YAML file, such ascustom-recording-rule.yaml, ensuring thenetobserv: "true"label andnetobserv.io/network-healthannotation are included:apiVersion: monitoring.coreos.com/v1 kind: PrometheusRule metadata: name: my-recording-rules namespace: openshift-monitoring labels: netobserv: "true" annotations: netobserv.io/network-health: | { "my_metric_per_namespace": { "summary": "Custom metric is {{ $value }} in the namespace {{ $labels.namespace }}", "description": "Custom metric is {{ $value }} in the namespace {{ $labels.namespace }}", "netobserv_io_network_health": "{\"unit\":\"%\",\"upperBound\":\"100\",\"namespaceLabels\":[\"namespace\"],\"recordingThresholds\":{\"info\":\"10\",\"warning\":\"25\",\"critical\":\"50\"}}" } } spec: groups: - name: MyRecordingRules interval: 30s rules: - record: my_metric_per_namespace expr: (count by (namespace) (kube_pod_info) * 0 + 20) labels: netobserv: "true" -
Apply the custom recording rule by running the following command:
$ oc apply -f custom-recording-rule.yaml
-
Confirm the
PrometheusRuleresource exists by running the following command:$ oc get prometheusrules my-recording-rules -n openshift-monitoring -o yaml -
Confirm the recording rule appears in the Red Hat OpenShift Container Platform web console by navigating to Observe → Network Health.