Skip to content

Commit

Permalink
Merge branch 'main' into namspaces-filtering-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaveMyYard authored Aug 10, 2023
2 parents 0cb0a3b + ea01100 commit 90e8af6
Show file tree
Hide file tree
Showing 19 changed files with 972 additions and 836 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,38 @@ Than run the following command with PROMETHEUS_URL substituted for your Azure Ma
```sh
python krr.py simple --namespace default -p PROMETHEUS_URL --prometheus-auth-header "Bearer $AZURE_BEARER"
```
<p ><a href="#scanning-with-a-centralized-prometheus">See here about configuring labels for centralized prometheus</a></p>

<p align="right">(<a href="#readme-top">back to top</a>)</p>

## EKS managed Prometheus

For EKS managed Prometheus you need to add your prometheus link and the flag --eks-managed-prom and krr will automatically use your aws credentials

```sh
python krr.py simple -p "https://aps-workspaces.REGION.amazonaws.com/workspaces/..." --eks-managed-prom
```
Additional optional parameters are:
```sh
--eks-profile-name PROFILE_NAME_HERE # to specify the profile to use from your config
--eks-access-key ACCESS_KEY # to specify your access key
--eks-secret-key SECRET_KEY # to specify your secret key
--eks-service-name SERVICE_NAME # to use a specific service name in the signature
--eks-managed-prom-region REGION_NAME # to specify the region the prometheus is in
```
<p ><a href="#scanning-with-a-centralized-prometheus">See here about configuring labels for centralized prometheus</a></p>

<p align="right">(<a href="#readme-top">back to top</a>)</p>

## Coralogix managed Prometheus

For Coralogix managed Prometheus you need to specify your prometheus link and add the flag coralogix_token with your Logs Query Key

```sh
python krr.py simple -p "https://prom-api.coralogix..." --coralogix_token
```

<p ><a href="#scanning-with-a-centralized-prometheus">See here about configuring labels for centralized prometheus</a></p>

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
1,464 changes: 793 additions & 671 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ kubernetes = "^26.1.0"
prometheus-api-client = "^0.5.3"
numpy = "^1.24.2"
alive-progress = "^3.1.2"
prometrix = "^0.1.10"
slack-sdk = "^3.21.3"
aiostream = "^0.4.5"


[tool.poetry.group.dev.dependencies]
Expand All @@ -42,13 +45,11 @@ types-cachetools = "^5.3.0.4"
types-requests = "^2.28.11.15"
pyinstaller = "^5.9.0"
pytest = "^7.2.2"
aiostream = "^0.4.5"
slack-sdk = "^3.21.3"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"


