Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing to requests #307

Merged
merged 2 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ futures-util = { version = "0.3.15", optional = true }
secrecy = "0.8.0"
cfg-if = "1.0.0"
either = "1.8.0"
tokio = "1.25"
tracing = "0.1"

[dev-dependencies]
tokio = { version = "1.17.0", default-features = false, features = [
Expand All @@ -47,7 +49,7 @@ wiremock = "0.5.3"

[features]
default = ["native-tls"]
rustls = ["rustls-tls"] # Leagcy support (<=0.17.0)
rustls = ["rustls-tls"] # Legacy support (<=0.17.0)

# Enables native-tls specific functionality not available by default.
native-tls = ["reqwest/native-tls"]
Expand Down
39 changes: 38 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ pub use self::{
page::Page,
};

use tracing::{self, field};

const GITHUB_SERVICE: &str = "github.com";

/// A convenience type with a default error type of [`Error`].
pub type Result<T, E = error::Error> = std::result::Result<T, E>;

Expand Down Expand Up @@ -893,6 +897,39 @@ impl Octocrab {
}
}

#[tracing::instrument(
level="debug",
name="github api call",
skip(self, request_builder, attempt),
fields(err, url=field::Empty, service.name=field::Empty, http.method=field::Empty, http.url=field::Empty, http.status_code=field::Empty, http.resend_count=field::Empty)
)]
async fn send_call(
&self,
request_builder: reqwest::RequestBuilder,
attempt: u32,
) -> Result<reqwest::Response, reqwest::Error> {
let span = tracing::Span::current();
let service = self.base_url.host_str().unwrap_or(GITHUB_SERVICE);
span.record("service.name", service);
if attempt > 1 {
span.record("http.resend_count", attempt);
}
let request = request_builder.build()?;
span.record("http.method", request.method().as_str());
span.record("http.url", request.url().as_str());
let result = self.client.execute(request).await;
match &result {
Ok(v) => {
span.record("http.status_code", v.status().as_u16());
}
Err(e) => {
let status = e.status().and_then(|s| Some(s.as_u16()));
span.record("http.status_code", status);
}
};
result
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to pull things out to a sep async function because span would've moved if I used .instrument(span), which then prevents recording fields after the request.


/// Execute the given `request` using octocrab's Client.
pub async fn execute(&self, mut request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
let mut retries = 0;
Expand Down Expand Up @@ -920,7 +957,7 @@ impl Octocrab {
}
};

let result = request.send().await;
let result = self.send_call(request, retries).await;
let status = match &result {
Ok(v) => Some(v.status()),
Err(e) => e.status(),
Expand Down