improve code for obs

This commit is contained in:
houseme
2025-04-09 15:12:31 +08:00
parent dd5d84cc42
commit a07ca8ae81
17 changed files with 26 additions and 15 deletions

View File

@@ -11,9 +11,10 @@ members = [
"iam", # Identity and Access Management
"crypto", # Cryptography and security features
"cli/rustfs-gui", # Graphical user interface client
"packages/obs", # Observability utilities
"crates/obs", # Observability utilities
"s3select/api",
"s3select/query", "appauth",
"s3select/query",
"appauth",
]
resolver = "2"
@@ -88,7 +89,7 @@ reqwest = { version = "0.12.15", default-features = false, features = ["rustls-t
rfd = { version = "0.15.2", default-features = false, features = ["xdg-portal", "tokio"] }
rmp = "0.8.14"
rmp-serde = "1.3.0"
rustfs-obs = { path = "packages/obs", version = "0.0.1" }
rustfs-obs = { path = "crates/obs", version = "0.0.1" }
rust-embed = "8.6.0"
rustls = { version = "0.23" }
rustls-pki-types = "1.11.0"

View File

@@ -46,10 +46,17 @@ impl ObjectVersion {
}
}
impl Default for ObjectVersion {
fn default() -> Self {
Self::new()
}
}
/// Log kind/level enum
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum LogKind {
#[serde(rename = "INFO")]
#[default]
Info,
#[serde(rename = "WARNING")]
Warning,
@@ -59,12 +66,6 @@ pub enum LogKind {
Fatal,
}
impl Default for LogKind {
fn default() -> Self {
LogKind::Info
}
}
/// Trait for types that can be serialized to JSON and have a timestamp
/// This trait is used by `ServerLogEntry` to convert the log entry to JSON
/// and get the timestamp of the log entry

View File

@@ -99,6 +99,7 @@ impl LogRecord for ServerLogEntry {
/// - `console_msg` - the console message
/// - `node_name` - the node name
/// - `err` - the error message
///
/// The `ConsoleLogEntry` structure contains the following methods:
/// - `new` - create a new `ConsoleLogEntry`
/// - `new_with_console_msg` - create a new `ConsoleLogEntry` with console message and node name
@@ -107,6 +108,7 @@ impl LogRecord for ServerLogEntry {
/// - `set_node_name` - set the node name
/// - `set_console_msg` - set the console message
/// - `set_err` - set the error message
///
/// # Example
/// ```
/// use rustfs_obs::ConsoleLogEntry;
@@ -180,6 +182,12 @@ impl ConsoleLogEntry {
}
}
impl Default for ConsoleLogEntry {
fn default() -> Self {
Self::new()
}
}
impl LogRecord for ConsoleLogEntry {
fn to_json(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|_| String::from("{}"))
@@ -217,7 +225,7 @@ pub enum UnifiedLogEntry {
Server(ServerLogEntry),
#[serde(rename = "audit")]
Audit(AuditLogEntry),
Audit(Box<AuditLogEntry>),
#[serde(rename = "console")]
Console(ConsoleLogEntry),

View File

@@ -50,7 +50,7 @@ impl Logger {
/// Log an audit entry
#[tracing::instrument(skip(self), fields(log_source = "logger_audit"))]
pub async fn log_audit_entry(&self, entry: AuditLogEntry) -> Result<(), LogError> {
self.log_entry(UnifiedLogEntry::Audit(entry)).await
self.log_entry(UnifiedLogEntry::Audit(Box::new(entry))).await
}
/// Log a console entry
@@ -61,13 +61,14 @@ impl Logger {
/// Asynchronous logging of unified log entries
#[tracing::instrument(skip(self), fields(log_source = "logger"))]
#[tracing::instrument(level = "error", skip_all)]
pub async fn log_entry(&self, entry: UnifiedLogEntry) -> Result<(), LogError> {
// Extract information for tracing based on entry type
match &entry {
UnifiedLogEntry::Server(server) => {
tracing::Span::current()
.record("log_level", &server.level.0.as_str())
.record("log_message", &server.base.message.as_deref().unwrap_or(""))
.record("log_level", server.level.0.as_str())
.record("log_message", server.base.message.as_deref().unwrap_or("log message not set"))
.record("source", &server.source);
// Generate tracing event based on log level

View File

@@ -284,7 +284,7 @@ impl FileSink {
// If the file does not exist, create it
debug!("FileSink: File does not exist, creating a new file.");
// Create the file and write a header or initial content if needed
OpenOptions::new().create(true).write(true).open(&path).await?
OpenOptions::new().create(true).truncate(true).write(true).open(&path).await?
};
let writer = io::BufWriter::with_capacity(buffer_size, file);
let now = std::time::SystemTime::now()