diff --git a/scripts/frigga.py b/scripts/frigga.py index 9304504..77f0f32 100644 --- a/scripts/frigga.py +++ b/scripts/frigga.py @@ -56,7 +56,8 @@ def cli(config, ci): @click.option('--output-file-path', '-o', default='./.metrics.json', show_default=True, required=False, type=str) # @click.option('--file', '-f', prompt=False, default=".", help="Output metrics.json file to pwd") def grafana_list(grafana_url, grafana_api_key, output_file_path): - """Provide Grafana UrL and Grafana API Key (Viewer)\n + """Alias: gl\n +Provide Grafana URL and Grafana API Key (Viewer)\n Returns a list of metrics that are used in all dashboards""" if "http" not in grafana_url: print_msg( @@ -70,5 +71,13 @@ def grafana_list(grafana_url, grafana_api_key, output_file_path): @click.option('--prom-yaml-path', '-ppath', default='docker-compose/prometheus.yml', prompt=True, required=True, show_default=False, type=str) @click.option('--metrics-json-path', '-mjpath', default='./.metrics.json', show_default=True, prompt=True, required=False, type=str) @click.option('--create-backup-file', '-b', is_flag=True, default=True, required=False) -def prometheus_apply(prom_yaml_path, metrics_json_path, create_backup_file): - apply_yaml(prom_yaml_path, metrics_json_path, create_backup_file) +@click.option('--skip-rules-file', '-sr', is_flag=True, default=False, required=False) +def prometheus_apply(prom_yaml_path, metrics_json_path, create_backup_file, skip_rules_file): + """Alias: pa\n +Applies .metrics.json for a given prometheus.yml file\n +By default:\n +- Creates a backup of prometheus.yml to prometheus.yml.bak.yml (same dir as prometheus.yml)\n +- Creates a .prometheus-rules.yml file with all relabel_configs + """ + apply_yaml(prom_yaml_path, metrics_json_path, + create_backup_file, skip_rules_file) diff --git a/scripts/prometheus.py b/scripts/prometheus.py index 194c7c4..e534ee0 100644 --- a/scripts/prometheus.py +++ b/scripts/prometheus.py @@ -42,7 +42,7 @@ def get_ignored_words(): return sorted(list(set(words_list))) -def apply_yaml(prom_yaml_path, metrics_json_path, create_backup_file=True): +def apply_yaml(prom_yaml_path, metrics_json_path, create_backup_file=True, skip_rules_file=False): print_msg(msg_content=f"Reading documents from {prom_yaml_path}") with open(prom_yaml_path, "r") as file: prom_yaml = file.read() @@ -66,26 +66,41 @@ def apply_yaml(prom_yaml_path, metrics_json_path, create_backup_file=True): metrics_dict = json.load(file) print_msg(msg_content=f"Generating metrics_relabel_configs in memory") - metric_relabel_configs = [] + relabel_configs = [] for metric in metrics_dict['all_metrics']: - metric_relabel_configs.append({ + relabel_configs.append({ "source_labels": ['__name__'], "regex": f"^{metric}", "target_label": "__tmp_keep_me", "replacement": True }) - metric_relabel_configs.append({ + relabel_configs.append({ "source_labels": ["__tmp_keep_me"], "regex": True, "action": "keep" }) - for job in prom_doc['scrape_configs']: - if job['job_name'] not in frigga_doc['exclude_jobs']: - job['metric_relabel_configs'] = metric_relabel_configs noalias_dumper = yaml.dumper.SafeDumper noalias_dumper.ignore_aliases = lambda self, data: True + # write relabel configs to file + if not skip_rules_file: + rules_file_path = ".prometheus-rules.yml" + print_msg( + msg_content=f"Writing relabel_configs to {rules_file_path}") + with open(rules_file_path, 'w') as fs: + data = yaml.dump_all( + documents=[relabel_configs], + stream=fs, default_flow_style=False, + Dumper=noalias_dumper, + indent=2 + ) + + # add relabel configs to all jobs, except the ones in exclude_jobs + for job in prom_doc['scrape_configs']: + if job['job_name'] not in frigga_doc['exclude_jobs']: + job['metric_relabel_configs'] = relabel_configs + # backup old prom yaml if create_backup_file: print_msg(msg_content=f"Creating a backup file for {prom_yaml_path}")