chore: update gateway pressure observability

This commit is contained in:
elky
2026-06-30 17:01:39 +08:00
parent 974def5fef
commit f179ee72f9
69 changed files with 14843 additions and 1609 deletions
+88 -7
View File
@@ -85,22 +85,69 @@ fn parse_prometheus_line(line: &str) -> Option<PrometheusSample> {
fn parse_labels(raw: &str) -> BTreeMap<String, String> {
let mut labels = BTreeMap::new();
for pair in raw.split(',').filter(|pair| !pair.is_empty()) {
for pair in split_label_pairs(raw) {
if let Some((key, value)) = pair.split_once('=') {
labels.insert(
key.trim().to_string(),
value
.trim()
.trim_matches('"')
.replace("\\\"", "\"")
.replace("\\n", "\n")
.replace("\\\\", "\\"),
unescape_label_value(value.trim().trim_matches('"')),
);
}
}
labels
}
fn split_label_pairs(raw: &str) -> Vec<&str> {
let mut pairs = Vec::new();
let mut start = 0;
let mut in_string = false;
let mut escaped = false;
for (index, ch) in raw.char_indices() {
if escaped {
escaped = false;
continue;
}
match ch {
'\\' if in_string => escaped = true,
'"' => in_string = !in_string,
',' if !in_string => {
let pair = raw[start..index].trim();
if !pair.is_empty() {
pairs.push(pair);
}
start = index + ch.len_utf8();
}
_ => {}
}
}
let pair = raw[start..].trim();
if !pair.is_empty() {
pairs.push(pair);
}
pairs
}
fn unescape_label_value(value: &str) -> String {
let mut output = String::with_capacity(value.len());
let mut chars = value.chars();
while let Some(ch) = chars.next() {
if ch != '\\' {
output.push(ch);
continue;
}
match chars.next() {
Some('n') => output.push('\n'),
Some('\\') => output.push('\\'),
Some('"') => output.push('"'),
Some(next) => {
output.push('\\');
output.push(next);
}
None => output.push('\\'),
}
}
output
}
#[cfg(test)]
mod tests {
use super::{find_metric_value_u64, parse_prometheus_samples};
@@ -133,4 +180,38 @@ aether_gateway_concurrency_rejected_total{gate="gateway_requests"} 12
Some(12)
);
}
#[test]
fn parses_quoted_label_values_containing_commas() {
let samples = parse_prometheus_samples(
r#"
metric_with_sql{rank="1",query_prefix="SELECT id, name, created_at FROM request_candidates",state="active"} 2
"#,
);
assert_eq!(samples.len(), 1);
assert_eq!(samples[0].labels.get("rank").map(String::as_str), Some("1"));
assert_eq!(
samples[0].labels.get("query_prefix").map(String::as_str),
Some("SELECT id, name, created_at FROM request_candidates")
);
assert_eq!(
samples[0].labels.get("state").map(String::as_str),
Some("active")
);
}
#[test]
fn unescapes_quoted_label_values() {
let samples = parse_prometheus_samples(
r#"
metric_with_escape{message="bad\"line\nx\\y"} 1
"#,
);
assert_eq!(
samples[0].labels.get("message").map(String::as_str),
Some("bad\"line\nx\\y")
);
}
}