Skip to content

Commit

Permalink
Merge pull request #5 from sohonetlabs/add-metadata-arg-to-dispatch
Browse files Browse the repository at this point in the history
Add metadata argument to dispatch
  • Loading branch information
gianchub authored Jun 23, 2017
2 parents 2e3722d + d1b53a5 commit c86a6f4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Released 2017-06-*

* Use service name as source service, instead of ``all``, when dispatching Nameko events automatically.
* Add the ability to override the default Nameko ``event_type`` (used for the event logs) in the service config.
* Add MANIFEST.in
* Add ``MANIFEST.in``
* Add a ``metadata`` argument to the ``dispatch`` function to provide extra event metadata.

Version 0.1.0
-------------
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include CHANGELOG.rst
include CONTRIBUTORS.txt
include LICENSE
include README.rst

global-exclude __pycache__
global-exclude *.pyc
9 changes: 6 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ Include the ``EventLogDispatcher`` dependency in your service class:
@rpc
def foo_method(self):
self.eventlog_dispatcher('foo_event_type', {'value': 1})
self.eventlog_dispatcher(
'foo_event_type', {'value': 1}, metadata={'meta': 2}
)
``event_type`` and some ``event_data`` (optional) will be provided as
arguments. ``event_data`` must contain JSON serializable data.
``event_type``, ``event_data`` (optional) and ``metadata`` (optional)
can be provided as arguments. Both ``event_data`` and ``metadata`` must
be dictionaries and contain JSON serializable data.

Calling ``foo_method`` will dispatch an event from the ``foo`` service
with ``log_event`` as the event type. However ``foo_event_type`` will be
Expand Down
3 changes: 2 additions & 1 deletion nameko_eventlog_dispatcher/eventlog_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def _get_dispatch(self, worker_ctx):
kwargs = self.kwargs
dispatcher = event_dispatcher(self.config, headers=headers, **kwargs)

def dispatch(event_type, event_data=None):
def dispatch(event_type, event_data=None, metadata=None):
body = self._get_base_call_info(worker_ctx)
body.update(metadata or {})
body['timestamp'] = _get_formatted_utcnow()
body['event_type'] = event_type
body['data'] = event_data or {}
Expand Down
51 changes: 42 additions & 9 deletions test/interface/test_eventlog_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ def dummy_entrypoint(self):
pass

@dummy
def log_event_method(self, event_data=None):
def log_event_method(self, event_data=None, metadata=None):
if event_data:
self.eventlog_dispatcher('my_event_type', event_data)
if metadata:
self.eventlog_dispatcher(
'my_event_type', event_data, metadata
)
else:
self.eventlog_dispatcher('my_event_type', event_data)
else:
self.eventlog_dispatcher('my_event_type')
if metadata:
self.eventlog_dispatcher('my_event_type', metadata=metadata)
else:
self.eventlog_dispatcher('my_event_type')

@event_handler('test_service', 'log_event')
def log_event_handler(self, body):
Expand Down Expand Up @@ -168,9 +176,7 @@ def test_http_call_when_not_set_to_auto_capture(
def test_dummy_call(
self, container_factory, config, utcnow_mock, auto_capture
):
config['EVENTLOG_DISPATCHER'] = {
'auto_capture': auto_capture
}
config['EVENTLOG_DISPATCHER'] = {'auto_capture': auto_capture}
container = container_factory(TestService, config)
container.start()

Expand Down Expand Up @@ -233,9 +239,7 @@ def test_dispatch_event(
self, container_factory, config, utcnow_mock, auto_capture, event_data,
expected_data
):
config['EVENTLOG_DISPATCHER'] = {
'auto_capture': auto_capture
}
config['EVENTLOG_DISPATCHER'] = {'auto_capture': auto_capture}
container = container_factory(TestService, config)
container.start()

Expand Down Expand Up @@ -289,3 +293,32 @@ def test_dispatch_event_with_custom_generic_event_type(
'timestamp': '2017-05-08T15:22:43+00:00',
'event_type': 'my_event_type',
}

def test_dispatch_event_with_additional_metadata(
self, container_factory, config, utcnow_mock
):
container = container_factory(TestService, config)
container.start()

with entrypoint_waiter(
container, 'log_event_handler', timeout=1
) as result:
with entrypoint_hook(container, 'log_event_method') as log_event:
log_event({'a': 1}, {'b': 2})

expected_body = result.get()
call_id = expected_body.pop('call_id')
call_stack = expected_body.pop('call_stack')

assert call_id.startswith('test_service.log_event_method.')
assert len(call_stack) == 1
assert call_stack[0].startswith('test_service.log_event_method.')
assert expected_body == {
'data': {'a': 1},
'b': 2,
'entrypoint_name': 'log_event_method',
'entrypoint_protocol': 'Entrypoint',
'service_name': 'test_service',
'timestamp': '2017-05-08T15:22:43+00:00',
'event_type': 'my_event_type',
}

0 comments on commit c86a6f4

Please sign in to comment.