Skip to content

Commit 09e4f88

Browse files
committed
use async rust-extensions
Signed-off-by: Jorge Prendes <[email protected]>
1 parent c446b99 commit 09e4f88

File tree

9 files changed

+179
-119
lines changed

9 files changed

+179
-119
lines changed

Cargo.lock

Lines changed: 72 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/containerd-shim-wasm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ doctest = false
1414
[dependencies]
1515
anyhow = { workspace = true }
1616
chrono = { workspace = true }
17-
containerd-shim = { workspace = true }
17+
containerd-shim = { workspace = true, features = ["async"] }
1818
containerd-shim-wasm-test-modules = { workspace = true, optional = true }
1919
oci-tar-builder = { workspace = true, optional = true }
2020
env_logger = { workspace = true, optional = true }
@@ -38,6 +38,7 @@ prost = "0.13"
3838
toml = "0.8"
3939
trait-variant = "0.1"
4040
tokio-async-drop = "0.1"
41+
async-trait = "0.1.87"
4142

4243
# tracing
4344
# note: it's important to keep the version of tracing in sync with tracing-subscriber

crates/containerd-shim-wasm/src/sandbox/cli.rs

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
//! revision!(),
6969
//! "v1",
7070
//! Some(config),
71-
//! );
71+
//! ).await;
7272
//! ```
7373
//!
7474
//! When the `opentelemetry` feature is enabled, additional runtime config
@@ -168,6 +168,34 @@ fn init_zygote_and_logger(debug: bool, config: &Config) {
168168
);
169169
}
170170

171+
fn otel_traces_guard() -> Box<dyn Send + 'static> {
172+
#[cfg(feature = "opentelemetry")]
173+
if otel_traces_enabled() {
174+
let otlp_config = OtlpConfig::build_from_env().expect("Failed to build OtelConfig.");
175+
let guard = otlp_config
176+
.init()
177+
.expect("Failed to initialize OpenTelemetry.");
178+
return Box::new(guard);
179+
}
180+
Box::new(())
181+
}
182+
183+
fn init_traces_context() {
184+
#[cfg(feature = "opentelemetry")]
185+
// read TRACECONTEXT env var that's set by the parent process
186+
if let Ok(ctx) = std::env::var("TRACECONTEXT") {
187+
OtlpConfig::set_trace_context(&ctx).unwrap();
188+
} else {
189+
let ctx = OtlpConfig::get_trace_context().unwrap();
190+
// SAFETY: although it's in a multithreaded context,
191+
// it's safe to assume that all the other threads are not
192+
// messing with env vars at this point.
193+
unsafe {
194+
std::env::set_var("TRACECONTEXT", ctx);
195+
}
196+
}
197+
}
198+
171199
/// Main entry point for the shim.
172200
///
173201
/// If the `opentelemetry` feature is enabled, this function will start the shim with OpenTelemetry tracing.
@@ -199,69 +227,24 @@ pub fn shim_main<'a, I>(
199227
std::process::exit(0);
200228
}
201229

230+
let config = config.unwrap_or_default();
231+
202232
// Initialize the zygote and logger for the container process
203233
#[cfg(unix)]
204-
{
205-
let default_config = Config::default();
206-
let config = config.as_ref().unwrap_or(&default_config);
207-
init_zygote_and_logger(flags.debug, config);
208-
}
234+
init_zygote_and_logger(flags.debug, &config);
209235

210-
#[cfg(feature = "opentelemetry")]
211-
if otel_traces_enabled() {
212-
// opentelemetry uses tokio, so we need to initialize a runtime
213-
async {
214-
let otlp_config = OtlpConfig::build_from_env().expect("Failed to build OtelConfig.");
215-
let _guard = otlp_config
216-
.init()
217-
.expect("Failed to initialize OpenTelemetry.");
218-
tokio::task::block_in_place(move || {
219-
shim_main_inner::<I>(name, version, revision, shim_version, config);
220-
});
221-
}
222-
.block_on();
223-
} else {
224-
shim_main_inner::<I>(name, version, revision, shim_version, config);
225-
}
236+
async {
237+
let _guard = otel_traces_guard();
238+
init_traces_context();
226239

227-
#[cfg(not(feature = "opentelemetry"))]
228-
{
229-
shim_main_inner::<I>(name, version, revision, shim_version, config);
240+
let shim_version = shim_version.into().unwrap_or("v1");
241+
let lower_name = name.to_lowercase();
242+
let shim_id = format!("io.containerd.{lower_name}.{shim_version}");
243+
244+
run::<ShimCli<I>>(&shim_id, Some(config)).await;
230245
}
246+
.block_on();
231247

232248
#[cfg(target_os = "linux")]
233249
log_mem();
234250
}
235-
236-
#[cfg_attr(feature = "tracing", tracing::instrument(level = "Info"))]
237-
fn shim_main_inner<'a, I>(
238-
name: &str,
239-
version: &str,
240-
revision: impl Into<Option<&'a str>> + std::fmt::Debug,
241-
shim_version: impl Into<Option<&'a str>> + std::fmt::Debug,
242-
config: Option<Config>,
243-
) where
244-
I: 'static + Instance + Sync + Send,
245-
{
246-
#[cfg(feature = "opentelemetry")]
247-
{
248-
// read TRACECONTEXT env var that's set by the parent process
249-
if let Ok(ctx) = std::env::var("TRACECONTEXT") {
250-
OtlpConfig::set_trace_context(&ctx).unwrap();
251-
} else {
252-
let ctx = OtlpConfig::get_trace_context().unwrap();
253-
// SAFETY: although it's in a multithreaded context,
254-
// it's safe to assume that all the other threads are not
255-
// messing with env vars at this point.
256-
unsafe {
257-
std::env::set_var("TRACECONTEXT", ctx);
258-
}
259-
}
260-
}
261-
262-
let shim_version = shim_version.into().unwrap_or("v1");
263-
let lower_name = name.to_lowercase();
264-
let shim_id = format!("io.containerd.{lower_name}.{shim_version}");
265-
266-
run::<ShimCli<I>>(&shim_id, config);
267-
}

0 commit comments

Comments
 (0)