Skip to content

Commit

Permalink
fix empty results by returning dataframe with a null value (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoptical authored Feb 28, 2024
1 parent 157a486 commit 40591c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 9 additions & 0 deletions app/helper/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from requests import RequestException
from .logger import logger
import pandas as pd
import time


class Prometheus:
Expand All @@ -30,4 +31,12 @@ def get_current_value(self, query) -> pd.DataFrame:
"non 200 response status code on query {}".format(query),
stack_info=True,
)
# handle empty results as a dummy metric with current time and none value.
if len(result) == 0:
return MetricSnapshotDataFrame(
data={
"metric": {"__name__": "dummy_metric"},
"value": [time.time(), None],
}
)
return MetricSnapshotDataFrame(result)
19 changes: 12 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
from unittest.mock import patch, Mock
from app.main import main


@patch(
"app.helper.prometheus.PrometheusConnect.check_prometheus_connection",
Mock(return_value=True),
)
@patch(
"app.helper.prometheus.PrometheusConnect.custom_query",
# It uses two outputs for the prometheus: an ordinary and an empty result.
Mock(
return_value=[
{
"metric": {"__name__": "dummy_metric"},
"value": [1708184035.052, "0.29875"],
}
]
side_effect=[
[
{
"metric": {"__name__": "dummy_metric"},
"value": [1708184035.052, "0.29875"],
}
],
[],
],
),
)
def test_main():
main(config_path="tests/config.yaml", output_path="tests/output.json")
main(config_path="tests/config.yaml", output_path="tests/output.json")

0 comments on commit 40591c3

Please sign in to comment.