-
Notifications
You must be signed in to change notification settings - Fork 252
Update memory_query and cpu_query for Prometheus #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Update memory_query and cpu_query for Prometheus #420
Conversation
…ch no longer works in Azure Monitored Prometheus
WalkthroughThe Prometheus queries for cluster summary metrics were updated to retrieve data from Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10–15 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py(1 hunks)
| memory_query = f""" | ||
| sum(max by (instance) (machine_memory_bytes{{ {single_cluster_label} }})) | ||
| sum(kube_node_status_capacity{{ resource='memory' {single_cluster_label} }}) | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix PromQL syntax: use double quotes for label values.
PromQL requires double quotes for label values, not single quotes. The current syntax resource='memory' will cause a query syntax error.
Apply this diff to fix the syntax:
memory_query = f"""
- sum(kube_node_status_capacity{{ resource='memory' {single_cluster_label} }})
+ sum(kube_node_status_capacity{{ resource="memory" {single_cluster_label} }})
"""📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| memory_query = f""" | |
| sum(max by (instance) (machine_memory_bytes{{ {single_cluster_label} }})) | |
| sum(kube_node_status_capacity{{ resource='memory' {single_cluster_label} }}) | |
| """ | |
| memory_query = f""" | |
| sum(kube_node_status_capacity{{ resource="memory" {single_cluster_label} }}) | |
| """ |
🤖 Prompt for AI Agents
In
robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py
around lines 256 to 258, the PromQL uses single quotes for the label value
(resource='memory') which is invalid; change the label value to use double
quotes (resource="memory") in the f-string query, and scan nearby queries for
other label values using single quotes and convert them to double quotes as
well.
| cpu_query = f""" | ||
| sum(max by (instance) (machine_cpu_cores{{ {single_cluster_label} }})) | ||
| sum(kube_node_status_capacity{{ resource='cpu' {single_cluster_label} }}) | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Fix PromQL syntax: use double quotes for label values.
PromQL requires double quotes for label values, not single quotes. The current syntax resource='cpu' will cause a query syntax error.
Apply this diff to fix the syntax:
cpu_query = f"""
- sum(kube_node_status_capacity{{ resource='cpu' {single_cluster_label} }})
+ sum(kube_node_status_capacity{{ resource="cpu" {single_cluster_label} }})
"""After applying the fixes, please verify that both queries execute successfully in your Prometheus environments:
🏁 Script executed:
#!/bin/bash
# Description: Test the corrected Prometheus queries return valid results
# This verifies the syntax is correct and metrics exist
# Note: Replace PROMETHEUS_URL with your actual Prometheus endpoint
PROMETHEUS_URL="${PROMETHEUS_URL:-http://localhost:9090}"
echo "Testing memory query..."
curl -s "${PROMETHEUS_URL}/api/v1/query" \
--data-urlencode 'query=sum(kube_node_status_capacity{resource="memory"})' | jq '.data.result'
echo -e "\nTesting CPU query..."
curl -s "${PROMETHEUS_URL}/api/v1/query" \
--data-urlencode 'query=sum(kube_node_status_capacity{resource="cpu"})' | jq '.data.result'Length of output: 45
I need to verify the PromQL syntax rules and inspect the actual codebase since the Prometheus endpoint is unavailable in the sandbox.
Now let me inspect the actual file to verify the current state of the code:
---
🏁 Script executed:
```shell
#!/bin/bash
# Inspect the prometheus_metrics_service.py file around lines 260-262
fd -t f "prometheus_metrics_service.py" --exec cat {} +
Length of output: 16651
Fix PromQL syntax: use double quotes for label values in both queries.
PromQL requires double quotes for label values, not single quotes. The current syntax with single quotes will cause query syntax errors. Both the memory_query and cpu_query contain this issue.
Apply this diff to fix both queries:
memory_query = f"""
- sum(kube_node_status_capacity{{ resource='memory' {single_cluster_label} }})
+ sum(kube_node_status_capacity{{ resource="memory" {single_cluster_label} }})
"""
cpu_query = f"""
- sum(kube_node_status_capacity{{ resource='cpu' {single_cluster_label} }})
+ sum(kube_node_status_capacity{{ resource="cpu" {single_cluster_label} }})
"""Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In
robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py
around lines 260 to 262, the PromQL label values use single quotes which is
invalid; update the cpu_query and the corresponding memory_query to use
double-quoted label values (e.g. resource="cpu" and resource="memory"), making
sure the f-string and surrounding triple-quoted string are adjusted so the
double quotes appear in the final PromQL expression.
machine_memory_bytes and machine_cpu_cores have been [deprecated] (https://github.com/kubernetes/kube-state-metrics/blob/main/docs/metrics/cluster/node-metrics.md) [as per StackOverflow] (https://stackoverflow.com/questions/63901926/how-to-query-the-total-memory-available-to-kubernetes-nodes)
This issue came up for me when querying Azure Managed Prometheus, as these queries no longer exist there. I've tested these queries on both manually deployed Prometheus as well as Azure Managed Prometheus and the query works and returns the same data.