Skip to content

Instantly share code, notes, and snippets.

@jmhublar
Created September 1, 2023 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmhublar/4c1faa7737375ddf68a73b429d11a029 to your computer and use it in GitHub Desktop.
Save jmhublar/4c1faa7737375ddf68a73b429d11a029 to your computer and use it in GitHub Desktop.
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