Skip to content

Commit 97e0241

Browse files
authored
Merge pull request #4536 from StackStorm/notifier_service_instrumentation
Add some metric instrumentation to the notifer service code
2 parents 30f5d0f + 5e80f53 commit 97e0241

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Added
2626

2727
NOTE: Those options are only supported when using a default and officially supported AMQP backend
2828
with RabbitMQ server. (new feature) #4541
29+
* Add metrics instrumentation to the ``st2notifier`` service. For the available / exposed metrics,
30+
please refer to https://docs.stackstorm.com/reference/metrics.html. (improvement) #4536
2931

3032
Changed
3133
~~~~~~~

st2actions/st2actions/notifier/notifier.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
from st2common.constants.keyvalue import FULL_SYSTEM_SCOPE, SYSTEM_SCOPE, DATASTORE_PARENT_SCOPE
4646
from st2common.services.keyvalues import KeyValueLookup
4747
from st2common.transport.queues import NOTIFIER_ACTIONUPDATE_WORK_QUEUE
48+
from st2common.metrics.base import CounterWithTimer
49+
from st2common.metrics.base import Timer
4850

4951
__all__ = [
5052
'Notifier',
@@ -73,6 +75,7 @@ def __init__(self, connection, queues, trigger_dispatcher=None):
7375
pack=ACTION_TRIGGER_TYPE['pack'],
7476
name=ACTION_TRIGGER_TYPE['name'])
7577

78+
@CounterWithTimer(key='notifier.action.executions')
7679
def process(self, execution_db):
7780
execution_id = str(execution_db.id)
7881
extra = {'execution': execution_db}
@@ -86,12 +89,18 @@ def process(self, execution_db):
8689
# action execution will be applied by the workflow engine. A policy may affect the
8790
# final state of the action execution thereby impacting the state of the workflow.
8891
if not workflow_service.is_action_execution_under_workflow_context(execution_db):
89-
policy_service.apply_post_run_policies(liveaction_db)
92+
with CounterWithTimer(key='notifier.apply_post_run_policies'):
93+
policy_service.apply_post_run_policies(liveaction_db)
9094

91-
if liveaction_db.notify is not None:
92-
self._post_notify_triggers(liveaction_db=liveaction_db, execution_db=execution_db)
95+
if liveaction_db.notify:
96+
with CounterWithTimer(key='notifier.notify_trigger.post'):
97+
self._post_notify_triggers(liveaction_db=liveaction_db,
98+
execution_db=execution_db)
9399

94-
self._post_generic_trigger(liveaction_db=liveaction_db, execution_db=execution_db)
100+
if cfg.CONF.action_sensor.enable:
101+
with CounterWithTimer(key='notifier.generic_trigger.post'):
102+
self._post_generic_trigger(liveaction_db=liveaction_db,
103+
execution_db=execution_db)
95104

96105
def _get_execution_for_liveaction(self, liveaction):
97106
execution = ActionExecution.get(liveaction__id=str(liveaction.id))
@@ -127,7 +136,7 @@ def _post_notify_subsection_triggers(self, liveaction_db=None, execution_db=None
127136
notify_subsection=None,
128137
default_message_suffix=None):
129138
routes = (getattr(notify_subsection, 'routes') or
130-
getattr(notify_subsection, 'channels', None))
139+
getattr(notify_subsection, 'channels', [])) or []
131140

132141
execution_id = str(execution_db.id)
133142

@@ -142,13 +151,15 @@ def _post_notify_subsection_triggers(self, liveaction_db=None, execution_db=None
142151
)
143152

144153
try:
145-
message = self._transform_message(message=message,
146-
context=jinja_context)
154+
with Timer(key='notifier.transform_message'):
155+
message = self._transform_message(message=message,
156+
context=jinja_context)
147157
except:
148158
LOG.exception('Failed (Jinja) transforming `message`.')
149159

150160
try:
151-
data = self._transform_data(data=data, context=jinja_context)
161+
with Timer(key='notifier.transform_data'):
162+
data = self._transform_data(data=data, context=jinja_context)
152163
except:
153164
LOG.exception('Failed (Jinja) transforming `data`.')
154165

@@ -187,8 +198,10 @@ def _post_notify_subsection_triggers(self, liveaction_db=None, execution_db=None
187198
payload['channel'] = route
188199
LOG.debug('POSTing %s for %s. Payload - %s.', NOTIFY_TRIGGER_TYPE['name'],
189200
liveaction_db.id, payload)
190-
self._trigger_dispatcher.dispatch(self._notify_trigger, payload=payload,
191-
trace_context=trace_context)
201+
202+
with CounterWithTimer(key='notifier.notify_trigger.dispatch'):
203+
self._trigger_dispatcher.dispatch(self._notify_trigger, payload=payload,
204+
trace_context=trace_context)
192205
except:
193206
failed_routes.append(route)
194207

@@ -254,8 +267,10 @@ def _post_generic_trigger(self, liveaction_db=None, execution_db=None):
254267
trace_context = self._get_trace_context(execution_id=execution_id)
255268
LOG.debug('POSTing %s for %s. Payload - %s. TraceContext - %s',
256269
ACTION_TRIGGER_TYPE['name'], liveaction_db.id, payload, trace_context)
257-
self._trigger_dispatcher.dispatch(self._action_trigger, payload=payload,
258-
trace_context=trace_context)
270+
271+
with CounterWithTimer(key='notifier.generic_trigger.dispatch'):
272+
self._trigger_dispatcher.dispatch(self._action_trigger, payload=payload,
273+
trace_context=trace_context)
259274

260275
def _get_runner_ref(self, action_ref):
261276
"""

0 commit comments

Comments
 (0)