-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of the refactoring to organize the codebase. This moves the methods to talk to prometheus into a class in its own module. The unit tests have also been moved to a separate file. closes #69
- Loading branch information
Showing
6 changed files
with
87 additions
and
70 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import requests | ||
import time | ||
|
||
from urllib3.util.retry import Retry | ||
from requests.adapters import HTTPAdapter | ||
from openshift_metrics.utils import EmptyResultError | ||
|
||
class PrometheusClient: | ||
def __init__(self, prometheus_url: str, token: str, step_min: int=15): | ||
self.proemtheus_url = prometheus_url | ||
self.token = token | ||
self.step_min = step_min | ||
|
||
def query_metric(self, metric, start_date, end_date): | ||
"""Queries metric from the provided proemtheus_url""" | ||
data = None | ||
headers = {"Authorization": f"Bearer {self.token}"} | ||
day_url_vars = f"start={start_date}T00:00:00Z&end={end_date}T23:59:59Z" | ||
url = f"{self.proemtheus_url}/api/v1/query_range?query={metric}&{day_url_vars}&step={self.step_min}m" | ||
|
||
retries = Retry(total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504]) | ||
session = requests.Session() | ||
session.mount("https://", HTTPAdapter(max_retries=retries)) | ||
|
||
print(f"Retrieving metric: {metric}") | ||
|
||
for _ in range(3): | ||
response = session.get(url, headers=headers, verify=True) | ||
|
||
if response.status_code != 200: | ||
print(f"{response.status_code} Response: {response.reason}") | ||
else: | ||
data = response.json()["data"]["result"] | ||
if data: | ||
break | ||
print("Empty result set") | ||
time.sleep(3) | ||
|
||
if not data: | ||
raise EmptyResultError(f"Error retrieving metric: {metric}") | ||
return data |
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,37 @@ | ||
from requests.exceptions import ConnectionError | ||
from unittest import TestCase, mock | ||
|
||
from openshift_metrics.prometheus_client import PrometheusClient | ||
|
||
class TestQueryMetric(TestCase): | ||
|
||
@mock.patch('requests.Session.get') | ||
@mock.patch('time.sleep') | ||
def test_query_metric(self, mock_sleep, mock_get): | ||
mock_response = mock.Mock(status_code=200) | ||
mock_response.json.return_value = {"data": { | ||
"result": "this is data" | ||
}} | ||
mock_get.return_value = mock_response | ||
prom_client = PrometheusClient('https://fake-url', 'fake-token') | ||
metrics = prom_client.query_metric('fake-metric', '2022-03-14', '2022-03-14') | ||
self.assertEqual(metrics, "this is data") | ||
self.assertEqual(mock_get.call_count, 1) | ||
|
||
@mock.patch('requests.Session.get') | ||
@mock.patch('time.sleep') | ||
def test_query_metric_exception(self, mock_sleep, mock_get): | ||
mock_get.return_value = mock.Mock(status_code=404) | ||
prom_client = PrometheusClient('https://fake-url', 'fake-token') | ||
self.assertRaises(Exception, prom_client.query_metric, | ||
'fake-metric', '2022-03-14', '2022-03-14') | ||
self.assertEqual(mock_get.call_count, 3) | ||
|
||
@mock.patch('requests.Session.get') | ||
@mock.patch('time.sleep') | ||
def test_query_metric_connection_error(self, mock_sleep, mock_get): | ||
mock_get.side_effect = [ConnectionError] | ||
prom_client = PrometheusClient('https://fake-url', 'fake-token') | ||
self.assertRaises(ConnectionError, prom_client.query_metric, | ||
'fake-metric', '2022-03-14', '2022-03-14') | ||
self.assertEqual(mock_get.call_count, 1) |
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