Skip to main content

Nameko dependency provider that dispatches log data using Events (Pub-Sub).

Project description

Nameko dependency provider that dispatches log data using Events (Pub-Sub).

https://img.shields.io/pypi/v/nameko-eventlog-dispatcher.svg https://img.shields.io/pypi/pyversions/nameko-eventlog-dispatcher.svg https://img.shields.io/pypi/format/nameko-eventlog-dispatcher.svg https://travis-ci.org/sohonetlabs/nameko-eventlog-dispatcher.png?branch=master

Usage

Dispatching event log data

Include the EventLogDispatcher dependency in your service class:

from nameko.rpc import rpc
from nameko_eventlog_dispatcher import EventLogDispatcher


class FooService:

    name = 'foo'

    eventlog_dispatcher = EventLogDispatcher()

    @rpc
    def foo_method(self):
        self.eventlog_dispatcher(
          'foo_event_type', {'value': 1}, metadata={'meta': 2}
        )

Calling foo_method will dispatch an event from the foo service with log_event as the event type. However foo_event_type will be the event type stored as part of the event metadata.

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.

Then, any Nameko service will be able to handle this event.

from nameko.events import event_handler


class BarService:

    name = 'bar'

    @event_handler('foo', 'log_event')
    def foo_log_event_handler(self, body):
        """`body` will contain the event log data."""

Capturing log data when entrypoints are fired

Enable auto capture event logs in your Nameko configuration file:

# config.yaml

EVENTLOG_DISPATCHER:
  auto_capture: true
  entrypoints_to_exclude: []
  event_type: log_event

All the attributes above are optional and only used to override their default values.

With auto_capture set to true, a Nameko event will be dispatched every time an entrypoint is fired:

  • They can also be handled by listening log_event events from the service dispatching them.

  • entrypoint_fired will be the event type stored as part of the event metadata.

  • Only entrypoints listed in the ENTRYPOINT_TYPES_TO_LOG class attribute will be logged.

  • entrypoints_to_exclude can be used to provide a list of entrypoint method names to exclude when firing events automatically.

event_type can be added to the config to override the default Nameko event type used to dispatch this kind of events.

Format of the event log data

This is the format of the event log data:

{
  "service_name": "foo",
  "entrypoint_protocol": "Rpc",
  "entrypoint_name": "foo_method",
  "call_id": "foo.foo_method.d7e907ee-9425-48a6-84e6-89db19e3ce50",
  "call_stack": [
    "standalone_rpc_proxy.call.3f349ea4-ed3e-4a3b-93d0-a36fbf928ecb",
    "bla.bla_method.21d623b4-edc4-4232-9957-4fad72533b75",
    "foo.foo_method.d7e907ee-9425-48a6-84e6-89db19e3ce50"
  ],

  "event_type": "foo_event_type",  # "entrypoint_fired", ...
  "timestamp": "2017-06-12T13:48:16+00:00",

  "meta": 2,  # extra information provided as "metadata"
  "data": {"value": 1}  # extra information provided as "event_data"
}

The data attribute will contain the event data that was provided as an argument for the event_data parameter when dispatching the event.

If metadata was provided, then its elements will be included as top level attributes in the event log data.

Tests

It is assumed that RabbitMQ is up and running on the default URI guest:guest@localhost and uses the default ports. There is a Makefile target to run a RabbitMQ docker containers locally using the default ports and configuration:

$ make docker-rabbitmq-run

To run the tests locally:

$ # Create/activate a virtual environment
$ pip install tox
$ tox

There are other Makefile targets to run the tests, but extra dependencies will have to be installed:

$ pip install -U --editable ".[dev]"
$ make test
$ make coverage

A different RabbitMQ URI can be provided overriding the following environment variables: RABBIT_CTL_URI and AMQP_URI.

Additional pytest parameters can be also provided using the ARGS variable:

$ make test RABBIT_CTL_URI=http://guest:guest@dockermachine:15673 AMQP_URI=amqp://guest:guest@dockermachine:5673 ARGS='-x -vv --disable-pytest-warnings'
$ make coverage RABBIT_CTL_URI=http://guest:guest@dockermachine:15673 AMQP_URI=amqp://guest:guest@dockermachine:5673 ARGS='-x -vv --disable-pytest-warnings'

Nameko support

The following Nameko versions are supported:

  • 2.x series: 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12

Changelog

Consult the CHANGELOG document for fixes and enhancements of each version.

License

The MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nameko-eventlog-dispatcher-0.4.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file nameko-eventlog-dispatcher-0.4.1.tar.gz.

File metadata

  • Download URL: nameko-eventlog-dispatcher-0.4.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.17.3 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.4.5

File hashes

Hashes for nameko-eventlog-dispatcher-0.4.1.tar.gz
Algorithm Hash digest
SHA256 3e5274f0f2291a3b59423d2449a1815c7bd0197130082a005cfaea83d36770b8
MD5 c4511cae4652feb04c0dc0d051634154
BLAKE2b-256 a43ab839a67ad946aa13eb9ff0c3d467a274084756a47e4393a25a447d28c42f

See more details on using hashes here.

File details

Details for the file nameko_eventlog_dispatcher-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: nameko_eventlog_dispatcher-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.17.3 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.4.5

File hashes

Hashes for nameko_eventlog_dispatcher-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 428fb6a7759d712b0751d4f228bbf96018ba22ec8abbee6f312b0ca6fc340396
MD5 dcb48464ed32ede2c8750fc5f75535a2
BLAKE2b-256 9c0d0f6582ffb791f347b18a15a216a12ab9bba90663f513c5e529b71a1419bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page