def __init__(
        self,
        event_dispatcher,
        logger=None,
        start_on_init=False,
        event_queue=None,
        batch_size=None,
        flush_interval=None,
        timeout_interval=None,
        notification_center=None,
    ):
        """ BatchEventProcessor init method to configure event batching.

    Args:
      event_dispatcher: Provides a dispatch_event method which if given a URL and params sends a request to it.
      logger: Optional component which provides a log method to log messages. By default nothing would be logged.
      start_on_init: Optional boolean param which starts the consumer thread if set to True.
                     Default value is False.
      event_queue: Optional component which accumulates the events until dispacthed.
      batch_size: Optional param which defines the upper limit on the number of events in event_queue after which
                  the event_queue will be flushed.
      flush_interval: Optional floating point number representing time interval in seconds after which event_queue will
                      be flushed.
      timeout_interval: Optional floating point number representing time interval in seconds before joining the consumer
                        thread.
      notification_center: Optional instance of notification_center.NotificationCenter.
    """
        self.event_dispatcher = event_dispatcher or default_event_dispatcher
        self.logger = _logging.adapt_logger(logger or _logging.NoOpLogger())
        self.event_queue = event_queue or queue.Queue(
            maxsize=self._DEFAULT_QUEUE_CAPACITY)
        self.batch_size = (batch_size if self._validate_instantiation_props(
            batch_size, 'batch_size', self._DEFAULT_BATCH_SIZE) else
                           self._DEFAULT_BATCH_SIZE)
        self.flush_interval = (timedelta(
            seconds=flush_interval) if self._validate_instantiation_props(
                flush_interval, 'flush_interval', self._DEFAULT_FLUSH_INTERVAL)
                               else timedelta(
                                   seconds=self._DEFAULT_FLUSH_INTERVAL))
        self.timeout_interval = (timedelta(
            seconds=timeout_interval) if self._validate_instantiation_props(
                timeout_interval, 'timeout_interval',
                self._DEFAULT_TIMEOUT_INTERVAL) else timedelta(
                    seconds=self._DEFAULT_TIMEOUT_INTERVAL))

        self.notification_center = notification_center or _notification_center.NotificationCenter(
            self.logger)
        self._current_batch = list()

        if not validator.is_notification_center_valid(
                self.notification_center):
            self.logger.error(
                enums.Errors.INVALID_INPUT.format('notification_center'))
            self.logger.debug('Creating notification center for use.')
            self.notification_center = _notification_center.NotificationCenter(
                self.logger)

        self.executor = None
        if start_on_init is True:
            self.start()
Esempio n. 2
0
    def __init__(self, event_dispatcher, logger=None, notification_center=None):
        """ ForwardingEventProcessor init method to configure event dispatching.

    Args:
      event_dispatcher: Provides a dispatch_event method which if given a URL and params sends a request to it.
      logger: Optional component which provides a log method to log messages. By default nothing would be logged.
      notification_center: Optional instance of notification_center.NotificationCenter.
    """
        self.event_dispatcher = event_dispatcher or default_event_dispatcher
        self.logger = _logging.adapt_logger(logger or _logging.NoOpLogger())
        self.notification_center = notification_center or _notification_center.NotificationCenter(self.logger)

        if not validator.is_notification_center_valid(self.notification_center):
            self.logger.error(enums.Errors.INVALID_INPUT.format('notification_center'))
            self.notification_center = _notification_center.NotificationCenter()
Esempio n. 3
0
    def test_adapt_logger__noop(self):
        """Test that adapt_logger returns a standard python logger from a NoOpLogger."""
        noop_logger = _logger.NoOpLogger()
        standard_logger = _logger.adapt_logger(noop_logger)

        # adapt_logger knows about the loggers attached to this class.
        self.assertIs(noop_logger.logger, standard_logger)

        # Verify properties of the logger
        self.assertIsInstance(standard_logger, logging.Logger)
        self.assertEqual('optimizely.logger.NoOpLogger', standard_logger.name)
        self.assertEqual(logging.NOTSET, standard_logger.level)

        # Should have a single NullHandler (with a default formatter).
        self.assertEqual(1, len(standard_logger.handlers))
        handler = standard_logger.handlers[0]
        self.assertIsInstance(handler, logging.NullHandler)
        self.assertEqual(
            '%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s:%(message)s',
            handler.formatter._fmt)
