Skip to content

Commit

Permalink
refact: add tracing to versions, wrap tracing span to top-level app
Browse files Browse the repository at this point in the history
Reorganize StelaeRootSpan to include all the apps HTTP requests,
including versions. Add some initial tracing debugs to versions endpoint.
  • Loading branch information
n-dusan committed Jun 5, 2024
1 parent cc650c4 commit 6bbae25
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
9 changes: 3 additions & 6 deletions src/server/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
#![allow(clippy::exit)]
use std::{process, sync::OnceLock};

use crate::server::api::state;
use crate::stelae::{stele::Stele, types::repositories::Repositories};
use actix_service::ServiceFactory;
use actix_web::{
body::MessageBody,
dev::{ServiceRequest, ServiceResponse},
guard, web, App, Error, Scope,
};
use tracing_actix_web::TracingLogger;

use crate::server::{api::state, tracing::StelaeRootSpanBuilder};
use crate::stelae::{stele::Stele, types::repositories::Repositories};

use super::{serve::serve, state::Global, versions::versions};

Expand All @@ -27,6 +25,7 @@ static HEADER_VALUES: OnceLock<Vec<String>> = OnceLock::new();
///
/// # Errors
/// Errors if unable to register dynamic routes (e.g. if git repository cannot be opened)
#[tracing::instrument(skip(app, state))]
pub fn register_app<
T: Global + Clone + 'static,
U: MessageBody,
Expand Down Expand Up @@ -144,7 +143,6 @@ fn initialize_guarded_dynamic_routes<
app = app.service(
stelae_scope
.app_data(web::Data::new(shared_state))
.wrap(TracingLogger::<StelaeRootSpanBuilder>::new())
.configure(|cfg| {
register_root_routes(cfg, guarded_stele).unwrap_or_else(|_| {
tracing::error!(
Expand Down Expand Up @@ -188,7 +186,6 @@ fn initialize_dynamic_routes<
app = app.service(
web::scope("")
.app_data(web::Data::new(shared_state))
.wrap(TracingLogger::<StelaeRootSpanBuilder>::new())
.configure(|cfg| {
register_routes(cfg, state).unwrap_or_else(|_| {
tracing::error!(
Expand Down
9 changes: 8 additions & 1 deletion src/server/api/versions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@ pub mod request;
pub mod response;

/// Handler for the versions endpoint.
#[tracing::instrument(skip(req, data))]
pub async fn versions(
req: HttpRequest,
data: web::Data<AppState>,
params: web::Path<request::Version>,
) -> impl Responder {
let stele = match get_stele_from_request(&req, data.archive()) {
Ok(stele) => stele,
Err(err) => return HttpResponse::BadRequest().body(format!("Error: {err}")),
Err(err) => {
tracing::error!("Error getting stele from request: {err}");
return HttpResponse::BadRequest().body(format!("Error: {err}"));
}
};
let db = data.db();
let mut publications = publication::Manager::find_all_non_revoked_publications(db, &stele)
.await
.unwrap_or_default();

let Some(current_publication) = publications.first() else {
tracing::warn!("No publications found for stele: {stele}");
return HttpResponse::NotFound().body("No publications found.");
};

Expand Down Expand Up @@ -149,6 +154,7 @@ async fn publication_versions(
publication: &Publication,
url: String,
) -> Vec<response::Version> {
tracing::debug!("Fetching publication versions for '{url}'");
let mut versions = vec![];
let doc_mpath = document_change::Manager::find_doc_mpath_by_url(db, &url).await;
if let Ok(mpath) = doc_mpath {
Expand All @@ -175,6 +181,7 @@ async fn publication_versions(
versions = coll_versions.into_iter().map(Into::into).collect();
}
}
tracing::debug!("Found {} versions", versions.len());
versions
}

Expand Down
8 changes: 6 additions & 2 deletions src/server/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ use crate::server::api::state::App as AppState;
use crate::stelae::archive::Archive;
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::{App, Error, HttpServer};
use tracing_actix_web::TracingLogger;

use std::{io, path::PathBuf, process};

use actix_http::body::MessageBody;
use actix_service::ServiceFactory;

use super::api::state::Global;
use super::tracing::StelaeRootSpanBuilder;
use crate::server::api::routes;

/// Serve documents in a Stelae archive.
#[actix_web::main]
#[tracing::instrument(skip(raw_archive_path, archive_path, port, individual))]
pub async fn serve_archive(
raw_archive_path: &str,
archive_path: PathBuf,
Expand Down Expand Up @@ -81,6 +84,7 @@ pub fn init_app<T: Global + Clone + 'static>(
>,
>,
> {
let app = routes::register_app(App::new(), state)?;
Ok(app)
let app = App::new().wrap(TracingLogger::<StelaeRootSpanBuilder>::new());
let registered_app = routes::register_app(app, state)?;
Ok(registered_app)
}
2 changes: 1 addition & 1 deletion src/utils/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Repo {
/// in archive, or if there is something wrong with the git repository.
pub fn new(archive_path: &Path, org: &str, name: &str) -> anyhow::Result<Self> {
let archive_path_str = archive_path.to_string_lossy();
tracing::debug!(org, name, "Creating new Repo at {archive_path_str}");
tracing::trace!(org, name, "Creating new Repo at {archive_path_str}");
let repo_path = format!("{archive_path_str}/{org}/{name}");
Ok(Self {
archive_path: archive_path_str.into(),
Expand Down

0 comments on commit 6bbae25

Please sign in to comment.