Skip to content

Commit

Permalink
Add label support
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Nov 1, 2024
1 parent cdf4940 commit 78c102c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.7
4.0.8
2 changes: 1 addition & 1 deletion ansible-imalive/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
imalive_api_version: 4.0.7
imalive_api_version: 4.0.8
imalive_port: 8099
imalive_wait_time: 300
imalive_force_recreate: true
Expand Down
15 changes: 11 additions & 4 deletions src/utils/gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from prometheus_client import Gauge
from opentelemetry.metrics import Observation

from utils.common import sanitize_metric_name
from utils.common import is_not_empty, sanitize_metric_name
from utils.otel import get_otel_meter

_numeric_value_pattern = r"-?\d+\.?\d*"
_current_gauge_values = {}

def create_gauge(name, description):
def create_gauge(name, description, labels = []):
name = sanitize_metric_name(name)
_current_gauge_values[name] = 0.0

Expand All @@ -23,14 +23,21 @@ def observable_gauge_func(_):
)

return Gauge(
name,
description,
labelnames=labels
) if is_not_empty(labels) else Gauge(
name,
description
)

def set_gauge(gauge, value):
def set_gauge(gauge, value, labels = {}):
match = re.search(_numeric_value_pattern, "{}".format(value))

if match:
val = float(match.group())
gauge.set(val)
if is_not_empty(labels) and isinstance(labels, dict):
gauge.labels(**labels).set(val)
else:
gauge.set(val)
_current_gauge_values[gauge._name] = val
29 changes: 18 additions & 11 deletions src/utils/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
def check_http_monitor(monitor, gauges):
vdate = datetime.now()

labels = {
'name': monitor['name'],
'family': monitor['family'] if is_not_empty_key(monitor, 'family') else monitor['name']
}

if monitor['type'] != 'http':
log_msg("DEBUG", {
"status": "ok",
Expand All @@ -28,7 +33,7 @@ def check_http_monitor(monitor, gauges):
"message": "Not an http monitor",
"monitor": monitor
})
set_gauge(gauges['result'], 0)
set_gauge(gauges['result'], 0, labels)
return

if is_empty_key(monitor, 'url'):
Expand All @@ -39,7 +44,7 @@ def check_http_monitor(monitor, gauges):
"message": "Missing mandatory url",
"monitor": monitor
})
set_gauge(gauges['result'], 0)
set_gauge(gauges['result'], 0, labels)
return

method = get_or_else(monitor, 'method', 'GET')
Expand All @@ -60,11 +65,11 @@ def check_http_monitor(monitor, gauges):
if method == "GET":
response = requests.get(monitor['url'], auth=auth, timeout=timeout)
duration = response.elapsed.total_seconds()
set_gauge(gauges['duration'], duration)
set_gauge(gauges['duration'], duration, labels)
elif method == "POST":
response = requests.post(monitor['url'], auth=auth, timeout=timeout)
duration = response.elapsed.total_seconds()
set_gauge(gauges['duration'], duration)
set_gauge(gauges['duration'], duration, labels)
else:
log_msg("ERROR", {
"status": "ko",
Expand All @@ -73,7 +78,7 @@ def check_http_monitor(monitor, gauges):
"message": "Not supported http method: actual = {}".format(method),
"monitor": pmonitor
})
set_gauge(gauges['result'], 0)
set_gauge(gauges['result'], 0, labels)
return

if response.status_code != expected_http_code:
Expand All @@ -85,7 +90,7 @@ def check_http_monitor(monitor, gauges):
"message": "Not expected status code: expected = {}, actual = {}".format(expected_http_code, response.status_code),
"monitor": pmonitor
})
set_gauge(gauges['result'], 0)
set_gauge(gauges['result'], 0, labels)
return

if is_not_empty(expected_contain) and expected_contain not in response.text:
Expand All @@ -97,10 +102,10 @@ def check_http_monitor(monitor, gauges):
"message": "Response not valid: expected = {}, actual = {}".format(expected_contain, response.text),
"monitor": pmonitor
})
set_gauge(gauges['result'], 0)
set_gauge(gauges['result'], 0, labels)
return

set_gauge(gauges['result'], 1)
set_gauge(gauges['result'], 1, labels)
log_msg("INFO", {
"status": "ok",
"type": "monitor",
Expand All @@ -111,18 +116,20 @@ def check_http_monitor(monitor, gauges):
})

except Exception as e:
set_gauge(gauges['result'], 0)
set_gauge(gauges['result'], 0, labels)
log_msg("ERROR", {
"status": "ko",
"type": "monitor",
"time": vdate.isoformat(),
"message": "Unexpected error",
"error": "{}".format(e),
"family"
"monitor": pmonitor
})

gauges = {}
def monitors():
labels = ['name', 'family']
def loop_monitors():
config_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..', 'imalive.yml'))
with open(config_path, "r") as stream:
Expand All @@ -132,8 +139,8 @@ def loop_monitors():
continue

gauges[monitor['name']] = {
'result': create_gauge("monitor_{}_result".format(monitor['name']), "monitor {} result".format(monitor['name'])),
'duration': create_gauge("monitor_{}_duration".format(monitor['name']), "monitor {} duration".format(monitor['name']))
'result': create_gauge("monitor_{}_result".format(monitor['name']), "monitor {} result".format(monitor['name']), labels),
'duration': create_gauge("monitor_{}_duration".format(monitor['name']), "monitor {} duration".format(monitor['name']), labels)
}

while True:
Expand Down

0 comments on commit 78c102c

Please sign in to comment.