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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use super::{PipelineContext, ScriptResult, ScriptStage};
use crate::config::TimestampFilterConfig;
use crate::engine::RhaiEngine;
use crate::event::Event;
use crate::rhai_functions::columns;
use anyhow::Result;
/// Filter stage implementation
pub struct FilterStage {
compiled_filter: crate::engine::CompiledExpression,
stage_number: usize,
}
impl FilterStage {
pub fn new(filter: String, engine: &mut RhaiEngine) -> Result<Self> {
let compiled_filter = engine.compile_filter(&filter)?;
Ok(Self {
compiled_filter,
stage_number: 0,
})
}
pub fn with_stage_number(mut self, stage_number: usize) -> Self {
self.stage_number = stage_number;
self
}
}
impl ScriptStage for FilterStage {
fn apply(&mut self, event: Event, ctx: &mut PipelineContext) -> ScriptResult {
// Add stage-specific tracing
if let Some(ref tracer) = ctx.rhai.get_execution_tracer() {
tracer.trace_stage_execution(self.stage_number, "filter");
}
columns::set_parse_cols_strict(ctx.config.strict);
let result = if ctx.window.is_empty() {
// No window context - use standard method
ctx.rhai
.execute_compiled_filter(&self.compiled_filter, &event, &mut ctx.tracker)
} else {
// Window context available - use window-aware method
ctx.rhai.execute_compiled_filter_with_window(
&self.compiled_filter,
&event,
&ctx.window,
&mut ctx.tracker,
)
};
match result {
Ok(result) => {
if result {
ScriptResult::Emit(event)
} else {
ScriptResult::Skip
}
}
Err(e) => {
// Track error for reporting even in resilient mode
crate::rhai_functions::tracking::track_error(
"filter",
ctx.meta.line_num,
&format!("Filter error: {}", e),
Some(&event.original_line),
ctx.meta.filename.as_deref(),
ctx.config.verbose,
ctx.config.quiet_level,
Some(&ctx.config),
);
// New resiliency model: filter errors evaluate to false (Skip)
// unless in strict mode, where they still propagate as errors
if ctx.config.strict {
ScriptResult::Error(format!("Filter error: {}", e))
} else {
ScriptResult::Skip
}
}
}
}
}
/// Exec stage implementation
pub struct ExecStage {
compiled_exec: crate::engine::CompiledExpression,
stage_number: usize,
}
impl ExecStage {
pub fn new(exec: String, engine: &mut RhaiEngine) -> Result<Self> {
let compiled_exec = engine.compile_exec(&exec)?;
Ok(Self {
compiled_exec,
stage_number: 0,
})
}
pub fn with_stage_number(mut self, stage_number: usize) -> Self {
self.stage_number = stage_number;
self
}
}
impl ScriptStage for ExecStage {
fn apply(&mut self, event: Event, ctx: &mut PipelineContext) -> ScriptResult {
// Add stage-specific tracing
if let Some(ref tracer) = ctx.rhai.get_execution_tracer() {
tracer.trace_stage_execution(self.stage_number, "exec");
}
// Clear any previous emission state
crate::rhai_functions::emit::clear_suppression_flag();
// Atomic execution: work on a copy of the event for rollback behavior
let mut event_copy = event.clone();
columns::set_parse_cols_strict(ctx.config.strict);
let result = if ctx.window.is_empty() {
// No window context - use standard method
ctx.rhai
.execute_compiled_exec(&self.compiled_exec, &mut event_copy, &mut ctx.tracker)
} else {
// Window context available - use window-aware method
ctx.rhai.execute_compiled_exec_with_window(
&self.compiled_exec,
&mut event_copy,
&ctx.window,
&mut ctx.tracker,
)
};
match result {
Ok(()) => {
// Check for deferred emissions from emit_each()
let pending_emissions =
crate::rhai_functions::emit::get_and_clear_pending_emissions();
let should_suppress = crate::rhai_functions::emit::should_suppress_current_event();
if !pending_emissions.is_empty() {
// Convert pending emissions to events and emit them
let mut emitted_events = Vec::new();
for emission_map in pending_emissions {
let mut new_event =
Event::default_with_line(event_copy.original_line.clone());
new_event.line_num = event_copy.line_num;
new_event.filename = event_copy.filename.clone();
// Convert Rhai Map to Event fields
for (key, value) in emission_map {
new_event.fields.insert(key.to_string(), value);
}
emitted_events.push(new_event);
}
// Return multiple events - the first is primary, rest are additional
if should_suppress {
// Suppress original, return only emitted events
ScriptResult::EmitMultiple(emitted_events)
} else {
// Keep original and add emitted events
let mut all_events = vec![event_copy];
all_events.extend(emitted_events);
ScriptResult::EmitMultiple(all_events)
}
} else if should_suppress {
// emit_each was called but no events were actually emitted
// Still suppress the original as per specification
ScriptResult::Skip
} else {
// Normal execution: commit the modified event
ScriptResult::Emit(event_copy)
}
}
Err(e) => {
// Clear emission state on error
crate::rhai_functions::emit::clear_suppression_flag();
let _ = crate::rhai_functions::emit::get_and_clear_pending_emissions();
// Track error for reporting even in resilient mode
crate::rhai_functions::tracking::track_error(
"exec",
ctx.meta.line_num,
&format!("Exec error: {}", e),
Some(&event.original_line),
ctx.meta.filename.as_deref(),
ctx.config.verbose,
ctx.config.quiet_level,
Some(&ctx.config),
);
// New resiliency model: atomic rollback - return original event unchanged
// unless in strict mode, where errors still propagate
if ctx.config.strict {
ScriptResult::Error(format!("Exec error: {}", e))
} else {
// Rollback: return original event unchanged
ScriptResult::Emit(event)
}
}
}
}
}
/// Begin stage for --begin expressions
#[allow(dead_code)] // Used by builders.rs, instantiated in build() method
pub struct BeginStage {
compiled_begin: Option<crate::engine::CompiledExpression>,
}
impl BeginStage {
#[allow(dead_code)] // Used by builders.rs in build() method
pub fn new(begin: Option<String>, engine: &mut RhaiEngine) -> Result<Self> {
let compiled_begin = if let Some(begin_expr) = begin {
Some(engine.compile_begin(&begin_expr)?)
} else {
None
};
Ok(Self { compiled_begin })
}
#[allow(dead_code)] // Used by sequential processing pipeline
pub fn execute(&self, ctx: &mut PipelineContext) -> Result<()> {
if let Some(ref compiled) = self.compiled_begin {
columns::set_parse_cols_strict(ctx.config.strict);
let _init_map = ctx
.rhai
.execute_compiled_begin(compiled, &mut ctx.tracker)?;
Ok(())
} else {
Ok(())
}
}
}
/// End stage for --end expressions
#[allow(dead_code)] // Used by builders.rs, instantiated in build() method
pub struct EndStage {
compiled_end: Option<crate::engine::CompiledExpression>,
}
impl EndStage {
#[allow(dead_code)] // Used by builders.rs in build() method
pub fn new(end: Option<String>, engine: &mut RhaiEngine) -> Result<Self> {
let compiled_end = if let Some(end_expr) = end {
Some(engine.compile_end(&end_expr)?)
} else {
None
};
Ok(Self { compiled_end })
}
#[allow(dead_code)] // Used by sequential processing pipeline
pub fn execute(&self, ctx: &PipelineContext) -> Result<()> {
if let Some(ref compiled) = self.compiled_end {
columns::set_parse_cols_strict(ctx.config.strict);
ctx.rhai.execute_compiled_end(compiled, &ctx.tracker)
} else {
Ok(())
}
}
}
/// Level filtering stage for --levels and --exclude-levels options
pub struct LevelFilterStage {
levels: Vec<String>,
exclude_levels: Vec<String>,
}
impl LevelFilterStage {
pub fn new(levels: Vec<String>, exclude_levels: Vec<String>) -> Self {
Self {
levels,
exclude_levels,
}
}
/// Check if any filtering is needed
pub fn is_active(&self) -> bool {
!self.levels.is_empty() || !self.exclude_levels.is_empty()
}
}
impl ScriptStage for LevelFilterStage {
fn apply(&mut self, event: Event, _ctx: &mut PipelineContext) -> ScriptResult {
if !self.is_active() {
return ScriptResult::Emit(event);
}
// Get the level from the event fields map - check all possible level field names
let event_level = {
let mut found_level: Option<String> = None;
// Check all known level field names in the event's fields map
for level_field_name in crate::event::LEVEL_FIELD_NAMES {
if let Some(value) = event.fields.get(*level_field_name) {
if let Ok(level_str) = value.clone().into_string() {
found_level = Some(level_str);
break;
}
}
}
match found_level {
Some(level) => level,
None => {
// If no level field is found, check if we should include or exclude
if self.levels.is_empty() {
// Only exclude_levels specified, and no level found - include by default
return ScriptResult::Emit(event);
} else {
// levels specified but no level found - exclude
return ScriptResult::Skip;
}
}
}
};
// Apply exclude_levels first (higher priority) - case-insensitive
if !self.exclude_levels.is_empty() {
for exclude_level in &self.exclude_levels {
if event_level.eq_ignore_ascii_case(exclude_level) {
return ScriptResult::Skip;
}
}
}
// Apply levels filter - case-insensitive
if !self.levels.is_empty() {
for level in &self.levels {
if event_level.eq_ignore_ascii_case(level) {
return ScriptResult::Emit(event);
}
}
// No match found in levels list - exclude
return ScriptResult::Skip;
}
// No levels specified, only exclude_levels - include by default
ScriptResult::Emit(event)
}
}
/// Key filtering stage for --keys and --exclude-keys options
pub struct KeyFilterStage {
keys: Vec<String>,
exclude_keys: Vec<String>,
}
impl KeyFilterStage {
pub fn new(keys: Vec<String>, exclude_keys: Vec<String>) -> Self {
Self { keys, exclude_keys }
}
/// Check if any filtering is needed
pub fn is_active(&self) -> bool {
!self.keys.is_empty() || !self.exclude_keys.is_empty()
}
}
impl ScriptStage for KeyFilterStage {
fn apply(&mut self, mut event: Event, _ctx: &mut PipelineContext) -> ScriptResult {
if !self.is_active() {
return ScriptResult::Emit(event);
}
// Get available keys from the event
let available_keys: Vec<String> = event.fields.keys().cloned().collect();
// Calculate effective keys preserving the order specified by self.keys
let effective_keys = {
let mut result_keys = if self.keys.is_empty() {
// If no keys specified, start with all available keys
available_keys
} else {
// If keys specified, iterate through self.keys and only include those that exist in the event
// This preserves the order specified in self.keys rather than the original event order
self.keys
.iter()
.filter(|key| available_keys.contains(key))
.cloned()
.collect()
};
// Apply exclusions (higher priority)
result_keys.retain(|key| !self.exclude_keys.contains(key));
result_keys
};
// Apply the filtering
event.filter_keys(&effective_keys);
// Mark that key filtering has been applied to this event
if self.is_active() {
event.key_filtered = true;
}
// If any key filtering was applied and no fields remain, skip this event
if self.is_active() && event.fields.is_empty() {
ScriptResult::Skip
} else {
ScriptResult::Emit(event)
}
}
}
/// Timestamp filter stage for --since and --until filtering
pub struct TimestampFilterStage {
config: TimestampFilterConfig,
}
impl TimestampFilterStage {
pub fn new(config: TimestampFilterConfig) -> Self {
Self { config }
}
}
impl ScriptStage for TimestampFilterStage {
fn apply(&mut self, event: Event, ctx: &mut PipelineContext) -> ScriptResult {
// Get the parsed timestamp from the event
let event_timestamp = match event.parsed_ts {
Some(ts) => ts,
None => {
// No timestamp available - use new resiliency model
if ctx.config.strict {
// Stop processing on missing timestamp in strict mode
return ScriptResult::Error(
"Event has no valid timestamp for --since/--until filtering".to_string(),
);
} else {
// Filter out events without valid timestamps (resilient mode)
return ScriptResult::Skip;
}
}
};
// Check since filter (event must be >= since)
if let Some(since) = self.config.since {
if event_timestamp < since {
return ScriptResult::Skip;
}
}
// Check until filter (event must be <= until)
if let Some(until) = self.config.until {
if event_timestamp > until {
return ScriptResult::Skip;
}
}
// Event is within the time range
ScriptResult::Emit(event)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::TimestampFilterConfig;
use crate::pipeline::{MetaData, PipelineConfig};
use chrono::{Duration, Utc};
#[test]
fn test_timestamp_filter_stage_since() {
let since = Utc::now() - Duration::hours(1);
let config = TimestampFilterConfig {
since: Some(since),
until: None,
};
let mut stage = TimestampFilterStage::new(config);
// Create dummy context
let mut ctx = PipelineContext {
config: PipelineConfig {
error_report: crate::config::ErrorReportConfig {
style: crate::config::ErrorReportStyle::Summary,
},
brief: false,
wrap: true, // Default to enabled
color_mode: crate::config::ColorMode::Auto,
timestamp_formatting: crate::config::TimestampFormatConfig::default(),
strict: false,
verbose: 0,
quiet_level: 0,
no_emoji: false,
},
tracker: std::collections::HashMap::new(),
window: Vec::new(),
rhai: crate::engine::RhaiEngine::new(),
meta: MetaData::default(),
};
// Test event before since time (should be skipped)
let old_event = crate::event::Event {
parsed_ts: Some(since - Duration::minutes(30)),
..Default::default()
};
let result = stage.apply(old_event, &mut ctx);
matches!(result, ScriptResult::Skip);
// Test event after since time (should be emitted)
let new_event = crate::event::Event {
parsed_ts: Some(since + Duration::minutes(30)),
..Default::default()
};
let result = stage.apply(new_event, &mut ctx);
matches!(result, ScriptResult::Emit(_));
}
#[test]
fn test_timestamp_filter_stage_until() {
let until = Utc::now() - Duration::hours(1);
let config = TimestampFilterConfig {
since: None,
until: Some(until),
};
let mut stage = TimestampFilterStage::new(config);
// Create dummy context
let mut ctx = PipelineContext {
config: PipelineConfig {
error_report: crate::config::ErrorReportConfig {
style: crate::config::ErrorReportStyle::Summary,
},
brief: false,
wrap: true, // Default to enabled
color_mode: crate::config::ColorMode::Auto,
timestamp_formatting: crate::config::TimestampFormatConfig::default(),
strict: false,
verbose: 0,
quiet_level: 0,
no_emoji: false,
},
tracker: std::collections::HashMap::new(),
window: Vec::new(),
rhai: crate::engine::RhaiEngine::new(),
meta: MetaData::default(),
};
// Test event before until time (should be emitted)
let old_event = crate::event::Event {
parsed_ts: Some(until - Duration::minutes(30)),
..Default::default()
};
let result = stage.apply(old_event, &mut ctx);
matches!(result, ScriptResult::Emit(_));
// Test event after until time (should be skipped)
let new_event = crate::event::Event {
parsed_ts: Some(until + Duration::minutes(30)),
..Default::default()
};
let result = stage.apply(new_event, &mut ctx);
matches!(result, ScriptResult::Skip);
}
#[test]
fn test_timestamp_filter_stage_no_timestamp() {
let config = TimestampFilterConfig {
since: Some(Utc::now() - Duration::hours(1)),
until: Some(Utc::now() + Duration::hours(1)),
};
let mut stage = TimestampFilterStage::new(config);
// Create dummy context
let mut ctx = PipelineContext {
config: PipelineConfig {
error_report: crate::config::ErrorReportConfig {
style: crate::config::ErrorReportStyle::Summary,
},
brief: false,
wrap: true, // Default to enabled
color_mode: crate::config::ColorMode::Auto,
timestamp_formatting: crate::config::TimestampFormatConfig::default(),
strict: false,
verbose: 0,
quiet_level: 0,
no_emoji: false,
},
tracker: std::collections::HashMap::new(),
window: Vec::new(),
rhai: crate::engine::RhaiEngine::new(),
meta: MetaData::default(),
};
// Test event without timestamp (should be emitted - pass through behavior)
let event_no_ts = crate::event::Event {
parsed_ts: None,
..Default::default()
};
let result = stage.apply(event_no_ts, &mut ctx);
matches!(result, ScriptResult::Emit(_));
}
}