Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit bddc14e

Browse files
committed
adding CLI args to pelikan-rs
Adds CLI arguments to Rust Pelikan Backends to display usage, version, and stats information. Partially addresses twitter#361
1 parent 9f48b7c commit bddc14e

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/core/server/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ mod process;
9696
mod threads;
9797

9898
pub use process::{Process, ProcessBuilder};
99+
pub use threads::PERCENTILES;
99100

100101
use metrics::{static_metrics, Counter};
101102

src/rust/core/server/src/threads/admin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct Admin {
7070
log_drain: Box<dyn Drain>,
7171
}
7272

73-
static PERCENTILES: &[(&str, f64)] = &[
73+
pub static PERCENTILES: &[(&str, f64)] = &[
7474
("p25", 25.0),
7575
("p50", 50.0),
7676
("p75", 75.0),

src/rust/core/server/src/threads/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod listener;
77
mod traits;
88
mod worker;
99

10-
pub use admin::Admin;
10+
pub use admin::{Admin, PERCENTILES};
1111
pub use listener::Listener;
1212
pub use traits::EventLoop;
1313
pub use worker::{MultiWorker, SingleWorker, StorageWorker};

src/rust/server/segcache/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ debug = ["entrystore/debug"]
3838

3939
[dependencies]
4040
backtrace = "0.3.56"
41+
clap = "2.33.3"
4142
config = { path = "../../config" }
4243
entrystore = { path = "../../entrystore" }
4344
logger = { path = "../../logger" }

src/rust/server/segcache/src/main.rs

+63-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
extern crate logger;
77

88
use backtrace::Backtrace;
9+
use clap::{App, Arg};
910
use config::SegcacheConfig;
11+
use metrics::*;
1012
use pelikan_segcache_rs::Segcache;
13+
use server::PERCENTILES;
1114

1215
fn main() {
1316
// custom panic hook to terminate whole process after unwinding
@@ -17,10 +20,68 @@ fn main() {
1720
std::process::exit(101);
1821
}));
1922

23+
// parse command line options
24+
let matches = App::new(env!("CARGO_BIN_NAME"))
25+
.version(env!("CARGO_PKG_VERSION"))
26+
.version_short("v")
27+
.long_about(
28+
"One of the unified cache backends implemented in Rust. It \
29+
uses segment-based storage to cache key/val pairs. It speaks the \
30+
memcached ASCII protocol and supports some ASCII memcached \
31+
commands.",
32+
)
33+
.arg(
34+
Arg::with_name("stats")
35+
.short("s")
36+
.long("stats")
37+
.help("List all metrics in stats")
38+
.takes_value(false),
39+
)
40+
.arg(
41+
Arg::with_name("CONFIG")
42+
.help("Server configuration file")
43+
.index(1),
44+
)
45+
.get_matches();
46+
47+
if matches.is_present("stats") {
48+
println!("{:<31} {:<15} DESCRIPTION", "NAME", "TYPE");
49+
50+
let mut metrics = Vec::new();
51+
52+
for metric in &metrics::rustcommon_metrics::metrics() {
53+
let any = match metric.as_any() {
54+
Some(any) => any,
55+
None => {
56+
continue;
57+
}
58+
};
59+
60+
if any.downcast_ref::<Counter>().is_some() {
61+
metrics.push(format!("{:<31} counter", metric.name()));
62+
} else if any.downcast_ref::<Gauge>().is_some() {
63+
metrics.push(format!("{:<31} gauge", metric.name()));
64+
} else if any.downcast_ref::<Heatmap>().is_some() {
65+
for (label, _) in PERCENTILES {
66+
let name = format!("{}_{}", metric.name(), label);
67+
metrics.push(format!("{:<31} percentile", name));
68+
}
69+
} else {
70+
continue;
71+
}
72+
}
73+
74+
metrics.sort();
75+
for metric in metrics {
76+
println!("{}", metric);
77+
}
78+
std::process::exit(0);
79+
}
80+
2081
// load config from file
21-
let config = if let Some(file) = std::env::args().nth(1) {
82+
let config = if let Some(file) = matches.value_of("CONFIG") {
2283
debug!("loading config: {}", file);
23-
match SegcacheConfig::load(&file) {
84+
match SegcacheConfig::load(file) {
2485
Ok(c) => c,
2586
Err(e) => {
2687
error!("{}", e);

0 commit comments

Comments
 (0)