Esempio n. 4
0
  def test_adapt_logger__noop(self):
    """Test that adapt_logger returns a standard python logger from a NoOpLogger."""
    noop_logger = _logger.NoOpLogger()
    standard_logger = _logger.adapt_logger(noop_logger)

    # adapt_logger knows about the loggers attached to this class.
    self.assertIs(noop_logger.logger, standard_logger)

    # Verify properties of the logger
    self.assertIsInstance(standard_logger, logging.Logger)
    self.assertEqual('optimizely.logger.NoOpLogger', standard_logger.name)
    self.assertEqual(logging.NOTSET, standard_logger.level)

    # Should have a single NullHandler (with a default formatter).
    self.assertEqual(1, len(standard_logger.handlers))
    handler = standard_logger.handlers[0]
    self.assertIsInstance(handler, logging.NullHandler)
    self.assertEqual(
      '%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s:%(message)s',
      handler.formatter._fmt
    )
Esempio n. 5
0
  def test_adapt_logger__simple(self):
    """Test that adapt_logger returns a standard python logger from a SimpleLogger."""
    simple_logger = _logger.SimpleLogger()
    standard_logger = _logger.adapt_logger(simple_logger)

    # adapt_logger knows about the loggers attached to this class.
    self.assertIs(simple_logger.logger, standard_logger)

    # Verify the standard properties of the logger.
    self.assertIsInstance(standard_logger, logging.Logger)
    self.assertEqual('optimizely.logger.SimpleLogger', standard_logger.name)
    self.assertEqual(logging.INFO, standard_logger.level)

    # Should have a single StreamHandler with our default formatting.
    self.assertEqual(1, len(standard_logger.handlers))
    handler = standard_logger.handlers[0]
    self.assertIsInstance(handler, logging.StreamHandler)
    self.assertEqual(
      '%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s:%(message)s',
      handler.formatter._fmt
    )
Esempio n. 6
0
    def test_adapt_logger__simple(self):
        """Test that adapt_logger returns a standard python logger from a SimpleLogger."""
        simple_logger = _logger.SimpleLogger()
        standard_logger = _logger.adapt_logger(simple_logger)

        # adapt_logger knows about the loggers attached to this class.
        self.assertIs(simple_logger.logger, standard_logger)

        # Verify the standard properties of the logger.
        self.assertIsInstance(standard_logger, logging.Logger)
        self.assertEqual('optimizely.logger.SimpleLogger',
                         standard_logger.name)
        self.assertEqual(logging.INFO, standard_logger.level)

        # Should have a single StreamHandler with our default formatting.
        self.assertEqual(1, len(standard_logger.handlers))
        handler = standard_logger.handlers[0]
        self.assertIsInstance(handler, logging.StreamHandler)
        self.assertEqual(
            '%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s:%(message)s',
            handler.formatter._fmt)
Esempio n. 7
0
 def test_adapt_logger__unknown(self):
     """Test that adapt_logger gives back things it can't adapt."""
     obj = object()
     value = _logger.adapt_logger(obj)
     self.assertIs(obj, value)
Esempio n. 8
0
 def test_adapt_logger__standard_logger(self):
     """Test that adapt_logger does nothing to standard python loggers."""
     logger_name = str(uuid.uuid4())
     standard_logger = logging.getLogger(logger_name)
     adapted = _logger.adapt_logger(standard_logger)
     self.assertIs(standard_logger, adapted)
Esempio n. 9
0
 def test_adapt_logger__unknown(self):
   """Test that adapt_logger gives back things it can't adapt."""
   obj = object()
   value = _logger.adapt_logger(obj)
   self.assertIs(obj, value)
Esempio n. 10
0
 def test_adapt_logger__standard_logger(self):
   """Test that adapt_logger does nothing to standard python loggers."""
   logger_name = str(uuid.uuid4())
   standard_logger = logging.getLogger(logger_name)
   adapted = _logger.adapt_logger(standard_logger)
   self.assertIs(standard_logger, adapted)