How to use the rele.middleware.BaseMiddleware function in rele

To help you get started, we’ve selected a few rele examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mercadona / rele / rele / contrib / logging_middleware.py View on Github external
import logging
import time

from rele.middleware import BaseMiddleware


class LoggingMiddleware(BaseMiddleware):
    """Default logging middleware.

    Logging format has been configured for Prometheus.
    """

    def __init__(self):
        self._logger = None

    def setup(self, config, **kwargs):
        self._logger = logging.getLogger(__name__)
        self._app_name = config.app_name

    def _build_data_metrics(
        self, subscription, message, status, start_processing_time=None
    ):
        result = {
github mercadona / rele / rele / contrib / django_db_middleware.py View on Github external
from django import db

from rele.middleware import BaseMiddleware


class DjangoDBMiddleware(BaseMiddleware):
    """Django specific middleware for managing database connections.
    """

    def pre_process_message(self, *args):
        db.close_old_connections()

    def post_process_message(self):
        db.close_old_connections()

    def post_worker_stop(self):
        db.connections.close_all()
github mercadona / rele / rele / contrib / apm_middleware.py View on Github external
from elasticapm import Client
from elasticapm.contrib.opentracing import Tracer
from opentracing import Format

from rele.middleware import BaseMiddleware

ELASTIC_APM_TRACE_PARENT = 'elastic-apm-traceparent'
logger = logging.getLogger(__name__)


class Carrier(dict):
    def get_trace_parent(self):
        return str(self.get(ELASTIC_APM_TRACE_PARENT), "utf-8")


class APMMiddleware(BaseMiddleware):
    _tracer = None
    _carrier = None

    def setup(self, config):
        try:
            apm_client = Client({'SERVICE_NAME': config.gc_project_id})
            self._tracer = Tracer(apm_client)
            self._carrier = Carrier()
        except Exception as e:
            logger.warning(f'APM client could not be initialized. {e}')

    def pre_publish(self, topic, data, attrs):
        try:
            scope = self._tracer.start_active_span(topic, finish_on_close=False)
            self._inject_trace_parent(attrs, scope)
        except Exception as e: