-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-linting-errors
- Loading branch information
Showing
6 changed files
with
103 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "robusta-krr" | ||
version = "1.3.0-dev" | ||
version = "1.3.2-dev" | ||
description = "Robusta's Resource Recommendation engine for Kubernetes" | ||
authors = ["Pavel Zhukov <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .main import run | ||
|
||
__version__ = "1.3.0-dev" | ||
__version__ = "1.3.2-dev" | ||
__all__ = ["run", "__version__"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
from .loader import MetricsLoader | ||
from .metrics_service.prometheus_metrics_service import ( | ||
ClusterNotSpecifiedException, | ||
CustomPrometheusConnect, | ||
PrometheusDiscovery, | ||
PrometheusNotFound, | ||
) | ||
from .metrics_service.prometheus_metrics_service import PrometheusDiscovery, PrometheusNotFound | ||
from .prometheus_client import CustomPrometheusConnect, ClusterNotSpecifiedException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
robusta_krr/core/integrations/prometheus/prometheus_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from typing import no_type_check | ||
|
||
import requests | ||
from datetime import datetime | ||
from prometheus_api_client import PrometheusConnect, Retry, PrometheusApiClientException | ||
from requests.adapters import HTTPAdapter | ||
|
||
|
||
class ClusterNotSpecifiedException(Exception): | ||
""" | ||
An exception raised when a prometheus requires a cluster label but an invalid one is provided. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class CustomPrometheusConnect(PrometheusConnect): | ||
""" | ||
Custom PrometheusConnect class to handle retries. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
url: str = "http://127.0.0.1:9090", | ||
headers: dict = None, | ||
disable_ssl: bool = False, | ||
retry: Retry = None, | ||
auth: tuple = None, | ||
): | ||
super().__init__(url, headers, disable_ssl, retry, auth) | ||
self._session = requests.Session() | ||
self._session.mount(self.url, HTTPAdapter(max_retries=retry, pool_maxsize=10, pool_block=True)) | ||
|
||
def custom_query(self, query: str, params: dict = None): | ||
params = params or {} | ||
data = None | ||
query = str(query) | ||
# using the query API to get raw data | ||
response = self._session.post( | ||
"{0}/api/v1/query".format(self.url), | ||
data={"query": query, **params}, | ||
verify=self.ssl_verification, | ||
headers=self.headers, | ||
auth=self.auth, | ||
) | ||
if response.status_code == 200: | ||
data = response.json()["data"]["result"] | ||
else: | ||
raise PrometheusApiClientException( | ||
"HTTP Status Code {} ({!r})".format(response.status_code, response.content) | ||
) | ||
|
||
return data | ||
|
||
def custom_query_range( | ||
self, | ||
query: str, | ||
start_time: datetime, | ||
end_time: datetime, | ||
step: str, | ||
params: dict = None, | ||
): | ||
start = round(start_time.timestamp()) | ||
end = round(end_time.timestamp()) | ||
params = params or {} | ||
data = None | ||
query = str(query) | ||
# using the query_range API to get raw data | ||
response = self._session.post( | ||
"{0}/api/v1/query_range".format(self.url), | ||
data={ | ||
"query": query, | ||
"start": start, | ||
"end": end, | ||
"step": step, | ||
**params, | ||
}, | ||
verify=self.ssl_verification, | ||
headers=self.headers, | ||
auth=self.auth, | ||
) | ||
if response.status_code == 200: | ||
data = response.json()["data"]["result"] | ||
else: | ||
raise PrometheusApiClientException( | ||
"HTTP Status Code {} ({!r})".format(response.status_code, response.content) | ||
) | ||
return data |