-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
143 additions
and
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "rsloglib" | ||
version = "0.1.0" | ||
authors = ["Rostyslav Ivanika <Rostyslav.Ivanika@gmail.com>"] | ||
edition = "2018" | ||
|
||
|
||
[dependencies] | ||
redis = "0.19.0" | ||
log = "0.4.11" | ||
serde = {version="1", features=["derive"]} |
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,5 @@ | ||
mod slowlog; | ||
mod slowlog_reader; | ||
|
||
pub use slowlog::*; | ||
pub use slowlog_reader::*; |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,99 @@ | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
use std::convert::TryFrom; | ||
|
||
fn main(){ | ||
rslog::main(); | ||
} | ||
mod argument_parsing; | ||
use argument_parsing::OutputFormat; | ||
|
||
use rsloglib::{SlowlogRecord,SlowlogReader, RedisConnectionProvider}; | ||
|
||
|
||
fn print_rec(r: &SlowlogRecord, format: &OutputFormat) { | ||
match format { | ||
OutputFormat::Text => { | ||
println!( | ||
"[{}] id: {},\tduration: {},\tclient: {},\tclient_name: {},\tcommand: {:?}", | ||
r.time, r.id, r.duration, r.client_socket, r.client_name, r.command | ||
) | ||
} | ||
OutputFormat::Json => { | ||
println!("{}", serde_json::to_string(r).unwrap()) | ||
} | ||
} | ||
} | ||
|
||
fn error_handler(e: redis::RedisError) { | ||
match e.kind() { | ||
redis::ErrorKind::IoError => { | ||
log::error!("Can't establish connection to redis cluster: {}", e) | ||
} | ||
_ => unimplemented!("Error not handled: {}({:?})", e, e.kind()), | ||
} | ||
} | ||
|
||
fn create_slowlog_reader(con_provider: RedisConnectionProvider, interval: u64) -> SlowlogReader { | ||
log::debug!("Creating slowlog reader"); | ||
loop { | ||
match SlowlogReader::try_from(con_provider.clone()) { | ||
Err(e) => error_handler(e), | ||
Ok(slr) => return slr, | ||
} | ||
sleep(Duration::new(interval, 0)) | ||
} | ||
} | ||
|
||
fn read_once(con_provider: RedisConnectionProvider, config: &argument_parsing::Config) { | ||
match { | ||
move || -> Result<(), redis::RedisError> { | ||
for r in rsloglib::get_slowlog(&mut con_provider.get_connection()?, 128)?.iter() { | ||
print_rec(r, &config.output_format) | ||
} | ||
Ok(()) | ||
} | ||
}() { | ||
Err(e) => error_handler(e), | ||
Ok(_) => std::process::exit(0), | ||
} | ||
} | ||
|
||
fn read_continiously(con_provider: RedisConnectionProvider, config: &argument_parsing::Config) { | ||
let mut sl_reader = create_slowlog_reader(con_provider, config.interval); | ||
|
||
loop { | ||
match sl_reader | ||
.get() | ||
.map_err(|e| sl_reader.redis_error_handler(e)) | ||
{ | ||
Ok(records) => { | ||
for r in records.iter().rev() { | ||
print_rec(r, &config.output_format) | ||
} | ||
} | ||
Err(e) => { | ||
if let Err(e) = e { | ||
error_handler(e) | ||
} | ||
} | ||
} | ||
sleep(Duration::new(config.interval, 0)); | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let config = argument_parsing::get_config() | ||
.map_err(|e| e.exit()) | ||
.unwrap(); | ||
stderrlog::new() | ||
.timestamp(stderrlog::Timestamp::Second) | ||
.verbosity(config.verbosity) | ||
.quiet(config.quiet) | ||
.init() | ||
.unwrap(); | ||
let redis_client = redis::Client::open((&config.hostname, config.port)).unwrap(); | ||
let connection_provider = RedisConnectionProvider::from((redis_client, config.interval)); | ||
if config.follow { | ||
read_continiously(connection_provider, &config) | ||
} else { | ||
read_once(connection_provider, &config) | ||
} | ||
} |