Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add container ID and pod ID to RuntimeContext and logs #879

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Mossaka
Copy link
Member

@Mossaka Mossaka commented Feb 25, 2025

  • containerd-shim-wasm/src/sandbox/cli: add logger to the container process
  • feat(all): add container id to runtimeContext so that logs can refer to it

The purpose of adding id to RuntimeContext is that containers can add the id to their log messages to differentiate from other containers in the same pod.

The disadvantage of this method is that functions that do not import ctx: RuntimeContext will not be able to have the container id in their logs.

This builds on top of #878


Example

The following is containerd log for a pod that has three wasm tasks.

time="2025-02-25T22:12:39.421124341Z" level=info instance="7e7916133550e01b22c57fbcb85ae0b18282a28dac7a61da18d0ebc52f5cc43c" msg="setting up wasi"
time="2025-02-25T22:12:39.918793845Z" level=info instance="45facf66a6c0fd780272599bf7a1a15604445b9811102c9ebd39dfc24a7690c6" msg="setting up wasi"
time="2025-02-25T22:12:40.460339278Z" level=info instance="60ad7697d284b0ab4a51664ae30da83aefc95284213156ee8d1c5cc1929a3144" msg="setting up wasi"

@Mossaka Mossaka linked an issue Feb 25, 2025 that may be closed by this pull request
@Mossaka Mossaka changed the title add container ID to RuntimeContext and logs add container ID and pod ID to RuntimeContext and logs Feb 25, 2025
…cess

this commit initializes the containerd-shim::logger inside of the zygote global process (the container process) so that containers can send logs to contaienrd

Signed-off-by: Jiaxiao (mossaka) Zhou <duibao55328@gmail.com>
…to it

This commit adds the container id to the runtimeContext and uses it in the log macros.

It does so by adding a new `log` macros that takes a `RuntimeContext` and uses it to log the container id.

Signed-off-by: Jiaxiao (mossaka) Zhou <duibao55328@gmail.com>
Signed-off-by: Jiaxiao (mossaka) Zhou <duibao55328@gmail.com>
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

crates/containerd-shim-wasm/src/sys/unix/container/executor.rs:33

  • [nitpick] A new field 'id' is added to the Executor struct without accompanying inline documentation. Adding a comment that explains its purpose and how it is propagated to the container runtime context would improve code readability.
id: String,

crates/containerd-shim-wamr/src/instance.rs:83

  • [nitpick] The error log message may not provide enough context about the failure. Consider including additional details (e.g. function or module name) so that the log message better aids in debugging.
containerd_shim_wasm::error!(ctx, "Error: {:?}", err);

let os_args: Vec<_> = std::env::args_os().collect();

let flags = parse(&os_args[1..]).unwrap();
let argv0 = PathBuf::from(&os_args[0]);
Copy link
Preview

Copilot AI Feb 25, 2025

Choose a reason for hiding this comment

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

Accessing os_args[0] assumes that the argument vector is non-empty. Consider adding a check to ensure os_args contains at least one element to prevent a potential panic.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link
Contributor

@jsturtevant jsturtevant left a comment

Choose a reason for hiding this comment

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

This is great! Huge step forward from an operational standpoint with logging.

Maybe add something to the docs?

Comment on lines +39 to +48

// the container id for the running container
fn container_id(&self) -> &str;

// the pod id for the running container (if available)
// In Kubernetes environments, containers run within pods, and the pod ID is usually
// stored in the OCI spec annotations under "io.kubernetes.cri.sandbox-id"
fn pod_id(&self) -> Option<&str> {
None
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine. We might consider putting this behind a struct/function that hides this behind something like info() if we have more things we might want to log. But as of now I can't think of anything.

@@ -54,7 +56,7 @@ impl Engine for WasmEdgeEngine {
.register_module(Some(&mod_name), module)
.context("registering module")?;

log::debug!("running with method {func:?}");
containerd_shim_wasm::debug!(ctx, "running with method {func:?}");
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to do something like:

Suggested change
containerd_shim_wasm::debug!(ctx, "running with method {func:?}");
containerd_shim_wasm::debug!(ctx, func=func; "running method");

so down stream users could add there own structured logs?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this is supported. do you think we need to support it?

Copy link
Contributor

Choose a reason for hiding this comment

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

No not necessarily, I though it might just work with the marco as is or small tweak. It is a nice to have but not critical, a downstream consumer could bypass our convenience macro and pass their own structure if they wanted too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for structured logging
2 participants