-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): Add send-metric command (#2063)
Add CLI command send-metric for emitting metrics to Sentry. Add new command-parser that uses clap's Derive API. Future commands should use this as the Derive API makes it: -Easier to read, write, and modify arguments and commands. -Easier to keep the argument declaration and reading of argument in sync. -Easier to reuse shared arguments. Fixes GH-2001
- Loading branch information
Showing
63 changed files
with
1,251 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use super::{ | ||
errors::{ApiErrorKind, ApiResult}, | ||
Api, ApiResponse, Method, | ||
}; | ||
use crate::{api::errors::ApiError, constants::USER_AGENT}; | ||
use log::debug; | ||
use sentry::{types::Dsn, Envelope}; | ||
use std::sync::Arc; | ||
|
||
pub struct EnvelopesApi { | ||
api: Arc<Api>, | ||
dsn: Dsn, | ||
} | ||
|
||
impl EnvelopesApi { | ||
pub fn try_new() -> ApiResult<EnvelopesApi> { | ||
let api = Api::current(); | ||
api.config | ||
.get_dsn() | ||
.map(|dsn| EnvelopesApi { api, dsn }) | ||
.map_err(|_| ApiErrorKind::DsnMissing.into()) | ||
} | ||
|
||
pub fn send_envelope(&self, envelope: Envelope) -> ApiResult<ApiResponse> { | ||
let mut body = vec![]; | ||
envelope | ||
.to_writer(&mut body) | ||
.map_err(|e| ApiError::with_source(ApiErrorKind::CannotSerializeEnvelope, e))?; | ||
let url = self.dsn.envelope_api_url(); | ||
let auth = self.dsn.to_auth(Some(USER_AGENT)); | ||
debug!("Sending envelope:\n{}", String::from_utf8_lossy(&body)); | ||
self.api | ||
.request(Method::Post, url.as_str(), None)? | ||
.with_header("X-Sentry-Auth", &auth.to_string())? | ||
.with_body(body)? | ||
.send()? | ||
.into_result() | ||
} | ||
} |
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,36 @@ | ||
use crate::utils::auth_token::AuthToken; | ||
use crate::utils::value_parsers::kv_parser; | ||
use clap::{command, value_parser, ArgAction::SetTrue, Parser, Subcommand}; | ||
|
||
use super::send_metric::SendMetricArgs; | ||
|
||
#[derive(Parser)] | ||
pub(super) struct SentryCLI { | ||
#[command(subcommand)] | ||
pub(super) command: SentryCLICommand, | ||
|
||
#[arg(global=true, long="header", value_name="KEY:VALUE", value_parser=kv_parser)] | ||
#[arg(help = "Custom headers that should be attached to all requests{n}in key:value format")] | ||
pub(super) headers: Vec<(String, String)>, | ||
|
||
#[arg(global=true, long, value_parser=value_parser!(AuthToken))] | ||
#[arg(help = "Use the given Sentry auth token")] | ||
pub(super) auth_token: Option<AuthToken>, | ||
|
||
#[arg(global=true, ignore_case=true, value_parser=["trace", "debug", "info", "warn", "error"])] | ||
#[arg(long, help = "Set the log output verbosity")] | ||
pub(super) log_level: Option<String>, | ||
|
||
#[arg(global=true, action=SetTrue, visible_alias="silent", long)] | ||
#[arg(help = "Do not print any output while preserving correct exit code. \ | ||
This flag is currently implemented only for selected subcommands")] | ||
pub(super) quiet: bool, | ||
|
||
#[arg(global=true, action=SetTrue, long, hide=true, help="Always return 0 exit code")] | ||
pub(super) allow_failure: bool, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub(super) enum SentryCLICommand { | ||
SendMetric(SendMetricArgs), | ||
} |
Oops, something went wrong.