From 3703362068f94afc794f2985606c3aa6e3cedf45 Mon Sep 17 00:00:00 2001 From: Julio Trigo Date: Thu, 22 Jun 2017 15:18:04 +0100 Subject: [PATCH 1/2] Add metadata argument to the dispatch function --- CHANGELOG.rst | 3 +- README.rst | 9 ++-- .../eventlog_dispatcher.py | 3 +- test/interface/test_eventlog_dispatcher.py | 51 +++++++++++++++---- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4c3ad61..4f34d26 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ------------- diff --git a/README.rst b/README.rst index 4630beb..0b7a1dd 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/nameko_eventlog_dispatcher/eventlog_dispatcher.py b/nameko_eventlog_dispatcher/eventlog_dispatcher.py index 1b67dc2..e229083 100644 --- a/nameko_eventlog_dispatcher/eventlog_dispatcher.py +++ b/nameko_eventlog_dispatcher/eventlog_dispatcher.py @@ -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 {} diff --git a/test/interface/test_eventlog_dispatcher.py b/test/interface/test_eventlog_dispatcher.py index 81cf750..bad34b8 100644 --- a/test/interface/test_eventlog_dispatcher.py +++ b/test/interface/test_eventlog_dispatcher.py @@ -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): @@ -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() @@ -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() @@ -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', + } From d1b53a58738c0a7ab285efaaa05e2f81462c96f8 Mon Sep 17 00:00:00 2001 From: Julio Trigo Date: Thu, 22 Jun 2017 15:18:26 +0100 Subject: [PATCH 2/2] Improve MANIFEST --- MANIFEST.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MANIFEST.in b/MANIFEST.in index 592cd7d..8608fe5 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,5 +1,7 @@ +include CHANGELOG.rst include CONTRIBUTORS.txt include LICENSE include README.rst +global-exclude __pycache__ global-exclude *.pyc