Skip to content

Commit

Permalink
Add Remote Settings to heartbeat monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Apr 20, 2023
1 parent 0d4210a commit b8c7145
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/olympia/amo/monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,24 @@ def database():
monitor_log.critical(status)

return status, None


def remotesettings():
# check Remote Settings connection

from olympia.lib.remote_settings import RemoteSettings
from olympia.constants.blocklist import REMOTE_SETTINGS_COLLECTION_MLBF

client = RemoteSettings(
settings.REMOTE_SETTINGS_WRITER_BUCKET,
REMOTE_SETTINGS_COLLECTION_MLBF,
)

status = ''
try:
client.heartbeat()
except Exception as e:
status = f'Failed to contact Remote Settings server: {e}'
monitor_log.critical(status)

return status, None
22 changes: 22 additions & 0 deletions src/olympia/amo/tests/test_monitor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django.conf import settings
from django.test.utils import override_settings

Expand Down Expand Up @@ -77,3 +79,23 @@ def test_database(self):
status, result = monitors.database()
assert status == ''
assert result is None

def test_remotesettings_success(self):
responses.add(
responses.GET,
f'{settings.REMOTE_SETTINGS_WRITER_URL}__heartbeat__',
status=200,
body=json.dumps({'check': True}),
)
obtained, _ = monitors.remotesettings()
assert obtained == ''

def test_remotesettings_fail(self):
responses.add(
responses.GET,
f'{settings.REMOTE_SETTINGS_WRITER_URL}__heartbeat__',
status=503,
body=json.dumps({'check': False}),
)
obtained, _ = monitors.remotesettings()
assert obtained == 'boom'
1 change: 1 addition & 0 deletions src/olympia/amo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def heartbeat(request):
'rabbitmq',
'signer',
'database',
'remotesettings',
]

for check in checks:
Expand Down
6 changes: 6 additions & 0 deletions src/olympia/lib/remote_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def headers(self):
b64 = b64encode(f'{self.username}:{self.password}'.encode()).decode()
return {'Content-Type': 'application/json', 'Authorization': f'Basic {b64}'}

def heartbeat(self):
url = f'{settings.REMOTE_SETTINGS_WRITER_URL}__heartbeat__'
response = requests.get(url, headers=self.headers)
response.raise_for_status()
return response.json()

def setup_test_server_auth(self):
# check if the user already exists in remote setting's accounts
host = settings.REMOTE_SETTINGS_WRITER_URL
Expand Down

0 comments on commit b8c7145

Please sign in to comment.