-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add regex capture and rename capture_bytes to capture
- Loading branch information
1 parent
a31ec4a
commit c3e6877
Showing
12 changed files
with
273 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::metadata::Value; | ||
|
||
use super::Metrics; | ||
|
||
fn is_valid_size(contents: &[u8], size: usize, metrics: &Metrics) -> bool { | ||
// if the capture size is bigger than the packet size, then we drop the packet, | ||
// and occasionally warn | ||
if contents.len() < size { | ||
if metrics.packets_dropped_total.get() % 1000 == 0 { | ||
tracing::warn!(count = ?metrics.packets_dropped_total.get(), "Packets are being dropped due to their length being less than {} bytes", size); | ||
} | ||
metrics.packets_dropped_total.inc(); | ||
|
||
false | ||
} else { | ||
true | ||
} | ||
} | ||
|
||
/// Capture from the start of the packet. | ||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] | ||
pub struct Prefix { | ||
/// Whether captured bytes are removed from the original packet. | ||
#[serde(default)] | ||
pub remove: bool, | ||
/// The number of bytes to capture. | ||
pub size: usize, | ||
} | ||
|
||
impl super::CaptureStrategy for Prefix { | ||
fn capture(&self, contents: &mut Vec<u8>, metrics: &Metrics) -> Option<Value> { | ||
is_valid_size(contents, self.size, metrics).then(|| { | ||
if self.remove { | ||
Value::Bytes(contents.drain(..self.size).collect()) | ||
} else { | ||
Value::Bytes(contents.iter().take(self.size).copied().collect()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
/// Capture from the end of the packet. | ||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] | ||
pub struct Suffix { | ||
/// Whether captured bytes are removed from the original packet. | ||
pub size: usize, | ||
/// The number of bytes to capture. | ||
pub remove: bool, | ||
} | ||
|
||
impl super::CaptureStrategy for Suffix { | ||
fn capture(&self, contents: &mut Vec<u8>, metrics: &Metrics) -> Option<Value> { | ||
is_valid_size(contents, self.size, metrics).then(|| { | ||
let index = contents.len() - self.size; | ||
|
||
if self.remove { | ||
Value::Bytes(contents.split_off(index).into()) | ||
} else { | ||
Value::Bytes(contents.iter().skip(index).copied().collect()) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.