Skip to content

Commit 3f80800

Browse files
Add tracing capabilities through trace and tokio feature flag
1 parent f7c2baf commit 3f80800

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- uses: actions-rs/cargo@v1
2323
with:
2424
command: build
25-
args: --verbose --features metrics --features client
25+
args: --verbose --features metrics --features client --features tokio --features trace
2626
test:
2727
name: Test library
2828
runs-on: ubuntu-latest
@@ -77,7 +77,7 @@ jobs:
7777
- uses: actions-rs/cargo@v1
7878
with:
7979
command: clippy
80-
args: --verbose --features metrics --features client -- -D warnings
80+
args: --verbose --features metrics --features client --features tokio --features trace -- -D warnings
8181
doc:
8282
name: Build documentation
8383
runs-on: ubuntu-latest
@@ -91,5 +91,5 @@ jobs:
9191
- uses: actions-rs/cargo@v1
9292
with:
9393
command: doc
94-
args: --verbose --features metrics --features client
94+
args: --verbose --features metrics --features client --features tokio --features trace
9595
...

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ serde_json = { version = "^1.0.68", features = [
3333
], optional = true }
3434
sha2 = { version = "^0.9.6", optional = true }
3535
thiserror = { version = "^1.0.29", optional = true }
36+
tracing = { version = "^0.1.29", optional = true }
37+
tracing-futures = { version = "^0.2.5", optional = true }
3638
urlencoding = { version = "^2.1.0", optional = true }
3739
uuid = { version = "^0.8.2", features = ["serde", "v4"], optional = true }
3840

@@ -55,5 +57,7 @@ client = [
5557
"urlencoding",
5658
"uuid",
5759
]
58-
logging = ["log"]
60+
logging = ["log", "tracing/log-always"]
61+
trace = ["tracing", "tracing-futures"]
62+
tokio = ["tracing-futures/tokio"]
5963
metrics = ["lazy_static", "prometheus"]

src/client/mod.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
collections::BTreeMap,
1111
convert::TryFrom,
1212
error::Error,
13-
fmt::{self, Display, Formatter},
13+
fmt::{self, Debug, Display, Formatter},
1414
time::{SystemTime, SystemTimeError},
1515
};
1616

@@ -70,35 +70,38 @@ pub trait Request {
7070
payload: &T,
7171
) -> Result<U, Self::Error>
7272
where
73-
T: Serialize + Send + Sync,
74-
U: DeserializeOwned + Send + Sync;
73+
T: Serialize + Debug + Send + Sync,
74+
U: DeserializeOwned + Debug + Send + Sync;
7575
}
7676

7777
// -----------------------------------------------------------------------------
7878
// RestClient trait
7979

