Skip to content

Commit dcbc902

Browse files
committed
refactor(sandbox): replace perf_log with tracing::debug
Replace the custom file-based perf_log() helper with standard tracing::debug!() macros as requested in PR review. This removes the custom log file writes to /var/log/openshell-perf.log and routes all performance timing through the tracing framework at DEBUG level, consistent with the rest of the codebase. Made-with: Cursor
1 parent fa2dd3f commit dcbc902

File tree

3 files changed

+52
-104
lines changed

3 files changed

+52
-104
lines changed

crates/openshell-sandbox/src/identity.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,7 @@ use std::fs::Metadata;
1616
use std::os::unix::fs::MetadataExt;
1717
use std::path::{Path, PathBuf};
1818
use std::sync::Mutex;
19-
20-
fn perf_log(msg: &str) {
21-
use std::io::Write;
22-
for path in &["/var/log/openshell-perf.log", "/tmp/openshell-perf.log"] {
23-
if let Ok(mut f) = std::fs::OpenOptions::new()
24-
.create(true)
25-
.append(true)
26-
.open(path)
27-
{
28-
let now = std::time::SystemTime::now()
29-
.duration_since(std::time::UNIX_EPOCH)
30-
.unwrap_or_default();
31-
let _ = writeln!(f, "[{:.3}] {}", now.as_secs_f64(), msg);
32-
return;
33-
}
34-
}
35-
eprintln!("PERF_LOG_FALLBACK: {msg}");
36-
}
19+
use tracing::debug;
3720

3821
#[derive(Clone)]
3922
struct FileFingerprint {
@@ -133,24 +116,24 @@ impl BinaryIdentityCache {
133116
if let Some(cached_binary) = &cached
134117
&& cached_binary.fingerprint == fingerprint
135118
{
136-
perf_log(&format!(
119+
debug!(
137120
" verify_or_cache: {}ms CACHE HIT path={}",
138121
start.elapsed().as_millis(), path.display()
139-
));
122+
);
140123
return Ok(cached_binary.hash.clone());
141124
}
142125

143-
perf_log(&format!(
126+
debug!(
144127
" verify_or_cache: CACHE MISS size={} path={}",
145128
metadata.len(), path.display()
146-
));
129+
);
147130

148131
let hash_start = std::time::Instant::now();
149132
let current_hash = hash_file(path)?;
150-
perf_log(&format!(
133+
debug!(
151134
" verify_or_cache SHA256: {}ms path={}",
152135
hash_start.elapsed().as_millis(), path.display()
153-
));
136+
);
154137

155138
let mut hashes = self
156139
.hashes
@@ -176,10 +159,10 @@ impl BinaryIdentityCache {
176159
},
177160
);
178161

179-
perf_log(&format!(
162+
debug!(
180163
" verify_or_cache TOTAL (cold): {}ms path={}",
181164
start.elapsed().as_millis(), path.display()
182-
));
165+
);
183166

184167
Ok(current_hash)
185168
}

crates/openshell-sandbox/src/procfs.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,7 @@ use miette::{IntoDiagnostic, Result};
1010
use std::path::Path;
1111
#[cfg(target_os = "linux")]
1212
use std::path::PathBuf;
13-
14-
fn perf_log(msg: &str) {
15-
use std::io::Write;
16-
for path in &["/var/log/openshell-perf.log", "/tmp/openshell-perf.log"] {
17-
if let Ok(mut f) = std::fs::OpenOptions::new()
18-
.create(true)
19-
.append(true)
20-
.open(path)
21-
{
22-
let now = std::time::SystemTime::now()
23-
.duration_since(std::time::UNIX_EPOCH)
24-
.unwrap_or_default();
25-
let _ = writeln!(f, "[{:.3}] {}", now.as_secs_f64(), msg);
26-
return;
27-
}
28-
}
29-
eprintln!("PERF_LOG_FALLBACK: {msg}");
30-
}
13+
use tracing::debug;
3114

3215
/// Read the binary path of a process via `/proc/{pid}/exe` symlink.
3316
///
@@ -70,29 +53,29 @@ pub fn resolve_tcp_peer_identity(entrypoint_pid: u32, peer_port: u16) -> Result<
7053

7154
let phase = std::time::Instant::now();
7255
let inode = parse_proc_net_tcp(entrypoint_pid, peer_port)?;
73-
perf_log(&format!(
56+
debug!(
7457
" parse_proc_net_tcp: {}ms inode={}",
7558
phase.elapsed().as_millis(), inode
76-
));
59+
);
7760

7861
let phase = std::time::Instant::now();
7962
let pid = find_pid_by_socket_inode(inode, entrypoint_pid)?;
80-
perf_log(&format!(
63+
debug!(
8164
" find_pid_by_socket_inode: {}ms pid={}",
8265
phase.elapsed().as_millis(), pid
83-
));
66+
);
8467

8568
let phase = std::time::Instant::now();
8669
let path = binary_path(pid.cast_signed())?;
87-
perf_log(&format!(
70+
debug!(
8871
" binary_path: {}ms path={}",
8972
phase.elapsed().as_millis(), path.display()
90-
));
73+
);
9174

92-
perf_log(&format!(
75+
debug!(
9376
" resolve_tcp_peer_identity TOTAL: {}ms",
9477
start.elapsed().as_millis()
95-
));
78+
);
9679
Ok((path, pid))
9780
}
9881

@@ -274,25 +257,25 @@ fn find_pid_by_socket_inode(inode: u64, entrypoint_pid: u32) -> Result<u32> {
274257

275258
let phase = std::time::Instant::now();
276259
let descendants = collect_descendant_pids(entrypoint_pid);
277-
perf_log(&format!(
260+
debug!(
278261
" collect_descendant_pids: {}ms count={}",
279262
phase.elapsed().as_millis(), descendants.len()
280-
));
263+
);
281264

282265
let phase = std::time::Instant::now();
283266
for &pid in &descendants {
284267
if let Some(found) = check_pid_fds(pid, &target) {
285-
perf_log(&format!(
268+
debug!(
286269
" find_pid_by_socket_inode: {}ms found_pid={} scan=descendants",
287270
start.elapsed().as_millis(), found
288-
));
271+
);
289272
return Ok(found);
290273
}
291274
}
292-
perf_log(&format!(
275+
debug!(
293276
" descendant_fd_scan (not found): {}ms",
294277
phase.elapsed().as_millis()
295-
));
278+
);
296279

297280
let phase = std::time::Instant::now();
298281
if let Ok(proc_dir) = std::fs::read_dir("/proc") {
@@ -306,18 +289,18 @@ fn find_pid_by_socket_inode(inode: u64, entrypoint_pid: u32) -> Result<u32> {
306289
continue;
307290
}
308291
if let Some(found) = check_pid_fds(pid, &target) {
309-
perf_log(&format!(
292+
debug!(
310293
" find_pid_by_socket_inode: {}ms found_pid={} scan=full_proc",
311294
start.elapsed().as_millis(), found
312-
));
295+
);
313296
return Ok(found);
314297
}
315298
}
316299
}
317-
perf_log(&format!(
300+
debug!(
318301
" full_proc_scan (not found): {}ms",
319302
phase.elapsed().as_millis()
320-
));
303+
);
321304

322305
Err(miette::miette!(
323306
"No process found owning socket inode {} \
@@ -399,10 +382,10 @@ pub fn file_sha256(path: &Path) -> Result<String> {
399382
}
400383

401384
let hash = hasher.finalize();
402-
perf_log(&format!(
385+
debug!(
403386
" file_sha256: {}ms size={} path={}",
404387
start.elapsed().as_millis(), total_read, path.display()
405-
));
388+
);
406389
Ok(hex::encode(hash))
407390
}
408391

crates/openshell-sandbox/src/proxy.rs

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,6 @@ use tokio::sync::mpsc;
2020
use tokio::task::JoinHandle;
2121
use tracing::{debug, info, warn};
2222

23-
fn perf_log(msg: &str) {
24-
use std::io::Write;
25-
for path in &["/var/log/openshell-perf.log", "/tmp/openshell-perf.log"] {
26-
if let Ok(mut f) = std::fs::OpenOptions::new()
27-
.create(true)
28-
.append(true)
29-
.open(path)
30-
{
31-
let now = std::time::SystemTime::now()
32-
.duration_since(std::time::UNIX_EPOCH)
33-
.unwrap_or_default();
34-
let _ = writeln!(f, "[{:.3}] {}", now.as_secs_f64(), msg);
35-
return;
36-
}
37-
}
38-
eprintln!("PERF_LOG_FALLBACK: {msg}");
39-
}
40-
4123
const MAX_HEADER_BYTES: usize = 8192;
4224
const INFERENCE_LOCAL_HOST: &str = "inference.local";
4325

