-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): add send-metric command
Add CLI command that can emit metrics. Fixes GH-2001
- Loading branch information
Elias Ram
committed
Apr 29, 2024
1 parent
64d20b0
commit 5a72389
Showing
65 changed files
with
1,191 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 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,38 @@ | ||
use super::derive_parser::{SentryCLI, SentryCLICommand}; | ||
use crate::config::Config; | ||
use crate::utils::event; | ||
use crate::utils::metrics::payload::MetricsPayload; | ||
use anyhow::Context; | ||
use anyhow::Result; | ||
use clap::{command, Args, Subcommand}; | ||
use clap::{ArgMatches, Command, Parser}; | ||
use log::debug; | ||
use sentry::protocol::EnvelopeItem; | ||
use sentry::Envelope; | ||
use subcommands::SendMetricSubcommand; | ||
|
||
pub mod subcommands; | ||
|
||
#[derive(Args)] | ||
pub(super) struct SendMetricArgs { | ||
#[command(subcommand)] | ||
pub(super) subcommand: SendMetricSubcommand, | ||
} | ||
|
||
pub(super) fn make_command(command: Command) -> Command { | ||
SendMetricSubcommand::augment_subcommands(command) | ||
} | ||
|
||
pub(super) fn execute(_: &ArgMatches) -> Result<()> { | ||
let SentryCLICommand::SendMetric(SendMetricArgs { subcommand }) = SentryCLI::parse().command; | ||
let payload = MetricsPayload::from_subcommand(subcommand)?; | ||
let mut envelope = Envelope::new(); | ||
envelope.add_item(EnvelopeItem::Statsd(payload.to_bytes())); | ||
let dsn = Config::current().get_dsn().ok().context( | ||
"DSN not found. \ | ||
See: https://docs.sentry.io/product/crons/getting-started/cli/#configuration", | ||
)?; | ||
event::with_sentry_client(dsn, |c| c.send_envelope(envelope)); | ||
debug!("Metric payload sent: {}", (payload.to_string()?)); | ||
Ok(()) | ||
} |
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,63 @@ | ||
use clap::Args; | ||
use clap::{command, Subcommand}; | ||
|
||
use crate::utils::value_parsers; | ||
|
||
#[derive(Subcommand)] | ||
#[command(about = "Send a metric to Sentry.")] | ||
#[command(long_about = "Send a metric event to Sentry.{n}{n}\ | ||
This command will validate input parameters and attempt to send a metric to \ | ||
Sentry. Due to network errors and rate limits, the metric is not guaranteed to \ | ||
arrive.")] | ||
pub enum SendMetricSubcommand { | ||
#[command(about = "Send an increment to a counter metric")] | ||
Increment(IncrementArgs), | ||
#[command(about = "Send a value to a distribution metric")] | ||
Distribution(FloatValueMetricArgs), | ||
#[command(about = "Send a value to a gauge metric")] | ||
Gauge(FloatValueMetricArgs), | ||
#[command(about = "Send a value to a set metric")] | ||
Set(SetArgs), | ||
} | ||
|
||
#[derive(Args)] | ||
pub struct FloatValueMetricArgs { | ||
#[command(flatten)] | ||
pub common: CommonMetricArgs, | ||
|
||
#[arg(short, long, help = "Metric value")] | ||
pub value: f64, | ||
} | ||
|
||
#[derive(Args)] | ||
pub struct IncrementArgs { | ||
#[command(flatten)] | ||
pub common: CommonMetricArgs, | ||
|
||
#[arg(short, long, help = "Metric value", default_value = "1")] | ||
pub value: f64, | ||
} | ||
|
||
#[derive(Args)] | ||
pub struct SetArgs { | ||
#[command(flatten)] | ||
pub common: CommonMetricArgs, | ||
|
||
#[arg(short, long, value_parser=value_parsers::set_value_parser)] | ||
#[arg(help = "Metric value: strings and integers are supported, floats are floored")] | ||
pub value: f64, | ||
} | ||
|
||
#[derive(Args)] | ||
pub struct CommonMetricArgs { | ||
#[arg(short, long, help = "Metric key/name")] | ||
#[arg(visible_alias = "name", visible_short_alias = 'n')] | ||
pub key: String, | ||
|
||
#[arg(short, long, help = "Metric unit")] | ||
pub unit: Option<String>, | ||
|
||
#[arg(short, long, value_delimiter=',', value_name = "KEY:VALUE", num_args = 1..)] | ||
#[arg(value_parser=value_parsers::kv_parser, help = "Metric tags as key:value pairs")] | ||
pub tags: Vec<(String, String)>, | ||
} |
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,3 @@ | ||
pub mod payload; | ||
mod subcommand_impl; | ||
mod tags; |
Oops, something went wrong.