m09526 commented on code in PR #432: URL: https://github.com/apache/arrow-rs-object-store/pull/432#discussion_r2213162540
########## src/trace.rs: ########## @@ -0,0 +1,578 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! An object store that traces calls to the wrapped implementation. +use crate::{ + path::Path, GetOptions, GetRange, GetResult, ListResult, MultipartUpload, ObjectMeta, + ObjectStore, PutMultipartOptions, PutOptions, PutPayload, PutResult, Result, UploadPart, +}; +use async_trait::async_trait; +use futures::stream::BoxStream; +use tracing::debug; + +/// An [`ObjectStore`] wrapper that traces operations made to the wrapped store. +#[derive(Debug)] +pub struct TracingStore<T: ObjectStore> { + store: T, + prefix: String, + path_prefix: String, +} + +impl<T: ObjectStore> TracingStore<T> { + /// Create a new tracing store by wrapping an inner store. + #[must_use] + pub fn new(inner: T, prefix: impl Into<String>, path_prefix: impl Into<String>) -> Self { + Self { + store: inner, + prefix: prefix.into(), + path_prefix: path_prefix.into(), + } + } +} + +impl<T: ObjectStore> std::fmt::Display for TracingStore<T> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "TracingStore \"{}\" path prefix: \"{}\" ({})", + self.prefix, self.path_prefix, self.store + ) + } +} + +#[async_trait] +impl<T: ObjectStore> ObjectStore for TracingStore<T> { + async fn get_opts(&self, location: &Path, options: GetOptions) -> Result<GetResult> { + if !options.head { + match &options.range { + Some(GetRange::Bounded(get_range)) => { + let len = get_range + .end + .checked_sub(get_range.start) + .expect("Get range length is negative"); + debug!( + "{} get request for {}/{} byte range {} to {} = {} bytes", + self.prefix, + self.path_prefix, + location, + get_range.start, + get_range.end, + len, + ); + } + Some(GetRange::Offset(start_pos)) => { + debug!( + "{} get request for {}/{} for byte {} to EOF", + self.prefix, self.path_prefix, location, start_pos, + ); + } + Some(GetRange::Suffix(pos)) => { + debug!( + "{} get request for {}/{} for last {} bytes of object", + self.prefix, self.path_prefix, location, pos, + ); + } + None => { + debug!( + "{} get request for {}/{} for complete file range", + self.prefix, self.path_prefix, location + ); + } + } + } + self.store.get_opts(location, options).await + } + + async fn head(&self, location: &Path) -> Result<ObjectMeta> { + debug!( + "{} head request for {}/{}", + self.prefix, self.path_prefix, location + ); Review Comment: I've made some changes to use spans instead. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org