From 15cb998234616a3c2ac7c755bcc02f6177ecd582 Mon Sep 17 00:00:00 2001 From: goodspark Date: Sat, 25 Feb 2023 04:08:18 -0800 Subject: [PATCH] Add tracing to requests (#307) --- Cargo.toml | 1 + src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b907fd30..39d2c600 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ futures-util = { version = "0.3.15", optional = true } secrecy = "0.8.0" cfg-if = "1.0.0" either = "1.8.0" +tracing = "0.1" [dev-dependencies] tokio = { version = "1.17.0", default-features = false, features = [ diff --git a/src/lib.rs b/src/lib.rs index a0999b0f..1b983df5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = std::result::Result; @@ -911,6 +915,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 { + 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 + } + /// Execute the given `request` using octocrab's Client. pub async fn execute(&self, mut request: reqwest::RequestBuilder) -> Result { let mut retries = 1; @@ -935,7 +972,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(),