def process_web_request(self, request, path, params, fragment): options = dict(command_processor.get_global_options()) options.update(params) service_endpoints = spectator_client.determine_service_endpoints(options) data_map = self._get_data_map(service_endpoints, options) if accepts_html(request): content_type = 'text/html' by_service = self.service_map_to_html by_type = self.type_map_to_html else: content_type = 'text/plain' by_service = self.service_map_to_text by_type = self.type_map_to_text by = options.get('by', 'service') if by == 'service': content_data = by_service(data_map, params=params) else: content_data = by_type(data_map, params=params) if content_type == 'text/html': body = http_server.build_html_document( content_data, title='Current Metrics') else: body = content_data request.respond(200, {'ContentType': content_type}, body)
def __init__(self, options): self.__service_endpoints = spectator_client.determine_service_endpoints( options) self.__spectator = spectator_client.SpectatorClient(options) self.__add_metalabels = options.get('prometheus_add_source_metalabels', True) REGISTRY.register(self)
def __get_type_and_tag_map_and_active_services( self, service_endpoints, options): service_endpoints = spectator_client.determine_service_endpoints(options) spectator = self.make_spectator_client(options) type_map = spectator.scan_by_type(service_endpoints, params=options) service_tag_map, active_services = self.to_service_tag_map(type_map) return type_map, service_tag_map, active_services
def process_commandline_request(self, options): service_endpoints = spectator_client.determine_service_endpoints(options) data_map = self._get_data_map(service_endpoints, options) by = options.get('by', 'service') if by == 'service': content_data = self.service_map_to_text(data_map, params=options) else: content_data = self.type_map_to_text(data_map, params=options) self.output(options, content_data)
def __call__(self, options, metric_service_list): """This is the actual method that implements the CommandHandler. It is put here in a callable so that we can run this in a separate thread. The main thread will be the standard WebServer. """ period = options['period'] service_endpoints = spectator_client.determine_service_endpoints( options) spectator = spectator_client.SpectatorClient(options) publishing_services = [ service for service in metric_service_list if 'publish_metrics' in dir(service) ] logging.info('Starting Monitor') time_offset = int(time.time()) while True: if not publishing_services: # we still need this loop to keep the server running # but the loop doesnt do anything. time.sleep(period) continue start = time.time() done = start service_metric_map = spectator.scan_by_service(service_endpoints) collected = time.time() for service in publishing_services: try: start_publish = time.time() count = service.publish_metrics(service_metric_map) if count is None: count = 0 done = time.time() logging.info('Wrote %d metrics to %s in %d ms + %d ms', count, service.__class__.__name__, (collected - start) * 1000, (done - start_publish) * 1000) except: logging.error(traceback.format_exc()) # ignore exception, continue server. # Try to align time increments so we always collect around the same time # so that the measurements we report are in even intervals. # There is still going to be jitter on the collection end but we'll at # least always start with a steady rhythm. now = time.time() delta_time = (period - (int(now) - time_offset)) % period if delta_time == 0 and (int(now) == time_offset or (now - start <= 1)): delta_time = period time.sleep(delta_time)
def test_filtered_endpoints(self): options = args_to_options( ['--service_hosts=', '--clouddriver=abc:1234,xyz:4321', '--gate=even:2468,named:foo']) service_endpoints = spectator_client.determine_service_endpoints(options) self.assertEquals( {'clouddriver': [('abc', 1234), ('xyz', 4321)], 'gate': [('even', 2468), ('named', 'foo')]}, service_endpoints)
def process_commandline_request(self, options): service_endpoints = spectator_client.determine_service_endpoints(options) type_map, service_tag_map, active_services = ( self.__get_type_and_tag_map_and_active_services( service_endpoints, 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_multi_mutated_endpoints(self): options = args_to_options( ['--service_hosts=one,two,three', '--clouddriver=xyz', '--echo=', '--fiat=', '--front50=','--gate=', '--igor=']) service_endpoints = spectator_client.determine_service_endpoints(options) self.assertEquals( {'clouddriver': [('xyz', 7002)], 'orca': [('one', 8083), ('two', 8083), ('three', 8083)], 'rosco': [('one', 8087), ('two', 8087), ('three', 8087)]}, service_endpoints)
def test_default_endpoints(self): options = args_to_options([]) service_endpoints = spectator_client.determine_service_endpoints(options) self.assertEquals({'clouddriver': [('localhost', 7002)], 'echo': [('localhost', 8089)], 'fiat': [('localhost', 7003)], 'front50': [('localhost', 8080)], 'gate': [('localhost', 8084)], 'igor': [('localhost', 8088)], 'orca': [('localhost', 8083)], 'rosco': [('localhost', 8087)]}, service_endpoints)
def process_web_request(self, request, path, params, fragment): options = dict(command_processor.get_global_options()) options.update(params) service_endpoints = spectator_client.determine_service_endpoints(options) type_map, service_tag_map, active_services = ( self.__get_type_and_tag_map_and_active_services( service_endpoints, 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_web_request(self, request, path, params, fragment): options = dict(command_processor.get_global_options()) options.update(params) all_service_endpoints = spectator_client.determine_service_endpoints( options) param_services = params.get('services', 'all').split(',') if param_services == 'all': service_endpoints = all_service_endpoints else: service_endpoints = {key: value for key, value in all_service_endpoints.items() if key in all_service_endpoints.keys()} data_map = self._get_data_map(service_endpoints, options) body = json.JSONEncoder(indent=2).encode(data_map) request.respond(200, {'ContentType': 'application/json'}, body)
def __call__(self, options, metric_service): """This is the actual method that implements the CommandHandler. It is put here in a callable so that we can run this in a separate thread. The main thread will be the standard WebServer. """ period = options['period'] service_endpoints = spectator_client.determine_service_endpoints( options) spectator = spectator_client.SpectatorClient(options) logging.info('Starting Monitor') time_offset = int(time.time()) while True: start = time.time() done = start service_metric_map = spectator.scan_by_service(service_endpoints) collected = time.time() try: count = metric_service.publish_metrics(service_metric_map) if count is None: count = 0 done = time.time() logging.info('Wrote %d metrics in %d ms + %d ms', count, (collected - start) * 1000, (done - collected) * 1000) except BaseException as ex: traceback.print_exc(ex) logging.error(ex) # Try to align time increments so we always collect around the same time # so that the measurements we report are in even intervals. # There is still going to be jitter on the collection end but we'll at # least always start with a steady rhythm. delta_time = (period - (int(done) - time_offset)) % period if delta_time == 0 and (int(done) == time_offset or (done - start <= 1)): delta_time = period time.sleep(delta_time)
def process_commandline_request(self, options): service_endpoints = spectator_client.determine_service_endpoints(options) data_map = self._get_data_map(service_endpoints, options) json_text = json.JSONEncoder(indent=2).encode(data_map) self.output(options, json_text)