Skip to content

Instantly share code, notes, and snippets.

@krzysztofantczak
Created March 8, 2024 07:39
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 krzysztofantczak/c67a27761293abd498d806e941469f05 to your computer and use it in GitHub Desktop.
Save krzysztofantczak/c67a27761293abd498d806e941469f05 to your computer and use it in GitHub Desktop.
import os
from prometheus_api_client import PrometheusConnect, PrometheusApiClientException
import csv
import datetime
import re
def sanitize_filename(filename):
# Replace characters not allowed in filenames with underscore
return re.sub(r'[^\w\.]', '_', filename)
# Prometheus connection details
prometheus_url = 'http://demo.robustperception.io:9090'
prometheus = PrometheusConnect(url=prometheus_url)
# Define labels, metrics to skip (full and partial strings), and time range
labels_to_include = {'env': 'demo'} # Empty dictionary to include all metrics
metrics_to_skip = ['metric_to_skip1', 'metric_to_skip2']
partial_strings_to_skip = ['alertmanager']
start_time = datetime.datetime(2024, 3, 1, 0, 0, 0)
end_time = datetime.datetime(2024, 3, 8, 0, 0, 0)
step = '1h' # Step size for data resolution
# Construct Prometheus query with label filtering
label_filters = ','.join([f'{key}="{value}"' for key, value in labels_to_include.items()])
query = '{' + label_filters + '}' if label_filters else '{}'
# Query Prometheus for metrics
data = prometheus.custom_query_range(query=query, start_time=start_time, end_time=end_time, step=step)
# Create export directory if it doesn't exist
export_dir = f'export_{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}'
os.makedirs(export_dir, exist_ok=True)
# Filter metrics and labels
for metric in data:
metric_name = metric['metric']['__name__']
# Check if the metric should be skipped based on full strings
if metric_name in metrics_to_skip:
continue
# Check if the metric should be skipped based on partial strings
if any(partial_string in metric_name for partial_string in partial_strings_to_skip):
continue
# If the metric is not to be skipped, proceed to export it
labels = {**metric['metric'], **labels_to_include} # Combine all labels
values = metric['values']
# Write to CSV
csv_filename = os.path.join(export_dir, f"{sanitize_filename(metric_name)}.csv")
with open(csv_filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
# Write comment line with original labels
writer.writerow([f'# Original Labels: {labels}'])
# Write header
writer.writerow(['timestamp', 'value'])
# Write data
for value in values:
writer.writerow(value)
import argparse
import csv
import os
import matplotlib.pyplot as plt
def plot_csv(csv_filename, save_image=False, output_dir=None):
timestamps = []
values = []
# Read data from CSV file
with open(csv_filename, 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip header
for row in reader:
timestamps.append(row[0])
values.append(row[1])
# Convert timestamps and values to appropriate types
timestamps = [int(ts) for ts in timestamps]
values = [float(val) for val in values]
# Plot data
plt.plot(timestamps, values)
plt.xlabel('Timestamp')
plt.ylabel('Value')
plt.title('Metric Data')
# Save image if specified
if save_image:
# Set default output directory if not specified
if output_dir is None:
output_dir = os.path.splitext(csv_filename)[0] + "_images"
# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Construct output filename
image_filename = os.path.join(output_dir, os.path.basename(csv_filename).replace('.csv', '.png'))
# Save plot image
plt.savefig(image_filename)
print(f"Image saved to: {image_filename}")
else:
# Show plot
plt.show()
if __name__ == "__main__":
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Plot data from CSV file.')
parser.add_argument('csv_filename', type=str, help='Path to the CSV file')
parser.add_argument('--save-image', action='store_true', help='Save plot image to file')
parser.add_argument('--output-dir', type=str, default='plot_images', help='Output directory for plot image')
args = parser.parse_args()
# Plot data from the specified CSV file
plot_csv(args.csv_filename, args.save_image, args.output_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment