Skip to content

Commit

Permalink
Log slash vs prefix command usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tazz4843 committed Feb 2, 2024
1 parent 85d2640 commit ad4d1b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 13 additions & 2 deletions scripty_bot_utils/src/handler/pre_command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use poise::BoxFuture;

async fn _pre_command(ctx: crate::Context<'_>) {
use crate::Context;

async fn _pre_command(ctx: Context<'_>) {
scripty_metrics::measure_end_latency(ctx.id());

let metrics = scripty_metrics::get_metrics();
Expand All @@ -10,10 +12,19 @@ async fn _pre_command(ctx: crate::Context<'_>) {
.get_metric_with_label_values(&[&ctx.command().qualified_name])
.expect("exactly one label")
.inc();

match ctx {
Context::Prefix(..) => {
metrics.command_usage.prefix.inc();
}
Context::Application(..) => {
metrics.command_usage.slash.inc();
}
}
}

#[inline]
pub fn pre_command(ctx: crate::Context) -> BoxFuture<()> {
pub fn pre_command(ctx: Context) -> BoxFuture<()> {
debug!("pre_command");
Box::pin(_pre_command(ctx))
}
18 changes: 18 additions & 0 deletions scripty_metrics/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ make_static_metric! {
pub struct LatencyVec: IntGauge {
"latency_type" => LatencyType,
}

pub label_enum CommandUsage {
slash,
prefix,
}
pub struct CommandUsageVec: IntCounter {
"command_usage_type" => CommandUsage,
}
}

pub static METRICS: OnceCell<Arc<Metrics>> = OnceCell::new();
Expand Down Expand Up @@ -173,6 +181,7 @@ pub struct Metrics {
pub runtime_metrics: RuntimeMetricsVec,
pub latency: LatencyVec,
pub stt_time: Histogram,
pub command_usage: CommandUsageVec,
}

impl Metrics {
Expand Down Expand Up @@ -298,6 +307,14 @@ impl Metrics {
.expect("failed to init stt_time histogram");
registry.register(Box::new(stt_time.clone())).unwrap();

let command_usage_stats = IntCounterVec::new(
Opts::new("command_usage", "Breakdown of each command used"),
&["command_usage_type"],
)
.unwrap();
let command_usage = CommandUsageVec::from(&command_usage_stats);
registry.register(Box::new(command_usage_stats)).unwrap();

let up = IntCounter::new("up", "Always 1").unwrap();
up.inc();
registry.register(Box::new(up)).unwrap();
Expand All @@ -321,6 +338,7 @@ impl Metrics {
stt_server_fetch_success,
stt_server_fetch_failure,
stt_time,
command_usage,
})
}
}

0 comments on commit ad4d1b6

Please sign in to comment.