Changed scrapper to select initial and final time

This commit is contained in:
Alberto Soutullo 2024-03-14 15:00:36 +01:00
parent b79d4c8300
commit d75eb1397e
No known key found for this signature in database
GPG Key ID: A7CAC0D8343B0387
3 changed files with 12 additions and 11 deletions

View File

@ -1,7 +1,8 @@
scrape_config:
$__rate_interval: "60s"
$__rate_interval: "121s"
step: "60s"
until_hours_ago: 1
start_scrape: "2024-03-12 16:24:00"
finish_scrape: "2024-03-12 16:30:00"
metrics_to_scrape:
- "libp2p_peers": "instance"
- "libp2p_open_streams": "instance-type-dir"

View File

@ -1,22 +1,21 @@
# Pyton Imports
import datetime
from datetime import datetime
# Having now in an external function allows us to mock it in the tests
def _get_datetime_now() -> datetime.datetime:
return datetime.datetime.now()
def _get_datetime_now() -> datetime:
return datetime.now()
def create_promql(address: str, query: str, hours_passed: int, step: int) -> str:
def create_promql(address: str, query: str, start_scrape: str, finish_scrape: str, step: int) -> str:
promql = address + "query_range?query=" + query
start = datetime.datetime.timestamp(
_get_datetime_now() - datetime.timedelta(hours=hours_passed))
now = datetime.datetime.timestamp(_get_datetime_now())
start = datetime.strptime(start_scrape, "%Y-%m-%d %H:%M:%S").timestamp()
end = datetime.strptime(finish_scrape, "%Y-%m-%d %H:%M:%S").timestamp()
promql = (promql +
"&start=" + str(start) +
"&end=" + str(now) +
"&end=" + str(end) +
"&step=" + str(step))
return promql

View File

@ -46,7 +46,8 @@ class Scrapper:
if '__rate_interval' in metric:
metric = metric.replace('$__rate_interval', scrape_config['$__rate_interval'])
promql = scrape_utils.create_promql(self._url, metric,
scrape_config['until_hours_ago'],
scrape_config['start_scrape'],
scrape_config['finish_scrape'],
scrape_config['step'])
return promql