From ec5edbe50f8f21e68e7ab44d4b71a2e108507b00 Mon Sep 17 00:00:00 2001 From: avi robusta Date: Sun, 18 Jun 2023 14:16:01 +0300 Subject: added cluster label for kube_pod_owner --- .../metrics_service/prometheus_metrics_service.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py b/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py index 382e887..da12023 100644 --- a/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py +++ b/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py @@ -192,13 +192,14 @@ class PrometheusMetricsService(MetricsService): period_literal = f"{days_literal}d" pod_owners: list[str] pod_owner_kind: str - + cluster_label = self.get_prometheus_cluster_label() if object.kind == "Deployment": replicasets = await self.query( "kube_replicaset_owner{" f'owner_name="{object.name}", ' f'owner_kind="Deployment", ' f'namespace="{object.namespace}"' + f'{cluster_label}' "}" f"[{period_literal}]" ) @@ -214,6 +215,7 @@ class PrometheusMetricsService(MetricsService): f'owner_name=~"{owners_regex}", ' f'owner_kind="{pod_owner_kind}", ' f'namespace="{object.namespace}"' + f'{cluster_label}' "}" f"[{period_literal}]" ) @@ -225,3 +227,14 @@ class PrometheusMetricsService(MetricsService): for pod in related_pods if pod["metric"]["pod"] not in current_pods ] + + def get_prometheus_cluster_label(self) -> str: + """ + Generates the cluster label for querying a centralized Prometheus + + Returns: + str: a promql safe label string for querying the cluster. + """ + if self.config.prometheus_cluster_label is None: + return "" + return f', {self.config.prometheus_label}="{self.config.prometheus_cluster_label}"' \ No newline at end of file -- cgit v1.2.3 From 77fb7edef8ecef5c48b4c23703e815be292aa7c8 Mon Sep 17 00:00:00 2001 From: avi robusta Date: Sun, 18 Jun 2023 14:33:40 +0300 Subject: refactored function --- .../prometheus/metrics_service/base_metric_service.py | 11 +++++++++++ .../prometheus/metrics_service/prometheus_metrics_service.py | 11 ----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/robusta_krr/core/integrations/prometheus/metrics_service/base_metric_service.py b/robusta_krr/core/integrations/prometheus/metrics_service/base_metric_service.py index 9ac308b..bf164bf 100644 --- a/robusta_krr/core/integrations/prometheus/metrics_service/base_metric_service.py +++ b/robusta_krr/core/integrations/prometheus/metrics_service/base_metric_service.py @@ -52,3 +52,14 @@ class MetricsService(Configurable, abc.ABC): step: datetime.timedelta = datetime.timedelta(minutes=30), ) -> ResourceHistoryData: ... + + def get_prometheus_cluster_label(self) -> str: + """ + Generates the cluster label for querying a centralized Prometheus + + Returns: + str: a promql safe label string for querying the cluster. + """ + if self.config.prometheus_cluster_label is None: + return "" + return f', {self.config.prometheus_label}="{self.config.prometheus_cluster_label}"' diff --git a/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py b/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py index da12023..3827cbe 100644 --- a/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py +++ b/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py @@ -227,14 +227,3 @@ class PrometheusMetricsService(MetricsService): for pod in related_pods if pod["metric"]["pod"] not in current_pods ] - - def get_prometheus_cluster_label(self) -> str: - """ - Generates the cluster label for querying a centralized Prometheus - - Returns: - str: a promql safe label string for querying the cluster. - """ - if self.config.prometheus_cluster_label is None: - return "" - return f', {self.config.prometheus_label}="{self.config.prometheus_cluster_label}"' \ No newline at end of file -- cgit v1.2.3