8080
#[async_trait]
81-
pub trait RestClient {
81+
pub trait RestClient
82+
where
83+
Self: Debug,
84+
{
8285
type Error;
8386

8487
async fn get<T>(&self, endpoint: &str) -> Result<T, Self::Error>
8588
where
86-
T: DeserializeOwned + Send + Sync;
89+
T: DeserializeOwned + Debug + Send + Sync;
8790

8891
async fn post<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
8992
where
90-
T: Serialize + Send + Sync,
91-
U: DeserializeOwned + Send + Sync;
93+
T: Serialize + Debug + Send + Sync,
94+
U: DeserializeOwned + Debug + Send + Sync;
9295

9396
async fn put<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
9497
where
95-
T: Serialize + Send + Sync,
96-
U: DeserializeOwned + Send + Sync;
98+
T: Serialize + Debug + Send + Sync,
99+
U: DeserializeOwned + Debug + Send + Sync;
97100

98101
async fn patch<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
99102
where
100-
T: Serialize + Send + Sync,
101-
U: DeserializeOwned + Send + Sync;
103+
T: Serialize + Debug + Send + Sync,
104+
U: DeserializeOwned + Debug + Send + Sync;
102105

103106
async fn delete(&self, endpoint: &str) -> Result<(), Self::Error>;
104107
}
@@ -127,7 +130,10 @@ pub const OAUTH1_VERSION: &str = "oauth_version";
127130
pub const OAUTH1_VERSION_1: &str = "1.0";
128131
pub const OAUTH1_TOKEN: &str = "oauth_token";
129132

130-
pub trait OAuth1 {
133+
pub trait OAuth1
134+
where
135+
Self: Debug,
136+
{
131137
type Error;
132138

133139
// `params` returns OAuth1 parameters without the signature one
@@ -139,6 +145,7 @@ pub trait OAuth1 {
139145
// `signing_key` returns the key that is used to signed the signature
140146
fn signing_key(&self) -> String;
141147

148+
#[cfg_attr(feature = "trace", tracing::instrument)]
142149
// `sign` returns OAuth1 formatted Authorization header value
143150
fn sign(&self, method: &str, endpoint: &str) -> Result<String, Self::Error> {
144151
let signature = self.signature(method, endpoint)?;
@@ -171,7 +178,7 @@ pub struct ResponseError {
171178
}
172179

173180
impl Display for ResponseError {
174-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
181+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
175182
write!(
176183
f,
177184
"got response {} {}, {}",
@@ -208,6 +215,7 @@ pub struct Signer {
208215
impl OAuth1 for Signer {
209216
type Error = SignerError;
210217

218+
#[cfg_attr(feature = "trace", tracing::instrument)]
211219
fn params(&self) -> BTreeMap<String, String> {
212220
let mut params = BTreeMap::new();
213221

@@ -226,6 +234,7 @@ impl OAuth1 for Signer {
226234
params
227235
}
228236

237+
#[cfg_attr(feature = "trace", tracing::instrument)]
229238
fn signing_key(&self) -> String {
230239
format!(
231240
"{}&{}",
@@ -234,6 +243,7 @@ impl OAuth1 for Signer {
234243
)
235244
}
236245

246+
#[cfg_attr(feature = "trace", tracing::instrument)]
237247
fn signature(&self, method: &str, endpoint: &str) -> Result<String, Self::Error> {
238248
let (host, query) = match endpoint.find(|c| '?' == c) {
239249
None => (endpoint, ""),
@@ -283,6 +293,7 @@ impl OAuth1 for Signer {
283293
impl TryFrom<Credentials> for Signer {
284294
type Error = SignerError;
285295

296+
#[cfg_attr(feature = "trace", tracing::instrument)]
286297
fn try_from(credentials: Credentials) -> Result<Self, Self::Error> {
287298
let nonce = Uuid::new_v4().to_string();
288299
let timestamp = SystemTime::now()
@@ -334,15 +345,16 @@ pub struct Client {
334345
impl Request for Client {
335346
type Error = ClientError;
336347

348+
#[cfg_attr(feature = "trace", tracing::instrument)]
337349
async fn request<T, U>(
338350
&self,
339351
method: &Method,
340352
endpoint: &str,
341353
payload: &T,
342354
) -> Result<U, Self::Error>
343355
where
344-
T: Serialize + Send + Sync,
345-
U: DeserializeOwned + Send + Sync,
356+
T: Serialize + Debug + Send + Sync,
357+
U: DeserializeOwned + Debug + Send + Sync,
346358
{
347359
let buf = serde_json::to_vec(payload).map_err(ClientError::Serialize)?;
348360
let mut builder = hyper::Request::builder();
@@ -426,9 +438,10 @@ impl Request for Client {
426438
impl RestClient for Client {
427439
type Error = ClientError;
428440

441+
#[cfg_attr(feature = "trace", tracing::instrument)]
429442
async fn get<T>(&self, endpoint: &str) -> Result<T, Self::Error>
430443
where
431-
T: DeserializeOwned + Send + Sync,
444+
T: DeserializeOwned + Debug + Send + Sync,
432445
{
433446
let method = &Method::GET;
434447
let mut builder = hyper::Request::builder();
@@ -506,30 +519,34 @@ impl RestClient for Client {
506519
Ok(serde_json::from_reader(buf.reader()).map_err(ClientError::Deserialize)?)
507520
}
508521

522+
#[cfg_attr(feature = "trace", tracing::instrument)]
509523
async fn post<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
510524
where
511-
T: Serialize + Send + Sync,
512-
U: DeserializeOwned + Send + Sync,
525+
T: Serialize + Debug + Send + Sync,
526+
U: DeserializeOwned + Debug + Send + Sync,
513527
{
514528
self.request(&Method::POST, endpoint, payload).await
515529
}
516530

531+
#[cfg_attr(feature = "trace", tracing::instrument)]
517532
async fn put<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
518533
where
519-
T: Serialize + Send + Sync,
520-
U: DeserializeOwned + Send + Sync,
534+
T: Serialize + Debug + Send + Sync,
535+
U: DeserializeOwned + Debug + Send + Sync,
521536
{
522537
self.request(&Method::PUT, endpoint, payload).await
523538
}
524539

540+
#[cfg_attr(feature = "trace", tracing::instrument)]
525541
async fn patch<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
526542
where
527-
T: Serialize + Send + Sync,
528-
U: DeserializeOwned + Send + Sync,
543+
T: Serialize + Debug + Send + Sync,
544+
U: DeserializeOwned + Debug + Send + Sync,
529545
{
530546
self.request(&Method::PATCH, endpoint, payload).await
531547
}
532548

549+
#[cfg_attr(feature = "trace", tracing::instrument)]
533550
async fn delete(&self, endpoint: &str) -> Result<(), Self::Error> {
534551
let method = &Method::DELETE;
535552
let mut builder = hyper::Request::builder();
@@ -609,6 +626,7 @@ impl RestClient for Client {
609626
}
610627

611628
impl Default for Client {
629+
#[cfg_attr(feature = "trace", tracing::instrument)]
612630
fn default() -> Self {
613631
let connector = HttpsConnector::new();
614632
let inner = hyper::Client::builder().build(connector);
@@ -621,6 +639,7 @@ impl Default for Client {
621639
}
622640

623641
impl From<Credentials> for Client {
642+
#[cfg_attr(feature = "trace", tracing::instrument)]
624643
fn from(credentials: Credentials) -> Self {
625644
let mut client = Self::default();
626645
client.set_credentials(Some(credentials));
@@ -629,6 +648,7 @@ impl From<Credentials> for Client {
629648
}
630649

631650
impl Client {
651+
#[cfg_attr(feature = "trace", tracing::instrument)]
632652
pub fn set_credentials(&mut self, credentials: Option<Credentials>) {
633653
self.credentials = credentials;
634654
}

0 commit comments

Comments
 (0)