rustkernel-core 0.4.0

Core abstractions, traits, and registry for RustKernels GPU kernel library
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Structured Logging
//!
//! Provides structured logging with kernel context for production debugging.
//!
//! # Features
//!
//! - JSON structured output for log aggregation
//! - Context propagation (trace IDs, tenant IDs)
//! - Per-domain log levels
//! - Audit logging for security events
//!
//! # Example
//!
//! ```rust,ignore
//! use rustkernel_core::observability::logging::{LogConfig, StructuredLogger};
//!
//! let config = LogConfig::production();
//! config.init()?;
//!
//! StructuredLogger::info()
//!     .kernel("graph/pagerank")
//!     .tenant("tenant-123")
//!     .message("Kernel execution completed")
//!     .field("latency_us", 150)
//!     .log();
//! ```

use serde::{Deserialize, Serialize};

/// Log level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
    /// Trace level (most verbose)
    Trace,
    /// Debug level
    Debug,
    /// Info level
    #[default]
    Info,
    /// Warning level
    Warn,
    /// Error level
    Error,
}

impl std::fmt::Display for LogLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Trace => write!(f, "trace"),
            Self::Debug => write!(f, "debug"),
            Self::Info => write!(f, "info"),
            Self::Warn => write!(f, "warn"),
            Self::Error => write!(f, "error"),
        }
    }
}

impl std::str::FromStr for LogLevel {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "trace" => Ok(Self::Trace),
            "debug" => Ok(Self::Debug),
            "info" => Ok(Self::Info),
            "warn" | "warning" => Ok(Self::Warn),
            "error" => Ok(Self::Error),
            _ => Err(format!("Invalid log level: {}", s)),
        }
    }
}

impl From<LogLevel> for tracing::Level {
    fn from(level: LogLevel) -> Self {
        match level {
            LogLevel::Trace => tracing::Level::TRACE,
            LogLevel::Debug => tracing::Level::DEBUG,
            LogLevel::Info => tracing::Level::INFO,
            LogLevel::Warn => tracing::Level::WARN,
            LogLevel::Error => tracing::Level::ERROR,
        }
    }
}

/// Logging configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogConfig {
    /// Default log level
    pub level: LogLevel,
    /// Enable structured JSON output
    pub structured: bool,
    /// Include timestamps
    pub include_timestamps: bool,
    /// Include caller location
    pub include_location: bool,
    /// Include thread IDs
    pub include_thread_ids: bool,
    /// Per-domain log levels
    pub domain_levels: std::collections::HashMap<String, LogLevel>,
    /// Output target (stdout, stderr, file path)
    pub output: LogOutput,
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            level: LogLevel::Info,
            structured: false,
            include_timestamps: true,
            include_location: false,
            include_thread_ids: false,
            domain_levels: std::collections::HashMap::new(),
            output: LogOutput::Stdout,
        }
    }
}

impl LogConfig {
    /// Development configuration
    pub fn development() -> Self {
        Self {
            level: LogLevel::Debug,
            structured: false,
            include_location: true,
            ..Default::default()
        }
    }

    /// Production configuration
    pub fn production() -> Self {
        Self {
            level: LogLevel::Info,
            structured: true,
            include_timestamps: true,
            include_thread_ids: true,
            ..Default::default()
        }
    }

    /// Set log level for a specific domain
    pub fn with_domain_level(mut self, domain: impl Into<String>, level: LogLevel) -> Self {
        self.domain_levels.insert(domain.into(), level);
        self
    }

    /// Initialize logging
    pub fn init(&self) -> crate::error::Result<()> {
        use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};

        let filter = EnvFilter::try_from_default_env()
            .unwrap_or_else(|_| EnvFilter::new(self.level.to_string()));

        let subscriber = tracing_subscriber::registry().with(filter);

        if self.structured {
            let layer = fmt::layer()
                .json()
                .with_thread_ids(self.include_thread_ids)
                .with_file(self.include_location)
                .with_line_number(self.include_location);

            subscriber.with(layer).try_init().ok();
        } else {
            let layer = fmt::layer()
                .with_thread_ids(self.include_thread_ids)
                .with_file(self.include_location)
                .with_line_number(self.include_location);

            subscriber.with(layer).try_init().ok();
        }