[project]
name = "robusta_krr"
name = "robusta_krr"
9 changes: 7 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
about-time==4.2.1 ; python_version >= "3.9" and python_version < "3.12"
aiostream==0.4.5 ; python_version >= "3.9" and python_version < "3.12"
alive-progress==3.1.2 ; python_version >= "3.9" and python_version < "3.12"
boto3==1.28.21 ; python_version >= "3.9" and python_version < "3.12"
botocore==1.31.21 ; python_version >= "3.9" and python_version < "3.12"
cachetools==5.3.0 ; python_version >= "3.9" and python_version < "3.12"
certifi==2022.12.7 ; python_version >= "3.9" and python_version < "3.12"
charset-normalizer==3.0.1 ; python_version >= "3.9" and python_version < "3.12"
click==8.1.3 ; python_version >= "3.9" and python_version < "3.12"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "3.12"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "3.12" and platform_system == "Windows"
commonmark==0.9.1 ; python_version >= "3.9" and python_version < "3.12"
contourpy==1.0.7 ; python_version >= "3.9" and python_version < "3.12"
cycler==0.11.0 ; python_version >= "3.9" and python_version < "3.12"
Expand All @@ -16,6 +18,7 @@ grapheme==0.6.0 ; python_version >= "3.9" and python_version < "3.12"
httmock==1.4.0 ; python_version >= "3.9" and python_version < "3.12"
idna==3.4 ; python_version >= "3.9" and python_version < "3.12"
importlib-resources==5.12.0 ; python_version >= "3.9" and python_version < "3.10"
jmespath==1.0.1 ; python_version >= "3.9" and python_version < "3.12"
kiwisolver==1.4.4 ; python_version >= "3.9" and python_version < "3.12"
kubernetes==26.1.0 ; python_version >= "3.9" and python_version < "3.12"
matplotlib==3.7.1 ; python_version >= "3.9" and python_version < "3.12"
Expand All @@ -25,6 +28,7 @@ packaging==23.0 ; python_version >= "3.9" and python_version < "3.12"
pandas==1.5.3 ; python_version >= "3.9" and python_version < "3.12"
pillow==9.4.0 ; python_version >= "3.9" and python_version < "3.12"
prometheus-api-client==0.5.3 ; python_version >= "3.9" and python_version < "3.12"
prometrix==0.1.10 ; python_version >= "3.9" and python_version < "3.12"
pyasn1-modules==0.2.8 ; python_version >= "3.9" and python_version < "3.12"
pyasn1==0.4.8 ; python_version >= "3.9" and python_version < "3.12"
pydantic==1.10.7 ; python_version >= "3.9" and python_version < "3.12"
Expand All @@ -39,14 +43,15 @@ requests-oauthlib==1.3.1 ; python_version >= "3.9" and python_version < "3.12"
requests==2.28.2 ; python_version >= "3.9" and python_version < "3.12"
rich==12.6.0 ; python_version >= "3.9" and python_version < "3.12"
rsa==4.9 ; python_version >= "3.9" and python_version < "3.12"
s3transfer==0.6.1 ; python_version >= "3.9" and python_version < "3.12"
setuptools==67.4.0 ; python_version >= "3.9" and python_version < "3.12"
shellingham==1.5.0.post1 ; python_version >= "3.9" and python_version < "3.12"
six==1.16.0 ; python_version >= "3.9" and python_version < "3.12"
slack-sdk==3.21.3 ; python_version >= "3.9" and python_version < "3.12"
typer[all]==0.7.0 ; python_version >= "3.9" and python_version < "3.12"
typing-extensions==4.5.0 ; python_version >= "3.9" and python_version < "3.12"
tzdata==2022.7 ; python_version >= "3.9" and python_version < "3.12"
tzlocal==4.2 ; python_version >= "3.9" and python_version < "3.12"
urllib3==1.26.14 ; python_version >= "3.9" and python_version < "3.12"
websocket-client==1.5.1 ; python_version >= "3.9" and python_version < "3.12"
zipp==3.15.0 ; python_version >= "3.9" and python_version < "3.10"
slack-sdk==3.21.3 ; python_version >= "3.9" and python_version < "3.12"
2 changes: 1 addition & 1 deletion robusta_krr/core/integrations/prometheus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .loader import PrometheusMetricsLoader
from .metrics_service.prometheus_metrics_service import PrometheusDiscovery, PrometheusNotFound
from .prometheus_client import CustomPrometheusConnect, ClusterNotSpecifiedException
from .prometheus_utils import ClusterNotSpecifiedException
6 changes: 3 additions & 3 deletions robusta_krr/core/integrations/prometheus/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import datetime
from concurrent.futures import ThreadPoolExecutor
from typing import Optional, TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

from kubernetes import config as k8s_config
from kubernetes.client.api_client import ApiClient
from prometrix import MetricsNotFound, PrometheusNotFound

from robusta_krr.core.models.objects import K8sObjectData
from robusta_krr.utils.configurable import Configurable

from .metrics_service.base_metric_service import MetricsNotFound
from .metrics_service.prometheus_metrics_service import PrometheusMetricsService, PrometheusNotFound
from .metrics_service.thanos_metrics_service import ThanosMetricsService
from .metrics_service.victoria_metrics_service import VictoriaMetricsService

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

METRICS_SERVICES = {
Expand Down
4 changes: 2 additions & 2 deletions robusta_krr/core/integrations/prometheus/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .cpu import CPULoader, MaxCPULoader, PercentileCPULoader
from .memory import MemoryLoader, MaxMemoryLoader, PercentileMemoryLoader
from .base import PrometheusMetric
from .cpu import CPULoader, MaxCPULoader, PercentileCPULoader
from .memory import MaxMemoryLoader, MemoryLoader, PercentileMemoryLoader
2 changes: 1 addition & 1 deletion robusta_krr/core/integrations/prometheus/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import datetime
import enum
from concurrent.futures import ThreadPoolExecutor
from typing import Any, TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Any, Optional

import numpy as np
import pydantic as pd
Expand Down
2 changes: 1 addition & 1 deletion robusta_krr/core/integrations/prometheus/metrics/cpu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from robusta_krr.core.models.objects import K8sObjectData

from .base import QueryMetric, QueryRangeMetric, FilterMetric
from .base import FilterMetric, QueryMetric, QueryRangeMetric


class CPULoader(QueryRangeMetric, FilterMetric):
Expand Down
2 changes: 1 addition & 1 deletion robusta_krr/core/integrations/prometheus/metrics/memory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from robusta_krr.core.models.objects import K8sObjectData

from .base import QueryMetric, QueryRangeMetric, FilterMetric
from .base import FilterMetric, QueryMetric, QueryRangeMetric


class MemoryLoader(QueryRangeMetric, FilterMetric):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@
from ..metrics import PrometheusMetric


class MetricsNotFound(Exception):
"""
An exception raised when Metrics service is not found.
"""

pass


class MetricsService(Configurable, abc.ABC):
def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
import datetime
import time
from typing import List, Optional
from concurrent.futures import ThreadPoolExecutor
from typing import List, Optional

from kubernetes.client import ApiClient
from prometheus_api_client import PrometheusApiClientException
from prometrix import PrometheusNotFound, get_custom_prometheus_connect
from requests.exceptions import ConnectionError, HTTPError

from robusta_krr.core.abstract.strategies import PodsTimeData
Expand All @@ -14,8 +15,8 @@
from robusta_krr.utils.service_discovery import MetricsServiceDiscovery

from ..metrics import PrometheusMetric
from ..prometheus_client import ClusterNotSpecifiedException, CustomPrometheusConnect
from .base_metric_service import MetricsNotFound, MetricsService
from ..prometheus_utils import ClusterNotSpecifiedException, generate_prometheus_config
from .base_metric_service import MetricsService


class PrometheusDiscovery(MetricsServiceDiscovery):
Expand All @@ -41,14 +42,6 @@ def find_metrics_url(self, *, api_client: Optional[ApiClient] = None) -> Optiona
)


