Exemplo n.º 1
0
    def _flush_buffer(**kwargs):
        return_status = kwargs.get('return_status')

        if return_status:
            if is_json_serializable(return_status):
                LogLambda.info('LogLambda#return_status',
                               'return_status',
                               data=return_status)
            else:
                LogLambda.warn(
                    'LogLambda#_flush_buffer',
                    'provided return state is not json serializable')

        LogLambda.info('LogLambda#metrics',
                       'metrics',
                       data=LogLambdaMetrics.sanitized())
        LogLambdaMetrics.reset_metrics()
Exemplo n.º 2
0
 def test_sanitize_sets(self):
     obj = {
         'first': {'one', 'two'},
         'second': {'three', 'four'},
     }
     rv = LogLambdaMetrics.sanitize_sets(obj)
     self.assertTrue(isinstance(rv['first'], list))
     self.assertTrue(isinstance(rv['second'], list))
Exemplo n.º 3
0
 def test_sanitize_timestamps(self):
     obj = {
         'first': datetime.datetime.now(),
         'second': 1
     }
     rv = LogLambdaMetrics.sanitize_timestamps(obj)
     self.assertEqual(len(rv), 1)
     self.assertEqual(obj['second'], 1)
Exemplo n.º 4
0
    def test_gauge_item_not_json_serializable(self):
        LogLambdaMetrics.reset_metrics()
        obj = {'obj'}
        LogLambdaMetrics.gauge('name', obj)

        self.assertEqual(LogLambdaMetrics.metrics['gauges'].get('name'), None)
Exemplo n.º 5
0
    def test_gauge(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.gauge('name', 1)

        self.assertEqual(LogLambdaMetrics.metrics['gauges'].get('name'), 1)
Exemplo n.º 6
0
    def test_counter_object_not_int_convertible(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.counter('counter_name', 'something')

        self.assertIsNone(LogLambdaMetrics.metrics['counters'].get('counter_name'))
Exemplo n.º 7
0
    def test_counter_pre_existing_count(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.metrics['counters']['counter_name'] = 1
        LogLambdaMetrics.counter('counter_name', 1)

        self.assertTrue(LogLambdaMetrics.metrics['counters'].get('counter_name') == 2)
Exemplo n.º 8
0
 def counter(name: str, increment: int) -> None:
     """
     Wrapper to LogLambdaMetrics.counter
     """
     LogLambdaMetrics.counter(name, increment)
Exemplo n.º 9
0
    def test_sets_with_object_that_is_not_json_serializable(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.metrics['sets']['name'] = {'asdfasdf'}
        LogLambdaMetrics.sets('name', {'another set'})

        self.assertEqual(LogLambdaMetrics.metrics['sets'].get('name'), {'asdfasdf'})
Exemplo n.º 10
0
 def test_reset_metrics(self):
     LogLambdaMetrics.reset_metrics()
     self.assertEqual(LogLambdaMetrics.metrics, {'counters': {}, 'gauges': {}, 'sets': {}, 'timers': {}})
Exemplo n.º 11
0
    def test_stop_timer(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.metrics['timers']['name'] = datetime.datetime.now()
        LogLambdaMetrics.stop_timer('name')

        self.assertTrue(isinstance(LogLambdaMetrics.metrics['timers']['name'], float))
Exemplo n.º 12
0
    def test_start_timer(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.start_timer('name')

        self.assertTrue(isinstance(LogLambdaMetrics.metrics['timers']['name'], datetime.datetime))
Exemplo n.º 13
0
    def __call__(self, lambda_func):
        """
        If there are decorator arguments, __call__() is only called
        once, as part of the decoration process! You can only give
        it a single argument, which is the function object.
        """
        LogLambdaMetrics.reset_metrics()

        @functools.wraps(lambda_func)
        def log_decorator(*args, **kwargs):
            """
            Decorator function call to setup structured logger
            """
            event = kwargs.get('event', args[0])
            if event:
                event['body'] = convert_body(event.get('body'))

            # Setting the context here, it is safe to assume a 'global'
            # context setting will have no conflict since this is lambda
            # runtime specific
            context = kwargs.get('context', args[1])
            # context.structured_logger = self._set_structured_logger(context, lambda_func.__name__)
            context.structured_logger = self._set_structured_logger(
                context,
                logger_name=lambda_func.__name__,
                service_tag=self.service_tag)

            # TODO we can evaluate the entry point for an api gateway context and limit length based on its presence.
            # Currently using simplest path of removing log limit on lambda entry point to reduce overhead of
            # processing input event
            if event and event.get('source') == 'serverless-plugin-warmup':
                # Skipping serverless plugin logging
                self.debug(
                    'TRACE',
                    'serverless plugin warmup invocation, skipping processing',
                    state='Skip',
                    **kwargs)
                self._clear_structured_logger()
                return None

            self.info('TRACE', event, state='Invoked', **kwargs)
            try:
                # Capture code for serverless warmup plugin
                ret = lambda_func(*args, **kwargs)
            except Base5xxException as error:
                self.exception(error)
                ret = error.get_response(context)
                raise error
            except wavelengthBaseException as error:
                self.log_wavelength_exception(error)
                ret = error.get_response(context)
            except Exception as error:
                self.exception(error)
                ret = str(error)
                raise Base5xxException(str(error)) from error
            finally:
                self.info('TRACE', ret, state='Completed', **kwargs)
                self._flush_buffer(return_status=ret)
                self._clear_structured_logger()
            return convert_response(ret)

        return log_decorator
Exemplo n.º 14
0
 def sets(name: str, item) -> None:
     """
     Wrapper to LogLambdaMetrics.sets
     """
     LogLambdaMetrics.sets(name, item)
Exemplo n.º 15
0
 def gauge(name: str, item) -> None:
     """
     Wrapper to LogLambdaMetrics.gauge
     """
     LogLambdaMetrics.gauge(name, item)
Exemplo n.º 16
0
    def test_gauge_already_set(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.metrics['gauges']['name'] = 1
        LogLambdaMetrics.gauge('name', 'something')

        self.assertEqual(LogLambdaMetrics.metrics['gauges'].get('name'), 'something')
Exemplo n.º 17
0
    def test_sets(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.sets('name', 'something')

        self.assertEqual(LogLambdaMetrics.metrics['sets'].get('name'), {'something'})
        self.assertTrue(isinstance(LogLambdaMetrics.metrics['sets'].get('name'), set))
Exemplo n.º 18
0
    def test_stop_timer_no_start_time(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.stop_timer('name')

        self.assertIsNone(LogLambdaMetrics.metrics['timers'].get('name'))
Exemplo n.º 19
0
    def test_sets_preexistingset(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.metrics['sets']['name'] = {'asdfasdf'}
        LogLambdaMetrics.sets('name', 'something')

        self.assertEqual(LogLambdaMetrics.metrics['sets'].get('name'), {'asdfasdf', 'something'})
Exemplo n.º 20
0
    def test_stop_timer_start_time_different_class(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.metrics['timers']['name'] = 'something'
        LogLambdaMetrics.stop_timer('name')

        self.assertIsNone(LogLambdaMetrics.metrics['timers'].get('name'))
Exemplo n.º 21
0
    def test_counter(self):
        LogLambdaMetrics.reset_metrics()
        LogLambdaMetrics.counter('counter_name', 1)

        self.assertTrue(LogLambdaMetrics.metrics['counters'].get('counter_name') == 1)
Exemplo n.º 22
0
 def stop_timer(name):
     """
     Wrapper to LogLambdaMetrics.stop_timer
     """
     LogLambdaMetrics.stop_timer(name)