Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions scripts/e2e/compare_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

}


def should_exclude_metric(metric_name, labels):
"""
Determines if a metric should be excluded from comparison based on configured rules.
Expand All @@ -56,8 +55,6 @@ def should_exclude_metric(metric_name, labels):
pattern = rule_config['pattern']
if label in labels and re.match(pattern, labels[label]):
return True


return False


Expand Down Expand Up @@ -90,11 +87,13 @@ def read_metric_file(file_path):

def parse_metrics(content):
metrics = []
metrics_exclusion_count = 0
for family in text_string_to_metric_families(content):
for sample in family.samples:
labels = dict(sample.labels)

if should_exclude_metric(sample.name, labels):
metrics_exclusion_count += 1
continue

labels.pop('service_instance_id', None)
Expand All @@ -105,7 +104,7 @@ def parse_metrics(content):
metric = f"{family.name}{{{label_str}}}"
insort(metrics , metric)

return metrics
return metrics,metrics_exclusion_count


def generate_diff(file1_content, file2_content):
Expand All @@ -114,12 +113,17 @@ def generate_diff(file1_content, file2_content):
if isinstance(file2_content, list):
file2_content = ''.join(file2_content)

metrics1 = parse_metrics(file1_content)
metrics2 = parse_metrics(file2_content)
metrics1,excluded_metrics_count1 = parse_metrics(file1_content)
metrics2,excluded_metrics_count2 = parse_metrics(file2_content)

diff = unified_diff(metrics1, metrics2,lineterm='',n=0)
total_excluded = excluded_metrics_count1 + excluded_metrics_count2

exclusion_lines = ''
if total_excluded > 0:
exclusion_lines = f'\nMetrics excluded from A: {excluded_metrics_count1}\nMetrics excluded from B: {excluded_metrics_count2}'

return '\n'.join(diff)
return '\n'.join(diff) + exclusion_lines

def write_diff_file(diff_lines, output_path):

Expand Down
31 changes: 18 additions & 13 deletions scripts/e2e/metrics_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def parse_metrics(content):
def parse_diff_file(diff_path):
"""
Parses a unified diff file and categorizes changes into added, removed, and modified metrics.
Also captures the raw diff sections for each metric.
Also captures the raw diff sections for each metric and the exclusion count.
"""
changes = {
'added': defaultdict(list),
Expand All @@ -31,21 +31,24 @@ def parse_diff_file(diff_path):

# Store raw diff sections for each metric - just collect all lines related to each metric
raw_diff_sections = defaultdict(list)
exclusion_count = 0

with open(diff_path, 'r') as f:
lines = f.readlines()

current_metric = None

for line in lines:
original_line = line.rstrip()
stripped = line.strip()
original_line = line.rstrip('\n')
stripped = original_line.strip()

if stripped.startswith('Metrics excluded from A: ') or stripped.startswith('Metrics excluded from B: '):
count_str = stripped.split(': ')[1]
exclusion_count += int(count_str)
continue
# Skip diff headers
if stripped.startswith('+++') or stripped.startswith('---'):
continue

# Check if this line contains a metric change
# Check if this line contains a metric change
if stripped.startswith('+') or stripped.startswith('-'):
metric_name = extract_metric_name(stripped[1:].strip())
if metric_name:
Expand Down Expand Up @@ -75,7 +78,7 @@ def parse_diff_file(diff_path):
'removed': changes['removed'].pop(metric)
}

return changes, raw_diff_sections
return changes, raw_diff_sections, exclusion_count

def extract_metric_name(line):
"""Extracts metric name from a metric line, matching the diff generation format"""
Expand All @@ -97,7 +100,7 @@ def get_raw_diff_sample(raw_lines, max_lines=7):

return sample_lines

def generate_diff_summary(changes, raw_diff_sections):
def generate_diff_summary(changes, raw_diff_sections, exclusion_count):
"""
Generates a markdown summary from the parsed diff changes with raw diff samples.
"""
Expand All @@ -111,7 +114,8 @@ def generate_diff_summary(changes, raw_diff_sections):
summary.append(f"**Total Changes:** {total_added + total_removed + total_modified}\n")
summary.append(f"- 🆕 Added: {total_added} metrics")
summary.append(f"- ❌ Removed: {total_removed} metrics")
summary.append(f"- 🔄 Modified: {total_modified} metrics\n")
summary.append(f"- 🔄 Modified: {total_modified} metrics")
summary.append(f"- 🚫 Excluded: {exclusion_count} metrics\n")

# Added metrics
if changes['added']:
Expand Down Expand Up @@ -170,15 +174,16 @@ def main():

args = parser.parse_args()

changes, raw_diff_sections = parse_diff_file(args.diff)
summary = generate_diff_summary(changes, raw_diff_sections)
changes, raw_diff_sections, exclusion_count = parse_diff_file(args.diff)
summary = generate_diff_summary(changes, raw_diff_sections, exclusion_count)

with open(args.output, 'w') as f:
f.write(summary)

print(f"Generated diff summary with {len(changes['added'])} additions, "
f"{len(changes['removed'])} removals and "
f"{len(changes['modified'])} modifications")
f"{len(changes['removed'])} removals, "
f"{len(changes['modified'])} modifications and "
f"{exclusion_count} exclusions")
print(f"Summary saved to {args.output}")

if __name__ == '__main__':
Expand Down
Loading