class PrometheusNotFound(MetricsNotFound):
"""
An exception raised when Prometheus is not found.
"""

pass


class PrometheusMetricsService(MetricsService):
"""
A class for fetching metrics from Prometheus.
Expand Down Expand Up @@ -90,29 +83,19 @@ def __init__(
headers |= {"Authorization": self.auth_header}
elif not self.config.inside_cluster and self.api_client is not None:
self.api_client.update_params_for_auth(headers, {}, ["BearerToken"])

self.prometheus = CustomPrometheusConnect(url=self.url, disable_ssl=not self.ssl_enabled, headers=headers)
self.prom_config = generate_prometheus_config(
config, url=self.url, headers=headers, metrics_service=self
)
self.prometheus = get_custom_prometheus_connect(self.prom_config)

def check_connection(self):
"""
Checks the connection to Prometheus.
Raises:
PrometheusNotFound: If the connection to Prometheus cannot be established.
"""
try:
response = self.prometheus._session.get(
f"{self.prometheus.url}/api/v1/query",
verify=self.prometheus.ssl_verification,
headers=self.prometheus.headers,
# This query should return empty results, but is correct
params={"query": "example"},
)
response.raise_for_status()
except (ConnectionError, HTTPError) as e:
raise PrometheusNotFound(
f"Couldn't connect to Prometheus found under {self.prometheus.url}\nCaused by {e.__class__.__name__}: {e})"
) from e

self.prometheus.check_prometheus_connection()

async def query(self, query: str) -> dict:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(self.executor, lambda: self.prometheus.custom_query(query=query))
Expand Down Expand Up @@ -226,7 +209,7 @@ async def load_pods(self, object: K8sObjectData, period: datetime.timedelta) ->
current_pods_set = {pod["metric"]["pod"] for pod in current_pods}
del current_pods

object.pods += [
object.pods += set([
PodData(name=pod["metric"]["pod"], deleted=pod["metric"]["pod"] not in current_pods_set)
for pod in related_pods
]
])
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Optional

from kubernetes.client import ApiClient
from prometrix import MetricsNotFound, ThanosMetricsNotFound

from robusta_krr.utils.service_discovery import MetricsServiceDiscovery

from .prometheus_metrics_service import MetricsNotFound, PrometheusMetricsService
from .prometheus_metrics_service import PrometheusMetricsService


class ThanosMetricsDiscovery(MetricsServiceDiscovery):
Expand All @@ -27,14 +28,6 @@ def find_metrics_url(self, *, api_client: Optional[ApiClient] = None) -> Optiona
)


class ThanosMetricsNotFound(MetricsNotFound):
"""
An exception raised when Thanos is not found.
"""

pass


class ThanosMetricsService(PrometheusMetricsService):
"""
A class for fetching metrics from Thanos.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Optional

from kubernetes.client import ApiClient
from prometrix import MetricsNotFound, VictoriaMetricsNotFound

from robusta_krr.utils.service_discovery import MetricsServiceDiscovery

from .prometheus_metrics_service import MetricsNotFound, PrometheusMetricsService
from .prometheus_metrics_service import PrometheusMetricsService


class VictoriaMetricsDiscovery(MetricsServiceDiscovery):
Expand All @@ -26,14 +27,6 @@ def find_metrics_url(self, *, api_client: Optional[ApiClient] = None) -> Optiona
)


class VictoriaMetricsNotFound(MetricsNotFound):
"""
An exception raised when Victoria Metrics is not found.
"""

pass


class VictoriaMetricsService(PrometheusMetricsService):
"""
A class for fetching metrics from Victoria Metrics.
Expand Down
Loading

0 comments on commit 90e8af6

Please sign in to comment.