def test_contextmanager_args(self):
        state = {'foo': 1}

        @contextmanager
        def context_wrapper(*args, **keywords):
            state['before'] = True
            yield
            state['after'] = True

        wrapper = instrument.contextmanager_wrapper_factory(context_wrapper,
                                                            mapping={
                                                                'x': 0,
                                                                'y': 1
                                                            },
                                                            enable_if=None)

        def capture_args(x, y):
            state['x'] = x
            state['y'] = y

        wrapped = wrapper(capture_args)
        wrapped(1, 2)
        self.assertTrue(1, state.get('x'))
        self.assertTrue(2, state.get('y'))

        wrapper = instrument.contextmanager_wrapper_factory(context_wrapper,
                                                            enable_if=None)

        state.clear()
        wrapped = wrapper(capture_args)
        wrapped(1, 2)
        self.assertEqual(1, state.get('x'))
        self.assertEqual(2, state.get('y'))
    def test_contextmanager_args(self):
        state = {
            'foo': 1
        }

        @contextmanager
        def context_wrapper(*args, **keywords):
            state['before'] = True
            yield
            state['after'] = True

        wrapper = instrument.contextmanager_wrapper_factory(context_wrapper, mapping={'x': 0, 'y': 1}, enable_if=None)

        def capture_args(x, y):
            state['x'] = x
            state['y'] = y

        wrapped = wrapper(capture_args)
        wrapped(1, 2)
        self.assertTrue(1, state.get('x'))
        self.assertTrue(2, state.get('y'))

        wrapper = instrument.contextmanager_wrapper_factory(context_wrapper, enable_if=None)

        state.clear()
        wrapped = wrapper(capture_args)
        wrapped(1, 2)
        self.assertEqual(1, state.get('x'))
        self.assertEqual(2, state.get('y'))
Example #3
0
 def __init__(self):
     super(LoggingInstrumentor, self).__init__(
         {
             'logging.Logger.critical': contextmanager_wrapper_factory(increment_count('logging.critical.')),
             'logging.Logger.exception': contextmanager_wrapper_factory(increment_count('logging.exception.')),
             'logging.Logger.error': contextmanager_wrapper_factory(increment_count('logging.error.')),
             'logging.Logger.warning': contextmanager_wrapper_factory(increment_count('logging.warning.')),
         }
     )
    def test_contextmanager_wrapper(self):
        state = {
            'foo': 1
        }

        @contextmanager
        def context_wrapper(*args, **keywords):
            state['before'] = True
            yield
            state['after'] = True

        wrapper = instrument.contextmanager_wrapper_factory(context_wrapper, enable_if=None)

        wrapped = wrapper(lambda v: 'bar')
        ret = wrapped('this is ignored')
        self.assertTrue(state.get('before'))
        self.assertTrue(state.get('after'))

        def my_name_is_foo(a, b):
            return 'foo', a, b

        wrapped = wrapper(my_name_is_foo)
        ret = wrapped(1, 2)
        self.assertEqual(('foo', 1, 2), ret)
        self.assertEqual('my_name_is_foo', wrapped.__name__)
Example #5
0
def default_context_wrapper_factory(metric_name,
                                    state,
                                    mapping=None,
                                    enable_if='web',
                                    disable_if=None):
    """
    Use this default wrapper to record latency and count metrics for a code block or function
    """
    return contextmanager_wrapper_factory(
        telemetry_context_manager(metric_name), mapping, state, enable_if,
        disable_if)
            def __init__(self, subject, measured_methods, metric_name, state_name, enable_if, disable_if):
                super(MeasureWrapper, self).__init__(subject)

                # initialize all wrapped methods
                instrumented = {}
                for m in [m for m in inspect.getmembers(subject) if measured_methods is None or
                          m[0] in measured_methods]:
                    name = m[0]
                    factory = contextmanager_wrapper_factory(telemetry_context_manager(metric_name % name),
                                                             {}, state_name, enable_if, disable_if)
                    instrumented[name] = factory(m[1])
                object.__setattr__(self, '__instrumented__', instrumented)
            def __init__(self, subject, measured_methods, metric_name,
                         state_name, enable_if, disable_if):
                super(MeasureWrapper, self).__init__(subject)

                # initialize all wrapped methods
                instrumented = {}
                for m in [
                        m for m in inspect.getmembers(subject)
                        if measured_methods is None or m[0] in measured_methods
                ]:
                    name = m[0]
                    factory = contextmanager_wrapper_factory(
                        telemetry_context_manager(metric_name % name), {},
                        state_name, enable_if, disable_if)
                    instrumented[name] = factory(m[1])
                object.__setattr__(self, '__instrumented__', instrumented)
    def test_contextmanager_wrapper(self):
        state = {'foo': 1}

        @contextmanager
        def context_wrapper(*args, **keywords):
            state['before'] = True
            yield
            state['after'] = True

        wrapper = instrument.contextmanager_wrapper_factory(context_wrapper,
                                                            enable_if=None)

        wrapped = wrapper(lambda v: 'bar')
        ret = wrapped('this is ignored')
        self.assertTrue(state.get('before'))
        self.assertTrue(state.get('after'))

        def my_name_is_foo(a, b):
            return 'foo', a, b

        wrapped = wrapper(my_name_is_foo)
        ret = wrapped(1, 2)
        self.assertEqual(('foo', 1, 2), ret)
        self.assertEqual('my_name_is_foo', wrapped.__name__)
def default_context_wrapper_factory(metric_name, state, mapping=None, enable_if='web', disable_if=None):
    """
    Use this default wrapper to record latency and count metrics for a code block or function
    """
    return contextmanager_wrapper_factory(telemetry_context_manager(metric_name), mapping, state, enable_if,
                                          disable_if)