Skip to content

Commit 4604262

Browse files
authored
Fix #702: heartbeat now returns 200 on warnings (#862)
* Rewrite checks using Dockerflow checks * Make sure we return 200 on warning * Return Warning when some projects do not have permission * Improve consistency of assertions in test_router.py * Fix healthcheck on container * Replace hard-coded CPU count by OS call * Use default CPU count * Remove leftover * Use check.register_partial() when applicable, as suggested by @grahamalama * Use callable to obtain service
1 parent 5907bd2 commit 4604262

File tree

7 files changed

+428
-277
lines changed

7 files changed

+428
-277
lines changed

bin/healthcheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def check_server():
2222
hb_details = hb_response.json()
2323
# Check that pandoc is installed, but ignore other checks
2424
# like connection to Jira or Bugzilla.
25-
assert hb_details["jira"]["pandoc_install"]
25+
assert hb_details["checks"]["jira.pandoc_install"] == "ok"
2626
print("Ok")
2727

2828

jbi/bugzilla/service.py

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from functools import lru_cache
33

44
import requests
5+
from dockerflow import checks
56
from statsd.defaults.env import statsd
67

78
from jbi import environment
8-
from jbi.common.instrument import ServiceHealth
99

1010
from .client import BugzillaClient, BugzillaClientError
1111
from .models import Bug
@@ -21,49 +21,6 @@ class BugzillaService:
2121
def __init__(self, client: BugzillaClient) -> None:
2222
self.client = client
2323

24-
def check_health(self) -> ServiceHealth:
25-
"""Check health for Bugzilla Service"""
26-
logged_in = self.client.logged_in()
27-
all_webhooks_enabled = False
28-
if logged_in:
29-
all_webhooks_enabled = self._all_webhooks_enabled()
30-
31-
health: ServiceHealth = {
32-
"up": logged_in,
33-
"all_webhooks_enabled": all_webhooks_enabled,
34-
}
35-
return health
36-
37-
def _all_webhooks_enabled(self):
38-
# Check that all JBI webhooks are enabled in Bugzilla,
39-
# and report disabled ones.
40-
41-
try:
42-
jbi_webhooks = self.client.list_webhooks()
43-
except (BugzillaClientError, requests.HTTPError):
44-
return False
45-
46-
if len(jbi_webhooks) == 0:
47-
logger.info("No webhooks enabled")
48-
return True
49-
50-
for webhook in jbi_webhooks:
51-
# Report errors in each webhook
52-
statsd.gauge(f"jbi.bugzilla.webhooks.{webhook.slug}.errors", webhook.errors)
53-
# Warn developers when there are errors
54-
if webhook.errors > 0:
55-
logger.warning(
56-
"Webhook %s has %s error(s)", webhook.name, webhook.errors
57-
)
58-
if not webhook.enabled:
59-
logger.error(
60-
"Webhook %s is disabled (%s errors)",
61-
webhook.name,
62-
webhook.errors,
63-
)
64-
return False
65-
return True
66-
6724
def add_link_to_see_also(self, bug: Bug, link: str):
6825
"""Add link to Bugzilla ticket"""
6926

@@ -102,3 +59,58 @@ def get_service():
10259
settings.bugzilla_base_url, api_key=str(settings.bugzilla_api_key)
10360
)
10461
return BugzillaService(client=client)
62+
63+
64+
@checks.register(name="bugzilla.up")
65+
def check_bugzilla_connection(service=None):
66+
service = service or get_service()
67+
if not service.client.logged_in():
68+
return [checks.Error("Login fails or service down", id="bugzilla.login")]
69+
return []
70+
71+
72+
@checks.register(name="bugzilla.all_webhooks_enabled")
73+
def check_bugzilla_webhooks(service=None):
74+
service = service or get_service()
75+
76+
# Do not bother executing the rest of checks if connection fails.
77+
if messages := check_bugzilla_connection(service):
78+
return messages
79+
80+
# Check that all JBI webhooks are enabled in Bugzilla,
81+
# and report disabled ones.
82+
try:
83+
jbi_webhooks = service.list_webhooks()
84+
except (BugzillaClientError, requests.HTTPError) as e:
85+
return [
86+
checks.Error(f"Could not list webhooks ({e})", id="bugzilla.webhooks.fetch")
87+
]
88+
89+
results = []
90+
91+
if len(jbi_webhooks) == 0:
92+
results.append(
93+
checks.Warning("No webhooks enabled", id="bugzilla.webhooks.empty")
94+
)
95+
96+
for webhook in jbi_webhooks:
97+
# Report errors in each webhook
98+
statsd.gauge(f"jbi.bugzilla.webhooks.{webhook.slug}.errors", webhook.errors)
99+
# Warn developers when there are errors
100+
if webhook.errors > 0:
101+
results.append(
102+
checks.Warning(
103+
f"Webhook {webhook.name} has {webhook.errors} error(s)",
104+
id="bugzilla.webhooks.errors",
105+
)
106+
)
107+
108+
if not webhook.enabled:
109+
results.append(
110+
checks.Error(
111+
f"Webhook {webhook.name} is disabled ({webhook.errors} errors)",
112+
id="bugzilla.webhooks.disabled",
113+
)
114+
)
115+
116+
return results

0 commit comments

Comments
 (0)