Skip to content

Commit

Permalink
feat: add tracing
Browse files Browse the repository at this point in the history
This commit adds basic tracing subscriber setup with output to STDERR.
  • Loading branch information
matyama committed Aug 19, 2024
1 parent 0ffff1e commit 3d3c886
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 23 deletions.
127 changes: 124 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ tokio = { version = "1.38", default-features = false, features = [
"parking_lot",
"rt",
] }
tracing = "0.1.40"
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = [
"env-filter",
"parking_lot",
] }
url = { version = "2.5", features = ["serde"] }
xdg = "2.5"

Expand Down
15 changes: 12 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;
use futures_util::stream::{Stream, StreamExt as _};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use tracing::{debug, instrument};
use url::Url;

use crate::cache::BitlinkCache;
Expand Down Expand Up @@ -96,14 +97,15 @@ impl ClientInner {
api_url(&self.cfg.api_url, endpoint)
}

#[instrument(level = "debug", skip(self))]
async fn fetch_user(&self) -> Result<User> {
let Some(ref http) = self.http else {
return Err(Error::Offline("user"));
};

let endpoint = self.api_url("user");

//println!("fetching user info");
debug!("fetching user info");
let resp = http
.get(endpoint)
.bearer_auth(self.cfg.api_token())
Expand All @@ -121,8 +123,9 @@ impl ClientInner {
}
}

#[instrument(level = "debug", fields(%long_url), skip_all)]
async fn shorten(&self, long_url: Url) -> Result<Bitlink> {
//println!("shortening {long_url}");
debug!("shortening URL");

let group_guid = match &self.cfg.default_group_guid {
Some(group_guid) => Cow::from(group_guid),
Expand Down Expand Up @@ -157,7 +160,8 @@ impl ClientInner {

let endpoint = self.api_url("shorten");

//println!("sending shorten request: {payload:#?}");
debug!(?payload, "sending shorten request");

let resp = http
.post(endpoint)
.bearer_auth(self.cfg.api_token())
Expand Down Expand Up @@ -188,6 +192,7 @@ impl ClientInner {
result
}

#[instrument(level = "debug", skip_all)]
fn shorten_all(
self: Arc<Self>,
urls: impl Stream<Item = Url>,
Expand All @@ -205,10 +210,13 @@ pub struct Client {

// TODO: handle timeouts, cancellation, API limits (see `GET /v4/user/platform_limits`), etc.
impl Client {
#[instrument(name = "init_client", level = "debug")]
pub async fn new(cfg: Config) -> Self {
let http = if cfg.offline {
debug!("offline mode enabled, skipping HTTP client initialization");
None
} else {
debug!("initializing HTTP client");
Some(reqwest::Client::new())
};

Expand All @@ -219,6 +227,7 @@ impl Client {
}
}

#[instrument(level = "debug", skip(self, urls))]
pub fn shorten<'a, S>(
&self,
urls: S,
Expand Down
Loading

0 comments on commit 3d3c886

Please sign in to comment.