From c59fd4e1c91c5828c016942910bc6877ee6c40bc Mon Sep 17 00:00:00 2001 From: Alex Krantz Date: Mon, 5 Dec 2022 20:00:12 -0800 Subject: [PATCH] feat(gateway): add more tracing events --- gateway/src/api/latest.rs | 11 ++++++++++- gateway/src/main.rs | 30 +++++++++++++++++------------- gateway/src/project.rs | 12 +++++++++++- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/gateway/src/api/latest.rs b/gateway/src/api/latest.rs index 1581f473f5..e9ed968e55 100644 --- a/gateway/src/api/latest.rs +++ b/gateway/src/api/latest.rs @@ -20,7 +20,7 @@ use shuttle_common::models::error::ErrorKind; use shuttle_common::models::{project, user}; use tokio::sync::mpsc::Sender; use tower_http::trace::TraceLayer; -use tracing::{debug, debug_span, field, Span}; +use tracing::{debug, debug_span, field, instrument, Span}; use crate::acme::{AcmeClient, CustomDomain}; use crate::auth::{Admin, ScopedUser, User}; @@ -65,6 +65,7 @@ impl StatusResponse { } } +#[instrument(skip_all, fields(%account_name))] async fn get_user( State(RouterState { service, .. }): State, Path(account_name): Path, @@ -75,6 +76,7 @@ async fn get_user( Ok(AxumJson(user.into())) } +#[instrument(skip_all, fields(%account_name))] async fn post_user( State(RouterState { service, .. }): State, Path(account_name): Path, @@ -85,6 +87,7 @@ async fn post_user( Ok(AxumJson(user.into())) } +#[instrument(skip(service))] async fn get_project( State(RouterState { service, .. }): State, ScopedUser { scope, .. }: ScopedUser, @@ -98,6 +101,7 @@ async fn get_project( Ok(AxumJson(response)) } +#[instrument(skip_all, fields(%project))] async fn post_project( State(RouterState { service, sender }): State, User { name, .. }: User, @@ -121,6 +125,7 @@ async fn post_project( Ok(AxumJson(response)) } +#[instrument(skip_all, fields(%project))] async fn delete_project( State(RouterState { service, sender }): State, ScopedUser { scope: project, .. }: ScopedUser, @@ -149,6 +154,7 @@ async fn delete_project( Ok(AxumJson(response)) } +#[instrument(skip_all, fields(scope = %scoped_user.scope))] async fn route_project( State(RouterState { service, .. }): State, scoped_user: ScopedUser, @@ -176,6 +182,7 @@ async fn get_status(State(RouterState { sender, .. }): State) -> Re .unwrap() } +#[instrument(skip_all)] async fn revive_projects( _: Admin, State(RouterState { service, sender }): State, @@ -185,6 +192,7 @@ async fn revive_projects( .map_err(|_| Error::from_kind(ErrorKind::Internal)) } +#[instrument(skip_all, fields(%email, ?acme_server))] async fn create_acme_account( _: Admin, Extension(acme_client): Extension, @@ -196,6 +204,7 @@ async fn create_acme_account( Ok(AxumJson(res)) } +#[instrument(skip_all, fields(%project_name, %fqdn))] async fn request_acme_certificate( _: Admin, State(RouterState { service, sender }): State, diff --git a/gateway/src/main.rs b/gateway/src/main.rs index b96a36b08c..f45e78c2dc 100644 --- a/gateway/src/main.rs +++ b/gateway/src/main.rs @@ -19,7 +19,7 @@ use std::io::{self, Cursor}; use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::Duration; -use tracing::{debug, error, info, info_span, trace, warn}; +use tracing::{debug, error, info, info_span, trace, warn, Instrument}; use tracing_subscriber::{fmt, prelude::*, EnvFilter}; #[tokio::main(flavor = "multi_thread")] @@ -122,20 +122,24 @@ async fn start(db: SqlitePool, fs: PathBuf, args: StartArgs) -> io::Result<()> { "running health checks", healthcheck.num_projects = projects.len() ); - let _ = span.enter(); - for (project_name, _) in projects { - if let Ok(handle) = gateway - .new_task() - .project(project_name) - .and_then(task::check_health()) - .send(&sender) - .await - { - // we wait for the check to be done before - // queuing up the next one - handle.await + + async move { + for (project_name, _) in projects { + if let Ok(handle) = gateway + .new_task() + .project(project_name) + .and_then(task::check_health()) + .send(&sender) + .await + { + // we wait for the check to be done before + // queuing up the next one + handle.await + } } } + .instrument(span) + .await; } } } diff --git a/gateway/src/project.rs b/gateway/src/project.rs index e36f105f06..51e0ee320b 100644 --- a/gateway/src/project.rs +++ b/gateway/src/project.rs @@ -19,7 +19,7 @@ use once_cell::sync::Lazy; use rand::distributions::{Alphanumeric, DistString}; use serde::{Deserialize, Serialize}; use tokio::time::{self, timeout}; -use tracing::{debug, error}; +use tracing::{debug, error, instrument}; use crate::{ ContainerSettings, DockerContext, EndState, Error, ErrorKind, IntoTryState, ProjectName, @@ -276,6 +276,7 @@ where type Next = Self; type Error = Infallible; + #[instrument(skip_all, fields(state = %self.state()))] async fn next(self, ctx: &Ctx) -> Result { let previous = self.clone(); let previous_state = previous.state(); @@ -557,6 +558,7 @@ where type Next = ProjectStarting; type Error = ProjectError; + #[instrument(skip_all)] async fn next(self, ctx: &Ctx) -> Result { let container_name = self.container_name(ctx); let container = ctx @@ -593,6 +595,7 @@ where type Next = ProjectStarted; type Error = ProjectError; + #[instrument(skip_all)] async fn next(self, ctx: &Ctx) -> Result { let container_id = self.container.id.as_ref().unwrap(); ctx.docker() @@ -642,6 +645,7 @@ where type Next = ProjectReadying; type Error = ProjectError; + #[instrument(skip_all)] async fn next(self, ctx: &Ctx) -> Result { time::sleep(Duration::from_secs(1)).await; @@ -688,6 +692,7 @@ where type Next = Self; type Error = ProjectError; + #[instrument(skip_all)] async fn next(mut self, _ctx: &Ctx) -> Result { Ok(self) } @@ -781,6 +786,7 @@ where type Error = ProjectError; + #[instrument(skip_all)] async fn next(self, ctx: &Ctx) -> Result { let Self { container } = self; ctx.docker() @@ -808,6 +814,7 @@ where type Next = ProjectStarting; type Error = ProjectError; + #[instrument(skip_all)] async fn next(self, ctx: &Ctx) -> Result { let container = self.container; @@ -860,6 +867,7 @@ where type Next = ProjectDestroyed; type Error = ProjectError; + #[instrument(skip_all)] async fn next(self, ctx: &Ctx) -> Result { let container_id = self.container.id.as_ref().unwrap(); ctx.docker() @@ -895,6 +903,7 @@ where type Next = ProjectDestroyed; type Error = ProjectError; + #[instrument(skip_all)] async fn next(self, _ctx: &Ctx) -> Result { Ok(self) } @@ -980,6 +989,7 @@ where type Next = Self; type Error = Infallible; + #[instrument(skip_all)] async fn next(self, _ctx: &Ctx) -> Result { Ok(self) }