Optional tracing support

pull/34/head
Thijs Molendijk 2021-07-04 20:07:56 +02:00 committed by Mingwei Samuel
parent 032d3ac97d
commit 3c4f3c967a
4 changed files with 48 additions and 11 deletions

View File

@ -41,6 +41,7 @@ serde_repr = "0.1"
strum = "0.20" strum = "0.20"
strum_macros = "0.20" strum_macros = "0.20"
tokio = { version = "1", default-features = false, features = [ "time", "macros", "parking_lot" ] } tokio = { version = "1", default-features = false, features = [ "time", "macros", "parking_lot" ] }
tracing = { version = "0.1", optional = true }
[dev-dependencies] [dev-dependencies]
colored = "2" colored = "2"

View File

@ -71,12 +71,28 @@ Output:
9) Irelia 46465 (5) 9) Irelia 46465 (5)
10) Vladimir 37176 (5) 10) Vladimir 37176 (5)
``` ```
### Feature Flags
### Nightly vs Stable #### Nightly vs Stable
Enable the `nightly` feature to use nightly-only functionality. Mainly enables Enable the `nightly` feature to use nightly-only functionality. This enables
[nightly optimizations in the `parking_lot` crate](https://github.com/Amanieu/parking_lot#nightly-vs-stable). [nightly optimizations in the `parking_lot` crate](https://github.com/Amanieu/parking_lot#nightly-vs-stable).
Also required for running async integration tests.
```toml
riven = { version = "...", features = [ "nightly" ] }
```
#### rustls
Riven uses [reqwest](https://github.com/seanmonstar/reqwest) for making requests. By default, reqwest uses the native TLS library.
If you prefer using [rustls](https://github.com/ctz/rustls) you can do so by turning off the Riven default features
and specifying the `rustls-tls` feature:
```toml
riven = { version = "...", default-features = false, features = [ "rustls-tls" ] }
```
Riven is additionally able to produce [tracing](https://docs.rs/tracing) spans for requests if the `tracing` feature is enabled. This feature is disabled by default.
### Docs ### Docs

View File

@ -3,12 +3,14 @@ use std::sync::Arc;
use log; use log;
use reqwest::{ StatusCode, RequestBuilder }; use reqwest::{ StatusCode, RequestBuilder };
#[cfg(feature = "tracing")]
use tracing::Instrument;
use crate::Result;
use crate::ResponseInfo;
use crate::RiotApiError;
use crate::RiotApiConfig;
use crate::util::InsertOnlyCHashMap; use crate::util::InsertOnlyCHashMap;
use crate::ResponseInfo;
use crate::Result;
use crate::RiotApiConfig;
use crate::RiotApiError;
use super::RateLimit; use super::RateLimit;
use super::RateLimitType; use super::RateLimitType;
@ -46,11 +48,20 @@ impl RegionalRequester {
.get_or_insert_with(method_id, || RateLimit::new(RateLimitType::Method)); .get_or_insert_with(method_id, || RateLimit::new(RateLimitType::Method));
// Rate limit. // Rate limit.
RateLimit::acquire_both(&self.app_rate_limit, &*method_rate_limit).await; let rate_limit = RateLimit::acquire_both(&self.app_rate_limit, &*method_rate_limit);
#[cfg(feature = "tracing")]
let rate_limit = rate_limit.instrument(tracing::info_span!("rate_limit"));
rate_limit.await;
// Send request. // Send request.
let request_clone = request.try_clone().expect("Failed to clone request."); let request_clone = request
let response = request_clone.send().await .try_clone()
.expect("Failed to clone request.")
.send();
#[cfg(feature = "tracing")]
let request_clone = request_clone.instrument(tracing::info_span!("request"));
let response = request_clone
.await
.map_err(|e| RiotApiError::new(e, retries, None, None))?; .map_err(|e| RiotApiError::new(e, retries, None, None))?;
// Maybe update rate limits (based on response headers). // Maybe update rate limits (based on response headers).

View File

@ -13,6 +13,8 @@ use crate::models::*;
use std::future::Future; use std::future::Future;
use std::vec::Vec; use std::vec::Vec;
#[cfg(feature="tracing")]
use tracing::Instrument;
use reqwest::Method; use reqwest::Method;
use crate::Result; use crate::Result;
@ -197,7 +199,14 @@ impl<'a> {{= endpoint }}<'a> {
{{? bodyType }} {{? bodyType }}
let request = request.body(serde_json::ser::to_vec(body).unwrap()); let request = request.body(serde_json::ser::to_vec(body).unwrap());
{{?}} {{?}}
self.base.execute{{= hasReturn ? (returnOptional ? '_opt' : '_val') : '' }}{{= returnTypeTurbofish }}("{{= operationId }}", route_str, request) let future = self
.base
.execute{{= hasReturn ? (returnOptional ? '_opt' : '_val') : '' }}{{= returnTypeTurbofish }}("{{= operationId }}", route_str, request);
#[cfg(feature = "tracing")]
let future = future.instrument(tracing::info_span!("{{= operationId }}"));
future
} }
{{ {{