        Ok(())
    }
}

/// Log output target
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogOutput {
    /// Standard output
    #[default]
    Stdout,
    /// Standard error
    Stderr,
    /// File path
    File(String),
}

/// Structured logger builder
pub struct StructuredLogger {
    level: LogLevel,
    message: Option<String>,
    kernel_id: Option<String>,
    domain: Option<String>,
    tenant_id: Option<String>,
    trace_id: Option<String>,
    span_id: Option<String>,
    fields: std::collections::HashMap<String, serde_json::Value>,
}

impl StructuredLogger {
    /// Create a new logger at trace level
    pub fn trace() -> Self {
        Self::new(LogLevel::Trace)
    }

    /// Create a new logger at debug level
    pub fn debug() -> Self {
        Self::new(LogLevel::Debug)
    }

    /// Create a new logger at info level
    pub fn info() -> Self {
        Self::new(LogLevel::Info)
    }

    /// Create a new logger at warn level
    pub fn warn() -> Self {
        Self::new(LogLevel::Warn)
    }

    /// Create a new logger at error level
    pub fn error() -> Self {
        Self::new(LogLevel::Error)
    }

    fn new(level: LogLevel) -> Self {
        Self {
            level,
            message: None,
            kernel_id: None,
            domain: None,
            tenant_id: None,
            trace_id: None,
            span_id: None,
            fields: std::collections::HashMap::new(),
        }
    }

    /// Set the message
    pub fn message(mut self, msg: impl Into<String>) -> Self {
        self.message = Some(msg.into());
        self
    }

    /// Set the kernel ID
    pub fn kernel(mut self, id: impl Into<String>) -> Self {
        self.kernel_id = Some(id.into());
        self
    }

    /// Set the domain
    pub fn domain(mut self, domain: impl Into<String>) -> Self {
        self.domain = Some(domain.into());
        self
    }

    /// Set the tenant ID
    pub fn tenant(mut self, tenant: impl Into<String>) -> Self {
        self.tenant_id = Some(tenant.into());
        self
    }

    /// Set trace context
    pub fn trace_context(
        mut self,
        trace_id: impl Into<String>,
        span_id: impl Into<String>,
    ) -> Self {
        self.trace_id = Some(trace_id.into());
        self.span_id = Some(span_id.into());
        self
    }

    /// Add a field
    pub fn field(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
        if let Ok(json_value) = serde_json::to_value(value) {
            self.fields.insert(key.into(), json_value);
        }
        self
    }

    /// Emit the log
    pub fn log(self) {
        let msg = self.message.unwrap_or_default();

        match self.level {
            LogLevel::Trace => tracing::trace!(
                kernel_id = ?self.kernel_id,
                domain = ?self.domain,
                tenant_id = ?self.tenant_id,
                trace_id = ?self.trace_id,
                span_id = ?self.span_id,
                "{}",
                msg
            ),
            LogLevel::Debug => tracing::debug!(
                kernel_id = ?self.kernel_id,
                domain = ?self.domain,
                tenant_id = ?self.tenant_id,
                trace_id = ?self.trace_id,
                span_id = ?self.span_id,
                "{}",
                msg
            ),
            LogLevel::Info => tracing::info!(
                kernel_id = ?self.kernel_id,
                domain = ?self.domain,
                tenant_id = ?self.tenant_id,
                trace_id = ?self.trace_id,
                span_id = ?self.span_id,
                "{}",
                msg
            ),
            LogLevel::Warn => tracing::warn!(
                kernel_id = ?self.kernel_id,
                domain = ?self.domain,
                tenant_id = ?self.tenant_id,
                trace_id = ?self.trace_id,
                span_id = ?self.span_id,
                "{}",
                msg
            ),
            LogLevel::Error => tracing::error!(
                kernel_id = ?self.kernel_id,
                domain = ?self.domain,
                tenant_id = ?self.tenant_id,
                trace_id = ?self.trace_id,
                span_id = ?self.span_id,
                "{}",
                msg
            ),
        }
    }
}

