|
26 | 26 | # } |
27 | 27 | } |
28 | 28 |
|
| 29 | +METRIC_EXCLUSION_RULES = { |
| 30 | + # excluding HTTP 5xx responses as these can be flaky |
| 31 | + 'http_5xx_errors': { |
| 32 | + 'condition': 'label_match', |
| 33 | + 'label': 'http_response_status_code', |
| 34 | + 'pattern': r'^5\d{2}$', |
| 35 | + }, |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +def should_exclude_metric(metric_name, labels): |
| 41 | + """ |
| 42 | + Determines if a metric should be excluded from comparison based on configured rules. |
| 43 | + |
| 44 | + Args: |
| 45 | + metric_name: The name of the metric |
| 46 | + labels: Dictionary of labels for the metric |
| 47 | + |
| 48 | + Returns: |
| 49 | + tuple: (should_exclude: bool, reason: str or None) |
| 50 | + """ |
| 51 | + for rule_name, rule_config in METRIC_EXCLUSION_RULES.items(): |
| 52 | + condition = rule_config['condition'] |
| 53 | + |
| 54 | + if condition == 'label_match': |
| 55 | + label = rule_config['label'] |
| 56 | + pattern = rule_config['pattern'] |
| 57 | + if label in labels and re.match(pattern, labels[label]): |
| 58 | + return True |
| 59 | + |
| 60 | + |
| 61 | + return False, None |
| 62 | + |
| 63 | + |
29 | 64 | def suppress_transient_labels(metric_name, labels): |
30 | 65 | """ |
31 | 66 | Suppresses transient labels in metrics based on configured patterns. |
@@ -58,9 +93,12 @@ def parse_metrics(content): |
58 | 93 | for family in text_string_to_metric_families(content): |
59 | 94 | for sample in family.samples: |
60 | 95 | labels = dict(sample.labels) |
61 | | - #simply pop undesirable metric labels |
62 | | - labels.pop('service_instance_id',None) |
63 | 96 |
|
| 97 | + should_exclude= should_exclude_metric(sample.name, labels) |
| 98 | + if should_exclude: |
| 99 | + continue |
| 100 | + |
| 101 | + labels.pop('service_instance_id', None) |
64 | 102 | labels = suppress_transient_labels(sample.name, labels) |
65 | 103 |
|
66 | 104 | label_pairs = sorted(labels.items(), key=lambda x: x[0]) |
|
0 commit comments