Exemple #1
0
class FlaskRelayer(object):

    def __init__(self, app: Flask = None, logging_topic: str = None, kafka_hosts: str = None, **kwargs: str) -> None:
        if app:
            self.init_app(
                app,
                logging_topic,
                kafka_hosts=kafka_hosts,
                **kwargs,
            )

    def init_app(self, app: Flask, logging_topic: Any, kafka_hosts: str = None, **kwargs: str) -> None:
        kafka_hosts = kafka_hosts or app.config.get('KAFKA_HOSTS')
        self.event_relayer = Relayer(
            logging_topic,
            kafka_hosts=kafka_hosts,
            **kwargs,
        )

    def emit(self, *args: str, **kwargs: str) -> None:
        self.event_relayer.emit(*args, **kwargs)

    def emit_raw(self, *args: Any, **kwargs: Any) -> None:
        self.event_relayer.emit_raw(*args, **kwargs)

    def log(self, *args: str, **kwargs: str) -> None:
        self.event_relayer.log(*args, **kwargs)

    def flush(self, *args: str, **kwargs: str) -> None:
        self.event_relayer.flush()
Exemple #2
0
class TestRelayer(BaseTestCase):
    def setUp(self) -> None:
        super().setUp()
        self.relayer = Relayer('log', kafka_hosts='foo')

    def test_requires_kafka_hosts(self) -> None:
        self.assertRaises(ConfigurationError, Relayer, 'foo')

    def test_emit(self) -> None:
        self.relayer.emit('type', 'subtype', 'payload')

    def test_emit_with_partition_key(self) -> None:
        self.relayer.emit('type', 'subtype', 'payload', 'key')

    def test_source_not_present(self) -> None:
        relayer = Relayer('log', kafka_hosts='foo', topic_prefix='pre', topic_suffix='su')
        assert relayer.source == 'prelogsu'

    def test_source(self) -> None:
        relayer = Relayer('log', kafka_hosts='foo', source='container_1')
        assert relayer.source == 'container_1'

    def test_emit_raw(self) -> None:
        self.relayer.emit_raw('topic', {'message': 'content'}, 'key')

    def test_flush(self) -> None:
        self.relayer.flush()
        assert self.relayer._producer.flushed