mirror of
https://github.com/rustfs/rustfs.git
synced 2026-11-30 20:57:35 +00:00
perf(log-analyzer): avoid per-event String allocs in rule eval and observe (#9754)
This commit is contained in:
committed by
GitHub
parent
551bfab922
commit
eea2be63a3
@@ -212,10 +212,16 @@ impl Analyzer {
|
||||
event.node = Some(std::sync::Arc::from(redact::hash_value(node).as_str()));
|
||||
}
|
||||
|
||||
let level_key = event.level.map_or_else(|| "no_level".to_string(), |l| l.to_string());
|
||||
*self.level_counts.entry(level_key).or_insert(0) += 1;
|
||||
let level_key = event.level.map_or("no_level", LogLevel::as_str);
|
||||
if let Some(count) = self.level_counts.get_mut(level_key) {
|
||||
*count += 1;
|
||||
} else {
|
||||
self.level_counts.insert(level_key.to_string(), 1);
|
||||
}
|
||||
|
||||
if let Some(node) = event.node.as_deref() {
|
||||
if let Some(node) = event.node.as_deref()
|
||||
&& !self.nodes.contains(node)
|
||||
{
|
||||
self.nodes.insert(node.to_string());
|
||||
}
|
||||
|
||||
@@ -226,14 +232,13 @@ impl Analyzer {
|
||||
});
|
||||
self.offsets.insert(format_offset(&ts));
|
||||
if let Some(node) = event.node.as_deref() {
|
||||
self.node_ranges
|
||||
.entry(node.to_string())
|
||||
.and_modify(|(min, max, count)| {
|
||||
*min = min.clone().min(ts.clone());
|
||||
*max = max.clone().max(ts.clone());
|
||||
*count += 1;
|
||||
})
|
||||
.or_insert((ts.clone(), ts.clone(), 1));
|
||||
if let Some((min, max, count)) = self.node_ranges.get_mut(node) {
|
||||
*min = min.clone().min(ts.clone());
|
||||
*max = max.clone().max(ts.clone());
|
||||
*count += 1;
|
||||
} else {
|
||||
self.node_ranges.insert(node.to_string(), (ts.clone(), ts.clone(), 1));
|
||||
}
|
||||
}
|
||||
let bucket = self
|
||||
.minute_buckets
|
||||
|
||||
@@ -48,17 +48,20 @@ impl LogLevel {
|
||||
.find(|(name, _)| s.eq_ignore_ascii_case(name))
|
||||
.map(|(_, level)| *level)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LogLevel {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(match self {
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
LogLevel::Trace => "TRACE",
|
||||
LogLevel::Debug => "DEBUG",
|
||||
LogLevel::Info => "INFO",
|
||||
LogLevel::Warn => "WARN",
|
||||
LogLevel::Error => "ERROR",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LogLevel {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,6 +137,18 @@ impl LogEvent {
|
||||
other => other.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Compare a field's display representation to `expected` without cloning
|
||||
/// JSON string values.
|
||||
pub fn field_display_eq(&self, name: &str, expected: &str) -> bool {
|
||||
match self.fields.get(name) {
|
||||
Some(serde_json::Value::String(s)) => s == expected,
|
||||
Some(serde_json::Value::Bool(b)) => (if *b { "true" } else { "false" }) == expected,
|
||||
Some(serde_json::Value::Null) => expected == "null",
|
||||
Some(other) => other.to_string().as_str() == expected,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-file parse accounting. Merged across files by ingest.
|
||||
|
||||
@@ -84,9 +84,15 @@ static FIELD_IN_TEXT: LazyLock<Regex> = LazyLock::new(|| {
|
||||
/// `h:` + first 16 hex chars (64 bits) of sha256 — long enough that
|
||||
/// collisions across a report's identifiers stay negligible.
|
||||
pub(crate) fn hash_value(value: &str) -> String {
|
||||
const HEX: &[u8; 16] = b"0123456789abcdef";
|
||||
let digest = Sha256::digest(value.as_bytes());
|
||||
let hex: String = digest.iter().take(8).map(|b| format!("{b:02x}")).collect();
|
||||
format!("h:{hex}")
|
||||
let mut out = String::with_capacity(18);
|
||||
out.push_str("h:");
|
||||
for &b in &digest[..8] {
|
||||
out.push(char::from(HEX[usize::from(b >> 4)]));
|
||||
out.push(char::from(HEX[usize::from(b & 0x0f)]));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) fn redact_text(text: &str) -> String {
|
||||
|
||||
@@ -56,7 +56,7 @@ fn eval(matcher: &CompiledMatcher, event: &LogEvent) -> bool {
|
||||
CompiledMatcher::MessagePrefix(p) => event.message.starts_with(p.as_str()),
|
||||
CompiledMatcher::MessageContains(c) => event.message.contains(c.as_str()),
|
||||
CompiledMatcher::MessageRegex(re) => re.is_match(&event.message),
|
||||
CompiledMatcher::FieldEquals { name, value } => event.field_display(name).as_deref() == Some(value.as_str()),
|
||||
CompiledMatcher::FieldEquals { name, value } => event.field_display_eq(name, value),
|
||||
CompiledMatcher::TargetPrefix(p) => event.target.as_deref().is_some_and(|t| t.starts_with(p.as_str())),
|
||||
CompiledMatcher::IsPanic => event.kind == EventKind::Panic,
|
||||
CompiledMatcher::MinLevel(level) => event.level.is_some_and(|l| l >= *level),
|
||||
|
||||
Reference in New Issue
Block a user