Skip to content

Add logging and request id tracking to wasmtime serve#7366

Merged
elliottt merged 2 commits intobytecodealliance:mainfrom
elliottt:trevor/request-ids-serve
Oct 26, 2023
Merged

Add logging and request id tracking to wasmtime serve#7366
elliottt merged 2 commits intobytecodealliance:mainfrom
elliottt:trevor/request-ids-serve

Conversation

@elliottt
Copy link
Copy Markdown
Member

@elliottt elliottt commented Oct 25, 2023

Add some missing log handling to wasmtime serve:

  • Log the listening address and port before starting to accept incoming connections
  • Track individual requests with unique ids
  • Prefix stdout and stderr from the guest with the request id

Fixes #7257

@elliottt elliottt requested review from a team as code owners October 25, 2023 18:39
@elliottt elliottt requested review from alexcrichton and fitzgen and removed request for a team October 25, 2023 18:39
}

fn check_write(&mut self) -> StreamResult<usize> {
Ok(1024 * 1024)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is arbitrary.


#[async_trait::async_trait]
impl preview2::Subscribe for LogStream {
async fn ready(&mut self) {}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear that there's a reasonable implementation for this here. Perhaps using tokio::io::Stdout instead of a mutex guarded std::io::Stdout would make more sense?


builder.stderr(LogStream {
prefix: format!("stderr [{req_id}] :: "),
output,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be reasonable to make a separate Stderr handle here. My thought was that since the output is already prefixed with stdout/stderr, using the same output stream would be okay.

Copy link
Copy Markdown
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! One thought about whether or not this should be an async write, but otherwise seems fine to me

Comment on lines +415 to +428
for line in bytes.split(|c| *c == b'\n') {
if !line.is_empty() {
msg.extend_from_slice(&self.prefix.as_bytes());
msg.extend_from_slice(line);
msg.push(b'\n');
}
}

let output = self.output.clone();
tokio::task::spawn(async move {
use std::io::Write;
let mut output = output.lock().await;
output.write_all(&msg).expect("writing to stdout");
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think of going ahead and directly doing a blocking write to stdout/stderr here? That'd help applying a bit of backpressure (albeit not in a perfect way) if necessary and additionally would avoid the need to pass around streams much. That'd also perhaps make it a bit easier to write stdout to stdout and stderr to stderr.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! I've implemented that, and substantially simplified the LogStream::write implementation :)

@elliottt elliottt force-pushed the trevor/request-ids-serve branch from 4a1b335 to 698ec0f Compare October 25, 2023 22:32
@elliottt elliottt added this pull request to the merge queue Oct 25, 2023
Merged via the queue into bytecodealliance:main with commit 05a6b3b Oct 26, 2023
@elliottt elliottt deleted the trevor/request-ids-serve branch October 26, 2023 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasi-http: Add unique request ids in wasmtime serve

2 participants