Skip to content

Commit

Permalink
Merge branch 'main' of github.com:robusta-dev/krr
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaveMyYard committed Apr 2, 2024
2 parents 1e7f0aa + c3dccb9 commit d0e531b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
4 changes: 2 additions & 2 deletions robusta_krr/core/integrations/prometheus/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
from .metrics_service.prometheus_metrics_service import PrometheusMetricsService
from .metrics_service.thanos_metrics_service import ThanosMetricsService
from .metrics_service.victoria_metrics_service import VictoriaMetricsService
from .metrics_service.mimir_metrics_service import MimirMetricsService

if TYPE_CHECKING:
from robusta_krr.core.abstract.strategies import BaseStrategy, MetricsPodData

logger = logging.getLogger("krr")


class PrometheusMetricsLoader:
def __init__(self, *, cluster: Optional[str] = None) -> None:
"""
Expand Down Expand Up @@ -56,7 +56,7 @@ def get_metrics_service(
metrics_to_check = [PrometheusMetricsService]
else:
logger.info("No Prometheus URL is specified, trying to auto-detect a metrics service")
metrics_to_check = [VictoriaMetricsService, ThanosMetricsService, PrometheusMetricsService]
metrics_to_check = [VictoriaMetricsService, ThanosMetricsService, MimirMetricsService, PrometheusMetricsService]

for metric_service_class in metrics_to_check:
service_name = metric_service_class.name()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Optional

from kubernetes.client import ApiClient
from prometrix import MetricsNotFound

from robusta_krr.utils.service_discovery import MetricsServiceDiscovery

from .prometheus_metrics_service import PrometheusMetricsService

class MimirMetricsDiscovery(MetricsServiceDiscovery):
def find_metrics_url(self, *, api_client: Optional[ApiClient] = None) -> Optional[str]:
"""
Finds the Mimir Metrics URL using selectors.
Args:
api_client (Optional[ApiClient]): A Kubernetes API client. Defaults to None.
Returns:
Optional[str]: The discovered Mimir Metrics URL, or None if not found.
"""
return super().find_url(
selectors=[
"app.kubernetes.io/name=mimir,app.kubernetes.io/component=query-frontend",
]
)


class MimirMetricsService(PrometheusMetricsService):
"""
A class for fetching metrics from Mimir Metrics.
"""

service_discovery = MimirMetricsDiscovery
url_postfix = "/prometheus"
additional_headers = {"X-Scope-OrgID": "anonymous"}

def check_connection(self):
"""
Checks the connection to Prometheus.
Raises:
MimirMetricsNotFound: If the connection to Mimir Metrics cannot be established.
"""
try:
super().check_connection()
except MetricsNotFound as e:
# This is to clarify which metrics service had the issue and not say its a prometheus issue
raise MetricsNotFound(
f"Couldn't connect to Mimir Metrics found under {self.prometheus.url}\nCaused by {e.__class__.__name__}: {e})"
) from e
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class PrometheusMetricsService(MetricsService):
"""

service_discovery: type[MetricsServiceDiscovery] = PrometheusDiscovery
url_postfix: str = ""
additional_headers: dict[str, str] = {}

def __init__(
self,
Expand Down Expand Up @@ -86,9 +88,12 @@ def __init__(
f"{self.name()} instance could not be found while scanning in {self.cluster} cluster."
)

self.url += self.url_postfix

logger.info(f"Using {self.name()} at {self.url} for cluster {cluster or 'default'}")

headers = settings.prometheus_other_headers
headers |= self.additional_headers

if self.auth_header:
headers |= {"Authorization": self.auth_header}
Expand Down
5 changes: 2 additions & 3 deletions robusta_krr/core/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,10 @@ def logging_console(self) -> Console:

def load_kubeconfig(self) -> None:
try:
config.load_incluster_config()
except ConfigException:
config.load_kube_config(config_file=self.kubeconfig, context=self.context)
self.inside_cluster = False
else:
except ConfigException:
config.load_incluster_config()
self.inside_cluster = True

def get_kube_client(self, context: Optional[str] = None):
Expand Down

0 comments on commit d0e531b

Please sign in to comment.