Created
September 1, 2023 19:54
-
-
Save jmhublar/4c1faa7737375ddf68a73b429d11a029 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import argparse | |
# Set up command-line argument parsing | |
parser = argparse.ArgumentParser(description='Check for duplicate metrics in Prometheus.') | |
parser.add_argument('url', help='The URL of the Prometheus server.') | |
args = parser.parse_args() | |
# Get all metric names | |
response = requests.get(args.url + '/api/v1/label/__name__/values') | |
response_json = response.json() | |
if 'data' in response_json: | |
metrics = response_json['data'] | |
else: | |
print(f"Unexpected response: {response.content}") | |
exit(1) | |
print(f"Found {len(metrics)} metrics. Checking for duplicates...") | |
# Open output file | |
with open('output.txt', 'w') as f: | |
# Check each metric | |
for i, metric in enumerate(metrics, 1): | |
print(f"Checking metric {i}/{len(metrics)}: {metric}") | |
response = requests.get(args.url + '/api/v1/series', params={'match[]': '{__name__="' + metric + '"}'}) | |
response_json = response.json() | |
if 'status' in response_json and response_json['status'] == 'success': | |
if 'data' in response_json: | |
jobs = [x['job'] for x in response_json['data'] if 'job' in x] | |
unique_jobs = set(jobs) | |
if len(unique_jobs) > 1: | |
metric_jobs = {'metric': metric, 'jobs': list(unique_jobs)} | |
f.write(json.dumps(metric_jobs) + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment