-
-
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
May 14, 2024
1 parent
554f4fc
commit b40f9ce
Showing
67 changed files
with
1,507 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use super::{ | ||
errors::{ApiErrorKind, ApiResult}, | ||
Api, Method, | ||
}; | ||
use crate::constants::USER_AGENT; | ||
use anyhow::{anyhow, Result}; | ||
use sentry::{protocol::EnvelopeItem, 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(); | ||
match api.config.get_dsn() { | ||
Ok(dsn) => Ok(EnvelopesApi { api, dsn }), | ||
Err(_) => Err(ApiErrorKind::DsnMissing.into()), | ||
} | ||
} | ||
|
||
pub fn send_item(&self, item: EnvelopeItem) -> Result<()> { | ||
let mut envelope = Envelope::new(); | ||
envelope.add_item(item); | ||
self.send_envelope(envelope) | ||
} | ||
|
||
pub fn send_envelope(&self, envelope: Envelope) -> Result<()> { | ||
let mut body: Vec<u8> = Vec::new(); | ||
envelope.to_writer(&mut body)?; | ||
let url = self.dsn.envelope_api_url(); | ||
let auth = self.dsn.to_auth(Some(USER_AGENT)); | ||
let response = self | ||
.api | ||
.request(Method::Post, url.as_str(), None)? | ||
.with_header("X-Sentry-Auth", &auth.to_string())? | ||
.with_body(body)? | ||
.send()?; | ||
match response.ok() { | ||
true => Ok(()), | ||
false => Err(anyhow!("Failed to send envelope.")), | ||
} | ||
} | ||
} |
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
Oops, something went wrong.