diff --git a/src/files.rs b/src/files.rs index bc9cff6..79fda03 100644 --- a/src/files.rs +++ b/src/files.rs @@ -15,7 +15,8 @@ use actix_web::{ HttpResponse, }; use log::{debug, error, info, trace}; - +use opentelemetry::KeyValue; +use opentelemetry::trace::{Span, Status, Tracer}; use crate::data::{Claims, FilesRequest, FilterMode, PhotosResponse, SortType}; use crate::{create_thumbnails, AppState}; @@ -27,6 +28,7 @@ use path_absolutize::*; use rand::prelude::SliceRandom; use rand::thread_rng; use serde::Deserialize; +use crate::otel::global_tracer; pub async fn list_photos( _: Claims, @@ -36,6 +38,10 @@ pub async fn list_photos( tag_dao: web::Data>, ) -> HttpResponse { let search_path = &req.path; + + let tracer = global_tracer(); + let mut span = tracer.start("list_photos"); + span.set_attribute(KeyValue::new("path", search_path.to_string())); let search_recursively = req.recursive.unwrap_or(false); if let Some(tag_ids) = &req.tag_ids { @@ -99,6 +105,8 @@ pub async fn list_photos( tagged_files.len(), tagged_files ); + span.set_attribute(KeyValue::new("file_count", tagged_files.len().to_string())); + span.set_status(Status::Ok); HttpResponse::Ok().json(PhotosResponse { photos: tagged_files, @@ -190,12 +198,16 @@ pub async fn list_photos( .map(|f| f.to_str().unwrap().to_string()) .collect::>(); + span.set_attribute(KeyValue::new("file_count", files.len().to_string())); + span.set_status(Status::Ok); + HttpResponse::Ok().json(PhotosResponse { photos: response_files, dirs, }) } else { error!("Bad photos request: {}", req.path); + span.set_status(Status::error("Invalid path")); HttpResponse::BadRequest().finish() } } @@ -224,12 +236,18 @@ fn sort(mut files: Vec, sort_type: SortType) -> Vec { } pub fn list_files(dir: &Path) -> io::Result> { + let tracer = global_tracer(); + let mut span = tracer.start("list_files"); + let dir_name_string = dir.to_str().unwrap_or_default().to_string(); + span.set_attribute(KeyValue::new("dir", dir_name_string)); + let files = read_dir(dir)? .filter_map(|res| res.ok()) .filter(|entry| is_image_or_video(&entry.path()) || entry.file_type().unwrap().is_dir()) .map(|entry| entry.path()) .collect::>(); + span.set_attribute(KeyValue::new("file_count", files.len().to_string())); Ok(files) } @@ -372,6 +390,7 @@ impl FileSystemAccess for RealFileSystem { } fn move_file>(&self, from: P, destination: P) -> anyhow::Result<()> { + info!("Moving file: '{:?}' -> '{:?}'", from.as_ref(), destination.as_ref()); let name = from .as_ref() .file_name() @@ -393,6 +412,8 @@ impl Handler for StreamActor { type Result = (); fn handle(&mut self, _msg: RefreshThumbnailsMessage, _ctx: &mut Self::Context) -> Self::Result { + let tracer = global_tracer(); + let _ = tracer.start("RefreshThumbnailsMessage"); info!("Refreshing thumbnails after upload"); create_thumbnails() } diff --git a/src/main.rs b/src/main.rs index 5aeaa1f..5dbf5c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,6 +83,9 @@ async fn get_image( req: web::Query, app_state: Data, ) -> impl Responder { + let tracer = global_tracer(); + let mut span = tracer.start("get_image"); + if let Some(path) = is_valid_full_path(&app_state.base_path, &req.path, false) { let image_size = req.size.unwrap_or(PhotoSize::Full); if image_size == PhotoSize::Thumb { @@ -95,16 +98,21 @@ async fn get_image( trace!("Thumbnail path: {:?}", thumb_path); if let Ok(file) = NamedFile::open(&thumb_path) { + span.set_status(Status::Ok); file.into_response(&request) } else { + span.set_status(Status::error("Not found")); HttpResponse::NotFound().finish() } } else if let Ok(file) = NamedFile::open(path) { + span.set_status(Status::Ok); file.into_response(&request) } else { + span.set_status(Status::error("Not found")); HttpResponse::NotFound().finish() } } else { + span.set_status(Status::error("Bad photos request")); error!("Bad photos request: {}", req.path); HttpResponse::BadRequest().finish() } @@ -309,7 +317,7 @@ async fn get_video_part( path: web::Path, app_state: Data, ) -> impl Responder { - let tracer = global::tracer("image-server"); + let tracer = global_tracer(); let mut span = tracer.start("get_video_part"); let part = &path.path; @@ -381,15 +389,15 @@ async fn put_add_favorite( .await { Ok(Err(e)) if e.kind == DbErrorKind::AlreadyExists => { - debug!("Favorite: {} exists for user: {}", &body.path, user_id); + warn!("Favorite: {} exists for user: {}", &body.path, user_id); HttpResponse::Ok() } Ok(Err(e)) => { - info!("{:?} {}. for user: {}", e, body.path, user_id); + error!("{:?} {}. for user: {}", e, body.path, user_id); HttpResponse::BadRequest() } Ok(Ok(_)) => { - debug!("Adding favorite \"{}\" for userid: {}", body.path, user_id); + info!("Adding favorite \"{}\" for userid: {}", body.path, user_id); HttpResponse::Created() } Err(e) => { diff --git a/src/otel.rs b/src/otel.rs index cc6af16..5c9e121 100644 --- a/src/otel.rs +++ b/src/otel.rs @@ -13,8 +13,7 @@ pub fn init_tracing() { let resources = Resource::builder() .with_attributes([ KeyValue::new("service.name", "image-server"), - //TODO: Get this from somewhere - KeyValue::new("service.version", "1.0"), + KeyValue::new("service.version", env!("CARGO_PKG_VERSION")), ]) .build();