Skip to content

Commit

Permalink
feat(cargo-shuttle): basic request tracing (#1827)
Browse files Browse the repository at this point in the history
* bruh

* feat: basic request tracing
jonaro00 authored Jul 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d70e715 commit c537566
Showing 3 changed files with 72 additions and 14 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions cargo-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -41,12 +41,14 @@ percent-encoding = { workspace = true }
portpicker = { workspace = true }
regex = "1.9.5"
reqwest = { workspace = true, features = ["json"] }
reqwest-middleware = "0.2.5"
rmp-serde = { workspace = true }
semver = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
strum = { workspace = true }
tar = { workspace = true }
task-local-extensions = "0.1.4"
tempfile = { workspace = true }
tokio = { workspace = true, features = ["macros", "signal", "rt-multi-thread"] }
tokio-tungstenite = { version = "0.20.1", features = [
58 changes: 44 additions & 14 deletions cargo-shuttle/src/client.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ use anyhow::{Context, Result};
use headers::{Authorization, HeaderMapExt};
use percent_encoding::utf8_percent_encode;
use reqwest::header::HeaderMap;
use reqwest::{RequestBuilder, Response};
use reqwest::{Request, Response};
use reqwest_middleware::{ClientWithMiddleware, Middleware, Next, RequestBuilder};
use serde::{Deserialize, Serialize};
use shuttle_common::constants::headers::X_CARGO_SHUTTLE_VERSION;
use shuttle_common::log::{LogsRange, LogsResponseBeta};
@@ -14,33 +15,38 @@ use shuttle_common::models::deployment::{
};
use shuttle_common::models::{deployment, project, service, team, user, ToJson};
use shuttle_common::{resource, ApiKey, LogItem, VersionInfo};
use task_local_extensions::Extensions;
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
use tracing::error;
use tracing::{debug, error};
use uuid::Uuid;

#[derive(Clone)]
pub struct ShuttleApiClient {
client: reqwest::Client,
client: ClientWithMiddleware,
api_url: String,
api_key: Option<ApiKey>,
}

impl ShuttleApiClient {
pub fn new(api_url: String, api_key: Option<ApiKey>) -> Self {
Self {
client: reqwest::Client::builder()
.default_headers(
HeaderMap::try_from(&HashMap::from([(
X_CARGO_SHUTTLE_VERSION.clone(),
crate::VERSION.to_owned(),
)]))
.unwrap(),
)
.timeout(Duration::from_secs(60))
.build()
let client = reqwest::Client::builder()
.default_headers(
HeaderMap::try_from(&HashMap::from([(
X_CARGO_SHUTTLE_VERSION.clone(),
crate::VERSION.to_owned(),
)]))
.unwrap(),
)
.timeout(Duration::from_secs(60))
.build()
.unwrap();
let client = reqwest_middleware::ClientBuilder::new(client)
.with(LoggingMiddleware)
.build();
Self {
client,
api_url,
api_key,
}
@@ -479,3 +485,27 @@ impl ShuttleApiClient {
.await
}
}

struct LoggingMiddleware;

#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
async fn handle(
&self,
req: Request,
extensions: &mut Extensions,
next: Next<'_>,
) -> reqwest_middleware::Result<Response> {
debug!("Request: {} {}", req.method(), req.url());
let res = next.run(req, extensions).await;
match res {
Ok(ref res) => {
debug!("Response: {}", res.status());
}
Err(ref e) => {
debug!("Response error: {}", e);
}
}
res
}
}

0 comments on commit c537566

Please sign in to comment.