Skip to content

Commit 9514f73

Browse files
committed
Add counter and timer for action execution (fixes #23)
1 parent b15858a commit 9514f73

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,15 @@ GET /whiteboard_tags/
218218
...
219219
}
220220
```
221+
222+
## Metrics
223+
224+
The following metrics are sent via Prometheus:
225+
226+
- `jbi_bugzilla_ignored_total`
227+
- `jbi_bugzilla_processed_total`
228+
- `jbi_action_execution_milliseconds`
229+
- `jbi_jira_methods_milliseconds`
230+
- `jbi_jira_methods_total`
231+
- `jbi_bugzilla_methods_total`
232+
- `jbi_bugzilla_methods_milliseconds`

src/jbi/runner.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@
33
"""
44
import logging
55

6+
from prometheus_client import Counter, Summary
7+
68
from src.app.environment import Settings
79
from src.jbi.bugzilla import BugzillaBug, BugzillaWebhookRequest
810
from src.jbi.errors import ActionNotFoundError, IgnoreInvalidRequestError
911
from src.jbi.models import Actions
1012

1113
logger = logging.getLogger(__name__)
1214

15+
counter_ignored = Counter("jbi_bugzilla_ignored_total", "Bugzilla WebHooks ignored")
16+
counter_processed = Counter(
17+
"jbi_bugzilla_processed_total", "Bugzilla WebHooks processed"
18+
)
19+
action_execution_timer = Summary(
20+
"jbi_action_execution_milliseconds", "Action execution duration"
21+
)
22+
1323

1424
class Operations:
1525
"""Track status of incoming requests in log entries."""
@@ -20,6 +30,7 @@ class Operations:
2030
SUCCESS = "success"
2131

2232

33+
@action_execution_timer.time()
2334
def execute_action(
2435
request: BugzillaWebhookRequest,
2536
actions: Actions,
@@ -85,11 +96,13 @@ def execute_action(
8596
bug_obj.id,
8697
extra={"operation": Operations.SUCCESS, **log_context},
8798
)
99+
counter_processed.inc()
88100
return content
89101
except IgnoreInvalidRequestError as exception:
90102
logger.debug(
91103
"Ignore incoming request: %s",
92104
exception,
93105
extra={"operation": Operations.IGNORE, **log_context},
94106
)
107+
counter_ignored.inc()
95108
raise

tests/unit/jbi/test_runner.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,35 @@ def test_execution_logging_for_ignored_requests(
130130
"Handling incoming request",
131131
"Ignore incoming request: no action matching bug whiteboard tags: foo",
132132
]
133+
134+
135+
def test_counter_is_incremented_on_ignored_requests(
136+
webhook_create_example: BugzillaWebhookRequest,
137+
actions_example: Actions,
138+
settings: Settings,
139+
):
140+
assert webhook_create_example.bug
141+
webhook_create_example.bug.whiteboard = "foo"
142+
143+
with mock.patch("src.jbi.runner.counter_ignored") as mocked:
144+
with pytest.raises(IgnoreInvalidRequestError):
145+
execute_action(
146+
request=webhook_create_example,
147+
actions=actions_example,
148+
settings=settings,
149+
)
150+
assert mocked.inc.called, "ignored counter was called"
151+
152+
153+
def test_counter_is_incremented_on_processed_requests(
154+
webhook_create_example: BugzillaWebhookRequest,
155+
actions_example: Actions,
156+
settings: Settings,
157+
):
158+
with mock.patch("src.jbi.runner.counter_processed") as mocked:
159+
execute_action(
160+
request=webhook_create_example,
161+
actions=actions_example,
162+
settings=settings,
163+
)
164+
assert mocked.inc.called, "processed counter was called"

0 commit comments

Comments
 (0)