Skip to content

Instantly share code, notes, and snippets.

@egallis31
Last active June 3, 2024 20:29
Show Gist options
  • Select an option

  • Save egallis31/26d95a6c4c99b520eedb4776d778e442 to your computer and use it in GitHub Desktop.

Select an option

Save egallis31/26d95a6c4c99b520eedb4776d778e442 to your computer and use it in GitHub Desktop.
Get Azure Managed Promtheus metrics and guestimate cost per metric, with ability to search on job label
# Summarize per interval cost by Metric for Azure Managed Prometheus
# Author: egallis31
# Utilizes MSAL Native authentication to authenticated via OAUTH as required
# Requirements: prometheus_api_client, azure.identity
import json
from time import sleep
from azure.identity import DefaultAzureCredential
from prometheus_api_client import PrometheusConnect
from prometheus_api_client.metric import Metric
# Metric Search
search_job = "cadvisor"
search_job_interval = 60 # seconds
job_interval = 60 # avg based on the job_interval
azure_monitor_workspace_url = "" #Azure Managed Prometheus URL
azure_monitor_workspace_pricing = 0.16/10000000 # $0.16 per 10,000,000 samples
# Create a DefaultAzureCredential object
credential = DefaultAzureCredential()
# Connect to the Prometheus server
prom = PrometheusConnect(url=azure_monitor_workspace_url, disable_ssl=False, headers={"Authorization": f"Bearer {credential.get_token('https://data.monitor.azure.com/.default').token}"})
# Get the list of all metrics
all_metrics = prom.all_metrics()
print(f"The count of all metrics is {len(all_metrics)}.")
#print(f"The first 5 metrics are: {all_metrics[:5]}")
# Initialize the count of metric samples
count = 0
total_cost = 0
total_search_cost = 0
metric_summary = {}
# Iterate over all metrics
for metric_name in all_metrics:
# Query the metric data
metric_data = prom.get_current_metric_value(metric_name)
# Print the metrics name
print(f"Checking Metric Name: {metric_name}")
#print(metric_data[0]['metric'])
# Get Job Labels per metric name with sample count
jobs = []
for metric in metric_data:
if metric['metric'].get('job') is not None:
if metric['metric']['job'] not in jobs:
jobs.append(metric['metric']['job'])
print(f" Job: {metric['metric']['job']}")
# Calculate the cost of the metric
cost = len(metric_data) * azure_monitor_workspace_pricing
total_cost += cost
print(f" Cost: ${cost:.9f}")
# Append the count of metric samples to metric_summary
#print(metric_data)
if search_job in jobs:
print(f" Found Metric Name: {metric_name} with Search Job: {search_job}")
total_search_cost += cost
metric_summary[metric_name] = {'samples':len(metric_data), 'job': jobs, 'cost': cost}
elif search_job == "":
metric_summary[metric_name] = {'samples':len(metric_data), 'job': jobs, 'cost': cost}
sleep(1) #for Azure Monitor ratelimit
# Pretty Print the metric_summary json
print("=============================================")
print("Metric Summary")
print("=============================================")
print("Total Metrics: {}".format(len(all_metrics)))
print("Total Per Interval Cost Samples: ${:.9f}".format(total_cost))
print("Total Per Searched Interval Cost Samples: ${:.9f}".format(total_search_cost))
print("AVG Cost Per Day: ${:.9f}".format(total_cost*86400/job_interval))
print("Total Search Cost Per Day: ${:.9f}".format(total_search_cost*86400/search_job_interval))
print("=============================================")
print(json.dumps(metric_summary, indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment