Skip to content

Commit

Permalink
improve compute credits prom query (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
zubenkoivan authored Jul 30, 2024
1 parent e60687d commit c82201a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/platform_reports/metrics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,8 @@ class PrometheusQueryFactory:
def create_compute_credits(
cls, *, org_name: str | None = None, project_name: str | None = None
) -> str:
label_matchers = []
if org_name:
label_matchers.append(f'label_platform_neuromation_io_org="{org_name}"')
if project_name:
label_matchers.append(
f'label_platform_neuromation_io_project="{project_name}"'
)
query = [
"max by(pod) (kube_pod_credits_total) * on(pod) group_right",
"max by(pod) (kube_pod_credits_total) * on(pod) group_right() ",
]
if org_name or project_name:
jobs_label_matchers = cls._get_jobs_label_matchers(
Expand All @@ -54,11 +47,14 @@ def create_compute_credits(
org_name=org_name, project_name=project_name
)
query.append(
f"() (kube_pod_labels{{{jobs_label_matchers}}} or "
f"(kube_pod_labels{{{jobs_label_matchers}}} or "
f"kube_pod_labels{{{apps_label_matchers}}})"
)
else:
query.append(" kube_pod_labels")
query.append(
f'(kube_pod_labels{{{PrometheusLabel.NEURO_PROJECT_KEY}!=""}} or '
f'kube_pod_labels{{{PrometheusLabel.APOLO_PROJECT_KEY}!=""}})'
)
return "".join(query)

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_metrics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def test_create_compute_credits(self) -> None:
query = PrometheusQueryFactory().create_compute_credits()

assert query == (
"max by(pod) (kube_pod_credits_total) * "
"on(pod) group_right kube_pod_labels"
"max by(pod) (kube_pod_credits_total) * on(pod) group_right() "
'(kube_pod_labels{label_platform_neuromation_io_project!=""} or '
'kube_pod_labels{label_platform_apolo_us_project!=""})'
)

def test_create_compute_credits__filter_by_org_project(self) -> None:
Expand Down

0 comments on commit c82201a

Please sign in to comment.