2
2
from functools import lru_cache
3
3
4
4
import requests
5
+ from dockerflow import checks
5
6
from statsd .defaults .env import statsd
6
7
7
8
from jbi import environment
8
- from jbi .common .instrument import ServiceHealth
9
9
10
10
from .client import BugzillaClient , BugzillaClientError
11
11
from .models import Bug
@@ -21,49 +21,6 @@ class BugzillaService:
21
21
def __init__ (self , client : BugzillaClient ) -> None :
22
22
self .client = client
23
23
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
-
67
24
def add_link_to_see_also (self , bug : Bug , link : str ):
68
25
"""Add link to Bugzilla ticket"""
69
26
@@ -102,3 +59,58 @@ def get_service():
102
59
settings .bugzilla_base_url , api_key = str (settings .bugzilla_api_key )
103
60
)
104
61
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