@@ -355,7 +337,7 @@ async fn handle_tcp_connection(
355337
let local_addr = client.local_addr().into_diagnostic()?;
356338

357339
let connect_start = std::time::Instant::now();
358-
perf_log(&format!("handle_tcp_connection START host={host_lc} port={port}"));
340+
debug!("handle_tcp_connection START host={host_lc} port={port}");
359341

360342
// Evaluate OPA policy with process-identity binding.
361343
// Wrapped in spawn_blocking because identity resolution does heavy sync I/O:
@@ -376,10 +358,10 @@ async fn handle_tcp_connection(
376358
})
377359
.await
378360
.map_err(|e| miette::miette!("identity resolution task panicked: {e}"))?;
379-
perf_log(&format!(
361+
debug!(
380362
"handle_tcp_connection evaluate_opa_tcp: {}ms",
381363
connect_start.elapsed().as_millis()
382-
));
364+
);
383365

384366
// Extract action string and matched policy for logging
385367
let (matched_policy, deny_reason) = match &decision.action {
@@ -533,10 +515,10 @@ async fn handle_tcp_connection(
533515
}
534516
};
535517

536-
perf_log(&format!(
518+
debug!(
537519
"handle_tcp_connection dns_resolve_and_tcp_connect: {}ms host={host_lc}",
538520
dns_connect_start.elapsed().as_millis()
539-
));
521+
);
540522

541523
respond(&mut client, b"HTTP/1.1 200 Connection Established\r\n\r\n").await?;
542524

@@ -744,7 +726,7 @@ fn evaluate_opa_tcp(
744726

745727
let total_start = std::time::Instant::now();
746728
let peer_port = peer_addr.port();
747-
perf_log(&format!("evaluate_opa_tcp START host={host} port={port}"));
729+
debug!("evaluate_opa_tcp START host={host} port={port}");
748730

749731
let phase_start = std::time::Instant::now();
750732
let (bin_path, binary_pid) = match crate::procfs::resolve_tcp_peer_identity(pid, peer_port) {
@@ -759,12 +741,12 @@ fn evaluate_opa_tcp(
759741
);
760742
}
761743
};
762-
perf_log(&format!(
744+
debug!(
763745
" resolve_tcp_peer_identity: {}ms binary={} pid={}",
764746
phase_start.elapsed().as_millis(),
765747
bin_path.display(),
766748
binary_pid
767-
));
749+
);
768750

769751
let phase_start = std::time::Instant::now();
770752
let bin_hash = match identity_cache.verify_or_cache(&bin_path) {
@@ -779,19 +761,19 @@ fn evaluate_opa_tcp(
779761
);
780762
}
781763
};
782-
perf_log(&format!(
764+
debug!(
783765
" tofu_verify_binary: {}ms binary={}",
784766
phase_start.elapsed().as_millis(),
785767
bin_path.display()
786-
));
768+
);
787769

788770
let phase_start = std::time::Instant::now();
789771
let ancestors = crate::procfs::collect_ancestor_binaries(binary_pid, pid);
790-
perf_log(&format!(
772+
debug!(
791773
" collect_ancestor_binaries: {}ms count={}",
792774
phase_start.elapsed().as_millis(),
793775
ancestors.len()
794-
));
776+
);
795777

796778
let phase_start = std::time::Instant::now();
797779
for ancestor in &ancestors {
@@ -808,25 +790,25 @@ fn evaluate_opa_tcp(
808790
vec![],
809791
);
810792
}
811-
perf_log(&format!(
793+
debug!(
812794
" tofu_verify_ancestor: {}ms ancestor={}",
813795
ancestor_start.elapsed().as_millis(),
814796
ancestor.display()
815-
));
797+
);
816798
}
817-
perf_log(&format!(
799+
debug!(
818800
" tofu_verify_all_ancestors: {}ms",
819801
phase_start.elapsed().as_millis()
820-
));
802+
);
821803

822804
let phase_start = std::time::Instant::now();
823805
let mut exclude = ancestors.clone();
824806
exclude.push(bin_path.clone());
825807
let cmdline_paths = crate::procfs::collect_cmdline_paths(binary_pid, pid, &exclude);
826-
perf_log(&format!(
808+
debug!(
827809
" collect_cmdline_paths: {}ms",
828810
phase_start.elapsed().as_millis()
829-
));
811+
);
830812

831813
let phase_start = std::time::Instant::now();
832814
let input = NetworkInput {
@@ -854,14 +836,14 @@ fn evaluate_opa_tcp(
854836
cmdline_paths,
855837
),
856838
};
857-
perf_log(&format!(
839+
debug!(
858840
" opa_evaluate_network_action: {}ms",
859841
phase_start.elapsed().as_millis()
860-
));
861-
perf_log(&format!(
842+
);
843+
debug!(
862844
"evaluate_opa_tcp TOTAL: {}ms host={host} port={port}",
863845
total_start.elapsed().as_millis()
864-
));
846+
);
865847
result
866848
}
867849

0 commit comments

Comments
 (0)