Spaces:
Sleeping
Sleeping
File size: 7,373 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | use napi_derive::napi;
use serde::Serialize;
use serde_json::Value;
use std::sync::{Arc, Mutex};
use tracing::field::{Field, Visit};
use tracing::Level;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Layer;
/// Context passed from TypeScript to continue the trace.
#[derive(Clone)]
#[napi(object)]
pub struct NativeContext {
pub scrape_id: String,
pub url: String,
}
/// A single log entry captured during Rust execution.
#[derive(Clone, Debug, Serialize)]
#[napi(object)]
pub struct NativeLogEntry {
pub level: String,
pub target: String,
pub message: String,
pub fields: Value,
pub timestamp_ms: f64,
}
struct LogCollector {
logs: Arc<Mutex<Vec<NativeLogEntry>>>,
}
struct FieldVisitor {
fields: serde_json::Map<String, Value>,
message: Option<String>,
}
impl Visit for FieldVisitor {
fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
if field.name() == "message" {
self.message = Some(format!("{:?}", value));
} else {
self
.fields
.insert(field.name().to_string(), Value::String(format!("{:?}", value)));
}
}
fn record_str(&mut self, field: &Field, value: &str) {
if field.name() == "message" {
self.message = Some(value.to_string());
} else {
self
.fields
.insert(field.name().to_string(), Value::String(value.to_string()));
}
}
fn record_i64(&mut self, field: &Field, value: i64) {
self
.fields
.insert(field.name().to_string(), Value::Number(value.into()));
}
fn record_u64(&mut self, field: &Field, value: u64) {
self
.fields
.insert(field.name().to_string(), Value::Number(value.into()));
}
fn record_f64(&mut self, field: &Field, value: f64) {
if let Some(n) = serde_json::Number::from_f64(value) {
self
.fields
.insert(field.name().to_string(), Value::Number(n));
}
}
fn record_bool(&mut self, field: &Field, value: bool) {
self
.fields
.insert(field.name().to_string(), Value::Bool(value));
}
}
impl<S: tracing::Subscriber> Layer<S> for LogCollector {
fn on_event(&self, event: &tracing::Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) {
let mut visitor = FieldVisitor {
fields: serde_json::Map::new(),
message: None,
};
event.record(&mut visitor);
let level = match *event.metadata().level() {
Level::ERROR => "error",
Level::WARN => "warn",
Level::INFO => "info",
Level::DEBUG => "debug",
Level::TRACE => "trace",
};
let entry = NativeLogEntry {
level: level.to_string(),
target: event.metadata().target().to_string(),
message: visitor.message.unwrap_or_default(),
fields: Value::Object(visitor.fields),
timestamp_ms: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs_f64() * 1000.0)
.unwrap_or(0.0),
};
if let Ok(mut logs) = self.logs.lock() {
logs.push(entry);
}
}
}
#[derive(Debug)]
pub struct TracingResult<T> {
pub value: T,
pub logs: Vec<NativeLogEntry>,
}
/// Run a closure with tracing enabled, capturing all log events.
/// Wraps the closure in `catch_unwind` for panic safety.
///
/// Returns `TracingResult<napi::Result<T>>` so that logs are **always**
/// available — even when the closure returns `Err` or panics.
pub fn with_native_tracing<T, F>(
ctx: Option<&NativeContext>,
module: &str,
f: F,
) -> TracingResult<napi::Result<T>>
where
F: FnOnce() -> napi::Result<T>,
{
let logs = Arc::new(Mutex::new(Vec::new()));
let collector = LogCollector { logs: logs.clone() };
let subscriber = tracing_subscriber::Registry::default().with(collector);
let result = tracing::subscriber::with_default(subscriber, || {
let _span = match ctx {
Some(c) => tracing::info_span!(
"native",
scrape_id = %c.scrape_id,
url = %c.url,
module = %module,
)
.entered(),
None => tracing::info_span!("native", module = %module).entered(),
};
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
Ok(result) => result,
Err(panic_info) => {
let msg = if let Some(s) = panic_info.downcast_ref::<&str>() {
s.to_string()
} else if let Some(s) = panic_info.downcast_ref::<String>() {
s.clone()
} else {
"unknown panic".to_string()
};
let backtrace = std::backtrace::Backtrace::force_capture();
tracing::error!(
panic = true,
backtrace = %backtrace,
"native panic in {}: {}", module, msg,
);
Err(napi::Error::new(
napi::Status::GenericFailure,
format!("Rust panic in {module}: {msg}\nBacktrace:\n{backtrace}"),
))
}
}
});
let collected = logs.lock().map(|l| l.clone()).unwrap_or_default();
TracingResult {
value: result,
logs: collected,
}
}
/// Append serialized logs to a NAPI error so they survive the FFI boundary.
/// The TS side can extract them from `error.message` via `extractNativeLogs`.
pub fn embed_logs_in_error(err: napi::Error, logs: &[NativeLogEntry]) -> napi::Error {
if logs.is_empty() {
return err;
}
if let Ok(logs_json) = serde_json::to_string(logs) {
napi::Error::new(
err.status,
format!("{}\n__native_logs__:{logs_json}", err.reason),
)
} else {
err
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_collects_logs() {
let traced = with_native_tracing(None, "test", || {
tracing::info!("hello from rust");
Ok(42)
});
let value = traced.value.unwrap();
assert_eq!(value, 42);
assert_eq!(traced.logs.len(), 1);
assert_eq!(traced.logs[0].level, "info");
assert!(traced.logs[0].message.contains("hello from rust"));
}
#[test]
fn test_with_context() {
let ctx = NativeContext {
scrape_id: "test-123".to_string(),
url: "https://example.com".to_string(),
};
let traced = with_native_tracing(Some(&ctx), "pdf", || {
tracing::warn!("something odd");
Ok("ok")
});
assert_eq!(traced.value.unwrap(), "ok");
assert_eq!(traced.logs.len(), 1);
assert_eq!(traced.logs[0].level, "warn");
}
#[test]
fn test_captures_panic_with_logs() {
let traced: TracingResult<napi::Result<()>> = with_native_tracing(None, "test", || {
panic!("test panic");
});
assert!(traced.value.is_err());
let err = traced.value.unwrap_err();
assert!(err.reason.contains("test panic"));
assert!(err.reason.contains("Backtrace"));
// Panic log is preserved even though the closure failed
assert!(!traced.logs.is_empty());
assert_eq!(traced.logs[0].level, "error");
assert!(traced.logs[0].message.contains("test panic"));
}
#[test]
fn test_error_preserves_logs() {
let traced: TracingResult<napi::Result<()>> = with_native_tracing(None, "test", || {
tracing::info!("before error");
Err(napi::Error::new(
napi::Status::GenericFailure,
"test error",
))
});
assert!(traced.value.is_err());
// Logs are preserved even on error paths
assert_eq!(traced.logs.len(), 1);
assert_eq!(traced.logs[0].level, "info");
assert!(traced.logs[0].message.contains("before error"));
}
}
|