|
| 1 | +// Copyright 2021-Present Datadog, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::collections::HashMap; |
| 16 | + |
| 17 | +use regex::Regex; |
| 18 | + |
| 19 | +#[derive(Debug, PartialEq, Clone)] |
| 20 | +pub struct PrometheusMetric { |
| 21 | + pub name: String, |
| 22 | + pub labels: HashMap<String, String>, |
| 23 | + pub metric_value: f64, |
| 24 | +} |
| 25 | + |
| 26 | +/// Parse Prometheus metrics serialized with prometheus::TextEncoder |
| 27 | +/// |
| 28 | +/// Unfortunately, the prometheus crate does not provide a way to parse metrics |
| 29 | +pub fn parse_prometheus_metrics(input: &str) -> Vec<PrometheusMetric> { |
| 30 | + let mut metrics = Vec::new(); |
| 31 | + let re = Regex::new(r"(?P<name>[^{]+)(?:\{(?P<labels>[^\}]*)\})? (?P<value>.+)").unwrap(); |
| 32 | + |
| 33 | + for line in input.lines() { |
| 34 | + if line.starts_with('#') { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + if let Some(caps) = re.captures(line) { |
| 39 | + let name = caps.name("name").unwrap().as_str().to_string(); |
| 40 | + let metric_value: f64 = caps |
| 41 | + .name("value") |
| 42 | + .unwrap() |
| 43 | + .as_str() |
| 44 | + .parse() |
| 45 | + .expect("Failed to parse value"); |
| 46 | + |
| 47 | + let labels = caps.name("labels").map_or(HashMap::new(), |m| { |
| 48 | + m.as_str() |
| 49 | + .split(',') |
| 50 | + .map(|label| { |
| 51 | + let mut parts = label.splitn(2, '='); |
| 52 | + let key = parts.next().unwrap().to_string(); |
| 53 | + let value = parts.next().unwrap().trim_matches('"').to_string(); |
| 54 | + (key, value) |
| 55 | + }) |
| 56 | + .collect() |
| 57 | + }); |
| 58 | + |
| 59 | + metrics.push(PrometheusMetric { |
| 60 | + name, |
| 61 | + labels, |
| 62 | + metric_value, |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + metrics |
| 68 | +} |
| 69 | + |
| 70 | +/// Filter metrics by name and a subset of the available labels |
| 71 | +/// |
| 72 | +/// Specify an empty Vec of labels to return all metrics with the specified name |
| 73 | +pub fn filter_metrics( |
| 74 | + metrics: &[PrometheusMetric], |
| 75 | + name: &str, |
| 76 | + labels: Vec<(&'static str, &'static str)>, |
| 77 | +) -> Vec<PrometheusMetric> { |
| 78 | + metrics |
| 79 | + .iter() |
| 80 | + .filter(|metric| metric.name == name) |
| 81 | + .filter(|metric| { |
| 82 | + labels |
| 83 | + .iter() |
| 84 | + .all(|(key, value)| metric.labels.get(*key).map(String::as_str) == Some(*value)) |
| 85 | + }) |
| 86 | + .cloned() |
| 87 | + .collect() |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use super::*; |
| 93 | + |
| 94 | + const TEST_INPUT: &str = r#" |
| 95 | +quickwit_search_leaf_search_single_split_warmup_num_bytes_sum 0 |
| 96 | +# HELP quickwit_storage_object_storage_request_duration_seconds Duration of object storage requests in seconds. |
| 97 | +# TYPE quickwit_storage_object_storage_request_duration_seconds histogram |
| 98 | +quickwit_storage_object_storage_request_duration_seconds_bucket{action="delete_objects",le="30"} 0 |
| 99 | +quickwit_storage_object_storage_request_duration_seconds_bucket{action="delete_objects",le="+Inf"} 0 |
| 100 | +quickwit_storage_object_storage_request_duration_seconds_sum{action="delete_objects"} 0 |
| 101 | +quickwit_search_root_search_request_duration_seconds_sum{kind="server",status="success"} 0.004093958 |
| 102 | +quickwit_storage_object_storage_requests_total{action="delete_object"} 0 |
| 103 | +quickwit_storage_object_storage_requests_total{action="delete_objects"} 0 |
| 104 | +"#; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_parse_prometheus_metrics() { |
| 108 | + let metrics = parse_prometheus_metrics(TEST_INPUT); |
| 109 | + assert_eq!(metrics.len(), 7); |
| 110 | + assert_eq!( |
| 111 | + metrics[0], |
| 112 | + PrometheusMetric { |
| 113 | + name: "quickwit_search_leaf_search_single_split_warmup_num_bytes_sum".to_string(), |
| 114 | + labels: HashMap::new(), |
| 115 | + metric_value: 0.0, |
| 116 | + } |
| 117 | + ); |
| 118 | + assert_eq!( |
| 119 | + metrics[1], |
| 120 | + PrometheusMetric { |
| 121 | + name: "quickwit_storage_object_storage_request_duration_seconds_bucket".to_string(), |
| 122 | + labels: [ |
| 123 | + ("action".to_string(), "delete_objects".to_string()), |
| 124 | + ("le".to_string(), "30".to_string()) |
| 125 | + ] |
| 126 | + .iter() |
| 127 | + .cloned() |
| 128 | + .collect(), |
| 129 | + metric_value: 0.0, |
| 130 | + } |
| 131 | + ); |
| 132 | + assert_eq!( |
| 133 | + metrics[2], |
| 134 | + PrometheusMetric { |
| 135 | + name: "quickwit_storage_object_storage_request_duration_seconds_bucket".to_string(), |
| 136 | + labels: [ |
| 137 | + ("action".to_string(), "delete_objects".to_string()), |
| 138 | + ("le".to_string(), "+Inf".to_string()) |
| 139 | + ] |
| 140 | + .iter() |
| 141 | + .cloned() |
| 142 | + .collect(), |
| 143 | + metric_value: 0.0, |
| 144 | + } |
| 145 | + ); |
| 146 | + assert_eq!( |
| 147 | + metrics[3], |
| 148 | + PrometheusMetric { |
| 149 | + name: "quickwit_storage_object_storage_request_duration_seconds_sum".to_string(), |
| 150 | + labels: [("action".to_string(), "delete_objects".to_string())] |
| 151 | + .iter() |
| 152 | + .cloned() |
| 153 | + .collect(), |
| 154 | + metric_value: 0.0, |
| 155 | + } |
| 156 | + ); |
| 157 | + assert_eq!( |
| 158 | + metrics[4], |
| 159 | + PrometheusMetric { |
| 160 | + name: "quickwit_search_root_search_request_duration_seconds_sum".to_string(), |
| 161 | + labels: [ |
| 162 | + ("kind".to_string(), "server".to_string()), |
| 163 | + ("status".to_string(), "success".to_string()) |
| 164 | + ] |
| 165 | + .iter() |
| 166 | + .cloned() |
| 167 | + .collect(), |
| 168 | + metric_value: 0.004093958, |
| 169 | + } |
| 170 | + ); |
| 171 | + assert_eq!( |
| 172 | + metrics[5], |
| 173 | + PrometheusMetric { |
| 174 | + name: "quickwit_storage_object_storage_requests_total".to_string(), |
| 175 | + labels: [("action".to_string(), "delete_object".to_string())] |
| 176 | + .iter() |
| 177 | + .cloned() |
| 178 | + .collect(), |
| 179 | + metric_value: 0.0, |
| 180 | + } |
| 181 | + ); |
| 182 | + assert_eq!( |
| 183 | + metrics[6], |
| 184 | + PrometheusMetric { |
| 185 | + name: "quickwit_storage_object_storage_requests_total".to_string(), |
| 186 | + labels: [("action".to_string(), "delete_objects".to_string())] |
| 187 | + .iter() |
| 188 | + .cloned() |
| 189 | + .collect(), |
| 190 | + metric_value: 0.0, |
| 191 | + } |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + #[test] |
| 196 | + fn test_filter_prometheus_metrics() { |
| 197 | + let metrics = parse_prometheus_metrics(TEST_INPUT); |
| 198 | + { |
| 199 | + let filtered_metric = filter_metrics( |
| 200 | + &metrics, |
| 201 | + "quickwit_storage_object_storage_request_duration_seconds_bucket", |
| 202 | + vec![], |
| 203 | + ); |
| 204 | + assert_eq!(filtered_metric.len(), 2); |
| 205 | + } |
| 206 | + { |
| 207 | + let filtered_metric = filter_metrics( |
| 208 | + &metrics, |
| 209 | + "quickwit_search_root_search_request_duration_seconds_sum", |
| 210 | + vec![("status", "success")], |
| 211 | + ); |
| 212 | + assert_eq!(filtered_metric.len(), 1); |
| 213 | + } |
| 214 | + { |
| 215 | + let filtered_metric = |
| 216 | + filter_metrics(&metrics, "quickwit_doest_not_exist_metric", vec![]); |
| 217 | + assert_eq!(filtered_metric.len(), 0); |
| 218 | + } |
| 219 | + { |
| 220 | + let filtered_metric = filter_metrics( |
| 221 | + &metrics, |
| 222 | + "quickwit_storage_object_storage_requests_total", |
| 223 | + vec![("does_not_exist_label", "value")], |
| 224 | + ); |
| 225 | + assert_eq!(filtered_metric.len(), 0); |
| 226 | + } |
| 227 | + { |
| 228 | + let filtered_metric = filter_metrics( |
| 229 | + &metrics, |
| 230 | + "quickwit_storage_object_storage_requests_total", |
| 231 | + vec![("action", "does_not_exist_value")], |
| 232 | + ); |
| 233 | + assert_eq!(filtered_metric.len(), 0); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments