Skip to content

Commit

Permalink
feat: Support additional log filtering options
Browse files Browse the repository at this point in the history
Add the ` FilterFn` layer to support filtering by crates, modules and
spans. With `tracing::instrument` spans wrap functions, this would
support filtering to specific functions.

Signed-off-by: Jonathan Woollett-Light <[email protected]>
  • Loading branch information
Jonathan Woollett-Light committed Aug 17, 2023
1 parent ca449f1 commit 1f38791
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 109 deletions.
8 changes: 5 additions & 3 deletions src/api_server/src/request/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn parse_put_logger(body: &Body) -> Result<ParsedRequest, Error> {
mod tests {
use std::path::PathBuf;

use vmm::vmm_config::logger::Level;
use vmm::vmm_config::logger::LevelFilter;

use super::*;
use crate::parsed_request::tests::vmm_action_from_request;
Expand All @@ -38,9 +38,10 @@ mod tests {

let mut expected_cfg = LoggerConfig {
log_path: Some(PathBuf::from("log")),
level: Some(Level::Warn),
level: Some(LevelFilter::Warn),
show_level: Some(false),
show_log_origin: Some(false),
filter: None,
};
match vmm_action_from_request(parse_put_logger(&Body::new(body)).unwrap()) {
VmmAction::ConfigureLogger(cfg) => assert_eq!(cfg, expected_cfg),
Expand All @@ -56,9 +57,10 @@ mod tests {

expected_cfg = LoggerConfig {
log_path: Some(PathBuf::from("log")),
level: Some(Level::Debug),
level: Some(LevelFilter::Debug),
show_level: Some(false),
show_log_origin: Some(false),
filter: None,
};
match vmm_action_from_request(parse_put_logger(&Body::new(body)).unwrap()) {
VmmAction::ConfigureLogger(cfg) => assert_eq!(cfg, expected_cfg),
Expand Down
7 changes: 5 additions & 2 deletions src/firecracker/src/api_server_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ impl MutEventSubscriber for ApiServerAdapter {
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn run_with_api(
pub(crate) fn run_with_api<
F: Fn(&tracing::Metadata<'_>) -> bool,
G: Fn(&tracing::Metadata<'_>) -> bool,
>(
seccomp_filters: &mut BpfThreadMap,
config_json: Option<String>,
bind_path: PathBuf,
Expand All @@ -142,7 +145,7 @@ pub(crate) fn run_with_api(
api_payload_limit: usize,
mmds_size_limit: usize,
metadata_json: Option<&str>,
logger_handles: vmm::vmm_config::logger::LoggerHandles,
logger_handles: vmm::vmm_config::logger::LoggerHandles<F, G>,
) -> Result<(), ApiServerError> {
// FD to notify of API events. This is a blocking eventfd by design.
// It is used in the config/pre-boot loop which is a simple blocking loop
Expand Down
45 changes: 41 additions & 4 deletions src/firecracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use vmm::resources::VmResources;
use vmm::signal_handler::register_signal_handlers;
use vmm::version_map::{FC_VERSION_TO_SNAP_VERSION, VERSION_MAP};
use vmm::vmm_config::instance_info::{InstanceInfo, VmState};
use vmm::vmm_config::logger::Level;
use vmm::vmm_config::logger::LevelFilter;
use vmm::vmm_config::metrics::{init_metrics, MetricsConfig};
use vmm::{EventManager, FcExitCode, HTTP_MAX_PAYLOAD_SIZE};

Expand All @@ -47,8 +47,13 @@ enum MainError {
ParseArguments(#[from] utils::arg_parser::Error),
#[error("When printing Snapshot Data format: {0}")]
PrintSnapshotDataFormat(#[from] SnapshotVersionError),
#[error("Invalid value for logger level: {0}.Possible values: [Error, Warning, Info, Debug]")]
InvalidLogLevel(<Level as FromStr>::Err),
#[error(
"Invalid value for logger level: {0}.Possible values: [Off, Error, Warning, Info, Debug, \
Trace]"
)]
InvalidLogLevel(<LevelFilter as FromStr>::Err),
#[error("Failed to deserialize log filter: {0}")]
DeserializeLogFilter(serde_json::Error),
#[error("Could not initialize logger: {0}")]
LoggerInitialization(vmm::vmm_config::logger::InitLoggerError),
#[error("Could not initialize metrics: {0:?}")]
Expand Down Expand Up @@ -173,6 +178,27 @@ fn main_exec() -> Result<(), MainError> {
"Path to a file that contains metadata in JSON format to add to the mmds.",
),
)
.arg(
Argument::new("start-time-us").takes_value(true).help(
"Process start time (wall clock, microseconds). This parameter is optional.",
),
)
.arg(Argument::new("start-time-cpu-us").takes_value(true).help(
"Process start CPU time (wall clock, microseconds). This parameter is optional.",
))
.arg(Argument::new("parent-cpu-time-us").takes_value(true).help(
"Parent process CPU time (wall clock, microseconds). This parameter is optional.",
))
.arg(
Argument::new("config-file")
.takes_value(true)
.help("Path to a file that contains the microVM configuration in JSON format."),
)
.arg(
Argument::new(MMDS_CONTENT_ARG).takes_value(true).help(
"Path to a file that contains metadata in JSON format to add to the mmds.",
),
)
.arg(
Argument::new("no-api")
.takes_value(false)
Expand All @@ -187,6 +213,11 @@ fn main_exec() -> Result<(), MainError> {
.takes_value(true)
.help("Path to a fifo or a file used for configuring the logger on startup."),
)
.arg(
Argument::new("log-filter")
.takes_value(true)
.help("Filter for logging, if set overrides `level`."),
)
.arg(
Argument::new("level")
.takes_value(true)
Expand Down Expand Up @@ -270,15 +301,21 @@ fn main_exec() -> Result<(), MainError> {
let logger_handles = {
let level_res = arguments
.single_value("level")
.map(|s| Level::from_str(s))
.map(|s| LevelFilter::from_str(s))
.transpose();
let level = level_res.map_err(MainError::InvalidLogLevel)?;

let filter = if let Some(log_filter) = arguments.single_value("log-filter") {
Some(serde_json::from_str(log_filter).map_err(MainError::DeserializeLogFilter)?)
} else {
None
};
let logger_config = vmm::vmm_config::logger::LoggerConfig {
log_path: arguments.single_value("log-path").map(PathBuf::from),
level,
show_level: Some(arguments.flag_present("show-level")),
show_log_origin: Some(arguments.flag_present("show-log-origin")),
filter,
};
logger_config
.init()
Expand Down
16 changes: 9 additions & 7 deletions src/vmm/src/rpc_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ trait MmdsRequestHandler {
}

/// Enables pre-boot setup and instantiation of a Firecracker VMM.
pub struct PrebootApiController<'a> {
pub struct PrebootApiController<'a, F, G> {
seccomp_filters: &'a BpfThreadMap,
instance_info: InstanceInfo,
vm_resources: &'a mut VmResources,
Expand All @@ -272,11 +272,11 @@ pub struct PrebootApiController<'a> {
// should cleanly teardown if they occur.
fatal_error: Option<FcExitCode>,
/// Handles that allow re-configuring the logger.
logger_handles: LoggerHandles,
logger_handles: LoggerHandles<F, G>,
}

// TODO Remove when `EventManager` implements `std::fmt::Debug`.
impl<'a> fmt::Debug for PrebootApiController<'a> {
impl<'a, F, G> fmt::Debug for PrebootApiController<'a, F, G> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PrebootApiController")
.field("seccomp_filters", &self.seccomp_filters)
Expand All @@ -290,7 +290,7 @@ impl<'a> fmt::Debug for PrebootApiController<'a> {
}
}

impl MmdsRequestHandler for PrebootApiController<'_> {
impl<F, G> MmdsRequestHandler for PrebootApiController<'_, F, G> {
fn mmds(&mut self) -> MutexGuard<'_, Mmds> {
self.vm_resources.locked_mmds_or_default()
}
Expand All @@ -315,14 +315,16 @@ pub type ApiRequest = Box<VmmAction>;
/// Shorthand type for a response containing a boxed Result.
pub type ApiResponse = Box<std::result::Result<VmmData, VmmActionError>>;

impl<'a> PrebootApiController<'a> {
impl<'a, F: Fn(&tracing::Metadata<'_>) -> bool, G: Fn(&tracing::Metadata<'_>) -> bool>
PrebootApiController<'a, F, G>
{
/// Constructor for the PrebootApiController.
pub fn new(
seccomp_filters: &'a BpfThreadMap,
instance_info: InstanceInfo,
vm_resources: &'a mut VmResources,
event_manager: &'a mut EventManager,
logger_handles: LoggerHandles,
logger_handles: LoggerHandles<F, G>,
) -> Self {
Self {
seccomp_filters,
Expand Down Expand Up @@ -350,7 +352,7 @@ impl<'a> PrebootApiController<'a> {
boot_timer_enabled: bool,
mmds_size_limit: usize,
metadata_json: Option<&str>,
logger_handles: LoggerHandles,
logger_handles: LoggerHandles<F, G>,
) -> Result<(VmResources, Arc<Mutex<Vmm>>), FcExitCode> {
let mut vm_resources = VmResources::default();
// Silence false clippy warning. Clippy suggests using
Expand Down
Loading

0 comments on commit 1f38791

Please sign in to comment.