Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support logging in geyser plugins #34101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions geyser-plugin-interface/src/geyser_plugin_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,35 @@ pub type Result<T> = std::result::Result<T, GeyserPluginError>;
/// Geyser plugins must describe desired behavior for load and unload,
/// as well as how they will handle streamed data.
pub trait GeyserPlugin: Any + Send + Sync + std::fmt::Debug {
/// The callback to allow the plugin to setup the logging configuration using the logger
/// and log level specified by the validator. Will be called first on load/reload, before any other
/// callback, and only called once.
/// # Examples
///
/// ```
/// use solana_geyser_plugin_interface::geyser_plugin_interface::{GeyserPlugin,
/// GeyserPluginError, Result};
///
/// #[derive(Debug)]
/// struct SamplePlugin;
/// impl GeyserPlugin for SamplePlugin {
/// fn setup_logger(&self, logger: &'static dyn log::Log, level: log::LevelFilter) -> Result<()> {
/// log::set_max_level(level);
/// if let Err(err) = log::set_logger(logger) {
/// return Err(GeyserPluginError::Custom(Box::new(err)));
/// }
/// Ok(())
/// }
/// fn name(&self) -> &'static str {
/// &"sample"
/// }
/// }
/// ```
#[allow(unused_variables)]
fn setup_logger(&self, logger: &'static dyn log::Log, level: log::LevelFilter) -> Result<()> {
Ok(())
}

fn name(&self) -> &'static str;

/// The callback called when a plugin is loaded by the system,
Expand Down
18 changes: 18 additions & 0 deletions geyser-plugin-manager/src/geyser_plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ impl GeyserPluginManager {
});
}

setup_logger_for_plugin(&*new_plugin)?;

// Call on_load and push plugin
new_plugin
.on_load(new_config_file, false)
Expand Down Expand Up @@ -193,6 +195,8 @@ impl GeyserPluginManager {
});
}

setup_logger_for_plugin(&*new_plugin)?;

// Attempt to on_load with new plugin
match new_plugin.on_load(new_parsed_config_file, true) {
// On success, push plugin and library
Expand Down Expand Up @@ -227,6 +231,20 @@ impl GeyserPluginManager {
}
}

// Initialize logging for the plugin
fn setup_logger_for_plugin(new_plugin: &dyn GeyserPlugin) -> Result<(), jsonrpc_core::Error> {
new_plugin
.setup_logger(log::logger(), log::max_level())
.map_err(|setup_logger_err| jsonrpc_core::Error {
code: ErrorCode::InvalidRequest,
message: format!(
"setup_logger method of plugin {} failed: {setup_logger_err}",
new_plugin.name()
),
data: None,
})
}

#[derive(Debug)]
pub enum GeyserPluginManagerRequest {
ReloadPlugin {
Expand Down