Skip to content

Commit a2fc98b

Browse files
authored
Merge pull request #4 from cgwalters/tracing
Add a lot more tracing usage
2 parents 5b74e02 + 3ba2f01 commit a2fc98b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/imageproxy.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::pin::Pin;
1616
use std::process::Stdio;
1717
use std::sync::{Arc, Mutex};
1818
use tokio::io::{AsyncBufRead, AsyncReadExt};
19+
use tracing::instrument;
1920

2021
pub const OCI_TYPE_LAYER_GZIP: &str = "application/vnd.oci.image.layer.v1.tar+gzip";
2122
pub const OCI_TYPE_LAYER_TAR: &str = "application/vnd.oci.image.layer.v1.tar";
@@ -80,6 +81,7 @@ impl std::fmt::Debug for ImageProxy {
8081
}
8182

8283
/// Opaque identifier for an image
84+
#[derive(Debug, PartialEq, Eq)]
8385
pub struct OpenedImage(u32);
8486

8587
#[allow(unsafe_code)]
@@ -108,6 +110,7 @@ fn file_from_scm_rights(cmsg: ControlMessageOwned) -> Option<File> {
108110

109111
impl ImageProxy {
110112
/// Create an image proxy that fetches the target image.
113+
#[instrument]
111114
pub async fn new() -> Result<Self> {
112115
let (mysock, theirsock) = new_seqpacket_pair()?;
113116
let mut c = std::process::Command::new("skopeo");
@@ -117,6 +120,7 @@ impl ImageProxy {
117120
let mut c = tokio::process::Command::from(c);
118121
c.kill_on_drop(true);
119122
let child = c.spawn().context("Failed to spawn skopeo")?;
123+
tracing::debug!("Spawned skopeo pid={:?}", child.id());
120124
let childwait = Box::pin(child.wait_with_output());
121125

122126
let sockfd = Arc::new(Mutex::new(mysock));
@@ -142,6 +146,7 @@ impl ImageProxy {
142146
sockfd: Arc<Mutex<File>>,
143147
req: Request,
144148
) -> Result<(T, Option<(File, u32)>)> {
149+
tracing::trace!("sending request {}", req.method.as_str());
145150
// TODO: Investigate https://crates.io/crates/uds for SOCK_SEQPACKET tokio
146151
let r = tokio::task::spawn_blocking(move || {
147152
let sockfd = sockfd.lock().unwrap();
@@ -187,9 +192,11 @@ impl ImageProxy {
187192
Ok((reply, fdret))
188193
})
189194
.await??;
195+
tracing::trace!("completed request");
190196
Ok(r)
191197
}
192198

199+
#[instrument(skip(args))]
193200
async fn impl_request<R: serde::de::DeserializeOwned + Send + 'static, T, I>(
194201
&mut self,
195202
method: &str,
@@ -212,22 +219,28 @@ impl ImageProxy {
212219
}
213220
}
214221

222+
#[instrument]
215223
async fn finish_pipe(&mut self, pipeid: u32) -> Result<()> {
224+
tracing::debug!("closing pipe");
216225
let (r, fd) = self.impl_request("FinishPipe", [pipeid]).await?;
217226
if fd.is_some() {
218227
return Err(anyhow!("Unexpected fd in finish_pipe reply"));
219228
}
220229
Ok(r)
221230
}
222231

232+
#[instrument]
223233
pub async fn open_image(&mut self, imgref: &str) -> Result<OpenedImage> {
234+
tracing::debug!("opening image");
224235
let (imgid, _) = self
225236
.impl_request::<u32, _, _>("OpenImage", [imgref])
226237
.await?;
227238
Ok(OpenedImage(imgid))
228239
}
229240

241+
#[instrument]
230242
pub async fn close_image(&mut self, img: &OpenedImage) -> Result<()> {
243+
tracing::debug!("closing image");
231244
let (r, _) = self.impl_request("CloseImage", [img.0]).await?;
232245
Ok(r)
233246
}
@@ -250,6 +263,7 @@ impl ImageProxy {
250263
/// https://github.com/opencontainers/image-spec/blob/main/descriptor.md
251264
/// Note that right now the proxy does verification of the digest:
252265
/// https://github.com/cgwalters/container-image-proxy/issues/1#issuecomment-926712009
266+
#[instrument]
253267
pub async fn get_blob(
254268
&mut self,
255269
img: &OpenedImage,
@@ -259,6 +273,7 @@ impl ImageProxy {
259273
impl AsyncBufRead + Send + Unpin,
260274
impl Future<Output = Result<()>> + Unpin + '_,
261275
)> {
276+
tracing::debug!("fetching blob");
262277
let args: Vec<serde_json::Value> =
263278
vec![img.0.into(), digest.to_string().into(), size.into()];
264279
let (_bloblen, fd) = self.impl_request::<i64, _, _>("GetBlob", args).await?;
@@ -269,18 +284,21 @@ impl ImageProxy {
269284
}
270285

271286
/// Close the connection and wait for the child process to exit successfully.
287+
#[instrument]
272288
pub async fn finalize(self) -> Result<()> {
273289
let req = Request::new_bare("Shutdown");
274290
let sendbuf = serde_json::to_vec(&req)?;
275291
// SAFETY: Only panics if a worker thread already panic'd
276292
let sockfd = Arc::try_unwrap(self.sockfd).unwrap().into_inner().unwrap();
277293
nixsocket::send(sockfd.as_raw_fd(), &sendbuf, nixsocket::MsgFlags::empty())?;
278294
drop(sendbuf);
295+
tracing::debug!("sent shutdown request");
279296
let output = self.childwait.await?;
280297
if !output.status.success() {
281298
let stderr = String::from_utf8_lossy(&output.stderr);
282299
anyhow::bail!("proxy failed: {}\n{}", output.status, stderr)
283300
}
301+
tracing::debug!("proxy exited successfully");
284302
Ok(())
285303
}
286304
}

0 commit comments

Comments
 (0)