Skip to content

Commit

Permalink
Aggregate in case of node change
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaveMyYard committed Sep 4, 2023
1 parent 3d0b3bc commit a9d281d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
52 changes: 30 additions & 22 deletions robusta_krr/core/integrations/prometheus/metrics/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def get_query(self, object: K8sObjectData, duration: str, step: str) -> str:
pods_selector = "|".join(pod.name for pod in object.pods)
cluster_label = self.get_prometheus_cluster_label()
return f"""
rate(
container_cpu_usage_seconds_total{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}[{step}]
)
sum(
rate(
container_cpu_usage_seconds_total{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}[{step}]
)
) by (container, pod, job)
"""


Expand All @@ -27,14 +29,17 @@ def get_query(self, object: K8sObjectData, duration: str, step: str) -> str:
return f"""
quantile_over_time(
{round(percentile / 100, 2)},
rate(
container_cpu_usage_seconds_total{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}[{step}]
)[{duration}:{step}]
sum(
rate(
container_cpu_usage_seconds_total{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}[{step}]
)
) by (container, pod, job)
[{duration}:{step}]
)
"""

Expand All @@ -47,11 +52,14 @@ def get_query(self, object: K8sObjectData, duration: str, step: str) -> str:
cluster_label = self.get_prometheus_cluster_label()
return f"""
count_over_time(
container_cpu_usage_seconds_total{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}[{duration}]
sum(
container_cpu_usage_seconds_total{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}
) by (container, pod, job)
[{duration}:{step}]
)
"""
30 changes: 18 additions & 12 deletions robusta_krr/core/integrations/prometheus/metrics/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ def get_query(self, object: K8sObjectData, duration: str, step: str) -> str:
cluster_label = self.get_prometheus_cluster_label()
return f"""
max_over_time(
container_memory_working_set_bytes{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}[{duration}:{step}]
sum(
container_memory_working_set_bytes{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}
) by (container, pod, job)
[{duration}:{step}]
)
"""

Expand All @@ -41,11 +44,14 @@ def get_query(self, object: K8sObjectData, duration: str, step: str) -> str:
cluster_label = self.get_prometheus_cluster_label()
return f"""
count_over_time(
container_memory_working_set_bytes{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}[{duration}:{step}]
sum(
container_memory_working_set_bytes{{
namespace="{object.namespace}",
pod=~"{pods_selector}",
container="{object.container}"
{cluster_label}
}}
) by (container, pod, job)
[{duration}:{step}]
)
"""
2 changes: 1 addition & 1 deletion robusta_krr/strategies/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __calculate_cpu_proposal(
if object_data.hpa is not None and object_data.hpa.target_cpu_utilization_percentage is not None:
return ResourceRecommendation.undefined(info="HPA detected")

cpu_usage = self.settings.calculate_cpu_proposal(data)
cpu_usage = self.settings.calculate_cpu_proposal(filtered_data)
return ResourceRecommendation(request=cpu_usage, limit=None)

def __calculate_memory_proposal(
Expand Down

0 comments on commit a9d281d

Please sign in to comment.