From c50e53ad531b43fc9a7cd017923e8b3be6900850 Mon Sep 17 00:00:00 2001 From: BryantHe Date: Fri, 28 Jul 2023 11:04:33 +0800 Subject: [PATCH] feat: add metrics for nameko services 4 --- namekoplus/command.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/namekoplus/command.py b/namekoplus/command.py index 471a091..eafea14 100644 --- a/namekoplus/command.py +++ b/namekoplus/command.py @@ -330,6 +330,7 @@ def metric_config_gen(module, class_name_str): dest_dir = module.split('.')[0] file_name = module.split('.')[-1] _module = __import__(module) + config_list = [] for class_name in class_name_str.split(','): members = inspect.getmembers(getattr(getattr(_module, file_name), class_name), predicate=inspect.isfunction) @@ -346,28 +347,30 @@ def metric_config_gen(module, class_name_str): }) # Generate one file of statsd config yaml for statsd exporter - metric_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs') - template_file_path = os.path.join(metric_configs_dir, 'statsd_mapping.yml.mako') - output_file = os.path.join('.', 'statsd_mapping.yml') - template_to_file(template_file=template_file_path, dest=output_file, output_encoding='utf-8', - **{'config_list': config_list}) + with status(f'Creating statsd_mapping.yml'): + metric_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs') + template_file_path = os.path.join(metric_configs_dir, 'statsd_mapping.yml.mako') + output_file = os.path.join('.', 'statsd_mapping.yml') + template_to_file(template_file=template_file_path, dest=output_file, output_encoding='utf-8', + **{'config_list': config_list}) # Generate files of json for grafana dashboard if not os.access('grafana_dashboards', os.F_OK): with status(f'Creating directory {os.path.abspath("grafana_dashboards")!r}'): os.makedirs('grafana_dashboards') - for class_name in class_name_str.split(','): - grafana_list = [] - for config in config_list: - if config['class_name'] == class_name: - grafana_list.append(config) - grafana_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs') - grafana_file_path = os.path.join(grafana_configs_dir, 'grafana.json.mako') - output_file = os.path.join('grafana_dashboards', f'{class_name}_Grafana.json') - template_to_file(template_file=grafana_file_path, dest=output_file, output_encoding='utf-8', - **{'service_name': class_name, 'uid': shortuuid.uuid(), - 'grafana_list': grafana_list}) + with status(f'Creating files of Grafana.json into the directory of grafana_dashboards'): + for class_name in class_name_str.split(','): + grafana_list = [] + for config in config_list: + if config['class_name'] == class_name: + grafana_list.append(config) + grafana_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs') + grafana_file_path = os.path.join(grafana_configs_dir, 'grafana.json.mako') + output_file = os.path.join('grafana_dashboards', f'{class_name}_Grafana.json') + template_to_file(template_file=grafana_file_path, dest=output_file, output_encoding='utf-8', + **{'service_name': class_name, 'uid': shortuuid.uuid(), + 'grafana_list': grafana_list}) if __name__ == '__main__':