Example #1
0
 def test_load_from_config(self, module_mock):
     config = testing.setUp()
     config.registry.settings = self.settings
     statsd.load_from_config(config)
     module_mock.StatsClient.assert_called_with('foo',
                                                1234,
                                                prefix='prefix')
Example #2
0
 def test_load_from_config_uses_project_name_if_defined(self, module_mock):
     config = testing.setUp()
     config.registry.settings = self.settings.copy()
     config.registry.settings['project_name'] = 'projectname'
     statsd.load_from_config(config)
     module_mock.StatsClient.assert_called_with('foo', 1234,
                                                prefix='projectname')
Example #3
0
 def test_load_from_config_uses_project_name_if_defined(self, module_mock):
     config = testing.setUp()
     config.registry.settings = self.settings.copy()
     config.registry.settings['project_name'] = 'projectname'
     statsd.load_from_config(config)
     module_mock.StatsClient.assert_called_with('foo', 1234,
                                                prefix='projectname')
Example #4
0
 def test_load_from_config_uses_project_name_if_defined(self, module_mock):
     config = testing.setUp()
     config.registry.settings = {
         **self.settings, "project_name": "projectname"
     }
     statsd.load_from_config(config)
     module_mock.StatsClient.assert_called_with("foo",
                                                1234,
                                                prefix="projectname")
Example #5
0
def setup_statsd(config):
    settings = config.get_settings()
    config.registry.statsd = None

    if settings['statsd_url']:
        client = statsd.load_from_config(config)

        config.registry.statsd = client

        client.watch_execution_time(config.registry.cache, prefix='cache')
        client.watch_execution_time(config.registry.storage, prefix='storage')
        client.watch_execution_time(config.registry.permission,
                                    prefix='permission')

        # Commit so that configured policy can be queried.
        config.commit()
        policy = config.registry.queryUtility(IAuthenticationPolicy)
        if isinstance(policy, MultiAuthenticationPolicy):
            for name, subpolicy in policy.get_policies():
                client.watch_execution_time(subpolicy,
                                            prefix='authentication',
                                            classname=name)
        else:
            client.watch_execution_time(policy, prefix='authentication')

        def on_new_response(event):
            request = event.request

            # Count unique users.
            user_id = request.prefixed_userid
            if user_id:
                client.count('users', unique=user_id)

            # Count authentication verifications.
            if hasattr(request, 'authn_type'):
                client.count('%s.%s' % ('authn_type', request.authn_type))

            # Count view calls.
            pattern = request.matched_route.pattern
            services = request.registry.cornice_services
            service = services.get(pattern)
            if service:
                client.count('view.%s.%s' % (service.name, request.method))

        config.add_subscriber(on_new_response, NewResponse)

        return client
Example #6
0
def setup_statsd(config):
    settings = config.get_settings()
    config.registry.statsd = None

    if settings['statsd_url']:
        client = statsd.load_from_config(config)

        config.registry.statsd = client

        client.watch_execution_time(config.registry.cache, prefix='cache')
        client.watch_execution_time(config.registry.storage, prefix='storage')
        client.watch_execution_time(config.registry.permission,
                                    prefix='permission')

        # Commit so that configured policy can be queried.
        config.commit()
        policy = config.registry.queryUtility(IAuthenticationPolicy)
        if isinstance(policy, MultiAuthenticationPolicy):
            for name, subpolicy in policy.get_policies():
                client.watch_execution_time(subpolicy,
                                            prefix='authentication',
                                            classname=name)
        else:
            client.watch_execution_time(policy, prefix='authentication')

        def on_new_response(event):
            request = event.request

            # Count unique users.
            user_id = request.prefixed_userid
            if user_id:
                client.count('users', unique=user_id)

            # Count authentication verifications.
            if hasattr(request, 'authn_type'):
                client.count('%s.%s' % ('authn_type', request.authn_type))

            # Count view calls.
            pattern = request.matched_route.pattern
            services = request.registry.cornice_services
            service = services.get(pattern)
            if service:
                client.count('view.%s.%s' % (service.name, request.method))

        config.add_subscriber(on_new_response, NewResponse)

        return client
Example #7
0
 def test_load_from_config(self, module_mock):
     config = testing.setUp()
     config.registry.settings = self.settings
     statsd.load_from_config(config)
     module_mock.StatsClient.assert_called_with('foo', 1234,
                                                prefix='prefix')
Example #8
0
 def test_client_instantiation_raises_properly(self):
     with self.assertRaises(ConfigurationError):
         statsd.load_from_config(mock.MagicMock())
Example #9
0
 def test_client_instantiation_raises_properly(self):
     with self.assertRaises(ConfigurationError):
         statsd.load_from_config(mock.MagicMock())
Example #10
0
 def test_load_from_config_uses_project_name_if_defined(self, module_mock):
     config = testing.setUp()
     config.registry.settings = {**self.settings, "project_name": "projectname"}
     statsd.load_from_config(config)
     module_mock.StatsClient.assert_called_with("foo", 1234, prefix="projectname")