Skip to content

Commit

Permalink
Merge pull request #13 from unfor19/feature/prometheus-rules-file
Browse files Browse the repository at this point in the history
Feature/prometheus rules file
  • Loading branch information
unfor19 committed Jan 13, 2021
2 parents 74045c7 + 2beee58 commit 19f25ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
15 changes: 12 additions & 3 deletions scripts/frigga.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
29 changes: 22 additions & 7 deletions scripts/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}")
Expand Down

0 comments on commit 19f25ff

Please sign in to comment.