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')
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['cliquet.project_name'] = 'projectname' statsd.load_from_config(config) module_mock.StatsClient.assert_called_with('foo', 1234, prefix='projectname')
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')
def handle_statsd(config): settings = config.get_settings() if settings['cliquet.statsd_url']: client = statsd.load_from_config(config) client.watch_execution_time(config.registry.cache, prefix='cache') client.watch_execution_time(config.registry.storage, prefix='storage') policy = config.registry.queryUtility(IAuthenticationPolicy) client.watch_execution_time(policy, prefix='authentication') def on_new_response(event): request = event.request # Count unique users. user_id = request.authenticated_userid if user_id: client.count('users', unique=user_id) # Count authentication verifications. if hasattr(request, 'auth_type'): client.count('%s.%s' % ('auth_type', request.auth_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
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
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) client.watch_execution_time(policy, prefix="authentication") def on_new_response(event): request = event.request # Count unique users. user_id = request.authenticated_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