/// Audit log entry for security-relevant events
#[derive(Debug, Clone, Serialize)]
pub struct AuditLog {
    /// Event timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Event type
    pub event_type: AuditEventType,
    /// Actor (user ID or system)
    pub actor: String,
    /// Resource being accessed
    pub resource: String,
    /// Action performed
    pub action: String,
    /// Result (success/failure)
    pub result: AuditResult,
    /// Additional details
    pub details: Option<serde_json::Value>,
    /// Tenant ID
    pub tenant_id: Option<String>,
    /// Request ID
    pub request_id: Option<String>,
}

impl AuditLog {
    /// Create a new audit log entry
    pub fn new(
        event_type: AuditEventType,
        actor: impl Into<String>,
        resource: impl Into<String>,
        action: impl Into<String>,
    ) -> Self {
        Self {
            timestamp: chrono::Utc::now(),
            event_type,
            actor: actor.into(),
            resource: resource.into(),
            action: action.into(),
            result: AuditResult::Success,
            details: None,
            tenant_id: None,
            request_id: None,
        }
    }

    /// Set the result
    pub fn with_result(mut self, result: AuditResult) -> Self {
        self.result = result;
        self
    }

    /// Set details
    pub fn with_details(mut self, details: impl Serialize) -> Self {
        self.details = serde_json::to_value(details).ok();
        self
    }

    /// Set tenant
    pub fn with_tenant(mut self, tenant: impl Into<String>) -> Self {
        self.tenant_id = Some(tenant.into());
        self
    }

    /// Set request ID
    pub fn with_request_id(mut self, id: impl Into<String>) -> Self {
        self.request_id = Some(id.into());
        self
    }

    /// Emit the audit log
    pub fn emit(self) {
        tracing::info!(
            target: "audit",
            event_type = ?self.event_type,
            actor = %self.actor,
            resource = %self.resource,
            action = %self.action,
            result = ?self.result,
            tenant_id = ?self.tenant_id,
            request_id = ?self.request_id,
            "AUDIT"
        );
    }
}

/// Audit event types
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditEventType {
    /// Authentication event
    Authentication,
    /// Authorization event
    Authorization,
    /// Kernel access
    KernelAccess,
    /// Configuration change
    ConfigChange,
    /// Data access
    DataAccess,
    /// Administrative action
    AdminAction,
}

/// Audit result
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditResult {
    /// Action succeeded
    Success,
    /// Action failed
    Failure,
    /// Action denied
    Denied,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_log_level_parsing() {
        assert_eq!("debug".parse::<LogLevel>().unwrap(), LogLevel::Debug);
        assert_eq!("INFO".parse::<LogLevel>().unwrap(), LogLevel::Info);
        assert_eq!("warning".parse::<LogLevel>().unwrap(), LogLevel::Warn);
    }

    #[test]
    fn test_log_config() {
        let config = LogConfig::production();
        assert!(config.structured);
        assert_eq!(config.level, LogLevel::Info);

        let dev_config = LogConfig::development();
        assert!(!dev_config.structured);
        assert_eq!(dev_config.level, LogLevel::Debug);
    }

    #[test]
    fn test_structured_logger() {
        // Just test that it builds correctly
        let logger = StructuredLogger::info()
            .message("Test message")
            .kernel("graph/pagerank")
            .tenant("tenant-123")
            .field("latency_us", 150);

        assert!(logger.message.is_some());
        assert!(logger.kernel_id.is_some());
    }

    #[test]
    fn test_audit_log() {
        let audit = AuditLog::new(
            AuditEventType::KernelAccess,
            "user-123",
            "graph/pagerank",
            "execute",
        )
        .with_result(AuditResult::Success)
        .with_tenant("tenant-456");

        assert_eq!(audit.actor, "user-123");
        assert!(audit.tenant_id.is_some());
    }
}