Skip to content

Commit

Permalink
Add header support monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Nov 4, 2024
1 parent a6aae19 commit 96fdf96
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ monitors:
timeout: 30 # optional (30 seconds if not present)
username: changeit # optional (no basic auth if not present)
password: changerit # optional (no basic auth if not present)
headers: # optional (no headers if empty)
- name: Accept
value: application/json
```

## Development / contributions
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.9
4.0.10
29 changes: 29 additions & 0 deletions src/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import re

from unittest import TestCase

from utils.common import sanitize_header_name

class TestCommon(TestCase):
def init(self, *args, **kwargs):
super(TestCommon, self).__init__(*args, **kwargs)

def test_sanitize_header_name_accept(self):
## Given
header = "accept"

#When
result = sanitize_header_name(header)

## Then
self.assertEqual("Accept", result)

def test_sanitize_header_name_contenttype(self):
## Given
header = "content-type"

#When
result = sanitize_header_name(header)

## Then
self.assertEqual("Content-Type", result)
3 changes: 3 additions & 0 deletions src/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ def is_not_uuid (var):
_allowed_chars_metric_pattern = re.compile(r'[^a-zA-Z0-9]')
def sanitize_metric_name(name: str):
return re.sub(_allowed_chars_metric_pattern, '_', name)

def sanitize_header_name(name: str) -> str:
return '-'.join(word.capitalize() for word in name.split('-'))
12 changes: 9 additions & 3 deletions src/utils/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from time import sleep
from requests.auth import HTTPBasicAuth

from utils.common import is_empty_key, get_or_else, is_not_empty, is_not_empty_key, del_key_if_exists
from utils.common import is_empty_key, get_or_else, is_not_empty, is_not_empty_key, del_key_if_exists, sanitize_header_name
from utils.gauge import create_gauge, set_gauge
from utils.heartbit import WAIT_TIME
from utils.logger import log_msg
Expand Down Expand Up @@ -53,21 +53,27 @@ def check_http_monitor(monitor, gauges):
expected_contain = get_or_else(monitor, 'expected_contain', None)
duration = None
auth = None
headers = {}

if is_not_empty_key(monitor, 'username') and is_not_empty_key(monitor, 'password'):
auth = HTTPBasicAuth(monitor['username'], monitor['password'])

if is_not_empty_key(monitor, 'headers'):
for header in monitor['headers']:
if is_not_empty_key(header, 'name') and is_not_empty_key(header, 'value'):
headers[sanitize_header_name(header['name'])] = header['value']

pmonitor = monitor.copy()
del_key_if_exists(pmonitor, 'username')
del_key_if_exists(pmonitor, 'password')

try:
if method == "GET":
response = requests.get(monitor['url'], auth=auth, timeout=timeout)
response = requests.get(monitor['url'], auth=auth, headers=headers, timeout=timeout)
duration = response.elapsed.total_seconds()
set_gauge(gauges['duration'], duration, {**labels, 'kind': 'duration'})
elif method == "POST":
response = requests.post(monitor['url'], auth=auth, timeout=timeout)
response = requests.post(monitor['url'], auth=auth, headers=headers, timeout=timeout)
duration = response.elapsed.total_seconds()
set_gauge(gauges['duration'], duration, {**labels, 'kind': 'duration'})
else:
Expand Down

0 comments on commit 96fdf96

Please sign in to comment.