rustfs_obs/entry/base.rs
1// Copyright 2024 RustFS Team
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
15use chrono::{DateTime, Utc};
16use serde::{Deserialize, Serialize};
17use serde_json::Value;
18use std::collections::HashMap;
19
20/// Base log entry structure shared by all log types
21/// This structure is used to serialize log entries to JSON
22/// and send them to the log sinks
23/// This structure is also used to deserialize log entries from JSON
24/// This structure is also used to store log entries in the database
25/// This structure is also used to query log entries from the database
26///
27/// The `BaseLogEntry` structure contains the following fields:
28/// - `timestamp` - the timestamp of the log entry
29/// - `request_id` - the request ID of the log entry
30/// - `message` - the message of the log entry
31/// - `tags` - the tags of the log entry
32///
33/// The `BaseLogEntry` structure contains the following methods:
34/// - `new` - create a new `BaseLogEntry` with default values
35/// - `message` - set the message
36/// - `request_id` - set the request ID
37/// - `tags` - set the tags
38/// - `timestamp` - set the timestamp
39///
40/// # Example
41/// ```
42/// use rustfs_obs::BaseLogEntry;
43/// use chrono::{DateTime, Utc};
44/// use std::collections::HashMap;
45///
46/// let timestamp = Utc::now();
47/// let request = Some("req-123".to_string());
48/// let message = Some("This is a log message".to_string());
49/// let tags = Some(HashMap::new());
50///
51/// let entry = BaseLogEntry::new()
52/// .timestamp(timestamp)
53/// .request_id(request)
54/// .message(message)
55/// .tags(tags);
56/// ```
57#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Default)]
58pub struct BaseLogEntry {
59 #[serde(rename = "time")]
60 pub timestamp: DateTime<Utc>,
61
62 #[serde(rename = "requestID", skip_serializing_if = "Option::is_none")]
63 pub request_id: Option<String>,
64
65 #[serde(rename = "message", skip_serializing_if = "Option::is_none")]
66 pub message: Option<String>,
67
68 #[serde(rename = "tags", skip_serializing_if = "Option::is_none")]
69 pub tags: Option<HashMap<String, Value>>,
70}
71
72impl BaseLogEntry {
73 /// Create a new BaseLogEntry with default values
74 pub fn new() -> Self {
75 BaseLogEntry {
76 timestamp: Utc::now(),
77 request_id: None,
78 message: None,
79 tags: None,
80 }
81 }
82
83 /// Set the message
84 pub fn message(mut self, message: Option<String>) -> Self {
85 self.message = message;
86 self
87 }
88
89 /// Set the request ID
90 pub fn request_id(mut self, request_id: Option<String>) -> Self {
91 self.request_id = request_id;
92 self
93 }
94
95 /// Set the tags
96 pub fn tags(mut self, tags: Option<HashMap<String, Value>>) -> Self {
97 self.tags = tags;
98 self
99 }
100
101 /// Set the timestamp
102 pub fn timestamp(mut self, timestamp: DateTime<Utc>) -> Self {
103 self.timestamp = timestamp;
104 self
105 }
106}