Skip to content

Commit 23c84b4

Browse files
committed
Include resolved SystemTime in formatted logs
In the formatted logs, include resolved SystemTime, instead of durations from the system boot time in nanoseconds. Signed-off-by: Daiki Ueno <dueno@redhat.com>
1 parent fe99594 commit 23c84b4

File tree

6 files changed

+135
-21
lines changed

6 files changed

+135
-21
lines changed

Cargo.lock

Lines changed: 100 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ serde = { version = "1.0", features = ["derive", "rc"] }
3737
serde_cbor = "0.11"
3838
serde_json = "1.0"
3939
serde_with = "3"
40+
sysinfo = "0.38"
4041
tempfile = "3"
4142
thiserror = "1.0"
4243
time = "0.3"

crypto-auditing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ libc.workspace = true
1212
serde.workspace = true
1313
serde_cbor.workspace = true
1414
serde_with = { workspace = true, features = ["hex"] }
15+
sysinfo.workspace = true
1516
thiserror.workspace = true
1617
tokio = { workspace = true, features = ["net", "rt"] }
1718
tokio-serde.workspace = true

crypto-auditing/src/types.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use std::cell::RefCell;
1010
use std::collections::BTreeMap;
1111
use std::ffi::CStr;
1212
use std::rc::Rc;
13-
use std::time::{Duration, Instant};
13+
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
14+
use sysinfo::System;
1415

1516
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
1617

@@ -29,17 +30,17 @@ where
2930
}
3031

3132
#[serde_as]
32-
#[derive(Debug, Default, Serialize)]
33+
#[derive(Debug, Serialize)]
3334
pub struct Context {
3435
#[serde_as(as = "Hex")]
3536
#[serde(rename = "context")]
3637
pub id: ContextId,
3738
#[serde_as(as = "Hex")]
3839
pub origin: Vec<u8>,
39-
#[serde_as(as = "serde_with::DurationNanoSeconds<u64>")]
40-
pub start: Duration,
41-
#[serde_as(as = "serde_with::DurationNanoSeconds<u64>")]
42-
pub end: Duration,
40+
#[serde_as(as = "serde_with::TimestampSecondsWithFrac<f64>")]
41+
pub start: SystemTime,
42+
#[serde_as(as = "serde_with::TimestampSecondsWithFrac<f64>")]
43+
pub end: SystemTime,
4344
pub events: BTreeMap<String, EventData>,
4445
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
4546
#[serde(serialize_with = "only_values")]
@@ -50,13 +51,19 @@ pub struct Context {
5051
pub struct ContextTracker {
5152
all_contexts: BTreeMap<ContextId, Rc<RefCell<Context>>>,
5253
root_contexts: Vec<(Instant, Rc<RefCell<Context>>)>,
54+
boot_time: SystemTime,
5355
}
5456

5557
impl ContextTracker {
56-
pub fn new() -> Self {
58+
pub fn new(boot_time: Option<SystemTime>) -> Self {
5759
Self {
5860
all_contexts: BTreeMap::new(),
5961
root_contexts: Vec::new(),
62+
boot_time: boot_time.unwrap_or_else(|| {
63+
UNIX_EPOCH
64+
.checked_add(Duration::from_secs(System::boot_time()))
65+
.unwrap()
66+
}),
6067
}
6168
}
6269

@@ -79,6 +86,14 @@ impl ContextTracker {
7986
}
8087

8188
pub fn handle_event_group(&mut self, group: &EventGroup) -> usize {
89+
let start = self
90+
.boot_time
91+
.checked_add(group.start())
92+
.unwrap_or(UNIX_EPOCH);
93+
let end = self
94+
.boot_time
95+
.checked_add(group.end())
96+
.unwrap_or(UNIX_EPOCH);
8297
let mut count = 0;
8398
for event in group.events() {
8499
match event {
@@ -89,9 +104,10 @@ impl ContextTracker {
89104
let context = Rc::new(RefCell::new(Context {
90105
id: *group.context(),
91106
origin: origin.to_owned(),
92-
start: group.start(),
93-
end: group.end(),
94-
..Default::default()
107+
start,
108+
end,
109+
events: Default::default(),
110+
spans: Default::default(),
95111
}));
96112
if let Some(parent) = self.all_contexts.get(&parent_context[..]) {
97113
parent
@@ -112,9 +128,11 @@ impl ContextTracker {
112128
// this message.
113129
let context_obj = Rc::new(RefCell::new(Context {
114130
id: *group.context(),
115-
start: group.start(),
116-
end: group.end(),
117-
..Default::default()
131+
origin: Default::default(),
132+
start,
133+
end,
134+
events: Default::default(),
135+
spans: Default::default(),
118136
}));
119137
self.root_contexts
120138
.push((Instant::now(), context_obj.clone()));

log-parser/src/log_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1919
let cli = Cli::parse();
2020
let log_file = std::fs::File::open(&cli.log_path)
2121
.with_context(|| format!("unable to read file `{}`", cli.log_path.display()))?;
22-
let mut tracker = ContextTracker::new();
22+
let mut tracker = ContextTracker::new(None);
2323
for group in Deserializer::from_reader(&log_file).into_iter::<EventGroup>() {
2424
tracker.handle_event_group(&group?);
2525
}

monitor/src/monitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct Writer {
9999
impl Writer {
100100
fn new(event_window: Duration, scopes: &Vec<String>) -> Self {
101101
Self {
102-
tracker: ContextTracker::new(),
102+
tracker: ContextTracker::new(None),
103103
event_window,
104104
scopes: scopes.to_owned(),
105105
timeouts: JoinSet::new(),

0 commit comments

Comments
 (0)