def process_web_request(self, request, path, params, fragment): options = dict(command_processor.get_global_options()) options.update(params) catalog = spectator_client.get_source_catalog(options) type_map, service_tag_map, active_services = ( self.__get_type_and_tag_map_and_active_services(catalog, options)) params = strip_non_html_params(options) html = self.to_html(type_map, service_tag_map, active_services, params) html_doc = http_server.build_html_document(html, title='Metric Usage') request.respond(200, {'ContentType': 'text/html'}, html_doc)
def process_commandline_request(self, options): catalog = spectator_client.get_source_catalog( config_dir=options['config_dir']) type_map, service_tag_map, active_services = ( self.__get_type_and_tag_map_and_active_services( catalog, options)) params = strip_non_html_params(options) html = self.to_html(type_map, service_tag_map, active_services, params) html_doc = http_server.build_html_document( html, title='Metric Usage') self.output(options, html_doc)
def test_get_source_catalog(self, mock_getmtime, mock_glob): mock_getmtime.return_value = 1234 mock_glob.return_value = ['one.conf', 'two.conf'] mo = mock.mock_open(read_data='metrics_url: http://testhost:1122') mo.side_effect = ( mo.return_value, mock.mock_open( read_data='metrics_url: http://testhost:3344').return_value) with patch('spectator_client.open', mo, create=True): catalog = spectator_client.get_source_catalog( config_dir='TestConfig') self.assertEqual( { 'one': { 'metrics_url': ['http://testhost:1122'] }, 'two': { 'metrics_url': ['http://testhost:3344'] } }, catalog) mock_getmtime.assert_called_with('TestConfig/sources') mock_glob.assert_called_with('TestConfig/sources/*.conf') self.assertEquals(1, mock_getmtime.call_count) # Verify that we dont rescan content if timestamp hasnt changed. again = spectator_client.get_source_catalog('TestConfig') self.assertEquals(catalog, again) self.assertEquals(2, mock_getmtime.call_count) self.assertEquals(1, mock_glob.call_count) # Verify that we query again if timestamp changes mock_getmtime.return_value = 1235 mock_glob.return_value = ['three.conf'] mo = mock.mock_open(read_data='metrics_url: http://testhost:3333') with patch('spectator_client.open', mo, create=True): retry = spectator_client.get_source_catalog('TestConfig') self.assertEqual({'three': { 'metrics_url': ['http://testhost:3333'] }}, retry)
def process_web_request(self, request, path, params, fragment): options = dict(command_processor.get_global_options()) options['disable_metric_filter'] = True options.update(params) spectator = self.make_spectator_client(options) catalog = spectator_client.get_source_catalog(options) service_map = spectator.scan_by_service(catalog, options) yaml_dict = self.process_service_map(service_map) html = '<pre>%s</pre>' % encode_yaml(yaml_dict) html_doc = http_server.build_html_document( html, title='Inferred Meter Specs') request.respond(200, {'ContentType': 'application/html'}, html_doc)
def process_web_request(self, request, path, params, fragment): options = dict(command_processor.get_global_options()) options.update(params) catalog = spectator_client.get_source_catalog(options) param_services = params.get('services', 'all').split(',') if param_services == ['all']: restricted_catalog = catalog else: restricted_catalog = {key: value for key, value in catalog.items() if key in param_services} data_map = self._get_data_map(restricted_catalog, options) body = json.JSONEncoder(indent=2).encode(data_map) request.respond(200, {'ContentType': 'application/json'}, body)
def test_default_dev_endpoints(self): got_urls = {name: config['metrics_url'] for name, config in spectator_client.get_source_catalog({}).items()} def localhost_urls(port): return ['http://localhost:{port}/spectator/metrics'.format(port=port)] self.assertEquals({'clouddriver': localhost_urls(7002), 'echo': localhost_urls(8089), 'fiat': localhost_urls(7003), 'front50': localhost_urls(8080), 'gate': localhost_urls(8084), 'igor': localhost_urls(8088), 'orca': localhost_urls(8083), 'rosco': localhost_urls(8087)}, got_urls)
def __init__(self, options): if not prometheus_available: raise ImportError( 'You must "pip install prometheus-client" to get the prometheus client library.' ) self.__catalog = spectator_client.get_source_catalog(options) self.__spectator = spectator_client.SpectatorClient(options) self.__add_metalabels = options.get( 'prometheus_add_source_metalabels', options.get('prometheus', {}).get('add_source_metalabels', True)) self.__push_gateway = options.get('prometheus', {}).get('push_gateway') if self.__push_gateway: self.publish_metrics = self.__publish_to_gateway self.__last_collect_time = 0 self.__last_collect_metric_map = {} REGISTRY.register(self) # Register this so it will call our collect()
def process_commandline_request(self, options): catalog = spectator_client.get_source_catalog(options) data_map = self._get_data_map(catalog, options) json_text = json.JSONEncoder(indent=2).encode(data_map) self.output(options, json_text)